author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 06 Jul 2010 15:10:48 +0300 | |
changeset 30 | 5dc02b23752f |
parent 18 | 2f34d5167611 |
child 37 | 758a864f9613 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 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] |
|
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] |
|
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] |
|
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] |
|
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] |
|
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((QString(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 |
||
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 |