1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
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 <QtGui>
|
---|
42 |
|
---|
43 | #include <stdlib.h>
|
---|
44 |
|
---|
45 | #include "sortingbox.h"
|
---|
46 |
|
---|
47 | //! [0]
|
---|
48 | SortingBox::SortingBox()
|
---|
49 | {
|
---|
50 | //! [0] //! [1]
|
---|
51 | setMouseTracking(true);
|
---|
52 | //! [1] //! [2]
|
---|
53 | setBackgroundRole(QPalette::Base);
|
---|
54 | //! [2]
|
---|
55 |
|
---|
56 | itemInMotion = 0;
|
---|
57 |
|
---|
58 | //! [3]
|
---|
59 | newCircleButton = createToolButton(tr("New Circle"),
|
---|
60 | QIcon(":/images/circle.png"),
|
---|
61 | SLOT(createNewCircle()));
|
---|
62 |
|
---|
63 | newSquareButton = createToolButton(tr("New Square"),
|
---|
64 | QIcon(":/images/square.png"),
|
---|
65 | SLOT(createNewSquare()));
|
---|
66 |
|
---|
67 | newTriangleButton = createToolButton(tr("New Triangle"),
|
---|
68 | QIcon(":/images/triangle.png"),
|
---|
69 | SLOT(createNewTriangle()));
|
---|
70 |
|
---|
71 | circlePath.addEllipse(QRect(0, 0, 100, 100));
|
---|
72 | squarePath.addRect(QRect(0, 0, 100, 100));
|
---|
73 |
|
---|
74 | qreal x = trianglePath.currentPosition().x();
|
---|
75 | qreal y = trianglePath.currentPosition().y();
|
---|
76 | trianglePath.moveTo(x + 120 / 2, y);
|
---|
77 | trianglePath.lineTo(0, 100);
|
---|
78 | trianglePath.lineTo(120, 100);
|
---|
79 | trianglePath.lineTo(x + 120 / 2, y);
|
---|
80 |
|
---|
81 | //! [3] //! [4]
|
---|
82 | setWindowTitle(tr("Tool Tips"));
|
---|
83 | resize(500, 300);
|
---|
84 |
|
---|
85 | createShapeItem(circlePath, tr("Circle"), initialItemPosition(circlePath),
|
---|
86 | initialItemColor());
|
---|
87 | createShapeItem(squarePath, tr("Square"), initialItemPosition(squarePath),
|
---|
88 | initialItemColor());
|
---|
89 | createShapeItem(trianglePath, tr("Triangle"),
|
---|
90 | initialItemPosition(trianglePath), initialItemColor());
|
---|
91 | }
|
---|
92 | //! [4]
|
---|
93 |
|
---|
94 | //! [5]
|
---|
95 | bool SortingBox::event(QEvent *event)
|
---|
96 | {
|
---|
97 | //! [5] //! [6]
|
---|
98 | if (event->type() == QEvent::ToolTip) {
|
---|
99 | QHelpEvent *helpEvent = static_cast<QHelpEvent *>(event);
|
---|
100 | int index = itemAt(helpEvent->pos());
|
---|
101 | if (index != -1) {
|
---|
102 | QToolTip::showText(helpEvent->globalPos(), shapeItems[index].toolTip());
|
---|
103 | } else {
|
---|
104 | QToolTip::hideText();
|
---|
105 | event->ignore();
|
---|
106 | }
|
---|
107 |
|
---|
108 | return true;
|
---|
109 | }
|
---|
110 | return QWidget::event(event);
|
---|
111 | }
|
---|
112 | //! [6]
|
---|
113 |
|
---|
114 | //! [7]
|
---|
115 | void SortingBox::resizeEvent(QResizeEvent * /* event */)
|
---|
116 | {
|
---|
117 | int margin = style()->pixelMetric(QStyle::PM_DefaultTopLevelMargin);
|
---|
118 | int x = width() - margin;
|
---|
119 | int y = height() - margin;
|
---|
120 |
|
---|
121 | y = updateButtonGeometry(newCircleButton, x, y);
|
---|
122 | y = updateButtonGeometry(newSquareButton, x, y);
|
---|
123 | updateButtonGeometry(newTriangleButton, x, y);
|
---|
124 | }
|
---|
125 | //! [7]
|
---|
126 |
|
---|
127 | //! [8]
|
---|
128 | void SortingBox::paintEvent(QPaintEvent * /* event */)
|
---|
129 | {
|
---|
130 | QPainter painter(this);
|
---|
131 | painter.setRenderHint(QPainter::Antialiasing);
|
---|
132 | foreach (ShapeItem shapeItem, shapeItems) {
|
---|
133 | //! [8] //! [9]
|
---|
134 | painter.translate(shapeItem.position());
|
---|
135 | //! [9] //! [10]
|
---|
136 | painter.setBrush(shapeItem.color());
|
---|
137 | painter.drawPath(shapeItem.path());
|
---|
138 | painter.translate(-shapeItem.position());
|
---|
139 | }
|
---|
140 | }
|
---|
141 | //! [10]
|
---|
142 |
|
---|
143 | //! [11]
|
---|
144 | void SortingBox::mousePressEvent(QMouseEvent *event)
|
---|
145 | {
|
---|
146 | if (event->button() == Qt::LeftButton) {
|
---|
147 | int index = itemAt(event->pos());
|
---|
148 | if (index != -1) {
|
---|
149 | itemInMotion = &shapeItems[index];
|
---|
150 | previousPosition = event->pos();
|
---|
151 | shapeItems.move(index, shapeItems.size() - 1);
|
---|
152 | update();
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 | //! [11]
|
---|
157 |
|
---|
158 | //! [12]
|
---|
159 | void SortingBox::mouseMoveEvent(QMouseEvent *event)
|
---|
160 | {
|
---|
161 | if ((event->buttons() & Qt::LeftButton) && itemInMotion)
|
---|
162 | moveItemTo(event->pos());
|
---|
163 | }
|
---|
164 | //! [12]
|
---|
165 |
|
---|
166 | //! [13]
|
---|
167 | void SortingBox::mouseReleaseEvent(QMouseEvent *event)
|
---|
168 | {
|
---|
169 | if (event->button() == Qt::LeftButton && itemInMotion) {
|
---|
170 | moveItemTo(event->pos());
|
---|
171 | itemInMotion = 0;
|
---|
172 | }
|
---|
173 | }
|
---|
174 | //! [13]
|
---|
175 |
|
---|
176 | //! [14]
|
---|
177 | void SortingBox::createNewCircle()
|
---|
178 | {
|
---|
179 | static int count = 1;
|
---|
180 | createShapeItem(circlePath, tr("Circle <%1>").arg(++count),
|
---|
181 | randomItemPosition(), randomItemColor());
|
---|
182 | }
|
---|
183 | //! [14]
|
---|
184 |
|
---|
185 | //! [15]
|
---|
186 | void SortingBox::createNewSquare()
|
---|
187 | {
|
---|
188 | static int count = 1;
|
---|
189 | createShapeItem(squarePath, tr("Square <%1>").arg(++count),
|
---|
190 | randomItemPosition(), randomItemColor());
|
---|
191 | }
|
---|
192 | //! [15]
|
---|
193 |
|
---|
194 | //! [16]
|
---|
195 | void SortingBox::createNewTriangle()
|
---|
196 | {
|
---|
197 | static int count = 1;
|
---|
198 | createShapeItem(trianglePath, tr("Triangle <%1>").arg(++count),
|
---|
199 | randomItemPosition(), randomItemColor());
|
---|
200 | }
|
---|
201 | //! [16]
|
---|
202 |
|
---|
203 | //! [17]
|
---|
204 | int SortingBox::itemAt(const QPoint &pos)
|
---|
205 | {
|
---|
206 | for (int i = shapeItems.size() - 1; i >= 0; --i) {
|
---|
207 | const ShapeItem &item = shapeItems[i];
|
---|
208 | if (item.path().contains(pos - item.position()))
|
---|
209 | return i;
|
---|
210 | }
|
---|
211 | return -1;
|
---|
212 | }
|
---|
213 | //! [17]
|
---|
214 |
|
---|
215 | //! [18]
|
---|
216 | void SortingBox::moveItemTo(const QPoint &pos)
|
---|
217 | {
|
---|
218 | QPoint offset = pos - previousPosition;
|
---|
219 | itemInMotion->setPosition(itemInMotion->position() + offset);
|
---|
220 | //! [18] //! [19]
|
---|
221 | previousPosition = pos;
|
---|
222 | update();
|
---|
223 | }
|
---|
224 | //! [19]
|
---|
225 |
|
---|
226 | //! [20]
|
---|
227 | int SortingBox::updateButtonGeometry(QToolButton *button, int x, int y)
|
---|
228 | {
|
---|
229 | QSize size = button->sizeHint();
|
---|
230 | button->setGeometry(x - size.rwidth(), y - size.rheight(),
|
---|
231 | size.rwidth(), size.rheight());
|
---|
232 |
|
---|
233 | return y - size.rheight()
|
---|
234 | - style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing);
|
---|
235 | }
|
---|
236 | //! [20]
|
---|
237 |
|
---|
238 | //! [21]
|
---|
239 | void SortingBox::createShapeItem(const QPainterPath &path,
|
---|
240 | const QString &toolTip, const QPoint &pos,
|
---|
241 | const QColor &color)
|
---|
242 | {
|
---|
243 | ShapeItem shapeItem;
|
---|
244 | shapeItem.setPath(path);
|
---|
245 | shapeItem.setToolTip(toolTip);
|
---|
246 | shapeItem.setPosition(pos);
|
---|
247 | shapeItem.setColor(color);
|
---|
248 | shapeItems.append(shapeItem);
|
---|
249 | update();
|
---|
250 | }
|
---|
251 | //! [21]
|
---|
252 |
|
---|
253 | //! [22]
|
---|
254 | QToolButton *SortingBox::createToolButton(const QString &toolTip,
|
---|
255 | const QIcon &icon, const char *member)
|
---|
256 | {
|
---|
257 | QToolButton *button = new QToolButton(this);
|
---|
258 | button->setToolTip(toolTip);
|
---|
259 | button->setIcon(icon);
|
---|
260 | button->setIconSize(QSize(32, 32));
|
---|
261 | connect(button, SIGNAL(clicked()), this, member);
|
---|
262 |
|
---|
263 | return button;
|
---|
264 | }
|
---|
265 | //! [22]
|
---|
266 |
|
---|
267 | //! [23]
|
---|
268 | QPoint SortingBox::initialItemPosition(const QPainterPath &path)
|
---|
269 | {
|
---|
270 | int x;
|
---|
271 | int y = (height() - (int)path.controlPointRect().height()) / 2;
|
---|
272 | if (shapeItems.size() == 0)
|
---|
273 | x = ((3 * width()) / 2 - (int)path.controlPointRect().width()) / 2;
|
---|
274 | else
|
---|
275 | x = (width() / shapeItems.size()
|
---|
276 | - (int)path.controlPointRect().width()) / 2;
|
---|
277 |
|
---|
278 | return QPoint(x, y);
|
---|
279 | }
|
---|
280 | //! [23]
|
---|
281 |
|
---|
282 | //! [24]
|
---|
283 | QPoint SortingBox::randomItemPosition()
|
---|
284 | {
|
---|
285 | return QPoint(qrand() % (width() - 120), qrand() % (height() - 120));
|
---|
286 | }
|
---|
287 | //! [24]
|
---|
288 |
|
---|
289 | //! [25]
|
---|
290 | QColor SortingBox::initialItemColor()
|
---|
291 | {
|
---|
292 | return QColor::fromHsv(((shapeItems.size() + 1) * 85) % 256, 255, 190);
|
---|
293 | }
|
---|
294 | //! [25]
|
---|
295 |
|
---|
296 | //! [26]
|
---|
297 | QColor SortingBox::randomItemColor()
|
---|
298 | {
|
---|
299 | return QColor::fromHsv(qrand() % 256, 255, 190);
|
---|
300 | }
|
---|
301 | //! [26]
|
---|