| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the examples of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:BSD$ | 
|---|
| 10 | ** You may use this file under the terms of the BSD license as follows: | 
|---|
| 11 | ** | 
|---|
| 12 | ** "Redistribution and use in source and binary forms, with or without | 
|---|
| 13 | ** modification, are permitted provided that the following conditions are | 
|---|
| 14 | ** met: | 
|---|
| 15 | **   * Redistributions of source code must retain the above copyright | 
|---|
| 16 | **     notice, this list of conditions and the following disclaimer. | 
|---|
| 17 | **   * Redistributions in binary form must reproduce the above copyright | 
|---|
| 18 | **     notice, this list of conditions and the following disclaimer in | 
|---|
| 19 | **     the documentation and/or other materials provided with the | 
|---|
| 20 | **     distribution. | 
|---|
| 21 | **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor | 
|---|
| 22 | **     the names of its contributors may be used to endorse or promote | 
|---|
| 23 | **     products derived from this software without specific prior written | 
|---|
| 24 | **     permission. | 
|---|
| 25 | ** | 
|---|
| 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 
|---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
|---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
|---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
|---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
|---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." | 
|---|
| 37 | ** $QT_END_LICENSE$ | 
|---|
| 38 | ** | 
|---|
| 39 | ****************************************************************************/ | 
|---|
| 40 |  | 
|---|
| 41 | #include "informationwindow.h" | 
|---|
| 42 | #include "imageitem.h" | 
|---|
| 43 | #include "view.h" | 
|---|
| 44 |  | 
|---|
| 45 | //! [0] | 
|---|
| 46 | View::View(const QString &offices, const QString &images, QWidget *parent) | 
|---|
| 47 | : QGraphicsView(parent) | 
|---|
| 48 | { | 
|---|
| 49 | officeTable = new QSqlRelationalTableModel(this); | 
|---|
| 50 | officeTable->setTable(offices); | 
|---|
| 51 | officeTable->setRelation(1, QSqlRelation(images, "locationid", "file")); | 
|---|
| 52 | officeTable->select(); | 
|---|
| 53 | //! [0] | 
|---|
| 54 |  | 
|---|
| 55 | //! [1] | 
|---|
| 56 | scene = new QGraphicsScene(this); | 
|---|
| 57 | scene->setSceneRect(0, 0, 465, 615); | 
|---|
| 58 | setScene(scene); | 
|---|
| 59 |  | 
|---|
| 60 | addItems(); | 
|---|
| 61 |  | 
|---|
| 62 | QGraphicsPixmapItem *logo = scene->addPixmap(QPixmap(":/logo.png")); | 
|---|
| 63 | logo->setPos(30, 515); | 
|---|
| 64 |  | 
|---|
| 65 | #ifndef Q_OS_SYMBIAN | 
|---|
| 66 | setMinimumSize(470, 620); | 
|---|
| 67 | setMaximumSize(470, 620); | 
|---|
| 68 | #else | 
|---|
| 69 | setDragMode(QGraphicsView::ScrollHandDrag); | 
|---|
| 70 | #endif | 
|---|
| 71 |  | 
|---|
| 72 | setWindowTitle(tr("Offices World Wide")); | 
|---|
| 73 | } | 
|---|
| 74 | //! [1] | 
|---|
| 75 |  | 
|---|
| 76 | //! [3] | 
|---|
| 77 | void View::addItems() | 
|---|
| 78 | { | 
|---|
| 79 | int officeCount = officeTable->rowCount(); | 
|---|
| 80 |  | 
|---|
| 81 | int imageOffset = 150; | 
|---|
| 82 | int leftMargin = 70; | 
|---|
| 83 | int topMargin = 40; | 
|---|
| 84 |  | 
|---|
| 85 | for (int i = 0; i < officeCount; i++) { | 
|---|
| 86 | ImageItem *image; | 
|---|
| 87 | QGraphicsTextItem *label; | 
|---|
| 88 | QSqlRecord record = officeTable->record(i); | 
|---|
| 89 |  | 
|---|
| 90 | int id = record.value("id").toInt(); | 
|---|
| 91 | QString file = record.value("file").toString(); | 
|---|
| 92 | QString location = record.value("location").toString(); | 
|---|
| 93 |  | 
|---|
| 94 | int columnOffset = ((i / 3) * 37); | 
|---|
| 95 | int x = ((i / 3) * imageOffset) + leftMargin + columnOffset; | 
|---|
| 96 | int y = ((i % 3) * imageOffset) + topMargin; | 
|---|
| 97 |  | 
|---|
| 98 | image = new ImageItem(id, QPixmap(":/" + file)); | 
|---|
| 99 | image->setData(0, i); | 
|---|
| 100 | image->setPos(x, y); | 
|---|
| 101 | scene->addItem(image); | 
|---|
| 102 |  | 
|---|
| 103 | label = scene->addText(location); | 
|---|
| 104 | QPointF labelOffset((150 - label->boundingRect().width()) / 2, 120.0); | 
|---|
| 105 | label->setPos(QPointF(x, y) + labelOffset); | 
|---|
| 106 | } | 
|---|
| 107 | } | 
|---|
| 108 | //! [3] | 
|---|
| 109 |  | 
|---|
| 110 | //! [5] | 
|---|
| 111 | void View::mouseReleaseEvent(QMouseEvent *event) | 
|---|
| 112 | { | 
|---|
| 113 | if (QGraphicsItem *item = itemAt(event->pos())) { | 
|---|
| 114 | if (ImageItem *image = qgraphicsitem_cast<ImageItem *>(item)) | 
|---|
| 115 | showInformation(image); | 
|---|
| 116 | } | 
|---|
| 117 | QGraphicsView::mouseReleaseEvent(event); | 
|---|
| 118 | } | 
|---|
| 119 | //! [5] | 
|---|
| 120 |  | 
|---|
| 121 | //! [6] | 
|---|
| 122 | void View::showInformation(ImageItem *image) | 
|---|
| 123 | { | 
|---|
| 124 | int id = image->id(); | 
|---|
| 125 | if (id < 0 || id >= officeTable->rowCount()) | 
|---|
| 126 | return; | 
|---|
| 127 |  | 
|---|
| 128 | InformationWindow *window = findWindow(id); | 
|---|
| 129 | if (window && window->isVisible()) { | 
|---|
| 130 | window->raise(); | 
|---|
| 131 | window->activateWindow(); | 
|---|
| 132 | } else if (window && !window->isVisible()) { | 
|---|
| 133 | #ifndef Q_OS_SYMBIAN | 
|---|
| 134 | window->show(); | 
|---|
| 135 | #else | 
|---|
| 136 | window->showMaximized(); | 
|---|
| 137 | #endif | 
|---|
| 138 | } else { | 
|---|
| 139 | InformationWindow *window; | 
|---|
| 140 | window = new InformationWindow(id, officeTable, this); | 
|---|
| 141 |  | 
|---|
| 142 | connect(window, SIGNAL(imageChanged(int,QString)), | 
|---|
| 143 | this, SLOT(updateImage(int,QString))); | 
|---|
| 144 |  | 
|---|
| 145 | #ifndef Q_OS_SYMBIAN | 
|---|
| 146 | window->move(pos() + QPoint(20, 40)); | 
|---|
| 147 | window->show(); | 
|---|
| 148 | #else | 
|---|
| 149 | window->showMaximized(); | 
|---|
| 150 | #endif | 
|---|
| 151 | informationWindows.append(window); | 
|---|
| 152 | } | 
|---|
| 153 | } | 
|---|
| 154 | //! [6] | 
|---|
| 155 |  | 
|---|
| 156 | //! [7] | 
|---|
| 157 | void View::updateImage(int id, const QString &fileName) | 
|---|
| 158 | { | 
|---|
| 159 | QList<QGraphicsItem *> items = scene->items(); | 
|---|
| 160 |  | 
|---|
| 161 | while(!items.empty()) { | 
|---|
| 162 | QGraphicsItem *item = items.takeFirst(); | 
|---|
| 163 |  | 
|---|
| 164 | if (ImageItem *image = qgraphicsitem_cast<ImageItem *>(item)) { | 
|---|
| 165 | if (image->id() == id){ | 
|---|
| 166 | image->setPixmap(QPixmap(":/" +fileName)); | 
|---|
| 167 | image->adjust(); | 
|---|
| 168 | break; | 
|---|
| 169 | } | 
|---|
| 170 | } | 
|---|
| 171 | } | 
|---|
| 172 | } | 
|---|
| 173 | //! [7] | 
|---|
| 174 |  | 
|---|
| 175 | //! [8] | 
|---|
| 176 | InformationWindow* View::findWindow(int id) | 
|---|
| 177 | { | 
|---|
| 178 | QList<InformationWindow*>::iterator i, beginning, end; | 
|---|
| 179 |  | 
|---|
| 180 | beginning = informationWindows.begin(); | 
|---|
| 181 | end = informationWindows.end(); | 
|---|
| 182 |  | 
|---|
| 183 | for (i = beginning; i != end; ++i) { | 
|---|
| 184 | InformationWindow *window = (*i); | 
|---|
| 185 | if (window && (window->id() == id)) | 
|---|
| 186 | return window; | 
|---|
| 187 | } | 
|---|
| 188 | return 0; | 
|---|
| 189 | } | 
|---|
| 190 | //! [8] | 
|---|
| 191 |  | 
|---|