From 38be0d13830efd2d98281c645c3a60afe05ffece Mon Sep 17 00:00:00 2001 From: Qt by Nokia Date: Wed, 27 Apr 2011 12:05:43 +0200 Subject: Initial import from the monolithic Qt. This is the beginning of revision history for this module. If you want to look at revision history older than this, please refer to the Qt Git wiki for how to use Git history grafting. At the time of writing, this wiki is located here: http://qt.gitorious.org/qt/pages/GitIntroductionWithQt If you have already performed the grafting and you don't see any history beyond this commit, try running "git log" with the "--follow" argument. Branched from the monolithic repo, Qt master branch, at commit 896db169ea224deb96c59ce8af800d019de63f12 --- examples/xml/streambookmarks/frank.xbel | 230 +++++++++++++++++++++++ examples/xml/streambookmarks/jennifer.xbel | 93 +++++++++ examples/xml/streambookmarks/main.cpp | 54 ++++++ examples/xml/streambookmarks/mainwindow.cpp | 174 +++++++++++++++++ examples/xml/streambookmarks/mainwindow.h | 79 ++++++++ examples/xml/streambookmarks/streambookmarks.pro | 16 ++ examples/xml/streambookmarks/xbelreader.cpp | 175 +++++++++++++++++ examples/xml/streambookmarks/xbelreader.h | 83 ++++++++ examples/xml/streambookmarks/xbelwriter.cpp | 92 +++++++++ examples/xml/streambookmarks/xbelwriter.h | 65 +++++++ 10 files changed, 1061 insertions(+) create mode 100644 examples/xml/streambookmarks/frank.xbel create mode 100644 examples/xml/streambookmarks/jennifer.xbel create mode 100644 examples/xml/streambookmarks/main.cpp create mode 100644 examples/xml/streambookmarks/mainwindow.cpp create mode 100644 examples/xml/streambookmarks/mainwindow.h create mode 100644 examples/xml/streambookmarks/streambookmarks.pro create mode 100644 examples/xml/streambookmarks/xbelreader.cpp create mode 100644 examples/xml/streambookmarks/xbelreader.h create mode 100644 examples/xml/streambookmarks/xbelwriter.cpp create mode 100644 examples/xml/streambookmarks/xbelwriter.h (limited to 'examples/xml/streambookmarks') diff --git a/examples/xml/streambookmarks/frank.xbel b/examples/xml/streambookmarks/frank.xbel new file mode 100644 index 0000000000..6bd63b8b07 --- /dev/null +++ b/examples/xml/streambookmarks/frank.xbel @@ -0,0 +1,230 @@ + + + + + Literate Programming + + Synopsis of Literate Programming + + + Literate Programming: Propaganda and Tools + + + Literate Programming by Henrik Turbell + + + Literate Programming Library + + + Literate Programming Basics + + + Literate Programming Overview + + + POD is not Literate Programming + + + Computers That We Can Count On + + + Literate Programming - Issues and Problems + + + Literate Programming - Wiki Pages + + + What is well-commented code? + + + Bibliography on literate programming - A searchable bibliography + + + Program comprehension and code reading bibliography + + + Elucidative Programming + + + AVL Trees (TexiWeb) + + + Literate Programming on Wikiverse + + + Physically Based Rendering: From Theory to Implementation + + + + Useful C++ Links + + STL + + STL Reference Documentation + + + STL Tutorial + + + STL Reference + + + + Qt + + Qt 2.3 Reference + + + Qt 3.3 Reference + + + Qt 4.0 Reference + + + Qt Home Page + + + + IOStreams + + IO Stream Library + + + Binary I/O + + + I/O Stream FAQ + + + + gdb + + GDB Tutorial + + + Debugging with GDB + + + GDB Quick Reference Page (PDF) (Handy) + + + + Classes and Constructors + + Constructor FAQ + + + Organizing Classes + + + + + Software Documentation or System Documentation + + The Almighty Thud + + + Microsoft Coding Techniques and Programming Practices + + + Software and Documentation + + + The Source Code is the Design + + + What is Software Design? + + + How To Write Unmaintainable Code + + + Self Documenting Program Code Remains a Distant Goal + + + Place Tab A in Slot B + + + UML Reference Card + + + + TeX Resources + + The TeX User's Group + + + MikTeX website + + + MetaPost website + + + HEVEA is a quite complete and fast LATEX to HTML translator + + + + Portable Document Format (PDF) + + Adobe - The postscript and PDF standards + + + Reference Manual Portable Document Format + + + Adobe Acrobat Software Development Kit + + + + Literature Sites + + Guide to Special Collections (Columbia University) + + + Literary Criticism on the Web from the Internet Public Library + + + Victorian Web. + + + Voice of the Shuttle. + + + Modernist Journals Project + + + Museum of American Poetics + + + Modern American Poetry + + + FindArticles.com + + + Literary History + + + Literary Encyclopedia + + + + The University of California Press + + + Wright American Fiction, 1851-1875 + + + Documenting the American South: Beginnings to 1920 + + + Electronic Text Center at the University of Virginia + + + The Schomburg Center for Research in Black Culture + + + Alex Catalogue of Electronic Texts. + + + diff --git a/examples/xml/streambookmarks/jennifer.xbel b/examples/xml/streambookmarks/jennifer.xbel new file mode 100644 index 0000000000..36256fda0a --- /dev/null +++ b/examples/xml/streambookmarks/jennifer.xbel @@ -0,0 +1,93 @@ + + + + + Qt Resources + + Qt Partners + + Training Partners + + + Consultants and System Integrators + + + Technology Partners + + + Value Added Resellers (VARs) + + + + Community Resources + + QtForum.org + + + The Independent Qt Tutorial + + + French PROG.Qt + + + German Qt Forum + + + Korean Qt Community Site + + + Russian Qt Forum + + + Digitalfanatics: The QT 4 Resource Center + + + QtQuestions + + + + Qt Quarterly + + + Qt home page + + + Qt 4.0 documentation + + + Frequently Asked Questions + + + + Online Dictionaries + + Dictionary.com + + + Merriam-Webster Online + + + Cambridge Dictionaries Online + + + OneLook Dictionary Search + + + + The New English-German Dictionary + + + TU Chemnitz German-English Dictionary + + + + Trésor de la Langue Française informatisé + + + Dictionnaire de l'Académie Française + + + Dictionnaire des synonymes + + + diff --git a/examples/xml/streambookmarks/main.cpp b/examples/xml/streambookmarks/main.cpp new file mode 100644 index 0000000000..c44e921e82 --- /dev/null +++ b/examples/xml/streambookmarks/main.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "mainwindow.h" + +//! [0] +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + MainWindow mainWin; + mainWin.show(); + mainWin.open(); + return app.exec(); +} +//! [0] diff --git a/examples/xml/streambookmarks/mainwindow.cpp b/examples/xml/streambookmarks/mainwindow.cpp new file mode 100644 index 0000000000..e9236e93e6 --- /dev/null +++ b/examples/xml/streambookmarks/mainwindow.cpp @@ -0,0 +1,174 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "mainwindow.h" +#include "xbelreader.h" +#include "xbelwriter.h" + +//! [0] +MainWindow::MainWindow() +{ + QStringList labels; + labels << tr("Title") << tr("Location"); + + treeWidget = new QTreeWidget; + treeWidget->header()->setResizeMode(QHeaderView::Stretch); + treeWidget->setHeaderLabels(labels); + setCentralWidget(treeWidget); + + createActions(); + createMenus(); + + statusBar()->showMessage(tr("Ready")); + + setWindowTitle(tr("QXmlStream Bookmarks")); + resize(480, 320); +} +//! [0] + +//! [1] +void MainWindow::open() +{ + QString fileName = + QFileDialog::getOpenFileName(this, tr("Open Bookmark File"), + QDir::currentPath(), + tr("XBEL Files (*.xbel *.xml)")); + if (fileName.isEmpty()) + return; + + treeWidget->clear(); + + + QFile file(fileName); + if (!file.open(QFile::ReadOnly | QFile::Text)) { + QMessageBox::warning(this, tr("QXmlStream Bookmarks"), + tr("Cannot read file %1:\n%2.") + .arg(fileName) + .arg(file.errorString())); + return; + } + + XbelReader reader(treeWidget); + if (!reader.read(&file)) { + QMessageBox::warning(this, tr("QXmlStream Bookmarks"), + tr("Parse error in file %1:\n\n%2") + .arg(fileName) + .arg(reader.errorString())); + } else { + statusBar()->showMessage(tr("File loaded"), 2000); + } + +} +//! [1] + +//! [2] +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("QXmlStream Bookmarks"), + tr("Cannot write file %1:\n%2.") + .arg(fileName) + .arg(file.errorString())); + return; + } + + XbelWriter writer(treeWidget); + if (writer.writeFile(&file)) + statusBar()->showMessage(tr("File saved"), 2000); +} +//! [2] + +//! [3] +void MainWindow::about() +{ + QMessageBox::about(this, tr("About QXmlStream Bookmarks"), + tr("The QXmlStream Bookmarks example demonstrates how to use Qt's " + "QXmlStream classes to read and write XML documents.")); +} +//! [3] + +//! [4] +void MainWindow::createActions() +{ + openAct = new QAction(tr("&Open..."), this); + openAct->setShortcuts(QKeySequence::Open); + connect(openAct, SIGNAL(triggered()), this, SLOT(open())); + + saveAsAct = new QAction(tr("&Save As..."), this); + saveAsAct->setShortcuts(QKeySequence::SaveAs); + connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs())); + + exitAct = new QAction(tr("E&xit"), this); + exitAct->setShortcuts(QKeySequence::Quit); + connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); + + aboutAct = new QAction(tr("&About"), this); + connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); + + aboutQtAct = new QAction(tr("About &Qt"), this); + connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); +} +//! [4] + +//! [5] +void MainWindow::createMenus() +{ + fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->addAction(openAct); + fileMenu->addAction(saveAsAct); + fileMenu->addAction(exitAct); + + menuBar()->addSeparator(); + + helpMenu = menuBar()->addMenu(tr("&Help")); + helpMenu->addAction(aboutAct); + helpMenu->addAction(aboutQtAct); +} +//! [5] diff --git a/examples/xml/streambookmarks/mainwindow.h b/examples/xml/streambookmarks/mainwindow.h new file mode 100644 index 0000000000..f27c4088bc --- /dev/null +++ b/examples/xml/streambookmarks/mainwindow.h @@ -0,0 +1,79 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +QT_BEGIN_NAMESPACE +class QTreeWidget; +QT_END_NAMESPACE + +//! [0] +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +public slots: + void open(); + void saveAs(); + void about(); + +private: + void createActions(); + void createMenus(); + + QTreeWidget *treeWidget; + + QMenu *fileMenu; + QMenu *helpMenu; + QAction *openAct; + QAction *saveAsAct; + QAction *exitAct; + QAction *aboutAct; + QAction *aboutQtAct; +}; +//! [0] + +#endif diff --git a/examples/xml/streambookmarks/streambookmarks.pro b/examples/xml/streambookmarks/streambookmarks.pro new file mode 100644 index 0000000000..febd3cd85f --- /dev/null +++ b/examples/xml/streambookmarks/streambookmarks.pro @@ -0,0 +1,16 @@ +HEADERS = mainwindow.h \ + xbelreader.h \ + xbelwriter.h +SOURCES = main.cpp \ + mainwindow.cpp \ + xbelreader.cpp \ + xbelwriter.cpp +QT += xml + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/xml/streambookmarks +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS streambookmarks.pro *.xbel +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/xml/streambookmarks +INSTALLS += target sources + +symbian: include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) diff --git a/examples/xml/streambookmarks/xbelreader.cpp b/examples/xml/streambookmarks/xbelreader.cpp new file mode 100644 index 0000000000..e6d1c64e64 --- /dev/null +++ b/examples/xml/streambookmarks/xbelreader.cpp @@ -0,0 +1,175 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "xbelreader.h" + +//! [0] +XbelReader::XbelReader(QTreeWidget *treeWidget) + : treeWidget(treeWidget) +{ + QStyle *style = treeWidget->style(); + + 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)); +} +//! [0] + +//! [1] +bool XbelReader::read(QIODevice *device) +{ + xml.setDevice(device); + + if (xml.readNextStartElement()) { + if (xml.name() == "xbel" && xml.attributes().value("version") == "1.0") + readXBEL(); + else + xml.raiseError(QObject::tr("The file is not an XBEL version 1.0 file.")); + } + + return !xml.error(); +} +//! [1] + +//! [2] +QString XbelReader::errorString() const +{ + return QObject::tr("%1\nLine %2, column %3") + .arg(xml.errorString()) + .arg(xml.lineNumber()) + .arg(xml.columnNumber()); +} +//! [2] + +//! [3] +void XbelReader::readXBEL() +{ + Q_ASSERT(xml.isStartElement() && xml.name() == "xbel"); + + while (xml.readNextStartElement()) { + if (xml.name() == "folder") + readFolder(0); + else if (xml.name() == "bookmark") + readBookmark(0); + else if (xml.name() == "separator") + readSeparator(0); + else + xml.skipCurrentElement(); + } +} +//! [3] + +//! [4] +void XbelReader::readTitle(QTreeWidgetItem *item) +{ + Q_ASSERT(xml.isStartElement() && xml.name() == "title"); + + QString title = xml.readElementText(); + item->setText(0, title); +} +//! [4] + +//! [5] +void XbelReader::readSeparator(QTreeWidgetItem *item) +{ + Q_ASSERT(xml.isStartElement() && xml.name() == "separator"); + + QTreeWidgetItem *separator = createChildItem(item); + separator->setFlags(item->flags() & ~Qt::ItemIsSelectable); + separator->setText(0, QString(30, 0xB7)); + xml.skipCurrentElement(); +} +//! [5] + +void XbelReader::readFolder(QTreeWidgetItem *item) +{ + Q_ASSERT(xml.isStartElement() && xml.name() == "folder"); + + QTreeWidgetItem *folder = createChildItem(item); + bool folded = (xml.attributes().value("folded") != "no"); + treeWidget->setItemExpanded(folder, !folded); + + while (xml.readNextStartElement()) { + if (xml.name() == "title") + readTitle(folder); + else if (xml.name() == "folder") + readFolder(folder); + else if (xml.name() == "bookmark") + readBookmark(folder); + else if (xml.name() == "separator") + readSeparator(folder); + else + xml.skipCurrentElement(); + } +} + +void XbelReader::readBookmark(QTreeWidgetItem *item) +{ + Q_ASSERT(xml.isStartElement() && xml.name() == "bookmark"); + + QTreeWidgetItem *bookmark = createChildItem(item); + bookmark->setFlags(bookmark->flags() | Qt::ItemIsEditable); + bookmark->setIcon(0, bookmarkIcon); + bookmark->setText(0, QObject::tr("Unknown title")); + bookmark->setText(1, xml.attributes().value("href").toString()); + + while (xml.readNextStartElement()) { + if (xml.name() == "title") + readTitle(bookmark); + else + xml.skipCurrentElement(); + } +} + +QTreeWidgetItem *XbelReader::createChildItem(QTreeWidgetItem *item) +{ + QTreeWidgetItem *childItem; + if (item) { + childItem = new QTreeWidgetItem(item); + } else { + childItem = new QTreeWidgetItem(treeWidget); + } + childItem->setData(0, Qt::UserRole, xml.name().toString()); + return childItem; +} diff --git a/examples/xml/streambookmarks/xbelreader.h b/examples/xml/streambookmarks/xbelreader.h new file mode 100644 index 0000000000..1ca1190235 --- /dev/null +++ b/examples/xml/streambookmarks/xbelreader.h @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef XBELREADER_H +#define XBELREADER_H + +#include +#include + +QT_BEGIN_NAMESPACE +class QTreeWidget; +class QTreeWidgetItem; +QT_END_NAMESPACE + +//! [0] +class XbelReader +{ +public: +//! [1] + XbelReader(QTreeWidget *treeWidget); +//! [1] + + bool read(QIODevice *device); + + QString errorString() const; + +private: +//! [2] + void readXBEL(); + void readTitle(QTreeWidgetItem *item); + void readSeparator(QTreeWidgetItem *item); + void readFolder(QTreeWidgetItem *item); + void readBookmark(QTreeWidgetItem *item); + + QTreeWidgetItem *createChildItem(QTreeWidgetItem *item); + + QXmlStreamReader xml; + QTreeWidget *treeWidget; +//! [2] + + QIcon folderIcon; + QIcon bookmarkIcon; +}; +//! [0] + +#endif diff --git a/examples/xml/streambookmarks/xbelwriter.cpp b/examples/xml/streambookmarks/xbelwriter.cpp new file mode 100644 index 0000000000..bd5454c941 --- /dev/null +++ b/examples/xml/streambookmarks/xbelwriter.cpp @@ -0,0 +1,92 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "xbelwriter.h" + +//! [0] +XbelWriter::XbelWriter(QTreeWidget *treeWidget) + : treeWidget(treeWidget) +{ + xml.setAutoFormatting(true); +} +//! [0] + +//! [1] +bool XbelWriter::writeFile(QIODevice *device) +{ + xml.setDevice(device); + + xml.writeStartDocument(); + xml.writeDTD(""); + xml.writeStartElement("xbel"); + xml.writeAttribute("version", "1.0"); + for (int i = 0; i < treeWidget->topLevelItemCount(); ++i) + writeItem(treeWidget->topLevelItem(i)); + + xml.writeEndDocument(); + return true; +} +//! [1] + +//! [2] +void XbelWriter::writeItem(QTreeWidgetItem *item) +{ + QString tagName = item->data(0, Qt::UserRole).toString(); + if (tagName == "folder") { + bool folded = !treeWidget->isItemExpanded(item); + xml.writeStartElement(tagName); + xml.writeAttribute("folded", folded ? "yes" : "no"); + xml.writeTextElement("title", item->text(0)); + for (int i = 0; i < item->childCount(); ++i) + writeItem(item->child(i)); + xml.writeEndElement(); + } else if (tagName == "bookmark") { + xml.writeStartElement(tagName); + if (!item->text(1).isEmpty()) + xml.writeAttribute("href", item->text(1)); + xml.writeTextElement("title", item->text(0)); + xml.writeEndElement(); + } else if (tagName == "separator") { + xml.writeEmptyElement(tagName); + } +} +//! [2] diff --git a/examples/xml/streambookmarks/xbelwriter.h b/examples/xml/streambookmarks/xbelwriter.h new file mode 100644 index 0000000000..8160a87b52 --- /dev/null +++ b/examples/xml/streambookmarks/xbelwriter.h @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef XBELWRITER_H +#define XBELWRITER_H + +#include + +QT_BEGIN_NAMESPACE +class QTreeWidget; +class QTreeWidgetItem; +QT_END_NAMESPACE + +//! [0] +class XbelWriter +{ +public: + XbelWriter(QTreeWidget *treeWidget); + bool writeFile(QIODevice *device); + +private: + void writeItem(QTreeWidgetItem *item); + QXmlStreamWriter xml; + QTreeWidget *treeWidget; +}; +//! [0] + +#endif -- cgit v1.2.3