1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include "informationwindow.h" |
52 | #include "imageitem.h" |
53 | #include "view.h" |
54 | |
55 | //! [0] |
56 | View::View(const QString &items, const QString &images, QWidget *parent) |
57 | : QGraphicsView(parent) |
58 | { |
59 | itemTable = new QSqlRelationalTableModel(this); |
60 | itemTable->setTable(items); |
61 | itemTable->setRelation(column: 1, relation: QSqlRelation(images, "itemid" , "file" )); |
62 | itemTable->select(); |
63 | //! [0] |
64 | |
65 | //! [1] |
66 | scene = new QGraphicsScene(this); |
67 | scene->setSceneRect(x: 0, y: 0, w: 465, h: 365); |
68 | setScene(scene); |
69 | |
70 | addItems(); |
71 | |
72 | setMinimumSize(minw: 470, minh: 370); |
73 | setMaximumSize(maxw: 470, maxh: 370); |
74 | |
75 | QLinearGradient gradient(QPointF(0, 0), QPointF(0, 370)); |
76 | gradient.setColorAt(pos: 0, color: QColor("#868482" )); |
77 | gradient.setColorAt(pos: 1, color: QColor("#5d5b59" )); |
78 | setBackgroundBrush(gradient); |
79 | } |
80 | //! [1] |
81 | |
82 | //! [3] |
83 | void View::addItems() |
84 | { |
85 | int itemCount = itemTable->rowCount(); |
86 | |
87 | int imageOffset = 150; |
88 | int leftMargin = 70; |
89 | int topMargin = 40; |
90 | |
91 | for (int i = 0; i < itemCount; i++) { |
92 | QSqlRecord record = itemTable->record(row: i); |
93 | |
94 | int id = record.value(name: "id" ).toInt(); |
95 | QString file = record.value(name: "file" ).toString(); |
96 | QString item = record.value(name: "itemtype" ).toString(); |
97 | |
98 | int columnOffset = ((i % 2) * 37); |
99 | int x = ((i % 2) * imageOffset) + leftMargin + columnOffset; |
100 | int y = ((i / 2) * imageOffset) + topMargin; |
101 | |
102 | ImageItem *image = new ImageItem(id, QPixmap(":/" + file)); |
103 | image->setData(key: 0, value: i); |
104 | image->setPos(ax: x, ay: y); |
105 | scene->addItem(item: image); |
106 | |
107 | QGraphicsTextItem *label = scene->addText(text: item); |
108 | label->setDefaultTextColor(QColor("#d7d6d5" )); |
109 | QPointF labelOffset((120 - label->boundingRect().width()) / 2, 120.0); |
110 | label->setPos(QPointF(x, y) + labelOffset); |
111 | } |
112 | } |
113 | //! [3] |
114 | |
115 | //! [5] |
116 | void View::mouseReleaseEvent(QMouseEvent *event) |
117 | { |
118 | if (QGraphicsItem *item = itemAt(pos: event->pos())) { |
119 | if (ImageItem *image = qgraphicsitem_cast<ImageItem *>(item)) |
120 | showInformation(image); |
121 | } |
122 | QGraphicsView::mouseReleaseEvent(event); |
123 | } |
124 | //! [5] |
125 | |
126 | //! [6] |
127 | void View::showInformation(ImageItem *image) |
128 | { |
129 | int id = image->id(); |
130 | if (id < 0 || id >= itemTable->rowCount()) |
131 | return; |
132 | |
133 | InformationWindow *window = findWindow(id); |
134 | if (!window) { |
135 | window = new InformationWindow(id, itemTable, this); |
136 | |
137 | connect(sender: window, signal: QOverload<int,const QString &>::of(ptr: &InformationWindow::imageChanged), |
138 | receiver: this, slot: QOverload<int,const QString &>::of(ptr: &View::updateImage)); |
139 | |
140 | window->move(pos() + QPoint(20, 40)); |
141 | window->show(); |
142 | informationWindows.append(t: window); |
143 | } |
144 | |
145 | if (window->isVisible()) { |
146 | window->raise(); |
147 | window->activateWindow(); |
148 | } else |
149 | window->show(); |
150 | } |
151 | //! [6] |
152 | |
153 | //! [7] |
154 | void View::updateImage(int id, const QString &fileName) |
155 | { |
156 | QList<QGraphicsItem *> items = scene->items(); |
157 | |
158 | while(!items.empty()) { |
159 | QGraphicsItem *item = items.takeFirst(); |
160 | |
161 | if (ImageItem *image = qgraphicsitem_cast<ImageItem *>(item)) { |
162 | if (image->id() == id){ |
163 | image->setPixmap(QPixmap(":/" +fileName)); |
164 | image->adjust(); |
165 | break; |
166 | } |
167 | } |
168 | } |
169 | } |
170 | //! [7] |
171 | |
172 | //! [8] |
173 | InformationWindow *View::findWindow(int id) const |
174 | { |
175 | for (auto window : informationWindows) { |
176 | if (window && (window->id() == id)) |
177 | return window; |
178 | } |
179 | return nullptr; |
180 | } |
181 | //! [8] |
182 | |
183 | |