|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 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:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
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 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #ifndef MAINWINDOW_H |
|
43 #define MAINWINDOW_H |
|
44 |
|
45 #include <QWidget> |
|
46 #include <QGraphicsPixmapItem> |
|
47 #include <QGraphicsWidget> |
|
48 #include <QGraphicsItem> |
|
49 #include <QGraphicsSceneMouseEvent> |
|
50 #include <QGraphicsView> |
|
51 #include <QStyleOptionGraphicsItem> |
|
52 |
|
53 |
|
54 class Pixmap : public QObject, public QGraphicsPixmapItem |
|
55 { |
|
56 Q_OBJECT |
|
57 Q_PROPERTY(QPointF pos READ pos WRITE setPos) |
|
58 public: |
|
59 Pixmap(const QPixmap &pix) |
|
60 : QObject(), QGraphicsPixmapItem(pix) |
|
61 { |
|
62 setCacheMode(DeviceCoordinateCache); |
|
63 } |
|
64 }; |
|
65 |
|
66 class Button : public QGraphicsWidget |
|
67 { |
|
68 Q_OBJECT |
|
69 public: |
|
70 Button(const QPixmap &pixmap, QGraphicsItem *parent = 0) |
|
71 : QGraphicsWidget(parent), _pix(pixmap) |
|
72 { |
|
73 setAcceptHoverEvents(true); |
|
74 setCacheMode(DeviceCoordinateCache); |
|
75 } |
|
76 |
|
77 QRectF boundingRect() const |
|
78 { |
|
79 return QRectF(-65, -65, 130, 130); |
|
80 } |
|
81 |
|
82 QPainterPath shape() const |
|
83 { |
|
84 QPainterPath path; |
|
85 path.addEllipse(boundingRect()); |
|
86 return path; |
|
87 } |
|
88 void click() |
|
89 { |
|
90 mousePressEvent( (QGraphicsSceneMouseEvent *)NULL ); |
|
91 mouseReleaseEvent( (QGraphicsSceneMouseEvent *)NULL ); |
|
92 } |
|
93 |
|
94 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) |
|
95 { |
|
96 bool down = option->state & QStyle::State_Sunken; |
|
97 QRectF r = boundingRect(); |
|
98 QLinearGradient grad(r.topLeft(), r.bottomRight()); |
|
99 grad.setColorAt(down ? 1 : 0, option->state & QStyle::State_MouseOver ? Qt::white : Qt::lightGray); |
|
100 grad.setColorAt(down ? 0 : 1, Qt::darkGray); |
|
101 painter->setPen(Qt::darkGray); |
|
102 painter->setBrush(grad); |
|
103 painter->drawEllipse(r); |
|
104 QLinearGradient grad2(r.topLeft(), r.bottomRight()); |
|
105 grad.setColorAt(down ? 1 : 0, Qt::darkGray); |
|
106 grad.setColorAt(down ? 0 : 1, Qt::lightGray); |
|
107 painter->setPen(Qt::NoPen); |
|
108 painter->setBrush(grad); |
|
109 if (down) |
|
110 painter->translate(2, 2); |
|
111 painter->drawEllipse(r.adjusted(5, 5, -5, -5)); |
|
112 painter->drawPixmap(-_pix.width()/2, -_pix.height()/2, _pix); |
|
113 } |
|
114 |
|
115 signals: |
|
116 void pressed(); |
|
117 |
|
118 protected: |
|
119 void mousePressEvent(QGraphicsSceneMouseEvent *) |
|
120 { |
|
121 emit pressed(); |
|
122 //update(); |
|
123 } |
|
124 |
|
125 void mouseReleaseEvent(QGraphicsSceneMouseEvent *) |
|
126 { |
|
127 //update(); |
|
128 } |
|
129 |
|
130 private: |
|
131 QPixmap _pix; |
|
132 }; |
|
133 |
|
134 class View : public QGraphicsView |
|
135 { |
|
136 public: |
|
137 View(QGraphicsScene *scene) : QGraphicsView(scene) { } |
|
138 |
|
139 protected: |
|
140 void resizeEvent(QResizeEvent *event) |
|
141 { |
|
142 QGraphicsView::resizeEvent(event); |
|
143 fitInView(sceneRect(), Qt::KeepAspectRatio); |
|
144 } |
|
145 }; |
|
146 |
|
147 |
|
148 //! [0] |
|
149 class MainWindow : public QWidget |
|
150 { |
|
151 Q_OBJECT |
|
152 |
|
153 public: |
|
154 MainWindow(QWidget *parent = 0); |
|
155 |
|
156 void showPosition(); |
|
157 |
|
158 Button * getEllipseButton() { return ellipseButton; }; |
|
159 Button *getFigure8Button() { return figure8Button; }; |
|
160 Button *getRandomButton() { return randomButton; }; |
|
161 Button *getTiledButton() { return tiledButton; }; |
|
162 Button *getCenteredButton() { return centeredButton; }; |
|
163 /* |
|
164 protected: |
|
165 void paintEvent(QPaintEvent *event); |
|
166 */ |
|
167 private: |
|
168 Button *ellipseButton; |
|
169 Button *figure8Button; |
|
170 Button *randomButton; |
|
171 Button *tiledButton; |
|
172 Button *centeredButton; |
|
173 |
|
174 QList<Pixmap *> items; |
|
175 |
|
176 }; |
|
177 //! [0] |
|
178 |
|
179 #endif // mainwindow_h |