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