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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
package MainWindow;
use strict;
use warnings;
use QtCore4;
use QtGui4;
use QtCore4::isa qw( Qt::MainWindow );
use QtCore4::slots
chooseImage => [],
printImage => [],
showAboutBox => [],
updateView => [];
use ImageModel;
use PixelDelegate;
use List::Util qw(min);
sub model() {
return this->{model};
}
sub printAction() {
return this->{printAction};
}
sub currentPath() {
return this->{currentPath};
}
sub view() {
return this->{view};
}
# [0]
sub NEW
{
my ($class, $parent) = @_;
$class->SUPER::NEW($parent);
# [0]
this->{currentPath} = Qt::Dir::homePath();
this->{model} = ImageModel(this);
my $centralWidget = Qt::Widget();
# [1]
this->{view} = Qt::TableView();
view->setShowGrid(0);
view->horizontalHeader()->hide();
view->verticalHeader()->hide();
view->horizontalHeader()->setMinimumSectionSize(1);
view->verticalHeader()->setMinimumSectionSize(1);
view->setModel(model);
# [1]
# [2]
my $delegate = PixelDelegate(this);
view->setItemDelegate($delegate);
# [2]
# [3]
my $pixelSizeLabel = Qt::Label(this->tr('Pixel size:'));
my $pixelSizeSpinBox = Qt::SpinBox();
$pixelSizeSpinBox->setMinimum(4);
$pixelSizeSpinBox->setMaximum(32);
$pixelSizeSpinBox->setValue(12);
# [3]
my $fileMenu = Qt::Menu(this->tr('&File'), this);
my $openAction = $fileMenu->addAction(this->tr('&Open...'));
$openAction->setShortcut(Qt::KeySequence(Qt::KeySequence::Open()));
this->{printAction} = $fileMenu->addAction(this->tr('&Print...'));
printAction->setEnabled(0);
printAction->setShortcut(Qt::KeySequence(Qt::KeySequence::Print()));
my $quitAction = $fileMenu->addAction(this->tr('E&xit'));
$quitAction->setShortcut(Qt::KeySequence(Qt::KeySequence::Quit()));
my $helpMenu = Qt::Menu(this->tr('&Help'), this);
my $aboutAction = $helpMenu->addAction(this->tr('&About'));
menuBar()->addMenu($fileMenu);
menuBar()->addSeparator();
menuBar()->addMenu($helpMenu);
this->connect($openAction, SIGNAL 'triggered()', this, SLOT 'chooseImage()');
this->connect(printAction, SIGNAL 'triggered()', this, SLOT 'printImage()');
this->connect($quitAction, SIGNAL 'triggered()', qApp, SLOT 'quit()');
this->connect($aboutAction, SIGNAL 'triggered()', this, SLOT 'showAboutBox()');
# [4]
this->connect($pixelSizeSpinBox, SIGNAL 'valueChanged(int)',
$delegate, SLOT 'setPixelSize(int)');
this->connect($pixelSizeSpinBox, SIGNAL 'valueChanged(int)',
this, SLOT 'updateView()');
# [4]
my $controlsLayout = Qt::HBoxLayout();
$controlsLayout->addWidget($pixelSizeLabel);
$controlsLayout->addWidget($pixelSizeSpinBox);
$controlsLayout->addStretch(1);
my $mainLayout = Qt::VBoxLayout();
$mainLayout->addWidget(view);
$mainLayout->addLayout($controlsLayout);
$centralWidget->setLayout($mainLayout);
setCentralWidget($centralWidget);
setWindowTitle(this->tr('Pixelator'));
resize(640, 480);
# [5]
}
# [5]
sub chooseImage
{
my $fileName = Qt::FileDialog::getOpenFileName(this,
this->tr('Choose an image'), currentPath, '*');
if ($fileName) {
openImage($fileName);
}
}
sub openImage
{
my ($fileName) = @_;
my $image = Qt::Image();
if ($image->load($fileName)) {
model->setImage($image);
if ($fileName !~ m#^:/#) {
this->{currentPath} = $fileName;
setWindowTitle(sprintf this->tr('%s - Pixelator'), currentPath);
}
printAction->setEnabled(1);
updateView();
}
}
sub printImage
{
#ifndef QT_NO_PRINTER
if (model->rowCount(Qt::ModelIndex())*model->columnCount(Qt::ModelIndex())
> 90000) {
my $answer = Qt::MessageBox::question(this, this->tr('Large Image Size'),
this->tr('The printed image may be very large. Are you sure that ' .
'you want to print it?'),
Qt::MessageBox::Yes() | Qt::MessageBox::No());
if ($answer == Qt::MessageBox::No()) {
return;
}
}
my $printer = Qt::Printer(Qt::Printer::HighResolution());
my $dlg = Qt::PrintDialog($printer, this);
$dlg->setWindowTitle(this->tr('Print Image'));
if ($dlg->exec() != Qt::Dialog::Accepted()) {
return;
}
my $painter = Qt::Painter();
$painter->begin($printer);
my $rows = model->rowCount(Qt::ModelIndex());
my $columns = model->columnCount(Qt::ModelIndex());
my $sourceWidth = ($columns+1) * PixelDelegate::ItemSize;
my $sourceHeight = ($rows+1) * PixelDelegate::ItemSize;
$painter->save();
my $xscale = $printer->pageRect()->width()/$sourceWidth;
my $yscale = $printer->pageRect()->height()/$sourceHeight;
my $scale = min($xscale, $yscale);
$painter->translate($printer->paperRect()->x() + $printer->pageRect()->width()/2,
$printer->paperRect()->y() + $printer->pageRect()->height()/2);
$painter->scale($scale, $scale);
$painter->translate(-$sourceWidth/2, -$sourceHeight/2);
my $option = Qt::StyleOptionViewItem();
my $parent = Qt::ModelIndex();
my $progress = Qt::ProgressDialog(this->tr('Printing...'), this->tr('Cancel'), 0, $rows, this);
$progress->setWindowModality(Qt::ApplicationModal());
my $y = PixelDelegate::ItemSize/2;
for (my $row = 0; $row < $rows; ++$row) {
$progress->setValue($row);
qApp->processEvents();
if ($progress->wasCanceled()) {
last;
}
my $x = PixelDelegate::ItemSize/2;
for (my $column = 0; $column < $columns; ++$column) {
$option->rect = Qt::Rect(int($x), int($y), PixelDelegate::ItemSize, PixelDelegate::ItemSize);
view->itemDelegate()->paint($painter, $option,
model->index($row, $column, $parent));
$x = $x + PixelDelegate::ItemSize;
}
$y = $y + PixelDelegate::ItemSize;
}
$progress->setValue($rows);
$painter->restore();
$painter->end();
if ($progress->wasCanceled()) {
Qt::MessageBox::information(this, this->tr('Printing canceled'),
this->tr('The printing process was canceled.'), Qt::MessageBox::Cancel());
}
#else
#Qt::MessageBox::information(this, this->tr('Printing canceled'),
#this->tr('Printing is not supported on this Qt build'), Qt::MessageBox::Cancel());
#endif
}
sub showAboutBox
{
Qt::MessageBox::about(this, this->tr('About the Pixelator example'),
this->tr("This example demonstrates how a standard view and a custom\n" .
"delegate can be used to produce a specialized representation\n" .
'of data in a simple custom model.'));
}
# [6]
sub updateView
{
view->resizeColumnsToContents();
view->resizeRowsToContents();
}
# [6]
1;
|