diff options
Diffstat (limited to 'examples/itemviews/simpledommodel')
-rw-r--r-- | examples/itemviews/simpledommodel/domitem.cpp | 102 | ||||
-rw-r--r-- | examples/itemviews/simpledommodel/domitem.h | 67 | ||||
-rw-r--r-- | examples/itemviews/simpledommodel/dommodel.cpp | 190 | ||||
-rw-r--r-- | examples/itemviews/simpledommodel/dommodel.h | 77 | ||||
-rw-r--r-- | examples/itemviews/simpledommodel/main.cpp | 53 | ||||
-rw-r--r-- | examples/itemviews/simpledommodel/mainwindow.cpp | 85 | ||||
-rw-r--r-- | examples/itemviews/simpledommodel/mainwindow.h | 71 | ||||
-rw-r--r-- | examples/itemviews/simpledommodel/simpledommodel.pro | 15 |
8 files changed, 660 insertions, 0 deletions
diff --git a/examples/itemviews/simpledommodel/domitem.cpp b/examples/itemviews/simpledommodel/domitem.cpp new file mode 100644 index 0000000..18f65ea --- /dev/null +++ b/examples/itemviews/simpledommodel/domitem.cpp @@ -0,0 +1,102 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtXml> + +#include "domitem.h" + +//! [0] +DomItem::DomItem(QDomNode &node, int row, DomItem *parent) +{ + domNode = node; +//! [0] + // Record the item's location within its parent. +//! [1] + rowNumber = row; + parentItem = parent; +} +//! [1] + +//! [2] +DomItem::~DomItem() +{ + QHash<int,DomItem*>::iterator it; + for (it = childItems.begin(); it != childItems.end(); ++it) + delete it.value(); +} +//! [2] + +//! [3] +QDomNode DomItem::node() const +{ + return domNode; +} +//! [3] + +//! [4] +DomItem *DomItem::parent() +{ + return parentItem; +} +//! [4] + +//! [5] +DomItem *DomItem::child(int i) +{ + if (childItems.contains(i)) + return childItems[i]; + + if (i >= 0 && i < domNode.childNodes().count()) { + QDomNode childNode = domNode.childNodes().item(i); + DomItem *childItem = new DomItem(childNode, i, this); + childItems[i] = childItem; + return childItem; + } + return 0; +} +//! [5] + +//! [6] +int DomItem::row() +{ + return rowNumber; +} +//! [6] diff --git a/examples/itemviews/simpledommodel/domitem.h b/examples/itemviews/simpledommodel/domitem.h new file mode 100644 index 0000000..61f2acc --- /dev/null +++ b/examples/itemviews/simpledommodel/domitem.h @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DOMITEM_H +#define DOMITEM_H + +#include <QDomNode> +#include <QHash> + +//! [0] +class DomItem +{ +public: + DomItem(QDomNode &node, int row, DomItem *parent = 0); + ~DomItem(); + DomItem *child(int i); + DomItem *parent(); + QDomNode node() const; + int row(); + +private: + QDomNode domNode; + QHash<int,DomItem*> childItems; + DomItem *parentItem; + int rowNumber; +}; +//! [0] + +#endif diff --git a/examples/itemviews/simpledommodel/dommodel.cpp b/examples/itemviews/simpledommodel/dommodel.cpp new file mode 100644 index 0000000..495fd55 --- /dev/null +++ b/examples/itemviews/simpledommodel/dommodel.cpp @@ -0,0 +1,190 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> +#include <QtXml> + +#include "domitem.h" +#include "dommodel.h" + +//! [0] +DomModel::DomModel(QDomDocument document, QObject *parent) + : QAbstractItemModel(parent), domDocument(document) +{ + rootItem = new DomItem(domDocument, 0); +} +//! [0] + +//! [1] +DomModel::~DomModel() +{ + delete rootItem; +} +//! [1] + +//! [2] +int DomModel::columnCount(const QModelIndex &/*parent*/) const +{ + return 3; +} +//! [2] + +//! [3] +QVariant DomModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + if (role != Qt::DisplayRole) + return QVariant(); + + DomItem *item = static_cast<DomItem*>(index.internalPointer()); + + QDomNode node = item->node(); +//! [3] //! [4] + QStringList attributes; + QDomNamedNodeMap attributeMap = node.attributes(); + + switch (index.column()) { + case 0: + return node.nodeName(); + case 1: + for (int i = 0; i < attributeMap.count(); ++i) { + QDomNode attribute = attributeMap.item(i); + attributes << attribute.nodeName() + "=\"" + +attribute.nodeValue() + "\""; + } + return attributes.join(" "); + case 2: + return node.nodeValue().split("\n").join(" "); + default: + return QVariant(); + } +} +//! [4] + +//! [5] +Qt::ItemFlags DomModel::flags(const QModelIndex &index) const +{ + if (!index.isValid()) + return 0; + + return Qt::ItemIsEnabled | Qt::ItemIsSelectable; +} +//! [5] + +//! [6] +QVariant DomModel::headerData(int section, Qt::Orientation orientation, + int role) const +{ + if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { + switch (section) { + case 0: + return tr("Name"); + case 1: + return tr("Attributes"); + case 2: + return tr("Value"); + default: + return QVariant(); + } + } + + return QVariant(); +} +//! [6] + +//! [7] +QModelIndex DomModel::index(int row, int column, const QModelIndex &parent) + const +{ + if (!hasIndex(row, column, parent)) + return QModelIndex(); + + DomItem *parentItem; + + if (!parent.isValid()) + parentItem = rootItem; + else + parentItem = static_cast<DomItem*>(parent.internalPointer()); +//! [7] + +//! [8] + DomItem *childItem = parentItem->child(row); + if (childItem) + return createIndex(row, column, childItem); + else + return QModelIndex(); +} +//! [8] + +//! [9] +QModelIndex DomModel::parent(const QModelIndex &child) const +{ + if (!child.isValid()) + return QModelIndex(); + + DomItem *childItem = static_cast<DomItem*>(child.internalPointer()); + DomItem *parentItem = childItem->parent(); + + if (!parentItem || parentItem == rootItem) + return QModelIndex(); + + return createIndex(parentItem->row(), 0, parentItem); +} +//! [9] + +//! [10] +int DomModel::rowCount(const QModelIndex &parent) const +{ + if (parent.column() > 0) + return 0; + + DomItem *parentItem; + + if (!parent.isValid()) + parentItem = rootItem; + else + parentItem = static_cast<DomItem*>(parent.internalPointer()); + + return parentItem->node().childNodes().count(); +} +//! [10] diff --git a/examples/itemviews/simpledommodel/dommodel.h b/examples/itemviews/simpledommodel/dommodel.h new file mode 100644 index 0000000..1178b2c --- /dev/null +++ b/examples/itemviews/simpledommodel/dommodel.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DOMMODEL_H +#define DOMMODEL_H + +#include <QAbstractItemModel> +#include <QDomDocument> +#include <QModelIndex> +#include <QVariant> + +class DomItem; + +//! [0] +class DomModel : public QAbstractItemModel +{ + Q_OBJECT + +public: + DomModel(QDomDocument document, QObject *parent = 0); + ~DomModel(); + + QVariant data(const QModelIndex &index, int role) const; + Qt::ItemFlags flags(const QModelIndex &index) const; + QVariant headerData(int section, Qt::Orientation orientation, + int role = Qt::DisplayRole) const; + QModelIndex index(int row, int column, + const QModelIndex &parent = QModelIndex()) const; + QModelIndex parent(const QModelIndex &child) const; + int rowCount(const QModelIndex &parent = QModelIndex()) const; + int columnCount(const QModelIndex &parent = QModelIndex()) const; + +private: + QDomDocument domDocument; + DomItem *rootItem; +}; +//! [0] + +#endif diff --git a/examples/itemviews/simpledommodel/main.cpp b/examples/itemviews/simpledommodel/main.cpp new file mode 100644 index 0000000..d2ecfc9 --- /dev/null +++ b/examples/itemviews/simpledommodel/main.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> + +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + MainWindow window; + window.resize(640, 480); + window.show(); + return app.exec(); +} diff --git a/examples/itemviews/simpledommodel/mainwindow.cpp b/examples/itemviews/simpledommodel/mainwindow.cpp new file mode 100644 index 0000000..ac96899 --- /dev/null +++ b/examples/itemviews/simpledommodel/mainwindow.cpp @@ -0,0 +1,85 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QDomDocument> +#include <QFile> +#include <QtGui> + +#include "dommodel.h" +#include "mainwindow.h" + +MainWindow::MainWindow() : QMainWindow(), model(0) +{ + fileMenu = menuBar()->addMenu(tr("&File")); + fileMenu->addAction(tr("&Open..."), this, SLOT(openFile()), + QKeySequence(tr("Ctrl+O"))); + fileMenu->addAction(tr("E&xit"), this, SLOT(close()), + QKeySequence(tr("Ctrl+Q"))); + + model = new DomModel(QDomDocument(), this); + view = new QTreeView(this); + view->setModel(model); + + setCentralWidget(view); + setWindowTitle(tr("Simple DOM Model")); +} + +void MainWindow::openFile() +{ + QString filePath = QFileDialog::getOpenFileName(this, tr("Open File"), + xmlPath, tr("XML files (*.xml);;HTML files (*.html);;" + "SVG files (*.svg);;User Interface files (*.ui)")); + + if (!filePath.isEmpty()) { + QFile file(filePath); + if (file.open(QIODevice::ReadOnly)) { + QDomDocument document; + if (document.setContent(&file)) { + DomModel *newModel = new DomModel(document, this); + view->setModel(newModel); + delete model; + model = newModel; + xmlPath = filePath; + } + file.close(); + } + } +} diff --git a/examples/itemviews/simpledommodel/mainwindow.h b/examples/itemviews/simpledommodel/mainwindow.h new file mode 100644 index 0000000..4bc967a --- /dev/null +++ b/examples/itemviews/simpledommodel/mainwindow.h @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> +#include <QString> + +class DomModel; +QT_BEGIN_NAMESPACE +class QMenu; +class QTreeView; +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(); + +public slots: + void openFile(); + +private: + DomModel *model; + QMenu *fileMenu; + QString xmlPath; + QTreeView *view; +}; + +#endif diff --git a/examples/itemviews/simpledommodel/simpledommodel.pro b/examples/itemviews/simpledommodel/simpledommodel.pro new file mode 100644 index 0000000..bc9b1ca --- /dev/null +++ b/examples/itemviews/simpledommodel/simpledommodel.pro @@ -0,0 +1,15 @@ +HEADERS = domitem.h \ + dommodel.h \ + mainwindow.h +SOURCES = domitem.cpp \ + dommodel.cpp \ + main.cpp \ + mainwindow.cpp +CONFIG += qt +QT += xml + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/itemviews/simpledommodel +sources.files = $$SOURCES $$HEADERS $$RESOURCES *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/itemviews/simpledommodel +INSTALLS += target sources |