45
|
1 /****************************************************************************
|
|
2 **
|
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 ** Contact: Qt Software Information (qt-info@nokia.com)
|
|
5 **
|
|
6 ** This file is part of the demonstration applications of the Qt Toolkit.
|
|
7 **
|
|
8 ** $QT_BEGIN_LICENSE:LGPL$
|
|
9 ** Commercial Usage
|
|
10 ** Licensees holding valid Qt Commercial licenses may use this file in
|
|
11 ** accordance with the Qt Commercial License Agreement provided with the
|
|
12 ** Software or, alternatively, in accordance with the terms contained in
|
|
13 ** a written agreement between you and Nokia.
|
|
14 **
|
|
15 ** GNU Lesser General Public License Usage
|
|
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
17 ** General Public License version 2.1 as published by the Free Software
|
|
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
19 ** packaging of this file. Please review the following information to
|
|
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
22 **
|
|
23 ** In addition, as a special exception, Nokia gives you certain
|
|
24 ** additional rights. These rights are described in the Nokia Qt LGPL
|
|
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|
26 ** 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 are unsure which license is appropriate for your use, please
|
|
37 ** contact the sales department at qt-sales@nokia.com.
|
|
38 ** $QT_END_LICENSE$
|
|
39 **
|
|
40 ****************************************************************************/
|
|
41
|
|
42 module chasewidget;
|
|
43
|
|
44
|
|
45 import QtGui.QWidget;
|
|
46
|
|
47 import QtCore.QSize;
|
|
48 import QtGui.QColor;
|
|
49 import QtGui.QPixmap;
|
|
50
|
|
51 import QtCore.QPoint;
|
|
52
|
|
53 import QtGui.QApplication;
|
|
54 import QtGui.QHideEvent;
|
|
55 import QtGui.QPainter;
|
|
56 import QtGui.QPaintEvent;
|
|
57 import QtGui.QShowEvent;
|
|
58
|
|
59 /*
|
|
60 QT_BEGIN_NAMESPACE
|
|
61 class QHideEvent;
|
|
62 class QShowEvent;
|
|
63 class QPaintEvent;
|
|
64 class QTimerEvent;
|
|
65 QT_END_NAMESPACE
|
|
66 */
|
|
67
|
|
68 class ChaseWidget : public QWidget
|
|
69 {
|
|
70 Q_OBJECT
|
|
71 public:
|
|
72 this(QWidget *parent = 0, QPixmap pixmap = QPixmap(), bool pixmapEnabled = false)
|
|
73 {
|
|
74 super(parent);
|
|
75 m_segment = 0;
|
|
76 m_delay = 100;
|
|
77 m_step = 40;
|
|
78 m_timerId = -1;
|
|
79 m_animated = false;
|
|
80 m_pixmap = pixmap;
|
|
81 m_pixmapEnabled = pixmapEnabled;
|
|
82 }
|
|
83
|
|
84 void setAnimated(bool value)
|
|
85 {
|
|
86 if (m_animated == value)
|
|
87 return;
|
|
88 m_animated = value;
|
|
89 if (m_timerId != -1) {
|
|
90 killTimer(m_timerId);
|
|
91 m_timerId = -1;
|
|
92 }
|
|
93 if (m_animated) {
|
|
94 m_segment = 0;
|
|
95 m_timerId = startTimer(m_delay);
|
|
96 }
|
|
97 update();
|
|
98 }
|
|
99
|
|
100 void setPixmapEnabled(bool enable);
|
|
101 {
|
|
102 m_pixmapEnabled = enable;
|
|
103 }
|
|
104
|
|
105
|
|
106 QSize sizeHint()
|
|
107 {
|
|
108 return QSize(32, 32);
|
|
109 }
|
|
110
|
|
111 protected:
|
|
112 void paintEvent(QPaintEvent *event)
|
|
113 {
|
|
114 Q_UNUSED(event);
|
|
115 QPainter p(this);
|
|
116 if (m_pixmapEnabled && !m_pixmap.isNull()) {
|
|
117 p.drawPixmap(0, 0, m_pixmap);
|
|
118 return;
|
|
119 }
|
|
120
|
|
121 int extent = qMin(width() - 8, height() - 8);
|
|
122 int displ = extent / 4;
|
|
123 int ext = extent / 4 - 1;
|
|
124
|
|
125 p.setRenderHint(QPainter::Antialiasing, true);
|
|
126
|
|
127 if(m_animated)
|
|
128 p.setPen(Qt.gray);
|
|
129 else
|
|
130 p.setPen(QPen(palette().dark().color()));
|
|
131
|
|
132 p.translate(width() / 2, height() / 2); // center
|
|
133
|
|
134 for (int segment = 0; segment < segmentCount(); ++segment) {
|
|
135 p.rotate(QApplication::isRightToLeft() ? m_step : -m_step);
|
|
136 if(m_animated)
|
|
137 p.setBrush(colorForSegment(segment));
|
|
138 else
|
|
139 p.setBrush(palette().background());
|
|
140 p.drawEllipse(QRect(displ, -ext / 2, ext, ext));
|
|
141 }
|
|
142 }
|
|
143
|
|
144 void timerEvent(QTimerEvent *event)
|
|
145 {
|
|
146 if (event->timerId() == m_timerId) {
|
|
147 ++m_segment;
|
|
148 update();
|
|
149 }
|
|
150 QWidget.timerEvent(event);
|
|
151 }
|
|
152
|
|
153 private:
|
|
154 int segmentCount()
|
|
155 {
|
|
156 return 360 / m_step;
|
|
157 }
|
|
158
|
|
159
|
|
160 QColor colorForSegment(int seg)
|
|
161 {
|
|
162 int index = ((seg + m_segment) % segmentCount());
|
|
163 int comp = qMax(0, 255 - (index * (255 / segmentCount())));
|
|
164 return QColor(comp, comp, comp, 255);
|
|
165 }
|
|
166
|
|
167 int m_segment;
|
|
168 int m_delay;
|
|
169 int m_step;
|
|
170 int m_timerId;
|
|
171 bool m_animated;
|
|
172 QPixmap m_pixmap;
|
|
173 bool m_pixmapEnabled;
|
|
174 }
|