diff options
author | Qt by Nokia <qt-info@nokia.com> | 2011-04-27 12:05:43 +0200 |
---|---|---|
committer | axis <qt-info@nokia.com> | 2011-04-27 12:05:43 +0200 |
commit | e1b2c9deb5943faae2b29be6a5c006f75bb73f06 (patch) | |
tree | fc79e45367c0a8fc71185e9afc33f7503a58653c /examples/xmlpatterns/filetree | |
download | qtxmlpatterns-e1b2c9deb5943faae2b29be6a5c006f75bb73f06.tar.gz |
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
Diffstat (limited to 'examples/xmlpatterns/filetree')
-rw-r--r-- | examples/xmlpatterns/filetree/filetree.cpp | 371 | ||||
-rw-r--r-- | examples/xmlpatterns/filetree/filetree.h | 102 | ||||
-rw-r--r-- | examples/xmlpatterns/filetree/filetree.pro | 17 | ||||
-rw-r--r-- | examples/xmlpatterns/filetree/forms/mainwindow.ui | 200 | ||||
-rw-r--r-- | examples/xmlpatterns/filetree/main.cpp | 53 | ||||
-rw-r--r-- | examples/xmlpatterns/filetree/mainwindow.cpp | 165 | ||||
-rw-r--r-- | examples/xmlpatterns/filetree/mainwindow.h | 72 | ||||
-rw-r--r-- | examples/xmlpatterns/filetree/queries.qrc | 7 | ||||
-rw-r--r-- | examples/xmlpatterns/filetree/queries/listCPPFiles.xq | 19 | ||||
-rw-r--r-- | examples/xmlpatterns/filetree/queries/wholeTree.xq | 1 |
10 files changed, 1007 insertions, 0 deletions
diff --git a/examples/xmlpatterns/filetree/filetree.cpp b/examples/xmlpatterns/filetree/filetree.cpp new file mode 100644 index 0000000..5b90cfb --- /dev/null +++ b/examples/xmlpatterns/filetree/filetree.cpp @@ -0,0 +1,371 @@ +/**************************************************************************** +** +** 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 <QtCore/QUrl> +#include <QtCore/QVariant> +#include <QtXmlPatterns/QXmlNamePool> +#include "filetree.h" + +/* +The model has two types of nodes: elements & attributes. + + <directory name=""> + <file name=""> + </file> + </directory> + + In QXmlNodeModelIndex we store two values. QXmlNodeIndex::data() + is treated as a signed int, and it is an index into m_fileInfos + unless it is -1, in which case it has no meaning and the value + of QXmlNodeModelIndex::additionalData() is a Type name instead. + */ + +/*! + The constructor passes \a pool to the base class, then loads an + internal vector with an instance of QXmlName for each of the + strings "file", "directory", "fileName", "filePath", "size", + "mimeType", and "suffix". + */ +//! [2] +FileTree::FileTree(const QXmlNamePool& pool) + : QSimpleXmlNodeModel(pool), + m_filterAllowAll(QDir::AllEntries | + QDir::AllDirs | + QDir::NoDotAndDotDot | + QDir::Hidden), + m_sortFlags(QDir::Name) +{ + QXmlNamePool np = namePool(); + m_names.resize(7); + m_names[File] = QXmlName(np, QLatin1String("file")); + m_names[Directory] = QXmlName(np, QLatin1String("directory")); + m_names[AttributeFileName] = QXmlName(np, QLatin1String("fileName")); + m_names[AttributeFilePath] = QXmlName(np, QLatin1String("filePath")); + m_names[AttributeSize] = QXmlName(np, QLatin1String("size")); + m_names[AttributeMIMEType] = QXmlName(np, QLatin1String("mimeType")); + m_names[AttributeSuffix] = QXmlName(np, QLatin1String("suffix")); +} +//! [2] + +/*! + Returns the QXmlNodeModelIndex for the model node representing + the directory \a dirName. + + It calls QDir::cleanPath(), because an instance of QFileInfo + constructed for a path ending in '/' will return the empty string in + fileName(), instead of the directory name. +*/ +QXmlNodeModelIndex FileTree::nodeFor(const QString& dirName) const +{ + QFileInfo dirInfo(QDir::cleanPath(dirName)); + Q_ASSERT(dirInfo.exists()); + return toNodeIndex(dirInfo); +} + +/*! + Since the value will always be in m_fileInfos, it is safe for + us to return a const reference to it. + */ +//! [6] +const QFileInfo& +FileTree::toFileInfo(const QXmlNodeModelIndex &nodeIndex) const +{ + return m_fileInfos.at(nodeIndex.data()); +} +//! [6] + +/*! + Returns the model node index for the node specified by the + QFileInfo and node Type. + */ +//! [1] +QXmlNodeModelIndex +FileTree::toNodeIndex(const QFileInfo &fileInfo, Type attributeName) const +{ + const int indexOf = m_fileInfos.indexOf(fileInfo); + + if (indexOf == -1) { + m_fileInfos.append(fileInfo); + return createIndex(m_fileInfos.count()-1, attributeName); + } + else + return createIndex(indexOf, attributeName); +} +//! [1] + +/*! + Returns the model node index for the node specified by the + QFileInfo, which must be a Type::File or Type::Directory. + */ +//! [0] +QXmlNodeModelIndex FileTree::toNodeIndex(const QFileInfo &fileInfo) const +{ + return toNodeIndex(fileInfo, fileInfo.isDir() ? Directory : File); +} +//! [0] + +/*! + This private helper function is only called by nextFromSimpleAxis(). + It is called whenever nextFromSimpleAxis() is called with an axis + parameter of either \c{PreviousSibling} or \c{NextSibling}. + */ +//! [5] +QXmlNodeModelIndex FileTree::nextSibling(const QXmlNodeModelIndex &nodeIndex, + const QFileInfo &fileInfo, + qint8 offset) const +{ + Q_ASSERT(offset == -1 || offset == 1); + + // Get the context node's parent. + const QXmlNodeModelIndex parent(nextFromSimpleAxis(Parent, nodeIndex)); + + if (parent.isNull()) + return QXmlNodeModelIndex(); + + // Get the parent's child list. + const QFileInfo parentFI(toFileInfo(parent)); + Q_ASSERT(Type(parent.additionalData()) == Directory); + const QFileInfoList siblings(QDir(parentFI.absoluteFilePath()).entryInfoList(QStringList(), + m_filterAllowAll, + m_sortFlags)); + Q_ASSERT_X(!siblings.isEmpty(), Q_FUNC_INFO, "Can't happen! We started at a child."); + + // Find the index of the child where we started. + const int indexOfMe = siblings.indexOf(fileInfo); + + // Apply the offset. + const int siblingIndex = indexOfMe + offset; + if (siblingIndex < 0 || siblingIndex > siblings.count() - 1) + return QXmlNodeModelIndex(); + else + return toNodeIndex(siblings.at(siblingIndex)); +} +//! [5] + +/*! + This function is called by the QtXmlPatterns query engine when it + wants to move to the next node in the model. It moves along an \a + axis, \e from the node specified by \a nodeIndex. + + This function is usually the one that requires the most design and + implementation work, because the implementation depends on the + perhaps unique structure of your non-XML data. + + There are \l {QAbstractXmlNodeModel::SimpleAxis} {four values} for + \a axis that the implementation must handle, but there are really + only two axes, i.e., vertical and horizontal. Two of the four values + specify direction on the vertical axis (\c{Parent} and + \c{FirstChild}), and the other two values specify direction on the + horizontal axis (\c{PreviousSibling} and \c{NextSibling}). + + The typical implementation will be a \c switch statement with + a case for each of the four \a axis values. + */ +//! [4] +QXmlNodeModelIndex +FileTree::nextFromSimpleAxis(SimpleAxis axis, const QXmlNodeModelIndex &nodeIndex) const +{ + const QFileInfo fi(toFileInfo(nodeIndex)); + const Type type = Type(nodeIndex.additionalData()); + + if (type != File && type != Directory) { + Q_ASSERT_X(axis == Parent, Q_FUNC_INFO, "An attribute only has a parent!"); + return toNodeIndex(fi, Directory); + } + + switch (axis) { + case Parent: + return toNodeIndex(QFileInfo(fi.path()), Directory); + + case FirstChild: + { + if (type == File) // A file has no children. + return QXmlNodeModelIndex(); + else { + Q_ASSERT(type == Directory); + Q_ASSERT_X(fi.isDir(), Q_FUNC_INFO, "It isn't really a directory!"); + const QDir dir(fi.absoluteFilePath()); + Q_ASSERT(dir.exists()); + + const QFileInfoList children(dir.entryInfoList(QStringList(), + m_filterAllowAll, + m_sortFlags)); + if (children.isEmpty()) + return QXmlNodeModelIndex(); + const QFileInfo firstChild(children.first()); + return toNodeIndex(firstChild); + } + } + + case PreviousSibling: + return nextSibling(nodeIndex, fi, -1); + + case NextSibling: + return nextSibling(nodeIndex, fi, 1); + } + + Q_ASSERT_X(false, Q_FUNC_INFO, "Don't ever get here!"); + return QXmlNodeModelIndex(); +} +//! [4] + +/*! + No matter what part of the file system we model (the whole file + tree or a subtree), \a node will always have \c{file:///} as + the document URI. + */ +QUrl FileTree::documentUri(const QXmlNodeModelIndex &node) const +{ + Q_UNUSED(node); + return QUrl("file:///"); +} + +/*! + This function returns QXmlNodeModelIndex::Element if \a node + is a directory or a file, and QXmlNodeModelIndex::Attribute + otherwise. + */ +QXmlNodeModelIndex::NodeKind +FileTree::kind(const QXmlNodeModelIndex &node) const +{ + switch (Type(node.additionalData())) { + case Directory: + case File: + return QXmlNodeModelIndex::Element; + default: + return QXmlNodeModelIndex::Attribute; + } +} + +/*! + No order is defined for this example, so we always return + QXmlNodeModelIndex::Precedes, just to keep everyone happy. + */ +QXmlNodeModelIndex::DocumentOrder +FileTree::compareOrder(const QXmlNodeModelIndex&, + const QXmlNodeModelIndex&) const +{ + return QXmlNodeModelIndex::Precedes; +} + +/*! + Returns the name of \a node. The caller guarantees that \a node is + not null and that it is contained in this node model. + */ +//! [3] +QXmlName FileTree::name(const QXmlNodeModelIndex &node) const +{ + return m_names.at(node.additionalData()); +} +//! [3] + +/*! + Always returns the QXmlNodeModelIndex for the root of the + file system, i.e. "/". + */ +QXmlNodeModelIndex FileTree::root(const QXmlNodeModelIndex &node) const +{ + Q_UNUSED(node); + return toNodeIndex(QFileInfo(QLatin1String("/"))); +} + +/*! + Returns the typed value for \a node, which must be either an + attribute or an element. The QVariant returned represents the atomic + value of an attribute or the atomic value contained in an element. + + If the QVariant is returned as a default constructed variant, + it means that \a node has no typed value. + */ +QVariant FileTree::typedValue(const QXmlNodeModelIndex &node) const +{ + const QFileInfo &fi = toFileInfo(node); + + switch (Type(node.additionalData())) { + case Directory: + // deliberate fall through. + case File: + return QString(); + case AttributeFileName: + return fi.fileName(); + case AttributeFilePath: + return fi.filePath(); + case AttributeSize: + return fi.size(); + case AttributeMIMEType: + { + /* We don't have any MIME detection code currently, so return + * the most generic one. */ + return QLatin1String("application/octet-stream"); + } + case AttributeSuffix: + return fi.suffix(); + } + + Q_ASSERT_X(false, Q_FUNC_INFO, "This line should never be reached."); + return QString(); +} + +/*! + Returns the attributes of \a element. The caller guarantees + that \a element is an element in this node model. + */ +QVector<QXmlNodeModelIndex> +FileTree::attributes(const QXmlNodeModelIndex &element) const +{ + QVector<QXmlNodeModelIndex> result; + + /* Both elements has this attribute. */ + const QFileInfo &forElement = toFileInfo(element); + result.append(toNodeIndex(forElement, AttributeFilePath)); + result.append(toNodeIndex(forElement, AttributeFileName)); + + if (Type(element.additionalData() == File)) { + result.append(toNodeIndex(forElement, AttributeSize)); + result.append(toNodeIndex(forElement, AttributeSuffix)); + //result.append(toNodeIndex(forElement, AttributeMIMEType)); + } + else { + Q_ASSERT(element.additionalData() == Directory); + } + + return result; +} + diff --git a/examples/xmlpatterns/filetree/filetree.h b/examples/xmlpatterns/filetree/filetree.h new file mode 100644 index 0000000..a36b9a4 --- /dev/null +++ b/examples/xmlpatterns/filetree/filetree.h @@ -0,0 +1,102 @@ +/**************************************************************************** +** +** 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 <QtCore/QFileInfo> +#include <QtCore/QDir> +#include <QtCore/QVector> +#include <QtXmlPatterns/QSimpleXmlNodeModel> + +class FileTree : public QSimpleXmlNodeModel +{ + public: + FileTree(const QXmlNamePool &namePool); + + QXmlNodeModelIndex nodeFor(const QString &fileName) const; + + //! [0] + virtual QXmlNodeModelIndex::DocumentOrder compareOrder(const QXmlNodeModelIndex&, const QXmlNodeModelIndex&) const; + virtual QXmlName name(const QXmlNodeModelIndex &node) const; + virtual QUrl documentUri(const QXmlNodeModelIndex &node) const; + virtual QXmlNodeModelIndex::NodeKind kind(const QXmlNodeModelIndex &node) const; + virtual QXmlNodeModelIndex root(const QXmlNodeModelIndex &node) const; + virtual QVariant typedValue(const QXmlNodeModelIndex &node) const; + //! [0] + protected: + //! [1] + virtual QVector<QXmlNodeModelIndex> attributes(const QXmlNodeModelIndex &element) const; + virtual QXmlNodeModelIndex nextFromSimpleAxis(SimpleAxis, const QXmlNodeModelIndex&) const; + //! [1] + + private: + //! [4] + enum Type { + File, + Directory, + AttributeFileName, + AttributeFilePath, + AttributeSize, + AttributeMIMEType, + AttributeSuffix + }; + //! [4] + + inline QXmlNodeModelIndex nextSibling(const QXmlNodeModelIndex &nodeIndex, + const QFileInfo &from, + qint8 offset) const; + inline const QFileInfo &toFileInfo(const QXmlNodeModelIndex &index) const; + inline QXmlNodeModelIndex toNodeIndex(const QFileInfo &index, + Type attributeName) const; + inline QXmlNodeModelIndex toNodeIndex(const QFileInfo &index) const; + + /* + One possible improvement is to use a hash, and use the &*&value() + trick to get a pointer, which would be stored in data() instead + of the index. + */ + //! [2] + mutable QVector<QFileInfo> m_fileInfos; + const QDir::Filters m_filterAllowAll; + const QDir::SortFlags m_sortFlags; + QVector<QXmlName> m_names; + //! [2] +}; + + //! [3] + //! [3] diff --git a/examples/xmlpatterns/filetree/filetree.pro b/examples/xmlpatterns/filetree/filetree.pro new file mode 100644 index 0000000..8d9c1fa --- /dev/null +++ b/examples/xmlpatterns/filetree/filetree.pro @@ -0,0 +1,17 @@ +SOURCES += main.cpp filetree.cpp mainwindow.cpp ../shared/xmlsyntaxhighlighter.cpp +HEADERS += filetree.h mainwindow.h ../shared/xmlsyntaxhighlighter.h +FORMS += forms/mainwindow.ui +QT += xmlpatterns +RESOURCES += queries.qrc +INCLUDEPATH += ../shared/ + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtxmlpatterns/xmlpatterns/filetree +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS *.pro *.xq *.html +sources.path = $$[QT_INSTALL_EXAMPLES]/qtxmlpatterns/xmlpatterns/filetree +INSTALLS += target sources + +symbian { + TARGET.UID3 = 0xA000D7C4 + include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) +} diff --git a/examples/xmlpatterns/filetree/forms/mainwindow.ui b/examples/xmlpatterns/filetree/forms/mainwindow.ui new file mode 100644 index 0000000..993050f --- /dev/null +++ b/examples/xmlpatterns/filetree/forms/mainwindow.ui @@ -0,0 +1,200 @@ +<ui version="4.0" > + <class>MainWindow</class> + <widget class="QMainWindow" name="MainWindow" > + <property name="geometry" > + <rect> + <x>0</x> + <y>0</y> + <width>910</width> + <height>676</height> + </rect> + </property> + <property name="windowTitle" > + <string>File Tree</string> + </property> + <widget class="QWidget" name="centralwidget" > + <property name="geometry" > + <rect> + <x>0</x> + <y>29</y> + <width>910</width> + <height>625</height> + </rect> + </property> + <layout class="QVBoxLayout" name="verticalLayout_3" > + <item> + <widget class="QLabel" name="label" > + <property name="sizePolicy" > + <sizepolicy vsizetype="Fixed" hsizetype="Preferred" > + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="font" > + <font> + <italic>true</italic> + </font> + </property> + <property name="text" > + <string></string> + </property> + </widget> + </item> + <item> + <widget class="QSplitter" name="splitter_2" > + <property name="orientation" > + <enum>Qt::Horizontal</enum> + </property> + <widget class="QWidget" name="" > + <layout class="QVBoxLayout" name="verticalLayout_2" > + <item> + <widget class="QLabel" name="treeInfo" > + <property name="sizePolicy" > + <sizepolicy vsizetype="Fixed" hsizetype="Preferred" > + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text" > + <string>TextLabel</string> + </property> + </widget> + </item> + <item> + <widget class="QTextEdit" name="fileTree" > + <property name="sizePolicy" > + <sizepolicy vsizetype="Minimum" hsizetype="Expanding" > + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="readOnly" > + <bool>true</bool> + </property> + <property name="html" > + <string><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> +<html><head><meta name="qrichtext" content="1" /><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;"> +<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"></p></body></html></string> + </property> + <property name="acceptRichText" > + <bool>false</bool> + </property> + </widget> + </item> + </layout> + </widget> + <widget class="QWidget" name="" > + <layout class="QVBoxLayout" name="verticalLayout" > + <item> + <widget class="QComboBox" name="queryBox" /> + </item> + <item> + <widget class="QSplitter" name="splitter" > + <property name="orientation" > + <enum>Qt::Vertical</enum> + </property> + <widget class="QTextEdit" name="queryEdit" > + <property name="readOnly" > + <bool>true</bool> + </property> + <property name="html" > + <string><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> +<html><head><meta name="qrichtext" content="1" /><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;"> +<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"></p></body></html></string> + </property> + <property name="acceptRichText" > + <bool>false</bool> + </property> + </widget> + <widget class="QTextEdit" name="output" > + <property name="sizePolicy" > + <sizepolicy vsizetype="Minimum" hsizetype="Expanding" > + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="readOnly" > + <bool>true</bool> + </property> + <property name="html" > + <string><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> +<html><head><meta name="qrichtext" content="1" /><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;"> +<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"></p></body></html></string> + </property> + <property name="acceptRichText" > + <bool>false</bool> + </property> + </widget> + </widget> + </item> + </layout> + </widget> + </widget> + </item> + </layout> + <zorder>label</zorder> + <zorder>splitter_2</zorder> + <zorder>queryBox</zorder> + <zorder>treeInfo</zorder> + <zorder>splitter</zorder> + </widget> + <widget class="QMenuBar" name="menubar" > + <property name="geometry" > + <rect> + <x>0</x> + <y>0</y> + <width>910</width> + <height>29</height> + </rect> + </property> + <widget class="QMenu" name="menuFile" > + <property name="title" > + <string>&File</string> + </property> + <addaction name="actionOpenDirectory" /> + </widget> + <widget class="QMenu" name="menu_Help" > + <property name="title" > + <string>&Help</string> + </property> + <addaction name="actionAbout" /> + </widget> + <addaction name="menuFile" /> + <addaction name="menu_Help" /> + </widget> + <widget class="QStatusBar" name="statusbar" > + <property name="geometry" > + <rect> + <x>0</x> + <y>654</y> + <width>910</width> + <height>22</height> + </rect> + </property> + </widget> + <action name="actionOpenDirectory" > + <property name="text" > + <string>Open Directory...</string> + </property> + <property name="shortcut" > + <string>Ctrl+O</string> + </property> + </action> + <action name="actionAbout" > + <property name="text" > + <string>&About</string> + </property> + <property name="shortcut" > + <string>Ctrl+A</string> + </property> + </action> + </widget> + <resources/> + <connections/> +</ui> diff --git a/examples/xmlpatterns/filetree/main.cpp b/examples/xmlpatterns/filetree/main.cpp new file mode 100644 index 0000000..939072a --- /dev/null +++ b/examples/xmlpatterns/filetree/main.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** 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 <QtGui/QApplication> + +#include "mainwindow.h" + +int main(int argc, char **argv) +{ + Q_INIT_RESOURCE(queries); + QApplication app(argc, argv); + MainWindow mainWindow; + + mainWindow.show(); + return app.exec(); +} diff --git a/examples/xmlpatterns/filetree/mainwindow.cpp b/examples/xmlpatterns/filetree/mainwindow.cpp new file mode 100644 index 0000000..43502ce --- /dev/null +++ b/examples/xmlpatterns/filetree/mainwindow.cpp @@ -0,0 +1,165 @@ +/**************************************************************************** +** +** 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 <QtGui> +#include <QLibraryInfo> +#include <QtXmlPatterns> + +#include "mainwindow.h" +#include "xmlsyntaxhighlighter.h" + +//! [0] +MainWindow::MainWindow() : m_fileTree(m_namePool) +{ + setupUi(this); + + new XmlSyntaxHighlighter(fileTree->document()); + + // Set up the font. + { + QFont font("Courier",10); + font.setFixedPitch(true); + + fileTree->setFont(font); + queryEdit->setFont(font); + output->setFont(font); + } + + const QString dir(QLibraryInfo::location(QLibraryInfo::ExamplesPath) + "/xmlpatterns/filetree"); + qDebug() << dir; + + if (QDir(dir).exists()) + loadDirectory(dir); + else + fileTree->setPlainText(tr("Use the Open menu entry to select a directory.")); + + const QStringList queries(QDir(":/queries/", "*.xq").entryList()); + int len = queries.count(); + + for (int i = 0; i < len; ++i) + queryBox->addItem(queries.at(i)); + +} +//! [0] + +//! [2] +void MainWindow::on_queryBox_currentIndexChanged() +{ + QFile queryFile(":/queries/" + queryBox->currentText()); + queryFile.open(QIODevice::ReadOnly); + + queryEdit->setPlainText(QString::fromLatin1(queryFile.readAll())); + evaluateResult(); +} +//! [2] + +//! [3] +void MainWindow::evaluateResult() +{ + if (queryBox->currentText().isEmpty()) + return; + + QXmlQuery query(m_namePool); + query.bindVariable("fileTree", m_fileNode); + query.setQuery(QUrl("qrc:/queries/" + queryBox->currentText())); + + QByteArray formatterOutput; + QBuffer buffer(&formatterOutput); + buffer.open(QIODevice::WriteOnly); + + QXmlFormatter formatter(query, &buffer); + query.evaluateTo(&formatter); + + output->setText(QString::fromLatin1(formatterOutput.constData())); +} +//! [3] + +//! [1] +void MainWindow::on_actionOpenDirectory_triggered() +{ + const QString directoryName = QFileDialog::getExistingDirectory(this); + if (!directoryName.isEmpty()) + loadDirectory(directoryName); +} +//! [1] + +//! [4] +//! [5] +void MainWindow::loadDirectory(const QString &directory) +{ + Q_ASSERT(QDir(directory).exists()); + + m_fileNode = m_fileTree.nodeFor(directory); +//! [5] + + QXmlQuery query(m_namePool); + query.bindVariable("fileTree", m_fileNode); + query.setQuery(QUrl("qrc:/queries/wholeTree.xq")); + + QByteArray output; + QBuffer buffer(&output); + buffer.open(QIODevice::WriteOnly); + + QXmlFormatter formatter(query, &buffer); + query.evaluateTo(&formatter); + + treeInfo->setText(tr("Model of %1 output as XML.").arg(directory)); + fileTree->setText(QString::fromLatin1(output.constData())); + evaluateResult(); +//! [6] +} +//! [6] +//! [4] + +void MainWindow::on_actionAbout_triggered() +{ + QMessageBox::about(this, tr("About File Tree"), + tr("<p>Select <b>File->Open Directory</b> and " + "choose a directory. The directory is then " + "loaded into the model, and the model is " + "displayed on the left as XML.</p>" + + "<p>From the query menu on the right, select " + "a query. The query is displayed and then run " + "on the model. The results are displayed below " + "the query.</p>")); +} + + diff --git a/examples/xmlpatterns/filetree/mainwindow.h b/examples/xmlpatterns/filetree/mainwindow.h new file mode 100644 index 0000000..8bf3196 --- /dev/null +++ b/examples/xmlpatterns/filetree/mainwindow.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** 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 <QFile> +#include <QMainWindow> +#include <QXmlNamePool> +//! [0] +#include "filetree.h" +#include "ui_mainwindow.h" + +class MainWindow : public QMainWindow, private Ui_MainWindow +{ + Q_OBJECT + + public: + MainWindow(); + + private slots: + void on_actionOpenDirectory_triggered(); + void on_actionAbout_triggered(); + void on_queryBox_currentIndexChanged(); + + private: + void loadDirectory(const QString &directory); + void evaluateResult(); + + const QXmlNamePool m_namePool; + const FileTree m_fileTree; + QXmlNodeModelIndex m_fileNode; +}; +//! [0] +#endif diff --git a/examples/xmlpatterns/filetree/queries.qrc b/examples/xmlpatterns/filetree/queries.qrc new file mode 100644 index 0000000..2e48601 --- /dev/null +++ b/examples/xmlpatterns/filetree/queries.qrc @@ -0,0 +1,7 @@ +<!DOCTYPE RCC> + <RCC version="1.0"> +<qresource> + <file>queries/listCPPFiles.xq</file> + <file>queries/wholeTree.xq</file> +</qresource> +</RCC> diff --git a/examples/xmlpatterns/filetree/queries/listCPPFiles.xq b/examples/xmlpatterns/filetree/queries/listCPPFiles.xq new file mode 100644 index 0000000..e311d27 --- /dev/null +++ b/examples/xmlpatterns/filetree/queries/listCPPFiles.xq @@ -0,0 +1,19 @@ +declare variable $where as xs:string := string($fileTree/@filePath); +<html> + <head> + <title>All cpp files in: {$where}</title> + </head> + <body> + <p> + cpp-files found in {$where} sorted by size: + </p> + <ul> { + for $file in $fileTree//file[@suffix = "cpp"] + order by xs:integer($file/@size) + return + <li> + {string($file/@fileName)}, size: {string($file/@size)} + </li> + } </ul> + </body> +</html> diff --git a/examples/xmlpatterns/filetree/queries/wholeTree.xq b/examples/xmlpatterns/filetree/queries/wholeTree.xq new file mode 100644 index 0000000..ec2977b --- /dev/null +++ b/examples/xmlpatterns/filetree/queries/wholeTree.xq @@ -0,0 +1 @@ +$fileTree |