mainwindow.cpp Example File | Qt 4.8 (original) (raw)
xmlpatterns/filetree/mainwindow.cpp
#include #include #include
#include "mainwindow.h" #include "xmlsyntaxhighlighter.h"
MainWindow::MainWindow() : m_fileTree(m_namePool) { setupUi(this);
new XmlSyntaxHighlighter(fileTree->document());
{
[QFont](qfont.html) font("Courier",10);
font.setFixedPitch(true);
fileTree->setFont(font);
queryEdit->setFont(font);
output->setFont(font);
}
const [QString](qstring.html) dir([QLibraryInfo](qlibraryinfo.html)::location([QLibraryInfo](qlibraryinfo.html)::ExamplesPath) + "/xmlpatterns/filetree");
[qDebug](qtglobal.html#qDebug)() << dir;
if ([QDir](qdir.html)(dir).exists())
loadDirectory(dir);
else
fileTree->setPlainText(tr("Use the Open menu entry to select a directory."));
const [QStringList](qstringlist.html) queries([QDir](qdir.html)(":/queries/", "*.xq").entryList());
int len = queries.count();
for (int i = 0; i < len; ++i)
queryBox->addItem(queries.at(i));}
void MainWindow::on_queryBox_currentIndexChanged() { QFile queryFile(":/queries/" + queryBox->currentText()); queryFile.open(QIODevice::ReadOnly);
queryEdit->setPlainText([QString](qstring.html)::fromLatin1(queryFile.readAll()));
evaluateResult();}
void MainWindow::evaluateResult() { if (queryBox->currentText().isEmpty()) return;
[QXmlQuery](qxmlquery.html) query(m_namePool);
query.bindVariable("fileTree", m_fileNode);
query.setQuery([QUrl](qurl.html)("qrc:/queries/" + queryBox->currentText()));
[QByteArray](qbytearray.html) formatterOutput;
[QBuffer](qbuffer.html) buffer(&formatterOutput);
buffer.open([QIODevice](qiodevice.html)::WriteOnly);
[QXmlFormatter](qxmlformatter.html) formatter(query, &buffer);
query.evaluateTo(&formatter);
output->setText([QString](qstring.html)::fromLatin1(formatterOutput.constData()));}
void MainWindow::on_actionOpenDirectory_triggered() { const QString directoryName = QFileDialog::getExistingDirectory(this); if (!directoryName.isEmpty()) loadDirectory(directoryName); }
void MainWindow::loadDirectory(const QString &directory) { Q_ASSERT(QDir(directory).exists());
m_fileNode = m_fileTree.nodeFor(directory);
[QXmlQuery](qxmlquery.html) query(m_namePool);
query.bindVariable("fileTree", m_fileNode);
query.setQuery([QUrl](qurl.html)("qrc:/queries/wholeTree.xq"));
[QByteArray](qbytearray.html) output;
[QBuffer](qbuffer.html) buffer(&output);
buffer.open([QIODevice](qiodevice.html)::WriteOnly);
[QXmlFormatter](qxmlformatter.html) formatter(query, &buffer);
query.evaluateTo(&formatter);
treeInfo->setText(tr("Model of %1 output as XML.").arg(directory));
fileTree->setText([QString](qstring.html)::fromLatin1(output.constData()));
evaluateResult();}
void MainWindow::on_actionAbout_triggered() { QMessageBox::about(this, tr("About File Tree"), tr("
Select File->Open Directory and " "choose a directory. The directory is then " "loaded into the model, and the model is " "displayed on the left as XML.
" "<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>"));}
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.