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
|
73
|
45 import qt.core.QSize;
|
|
46 import qt.core.QPoint;
|
45
|
47
|
73
|
48 import qt.gui.QColor;
|
|
49 import qt.gui.QPixmap;
|
|
50 import qt.gui.QWidget;
|
|
51 import qt.gui.QApplication;
|
|
52 import qt.gui.QHideEvent;
|
|
53 import qt.gui.QPainter;
|
|
54 import qt.gui.QPaintEvent;
|
|
55 import qt.gui.QShowEvent;
|
45
|
56
|
|
57
|
|
58 class ChaseWidget : public QWidget
|
|
59 {
|
|
60 public:
|
73
|
61
|
94
|
62 this(QWidget parent = null, QPixmap pixmap = new QPixmap(), bool pixmapEnabled = false)
|
73
|
63 {
|
|
64 super(parent);
|
|
65 m_segment = 0;
|
|
66 m_delay = 100;
|
|
67 m_step = 40;
|
|
68 m_timerId = -1;
|
|
69 m_animated = false;
|
|
70 m_pixmap = pixmap;
|
|
71 m_pixmapEnabled = pixmapEnabled;
|
|
72 }
|
45
|
73
|
73
|
74 void setAnimated(bool value)
|
|
75 {
|
|
76 if (m_animated == value)
|
|
77 return;
|
|
78 m_animated = value;
|
|
79 if (m_timerId != -1) {
|
|
80 killTimer(m_timerId);
|
|
81 m_timerId = -1;
|
|
82 }
|
|
83 if (m_animated) {
|
|
84 m_segment = 0;
|
|
85 m_timerId = startTimer(m_delay);
|
|
86 }
|
|
87 update();
|
|
88 }
|
45
|
89
|
74
|
90 void setPixmapEnabled(bool enable)
|
73
|
91 {
|
|
92 m_pixmapEnabled = enable;
|
|
93 }
|
|
94
|
|
95 QSize sizeHint()
|
|
96 {
|
|
97 return QSize(32, 32);
|
|
98 }
|
45
|
99
|
|
100 protected:
|
73
|
101
|
|
102 void paintEvent(QPaintEvent event)
|
|
103 {
|
|
104 auto p = new QPainter(this);
|
|
105 if (m_pixmapEnabled && !m_pixmap.isNull()) {
|
|
106 p.drawPixmap(0, 0, m_pixmap);
|
|
107 return;
|
|
108 }
|
45
|
109
|
73
|
110 int extent = qMin(width() - 8, height() - 8);
|
|
111 int displ = extent / 4;
|
|
112 int ext = extent / 4 - 1;
|
45
|
113
|
74
|
114 p.setRenderHint(QPainter.Antialiasing, true);
|
45
|
115
|
73
|
116 if(m_animated)
|
|
117 p.setPen(Qt.gray);
|
|
118 else
|
|
119 p.setPen(QPen(palette().dark().color()));
|
45
|
120
|
73
|
121 p.translate(width() / 2, height() / 2); // center
|
45
|
122
|
73
|
123 for (int segment = 0; segment < segmentCount(); ++segment) {
|
74
|
124 p.rotate(QApplication.isRightToLeft() ? m_step : -m_step);
|
73
|
125 if(m_animated)
|
|
126 p.setBrush(colorForSegment(segment));
|
|
127 else
|
|
128 p.setBrush(palette().background());
|
|
129 p.drawEllipse(QRect(displ, -ext / 2, ext, ext));
|
|
130 }
|
|
131 }
|
45
|
132
|
74
|
133 void timerEvent(QTimerEvent event)
|
73
|
134 {
|
74
|
135 if (event.timerId() == m_timerId) {
|
73
|
136 ++m_segment;
|
|
137 update();
|
|
138 }
|
|
139 QWidget.timerEvent(event);
|
|
140 }
|
45
|
141
|
|
142 private:
|
|
143
|
73
|
144 int segmentCount()
|
|
145 {
|
|
146 return 360 / m_step;
|
|
147 }
|
45
|
148
|
73
|
149 QColor colorForSegment(int seg)
|
|
150 {
|
|
151 int index = ((seg + m_segment) % segmentCount());
|
|
152 int comp = qMax(0, 255 - (index * (255 / segmentCount())));
|
|
153 return QColor(comp, comp, comp, 255);
|
|
154 }
|
45
|
155
|
73
|
156 int m_segment;
|
|
157 int m_delay;
|
|
158 int m_step;
|
|
159 int m_timerId;
|
|
160 bool m_animated;
|
|
161 QPixmap m_pixmap;
|
|
162 bool m_pixmapEnabled;
|
45
|
163 }
|