source: trunk/examples/widgets/tooltips/sortingbox.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

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