source: trunk/examples/sql/drilldown/view.cpp@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information (qt-info@nokia.com)
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at qt-sales@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "informationwindow.h"
43#include "imageitem.h"
44#include "view.h"
45
46//! [0]
47View::View(const QString &offices, const QString &images, QWidget *parent)
48 : QGraphicsView(parent)
49{
50 officeTable = new QSqlRelationalTableModel(this);
51 officeTable->setTable(offices);
52 officeTable->setRelation(1, QSqlRelation(images, "locationid", "file"));
53 officeTable->select();
54//! [0]
55
56//! [1]
57 scene = new QGraphicsScene(this);
58 scene->setSceneRect(0, 0, 465, 615);
59 setScene(scene);
60
61 addItems();
62
63 QGraphicsPixmapItem *logo = scene->addPixmap(QPixmap(":/logo.png"));
64 logo->setPos(30, 515);
65
66 setMinimumSize(470, 620);
67 setMaximumSize(470, 620);
68 setWindowTitle(tr("Offices World Wide"));
69}
70//! [1]
71
72//! [3]
73void View::addItems()
74{
75 int officeCount = officeTable->rowCount();
76
77 int imageOffset = 150;
78 int leftMargin = 70;
79 int topMargin = 40;
80
81 for (int i = 0; i < officeCount; i++) {
82 ImageItem *image;
83 QGraphicsTextItem *label;
84 QSqlRecord record = officeTable->record(i);
85
86 int id = record.value("id").toInt();
87 QString file = record.value("file").toString();
88 QString location = record.value("location").toString();
89
90 int columnOffset = ((i / 3) * 37);
91 int x = ((i / 3) * imageOffset) + leftMargin + columnOffset;
92 int y = ((i % 3) * imageOffset) + topMargin;
93
94 image = new ImageItem(id, QPixmap(":/" + file));
95 image->setData(0, i);
96 image->setPos(x, y);
97 scene->addItem(image);
98
99 label = scene->addText(location);
100 QPointF labelOffset((150 - label->boundingRect().width()) / 2, 120.0);
101 label->setPos(QPointF(x, y) + labelOffset);
102 }
103}
104//! [3]
105
106//! [5]
107void View::mouseReleaseEvent(QMouseEvent *event)
108{
109 if (QGraphicsItem *item = itemAt(event->pos())) {
110 if (ImageItem *image = qgraphicsitem_cast<ImageItem *>(item))
111 showInformation(image);
112 }
113 QGraphicsView::mouseReleaseEvent(event);
114}
115//! [5]
116
117//! [6]
118void View::showInformation(ImageItem *image)
119{
120 int id = image->id();
121 if (id < 0 || id >= officeTable->rowCount())
122 return;
123
124 InformationWindow *window = findWindow(id);
125 if (window && window->isVisible()) {
126 window->raise();
127 window->activateWindow();
128 } else if (window && !window->isVisible()) {
129 window->show();
130 } else {
131 InformationWindow *window;
132 window = new InformationWindow(id, officeTable, this);
133
134 connect(window, SIGNAL(imageChanged(int, QString)),
135 this, SLOT(updateImage(int, QString)));
136
137 window->move(pos() + QPoint(20, 40));
138 window->show();
139 informationWindows.append(window);
140 }
141}
142//! [6]
143
144//! [7]
145void View::updateImage(int id, const QString &fileName)
146{
147 QList<QGraphicsItem *> items = scene->items();
148
149 while(!items.empty()) {
150 QGraphicsItem *item = items.takeFirst();
151
152 if (ImageItem *image = qgraphicsitem_cast<ImageItem *>(item)) {
153 if (image->id() == id){
154 image->setPixmap(QPixmap(":/" +fileName));
155 image->adjust();
156 break;
157 }
158 }
159 }
160}
161//! [7]
162
163//! [8]
164InformationWindow* View::findWindow(int id)
165{
166 QList<InformationWindow*>::iterator i, beginning, end;
167
168 beginning = informationWindows.begin();
169 end = informationWindows.end();
170
171 for (i = beginning; i != end; ++i) {
172 InformationWindow *window = (*i);
173 if (window && (window->id() == id))
174 return window;
175 }
176 return 0;
177}
178//! [8]
179
Note: See TracBrowser for help on using the repository browser.