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
|
package View;
use strict;
use warnings;
use QtCore4;
use QtGui4;
use QtCore4::isa qw( Qt::GraphicsView );
use QtCore4::slots
updateImage => ['int', 'const QString &'];
use InformationWindow;
use ImageItem;
# [0]
sub NEW
{
my ($class, $offices, $images, $parent) = @_;
$class->SUPER::NEW( $parent );
this->{officeTable} = Qt::SqlRelationalTableModel(this);
this->{officeTable}->setTable($offices);
this->{officeTable}->setRelation(1, Qt::SqlRelation($images, 'locationid', 'file'));
this->{officeTable}->select();
# [0]
# [1]
this->{scene} = Qt::GraphicsScene(this);
this->{scene}->setSceneRect(0, 0, 465, 615);
this->setScene(this->{scene});
this->addItems();
my $logo = this->{scene}->addPixmap(Qt::Pixmap('logo.png'));
$logo->setPos(30, 515);
this->setMinimumSize(470, 620);
this->setMaximumSize(470, 620);
this->setWindowTitle(this->tr('Offices World Wide'));
}
# [1]
# [3]
sub addItems
{
my $officeCount = this->{officeTable}->rowCount();
my $imageOffset = 150;
my $leftMargin = 70;
my $topMargin = 40;
foreach my $i ( 0..$officeCount-1 ) {
my $image;
my $label;
my $record = this->{officeTable}->record($i);
my $id = $record->value('id')->toInt();
my $file = $record->value('file')->toString();
my $location = $record->value('location')->toString();
my $columnOffset = (sprintf( '%d', ($i / 3)) * 37);
my $x = (sprintf( '%d', ($i / 3)) * $imageOffset) + $leftMargin + $columnOffset;
my $y = (sprintf( '%d', ($i % 3)) * $imageOffset) + $topMargin;
$image = ImageItem($id, Qt::Pixmap($file));
$image->setData(0, Qt::Variant(Qt::Int($i)));
$image->setPos($x, $y);
this->{scene}->addItem($image);
# XXX Remove this once Issue 22 is resolved.
push @{this->{images}}, $image;
$label = this->{scene}->addText($location);
my $labelOffset = Qt::PointF((150 - $label->boundingRect()->width()) / 2, 120.0);
$label->setPos(Qt::PointF($x, $y) + $labelOffset);
}
}
# [3]
# [5]
sub mouseReleaseEvent
{
my ($event) = @_;
if (my $item = this->itemAt($event->pos())) {
if ($item->isa('ImageItem')) {
this->showInformation($item);
}
}
this->SUPER::mouseReleaseEvent($event);
}
# [5]
# [6]
sub showInformation
{
my ($image) = @_;
my $id = $image->id();
if ($id < 0 || $id >= this->{officeTable}->rowCount()) {
return;
}
my $window = this->findWindow($id);
if ($window && $window->isVisible()) {
$window->raise();
$window->activateWindow();
} elsif ($window && !$window->isVisible()) {
$window->show();
} else {
my $window = InformationWindow($id, this->{officeTable}, this);
this->connect($window, SIGNAL 'imageChanged(int, QString)',
this, SLOT 'updateImage(int, QString)');
$window->move(this->pos() + Qt::Point(20, 40));
$window->show();
push @{this->{informationWindows}}, $window;
}
}
# [6]
# [7]
sub updateImage
{
my ($id, $fileName) = @_;
my $items = this->{scene}->items();
foreach my $item (@{$items}) {
if ($item->isa('ImageItem')) {
my $image = $item;
if ($image->id() == $id){
$image->setPixmap(Qt::Pixmap($fileName));
$image->adjust();
last;
}
}
}
}
# [7]
# [8]
sub findWindow
{
my ($id) = @_;
foreach my $window ( @{this->{informationWindows}} ) {
if ( $window->id() == $id ) {
return $window;
}
}
return;
}
# [8]
1;
|