mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-03 15:55:27 +08:00
qt 6.5.1 original
This commit is contained in:
39
examples/xml/dombookmarks/CMakeLists.txt
Normal file
39
examples/xml/dombookmarks/CMakeLists.txt
Normal file
@ -0,0 +1,39 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(dombookmarks LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/xml/dombookmarks")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets Xml)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(dombookmarks
|
||||
main.cpp
|
||||
mainwindow.cpp mainwindow.h
|
||||
xbeltree.cpp xbeltree.h
|
||||
)
|
||||
|
||||
set_target_properties(dombookmarks PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(dombookmarks PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
Qt6::Xml
|
||||
)
|
||||
|
||||
install(TARGETS dombookmarks
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
BIN
examples/xml/dombookmarks/doc/images/dombookmarks-example.png
Normal file
BIN
examples/xml/dombookmarks/doc/images/dombookmarks-example.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 19 KiB |
19
examples/xml/dombookmarks/doc/src/dombookmarks.qdoc
Normal file
19
examples/xml/dombookmarks/doc/src/dombookmarks.qdoc
Normal file
@ -0,0 +1,19 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
|
||||
|
||||
/*!
|
||||
\example dombookmarks
|
||||
\title DOM Bookmarks Example
|
||||
\examplecategory {Input/Output}
|
||||
\ingroup xml-examples
|
||||
\brief Provides a reader for XML Bookmark Exchange Language files.
|
||||
|
||||
The DOM Bookmarks example provides a reader for XML Bookmark Exchange Language (XBEL)
|
||||
files that uses Qt's DOM-based XML API to read and parse the files. The SAX Bookmarks
|
||||
example provides an alternative way to read this type of file.
|
||||
|
||||
\image dombookmarks-example.png
|
||||
|
||||
See the \l{http://pyxml.sourceforge.net/topics/xbel/}{XML Bookmark Exchange Language
|
||||
Resource Page} for more information about XBEL files.
|
||||
*/
|
13
examples/xml/dombookmarks/dombookmarks.pro
Normal file
13
examples/xml/dombookmarks/dombookmarks.pro
Normal file
@ -0,0 +1,13 @@
|
||||
HEADERS = mainwindow.h \
|
||||
xbeltree.h
|
||||
SOURCES = main.cpp \
|
||||
mainwindow.cpp \
|
||||
xbeltree.cpp
|
||||
QT += xml widgets
|
||||
requires(qtConfig(filedialog))
|
||||
|
||||
EXAMPLE_FILES = frank.xbel jennifer.xbel
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/xml/dombookmarks
|
||||
INSTALLS += target
|
69
examples/xml/dombookmarks/jennifer.xbel
Normal file
69
examples/xml/dombookmarks/jennifer.xbel
Normal file
@ -0,0 +1,69 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE xbel>
|
||||
<xbel version="1.0">
|
||||
<folder folded="no">
|
||||
<title>Qt Resources</title>
|
||||
<bookmark href="http://qt.io/">
|
||||
<title>Qt home page</title>
|
||||
</bookmark>
|
||||
<bookmark href="https://www.qt.io/partners/">
|
||||
<title>Qt Partners</title>
|
||||
</bookmark>
|
||||
<bookmark href="https://www.qt.io/qt-training/">
|
||||
<title>Training</title>
|
||||
</bookmark>
|
||||
<bookmark href="http://doc.qt.io/">
|
||||
<title>Qt 5 documentation</title>
|
||||
</bookmark>
|
||||
<bookmark href="http://qt-project.org/faq/">
|
||||
<title>Frequently Asked Questions</title>
|
||||
</bookmark>
|
||||
<folder folded="yes">
|
||||
<title>Community Resources</title>
|
||||
<bookmark href="http://www.qtcentre.org/content/">
|
||||
<title>Qt Centre</title>
|
||||
</bookmark>
|
||||
<bookmark href="http://www.qtforum.org/">
|
||||
<title>QtForum.org</title>
|
||||
</bookmark>
|
||||
<bookmark href="http://digitalfanatics.org/projects/qt_tutorial/">
|
||||
<title>The Independent Qt Tutorial</title>
|
||||
</bookmark>
|
||||
<bookmark href="http://www.qtforum.de/">
|
||||
<title>German Qt Forum</title>
|
||||
</bookmark>
|
||||
<bookmark href="http://www.korone.net/">
|
||||
<title>Korean Qt Community Site</title>
|
||||
</bookmark>
|
||||
<bookmark href="http://prog.org.ru/">
|
||||
<title>Russian Qt Forum</title>
|
||||
</bookmark>
|
||||
</folder>
|
||||
</folder>
|
||||
<folder folded="no">
|
||||
<title>Online Dictionaries</title>
|
||||
<bookmark href="http://www.dictionary.com/">
|
||||
<title>Dictionary.com</title>
|
||||
</bookmark>
|
||||
<bookmark href="http://www.m-w.com/">
|
||||
<title>Merriam-Webster Online</title>
|
||||
</bookmark>
|
||||
<bookmark href="http://dictionary.cambridge.org/">
|
||||
<title>Cambridge Dictionaries Online</title>
|
||||
</bookmark>
|
||||
<bookmark href="http://www.onelook.com/">
|
||||
<title>OneLook Dictionary Search</title>
|
||||
</bookmark>
|
||||
<separator/>
|
||||
<bookmark href="http://dict.tu-chemnitz.de/">
|
||||
<title>TU Chemnitz German-English Dictionary</title>
|
||||
</bookmark>
|
||||
<separator/>
|
||||
<bookmark href="http://atilf.atilf.fr/tlf.htm">
|
||||
<title>Trésor de la Langue Française informatisé</title>
|
||||
</bookmark>
|
||||
<bookmark href="http://dictionnaires.atilf.fr/dictionnaires/ACADEMIE/">
|
||||
<title>Dictionnaire de l'Académie Française</title>
|
||||
</bookmark>
|
||||
</folder>
|
||||
</xbel>
|
16
examples/xml/dombookmarks/main.cpp
Normal file
16
examples/xml/dombookmarks/main.cpp
Normal file
@ -0,0 +1,16 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
#include "mainwindow.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
MainWindow mainWin;
|
||||
mainWin.show();
|
||||
|
||||
mainWin.open();
|
||||
return app.exec();
|
||||
}
|
92
examples/xml/dombookmarks/mainwindow.cpp
Normal file
92
examples/xml/dombookmarks/mainwindow.cpp
Normal file
@ -0,0 +1,92 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QtWidgets>
|
||||
|
||||
#include "mainwindow.h"
|
||||
#include "xbeltree.h"
|
||||
|
||||
MainWindow::MainWindow()
|
||||
{
|
||||
xbelTree = new XbelTree;
|
||||
setCentralWidget(xbelTree);
|
||||
|
||||
createMenus();
|
||||
|
||||
statusBar()->showMessage(tr("Ready"));
|
||||
|
||||
setWindowTitle(tr("DOM Bookmarks"));
|
||||
const QSize availableSize = screen()->availableGeometry().size();
|
||||
resize(availableSize.width() / 2, availableSize.height() / 3);
|
||||
}
|
||||
|
||||
void MainWindow::open()
|
||||
{
|
||||
QString fileName =
|
||||
QFileDialog::getOpenFileName(this, tr("Open Bookmark File"),
|
||||
QDir::currentPath(),
|
||||
tr("XBEL Files (*.xbel *.xml)"));
|
||||
if (fileName.isEmpty())
|
||||
return;
|
||||
|
||||
QFile file(fileName);
|
||||
if (!file.open(QFile::ReadOnly | QFile::Text)) {
|
||||
QMessageBox::warning(this, tr("SAX Bookmarks"),
|
||||
tr("Cannot read file %1:\n%2.")
|
||||
.arg(QDir::toNativeSeparators(fileName),
|
||||
file.errorString()));
|
||||
return;
|
||||
}
|
||||
|
||||
if (xbelTree->read(&file))
|
||||
statusBar()->showMessage(tr("File loaded"), 2000);
|
||||
}
|
||||
|
||||
void MainWindow::saveAs()
|
||||
{
|
||||
QString fileName =
|
||||
QFileDialog::getSaveFileName(this, tr("Save Bookmark File"),
|
||||
QDir::currentPath(),
|
||||
tr("XBEL Files (*.xbel *.xml)"));
|
||||
if (fileName.isEmpty())
|
||||
return;
|
||||
|
||||
QFile file(fileName);
|
||||
if (!file.open(QFile::WriteOnly | QFile::Text)) {
|
||||
QMessageBox::warning(this, tr("SAX Bookmarks"),
|
||||
tr("Cannot write file %1:\n%2.")
|
||||
.arg(QDir::toNativeSeparators(fileName),
|
||||
file.errorString()));
|
||||
return;
|
||||
}
|
||||
|
||||
if (xbelTree->write(&file))
|
||||
statusBar()->showMessage(tr("File saved"), 2000);
|
||||
}
|
||||
|
||||
void MainWindow::about()
|
||||
{
|
||||
QMessageBox::about(this, tr("About DOM Bookmarks"),
|
||||
tr("The <b>DOM Bookmarks</b> example demonstrates how to "
|
||||
"use Qt's DOM classes to read and write XML "
|
||||
"documents."));
|
||||
}
|
||||
|
||||
void MainWindow::createMenus()
|
||||
{
|
||||
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
|
||||
QAction *openAct = fileMenu->addAction(tr("&Open..."), this, &MainWindow::open);
|
||||
openAct->setShortcuts(QKeySequence::Open);
|
||||
|
||||
QAction *saveAsAct = fileMenu->addAction(tr("&Save As..."), this, &MainWindow::saveAs);
|
||||
saveAsAct->setShortcuts(QKeySequence::SaveAs);
|
||||
|
||||
QAction *exitAct = fileMenu->addAction(tr("E&xit"), this, &QWidget::close);
|
||||
exitAct->setShortcuts(QKeySequence::Quit);
|
||||
|
||||
menuBar()->addSeparator();
|
||||
|
||||
QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
|
||||
helpMenu->addAction(tr("&About"), this, &MainWindow::about);
|
||||
helpMenu->addAction(tr("About &Qt"), qApp, &QCoreApplication::quit);
|
||||
}
|
29
examples/xml/dombookmarks/mainwindow.h
Normal file
29
examples/xml/dombookmarks/mainwindow.h
Normal file
@ -0,0 +1,29 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef MAINWINDOW_H
|
||||
#define MAINWINDOW_H
|
||||
|
||||
#include <QMainWindow>
|
||||
|
||||
class XbelTree;
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
MainWindow();
|
||||
|
||||
public slots:
|
||||
void open();
|
||||
void saveAs();
|
||||
void about();
|
||||
|
||||
private:
|
||||
void createMenus();
|
||||
|
||||
XbelTree *xbelTree;
|
||||
};
|
||||
|
||||
#endif
|
174
examples/xml/dombookmarks/xbeltree.cpp
Normal file
174
examples/xml/dombookmarks/xbeltree.cpp
Normal file
@ -0,0 +1,174 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QtWidgets>
|
||||
|
||||
#include "xbeltree.h"
|
||||
|
||||
enum { DomElementRole = Qt::UserRole + 1 };
|
||||
|
||||
Q_DECLARE_METATYPE(QDomElement)
|
||||
|
||||
static inline QString titleElement() { return QStringLiteral("title"); }
|
||||
static inline QString folderElement() { return QStringLiteral("folder"); }
|
||||
static inline QString bookmarkElement() { return QStringLiteral("bookmark"); }
|
||||
|
||||
static inline QString versionAttribute() { return QStringLiteral("version"); }
|
||||
static inline QString hrefAttribute() { return QStringLiteral("href"); }
|
||||
static inline QString foldedAttribute() { return QStringLiteral("folded"); }
|
||||
|
||||
XbelTree::XbelTree(QWidget *parent)
|
||||
: QTreeWidget(parent)
|
||||
{
|
||||
QStringList labels;
|
||||
labels << tr("Title") << tr("Location");
|
||||
|
||||
header()->setSectionResizeMode(QHeaderView::Stretch);
|
||||
setHeaderLabels(labels);
|
||||
|
||||
folderIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirClosedIcon),
|
||||
QIcon::Normal, QIcon::Off);
|
||||
folderIcon.addPixmap(style()->standardPixmap(QStyle::SP_DirOpenIcon),
|
||||
QIcon::Normal, QIcon::On);
|
||||
bookmarkIcon.addPixmap(style()->standardPixmap(QStyle::SP_FileIcon));
|
||||
}
|
||||
|
||||
#if !defined(QT_NO_CONTEXTMENU) && !defined(QT_NO_CLIPBOARD)
|
||||
void XbelTree::contextMenuEvent(QContextMenuEvent *event)
|
||||
{
|
||||
const QTreeWidgetItem *item = itemAt(event->pos());
|
||||
if (!item)
|
||||
return;
|
||||
const QString url = item->text(1);
|
||||
QMenu contextMenu;
|
||||
QAction *copyAction = contextMenu.addAction(tr("Copy Link to Clipboard"));
|
||||
QAction *openAction = contextMenu.addAction(tr("Open"));
|
||||
QAction *action = contextMenu.exec(event->globalPos());
|
||||
if (action == copyAction)
|
||||
QGuiApplication::clipboard()->setText(url);
|
||||
else if (action == openAction)
|
||||
QDesktopServices::openUrl(QUrl(url));
|
||||
}
|
||||
#endif // !QT_NO_CONTEXTMENU && !QT_NO_CLIPBOARD
|
||||
|
||||
bool XbelTree::read(QIODevice *device)
|
||||
{
|
||||
QDomDocument::ParseResult result =
|
||||
domDocument.setContent(device, QDomDocument::ParseOption::UseNamespaceProcessing);
|
||||
if (!result) {
|
||||
QMessageBox::information(window(), tr("DOM Bookmarks"),
|
||||
tr("Parse error at line %1, column %2:\n%3")
|
||||
.arg(result.errorLine)
|
||||
.arg(result.errorColumn)
|
||||
.arg(result.errorMessage));
|
||||
return false;
|
||||
}
|
||||
|
||||
QDomElement root = domDocument.documentElement();
|
||||
if (root.tagName() != "xbel") {
|
||||
QMessageBox::information(window(), tr("DOM Bookmarks"),
|
||||
tr("The file is not an XBEL file."));
|
||||
return false;
|
||||
} else if (root.hasAttribute(versionAttribute())
|
||||
&& root.attribute(versionAttribute()) != QLatin1String("1.0")) {
|
||||
QMessageBox::information(window(), tr("DOM Bookmarks"),
|
||||
tr("The file is not an XBEL version 1.0 "
|
||||
"file."));
|
||||
return false;
|
||||
}
|
||||
|
||||
clear();
|
||||
|
||||
disconnect(this, &QTreeWidget::itemChanged, this, &XbelTree::updateDomElement);
|
||||
|
||||
QDomElement child = root.firstChildElement(folderElement());
|
||||
while (!child.isNull()) {
|
||||
parseFolderElement(child);
|
||||
child = child.nextSiblingElement(folderElement());
|
||||
}
|
||||
|
||||
connect(this, &QTreeWidget::itemChanged, this, &XbelTree::updateDomElement);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool XbelTree::write(QIODevice *device) const
|
||||
{
|
||||
const int IndentSize = 4;
|
||||
|
||||
QTextStream out(device);
|
||||
domDocument.save(out, IndentSize);
|
||||
return true;
|
||||
}
|
||||
|
||||
void XbelTree::updateDomElement(const QTreeWidgetItem *item, int column)
|
||||
{
|
||||
QDomElement element = qvariant_cast<QDomElement>(item->data(0, DomElementRole));
|
||||
if (!element.isNull()) {
|
||||
if (column == 0) {
|
||||
QDomElement oldTitleElement = element.firstChildElement(titleElement());
|
||||
QDomElement newTitleElement = domDocument.createElement(titleElement());
|
||||
|
||||
QDomText newTitleText = domDocument.createTextNode(item->text(0));
|
||||
newTitleElement.appendChild(newTitleText);
|
||||
|
||||
element.replaceChild(newTitleElement, oldTitleElement);
|
||||
} else {
|
||||
if (element.tagName() == bookmarkElement())
|
||||
element.setAttribute(hrefAttribute(), item->text(1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void XbelTree::parseFolderElement(const QDomElement &element,
|
||||
QTreeWidgetItem *parentItem)
|
||||
{
|
||||
QTreeWidgetItem *item = createItem(element, parentItem);
|
||||
|
||||
QString title = element.firstChildElement(titleElement()).text();
|
||||
if (title.isEmpty())
|
||||
title = QObject::tr("Folder");
|
||||
|
||||
item->setFlags(item->flags() | Qt::ItemIsEditable);
|
||||
item->setIcon(0, folderIcon);
|
||||
item->setText(0, title);
|
||||
|
||||
bool folded = (element.attribute(foldedAttribute()) != QLatin1String("no"));
|
||||
item->setExpanded(!folded);
|
||||
|
||||
QDomElement child = element.firstChildElement();
|
||||
while (!child.isNull()) {
|
||||
if (child.tagName() == folderElement()) {
|
||||
parseFolderElement(child, item);
|
||||
} else if (child.tagName() == bookmarkElement()) {
|
||||
QTreeWidgetItem *childItem = createItem(child, item);
|
||||
|
||||
QString title = child.firstChildElement(titleElement()).text();
|
||||
if (title.isEmpty())
|
||||
title = QObject::tr("Folder");
|
||||
|
||||
childItem->setFlags(item->flags() | Qt::ItemIsEditable);
|
||||
childItem->setIcon(0, bookmarkIcon);
|
||||
childItem->setText(0, title);
|
||||
childItem->setText(1, child.attribute(hrefAttribute()));
|
||||
} else if (child.tagName() == QLatin1String("separator")) {
|
||||
QTreeWidgetItem *childItem = createItem(child, item);
|
||||
childItem->setFlags(item->flags() & ~(Qt::ItemIsSelectable | Qt::ItemIsEditable));
|
||||
childItem->setText(0, QString(30, u'\xB7'));
|
||||
}
|
||||
child = child.nextSiblingElement();
|
||||
}
|
||||
}
|
||||
|
||||
QTreeWidgetItem *XbelTree::createItem(const QDomElement &element,
|
||||
QTreeWidgetItem *parentItem)
|
||||
{
|
||||
QTreeWidgetItem *item;
|
||||
if (parentItem) {
|
||||
item = new QTreeWidgetItem(parentItem);
|
||||
} else {
|
||||
item = new QTreeWidgetItem(this);
|
||||
}
|
||||
item->setData(0, DomElementRole, QVariant::fromValue(element));
|
||||
return item;
|
||||
}
|
40
examples/xml/dombookmarks/xbeltree.h
Normal file
40
examples/xml/dombookmarks/xbeltree.h
Normal file
@ -0,0 +1,40 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef XBELTREE_H
|
||||
#define XBELTREE_H
|
||||
|
||||
#include <QDomDocument>
|
||||
#include <QIcon>
|
||||
#include <QTreeWidget>
|
||||
|
||||
class XbelTree : public QTreeWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
XbelTree(QWidget *parent = nullptr);
|
||||
|
||||
bool read(QIODevice *device);
|
||||
bool write(QIODevice *device) const;
|
||||
|
||||
protected:
|
||||
#if !defined(QT_NO_CONTEXTMENU) && !defined(QT_NO_CLIPBOARD)
|
||||
void contextMenuEvent(QContextMenuEvent *event) override;
|
||||
#endif
|
||||
|
||||
private slots:
|
||||
void updateDomElement(const QTreeWidgetItem *item, int column);
|
||||
|
||||
private:
|
||||
void parseFolderElement(const QDomElement &element,
|
||||
QTreeWidgetItem *parentItem = nullptr);
|
||||
QTreeWidgetItem *createItem(const QDomElement &element,
|
||||
QTreeWidgetItem *parentItem = nullptr);
|
||||
|
||||
QDomDocument domDocument;
|
||||
QIcon folderIcon;
|
||||
QIcon bookmarkIcon;
|
||||
};
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user