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