1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
package MainWindow;
use strict;
use warnings;
use QtCore4;
use QtGui4;
use QtXmlPatterns4;
use QtCore4::isa qw( Qt::MainWindow );
use FileTree;
use Ui_MainWindow;
use QtCore4::slots
on_actionOpenDirectory_triggered => [],
on_actionAbout_triggered => [],
on_queryBox_currentIndexChanged => [];
use lib '../shared';
#use XmlSyntaxHighlighter;
sub ui() {
return this->{ui};
}
sub m_namePool() {
return this->{m_namePool};
}
sub m_fileTree() {
return this->{m_fileTree};
}
sub m_fileNode() {
return this->{m_fileNode};
}
# [0]
sub NEW {
my ($class) = @_;
$class->SUPER::NEW();
this->{m_namePool} = Qt::XmlNamePool();
this->{m_fileTree} = FileTree(m_namePool);
this->{ui} = Ui_MainWindow->setupUi(this);
#XmlSyntaxHighlighter(ui->fileTree->document());
# Set up the font.
{
my $font = Qt::Font('Courier',10);
$font->setFixedPitch(1);
ui->fileTree->setFont($font);
ui->queryEdit->setFont($font);
ui->output->setFont($font);
}
my $dir = Qt::LibraryInfo::location(Qt::LibraryInfo::ExamplesPath()) . '/xmlpatterns/filetree';
if (Qt::Dir($dir)->exists()) {
loadDirectory($dir);
}
else {
ui->fileTree->setPlainText(this->tr('Use the Open menu entry to select a directory.'));
}
my $queries = Qt::Dir(':/queries/', '*.xq')->entryList();
foreach my $query ( @{$queries} ) {
ui->queryBox->addItem($query);
}
}
# [0]
# [2]
sub on_queryBox_currentIndexChanged
{
my $queryFile = Qt::File(':/queries/' . ui->queryBox->currentText());
$queryFile->open(Qt::IODevice::ReadOnly());
ui->queryEdit->setPlainText($queryFile->readAll()->data());
evaluateResult();
}
# [2]
# [3]
sub evaluateResult
{
if (!defined ui->queryBox->currentText()) {
return;
}
my $query = Qt::XmlQuery(m_namePool);
$query->bindVariable('fileTree', Qt::XmlItem(m_fileNode));
$query->setQuery(Qt::Url('qrc:/queries/' . ui->queryBox->currentText()));
my $formatterOutput = Qt::ByteArray();
my $buffer = Qt::Buffer($formatterOutput);
$buffer->open(Qt::IODevice::WriteOnly());
my $formatter = Qt::XmlFormatter($query, $buffer);
$query->evaluateTo($formatter);
ui->output->setText($formatterOutput->constData());
}
# [3]
# [1]
sub on_actionOpenDirectory_triggered
{
my $directoryName = Qt::FileDialog::getExistingDirectory(this);
if (defined $directoryName) {
loadDirectory($directoryName);
}
}
# [1]
# [4]
# [5]
sub loadDirectory
{
my ($directory) = @_;
die "Directory $directory does not exist." unless Qt::Dir($directory)->exists();
this->{m_fileNode} = m_fileTree->nodeFor($directory);
# [5]
my $query = Qt::XmlQuery(m_namePool);
my $xmlItem = Qt::XmlItem(m_fileNode);
$query->bindVariable('fileTree', $xmlItem );
$query->setQuery(Qt::Url('qrc:/queries/wholeTree.xq'));
my $output = Qt::ByteArray();
my $buffer = Qt::Buffer($output);
$buffer->open(Qt::IODevice::WriteOnly());
my $formatter = Qt::XmlFormatter($query, $buffer);
$query->evaluateTo($formatter);
ui->treeInfo->setText(sprintf this->tr('Model of %s output as XML.'), $directory);
ui->fileTree->setText($output->constData());
evaluateResult();
# [6]
}
# [6]
# [4]
sub on_actionAbout_triggered
{
Qt::MessageBox::about(this, this->tr('About File Tree'),
this->tr('<p>Select <b>File->Open Directory</b> and ' .
'choose a directory. The directory is then ' .
'loaded into the model, and the model is ' .
'displayed on the left as XML.</p>' .
'<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>'));
}
1;
|