source: trunk/examples/xmlpatterns/filetree/mainwindow.cpp@ 1030

Last change on this file since 1030 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 5.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
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]
49MainWindow::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]
83void 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]
94void 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]
115void 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]
125void 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
151void 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
Note: See TracBrowser for help on using the repository browser.