1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of The Qt Company Ltd nor the names of its
21 **     contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 #include <QtGui>
42 #include <QLibraryInfo>
43 #include <QtXmlPatterns>
44 
45 #include "mainwindow.h"
46 #include "xmlsyntaxhighlighter.h"
47 
48 //! [0]
MainWindow()49 MainWindow::MainWindow() : m_fileTree(m_namePool)
50 {
51     setupUi(this);
52 
53     new XmlSyntaxHighlighter(fileTree->document());
54 
55     // Set up the font.
56     {
57         QFont font("Courier",10);
58         font.setFixedPitch(true);
59 
60         fileTree->setFont(font);
61         queryEdit->setFont(font);
62         output->setFont(font);
63     }
64 
65     const QString dir(QLibraryInfo::location(QLibraryInfo::ExamplesPath) + "/xmlpatterns/filetree");
66     qDebug() << dir;
67 
68     if (QDir(dir).exists())
69         loadDirectory(dir);
70     else
71         fileTree->setPlainText(tr("Use the Open menu entry to select a directory."));
72 
73     const QStringList queries(QDir(":/queries/", "*.xq").entryList());
74     int len = queries.count();
75 
76     for (int i = 0; i < len; ++i)
77         queryBox->addItem(queries.at(i));
78 
79 }
80 //! [0]
81 
82 //! [2]
on_queryBox_currentIndexChanged()83 void MainWindow::on_queryBox_currentIndexChanged()
84 {
85     QFile queryFile(":/queries/" + queryBox->currentText());
86     queryFile.open(QIODevice::ReadOnly);
87 
88     queryEdit->setPlainText(QString::fromLatin1(queryFile.readAll()));
89     evaluateResult();
90 }
91 //! [2]
92 
93 //! [3]
evaluateResult()94 void MainWindow::evaluateResult()
95 {
96     if (queryBox->currentText().isEmpty())
97         return;
98 
99     QXmlQuery query(m_namePool);
100     query.bindVariable("fileTree", m_fileNode);
101     query.setQuery(QUrl("qrc:/queries/" + queryBox->currentText()));
102 
103     QByteArray formatterOutput;
104     QBuffer buffer(&formatterOutput);
105     buffer.open(QIODevice::WriteOnly);
106 
107     QXmlFormatter formatter(query, &buffer);
108     query.evaluateTo(&formatter);
109 
110     output->setText(QString::fromLatin1(formatterOutput.constData()));
111 }
112 //! [3]
113 
114 //! [1]
on_actionOpenDirectory_triggered()115 void MainWindow::on_actionOpenDirectory_triggered()
116 {
117     const QString directoryName = QFileDialog::getExistingDirectory(this);
118     if (!directoryName.isEmpty())
119         loadDirectory(directoryName);
120 }
121 //! [1]
122 
123 //! [4]
124 //! [5]
loadDirectory(const QString & directory)125 void MainWindow::loadDirectory(const QString &directory)
126 {
127     Q_ASSERT(QDir(directory).exists());
128 
129     m_fileNode = m_fileTree.nodeFor(directory);
130 //! [5]
131 
132     QXmlQuery query(m_namePool);
133     query.bindVariable("fileTree", m_fileNode);
134     query.setQuery(QUrl("qrc:/queries/wholeTree.xq"));
135 
136     QByteArray output;
137     QBuffer buffer(&output);
138     buffer.open(QIODevice::WriteOnly);
139 
140     QXmlFormatter formatter(query, &buffer);
141     query.evaluateTo(&formatter);
142 
143     treeInfo->setText(tr("Model of %1 output as XML.").arg(directory));
144     fileTree->setText(QString::fromLatin1(output.constData()));
145     evaluateResult();
146 //! [6]
147 }
148 //! [6]
149 //! [4]
150 
on_actionAbout_triggered()151 void MainWindow::on_actionAbout_triggered()
152 {
153     QMessageBox::about(this, tr("About File Tree"),
154                    tr("<p>Select <b>File->Open Directory</b> and "
155                       "choose a directory. The directory is then "
156                       "loaded into the model, and the model is "
157                       "displayed on the left as XML.</p>"
158 
159                       "<p>From the query menu on the right, select "
160                       "a query. The query is displayed and then run "
161                       "on the model. The results are displayed below "
162                       "the query.</p>"));
163 }
164 
165 
166