Mercurial > projects > qtd
annotate examples/opengl/hellogl/glwidget.d @ 323:7a3c43424dca signals
make all examples compile with new signals/slots
author | eldar_ins@eldar-laptop |
---|---|
date | Mon, 28 Dec 2009 16:28:55 +0500 |
parents | 1f6923c8cba0 |
children |
rev | line source |
---|---|
1 | 1 /**************************************************************************** |
2 ** | |
3 ** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies). | |
4 ** Contact: Qt Software Information (qt-info@nokia.com) | |
5 ** | |
6 ** This file is part of the example classes of the Qt Toolkit. | |
7 ** | |
8 ** Commercial Usage | |
9 ** Licensees holding valid Qt Commercial licenses may use this file in | |
10 ** accordance with the Qt Commercial License Agreement provided with the | |
11 ** Software or, alternatively, in accordance with the terms contained in | |
12 ** a written agreement between you and Nokia. | |
13 ** | |
14 ** | |
15 ** GNU General Public License Usage | |
16 ** Alternatively, this file may be used under the terms of the GNU | |
17 ** General Public License versions 2.0 or 3.0 as published by the Free | |
18 ** Software Foundation and appearing in the file LICENSE.GPL included in | |
19 ** the packaging of this file. Please review the following information | |
20 ** to ensure GNU General Public Licensing requirements will be met: | |
21 ** http://www.fsf.org/licensing/licenses/info/GPLv2.html and | |
22 ** http://www.gnu.org/copyleft/gpl.html. In addition, as a special | |
23 ** exception, Nokia gives you certain additional rights. These rights | |
24 ** are described in the Nokia Qt GPL Exception version 1.3, included in | |
25 ** the file GPL_EXCEPTION.txt in this package. | |
26 ** | |
27 ** Qt for Windows(R) Licensees | |
28 ** As a special exception, Nokia, as the sole copyright holder for Qt | |
29 ** Designer, grants users of the Qt/Eclipse Integration plug-in the | |
30 ** right for the Qt/Eclipse Integration to link to functionality | |
31 ** provided by Qt Designer and its related libraries. | |
32 ** | |
33 ** If you are unsure which license is appropriate for your use, please | |
34 ** contact the sales department at qt-sales@nokia.com. | |
35 ** | |
36 ****************************************************************************/ | |
37 | |
215 | 38 import std.math; |
39 import std.conv; | |
1 | 40 |
41 import qt.core.QPoint; | |
42 import qt.gui.QMouseEvent; | |
43 import qt.opengl.QGLWidget; | |
44 import qt.gui.QColor; | |
45 import qt.core.QSize; | |
46 import qt.opengl.gl; | |
47 import qt.opengl.glu; | |
48 | |
49 class GLWidget : QGLWidget | |
50 { | |
51 // Q_OBJECT | |
5 | 52 |
1 | 53 public: |
54 this(QWidget parent = null) | |
55 { | |
56 super(parent); | |
57 object = 0; | |
58 xRot = 0; | |
59 yRot = 0; | |
60 zRot = 0; | |
5 | 61 |
1 | 62 trolltechGreen = QColor.fromCmykF(0.40, 0.0, 1.0, 0.0); |
63 trolltechPurple = QColor.fromCmykF(0.39, 0.39, 0.0, 0.0); | |
64 } | |
5 | 65 |
1 | 66 ~this() |
67 { | |
68 makeCurrent(); | |
69 glDeleteLists(object, 1); | |
5 | 70 } |
71 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
284
diff
changeset
|
72 QSize minimumSizeHint() const |
1 | 73 { |
74 return QSize(50, 50); | |
75 } | |
76 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
284
diff
changeset
|
77 QSize sizeHint() const |
1 | 78 { |
79 return QSize(400, 400); | |
80 } | |
81 | |
82 | |
83 public: // slots: | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
284
diff
changeset
|
84 void slot_setXRotation(int angle) |
1 | 85 { |
86 normalizeAngle(&angle); | |
87 if (angle != xRot) { | |
88 xRot = angle; | |
284 | 89 xRotationChanged(angle); |
1 | 90 updateGL(); |
91 } | |
92 } | |
5 | 93 |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
284
diff
changeset
|
94 void slot_setYRotation(int angle) |
1 | 95 { |
96 normalizeAngle(&angle); | |
97 if (angle != yRot) { | |
98 yRot = angle; | |
284 | 99 yRotationChanged(angle); |
1 | 100 updateGL(); |
101 } | |
102 } | |
5 | 103 |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
284
diff
changeset
|
104 void slot_setZRotation(int angle) |
1 | 105 { |
106 normalizeAngle(&angle); | |
107 if (angle != zRot) { | |
108 zRot = angle; | |
284 | 109 zRotationChanged(angle); |
1 | 110 updateGL(); |
111 } | |
112 } | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
284
diff
changeset
|
113 |
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
284
diff
changeset
|
114 final // signals |
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
284
diff
changeset
|
115 { |
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
284
diff
changeset
|
116 void signal_xRotationChanged(int); |
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
284
diff
changeset
|
117 void signal_yRotationChanged(int); |
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
284
diff
changeset
|
118 void signal_zRotationChanged(int); |
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
284
diff
changeset
|
119 } |
1 | 120 |
121 protected: | |
122 void initializeGL() | |
123 { | |
124 qglClearColor(trolltechPurple.darker()); | |
125 object = makeObject(); | |
126 glShadeModel(GL_FLAT); | |
127 glEnable(GL_DEPTH_TEST); | |
128 glEnable(GL_CULL_FACE); | |
129 } | |
130 | |
131 void paintGL() | |
132 { | |
133 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
134 glLoadIdentity(); | |
135 glTranslated(0.0, 0.0, -10.0); | |
136 glRotated(xRot / 16.0, 1.0, 0.0, 0.0); | |
137 glRotated(yRot / 16.0, 0.0, 1.0, 0.0); | |
138 glRotated(zRot / 16.0, 0.0, 0.0, 1.0); | |
139 glCallList(object); | |
140 } | |
141 | |
142 void resizeGL(int width, int height) | |
143 { | |
144 int side = qMin(width, height); | |
145 glViewport((width - side) / 2, (height - side) / 2, side, side); | |
5 | 146 |
1 | 147 glMatrixMode(GL_PROJECTION); |
148 glLoadIdentity(); | |
149 glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0); | |
150 glMatrixMode(GL_MODELVIEW); | |
151 } | |
152 | |
153 void mousePressEvent(QMouseEvent event) | |
154 { | |
155 lastPos = QPoint(event.pos.x, event.pos.y); | |
156 } | |
5 | 157 |
1 | 158 void mouseMoveEvent(QMouseEvent event) |
159 { | |
160 int dx = event.x - lastPos.x; | |
161 int dy = event.y - lastPos.y; | |
5 | 162 |
1 | 163 if (event.buttons() & Qt.LeftButton) { |
164 setXRotation(xRot + 8 * dy); | |
165 setYRotation(yRot + 8 * dx); | |
166 } else if (event.buttons() & Qt.RightButton) { | |
167 setXRotation(xRot + 8 * dy); | |
168 setZRotation(zRot + 8 * dx); | |
169 } | |
170 lastPos = QPoint(event.pos.x, event.pos.y); | |
171 } | |
172 private: | |
173 GLuint makeObject() | |
174 { | |
175 GLuint list = glGenLists(1); | |
176 glNewList(list, GL_COMPILE); | |
5 | 177 |
1 | 178 glBegin(GL_QUADS); |
5 | 179 |
1 | 180 GLdouble x1 = +0.06; |
181 GLdouble y1 = -0.14; | |
182 GLdouble x2 = +0.14; | |
183 GLdouble y2 = -0.06; | |
184 GLdouble x3 = +0.08; | |
185 GLdouble y3 = +0.00; | |
186 GLdouble x4 = +0.30; | |
187 GLdouble y4 = +0.22; | |
5 | 188 |
1 | 189 quad(x1, y1, x2, y2, y2, x2, y1, x1); |
190 quad(x3, y3, x4, y4, y4, x4, y3, x3); | |
5 | 191 |
1 | 192 extrude(x1, y1, x2, y2); |
193 extrude(x2, y2, y2, x2); | |
194 extrude(y2, x2, y1, x1); | |
195 extrude(y1, x1, x1, y1); | |
196 extrude(x3, y3, x4, y4); | |
197 extrude(x4, y4, y4, x4); | |
198 extrude(y4, x4, y3, x3); | |
5 | 199 |
1 | 200 const double Pi = 3.14159265358979323846; |
201 const int NumSectors = 200; | |
5 | 202 |
1 | 203 for (int i = 0; i < NumSectors; ++i) { |
204 double angle1 = (i * 2 * Pi) / NumSectors; | |
205 GLdouble x5 = 0.30 * sin(angle1); | |
206 GLdouble y5 = 0.30 * cos(angle1); | |
207 GLdouble x6 = 0.20 * sin(angle1); | |
208 GLdouble y6 = 0.20 * cos(angle1); | |
5 | 209 |
1 | 210 double angle2 = ((i + 1) * 2 * Pi) / NumSectors; |
211 GLdouble x7 = 0.20 * sin(angle2); | |
212 GLdouble y7 = 0.20 * cos(angle2); | |
213 GLdouble x8 = 0.30 * sin(angle2); | |
214 GLdouble y8 = 0.30 * cos(angle2); | |
5 | 215 |
1 | 216 quad(x5, y5, x6, y6, x7, y7, x8, y8); |
5 | 217 |
1 | 218 extrude(x6, y6, x7, y7); |
219 extrude(x8, y8, x5, y5); | |
220 } | |
5 | 221 |
1 | 222 glEnd(); |
223 | |
224 glEndList(); | |
225 return list; | |
226 } | |
5 | 227 |
1 | 228 void quad(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2, |
229 GLdouble x3, GLdouble y3, GLdouble x4, GLdouble y4) | |
230 { | |
231 qglColor(trolltechGreen); | |
5 | 232 |
1 | 233 glVertex3d(x1, y1, -0.05); |
234 glVertex3d(x2, y2, -0.05); | |
235 glVertex3d(x3, y3, -0.05); | |
236 glVertex3d(x4, y4, -0.05); | |
5 | 237 |
1 | 238 glVertex3d(x4, y4, +0.05); |
239 glVertex3d(x3, y3, +0.05); | |
240 glVertex3d(x2, y2, +0.05); | |
241 glVertex3d(x1, y1, +0.05); | |
242 } | |
5 | 243 |
1 | 244 void extrude(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2) |
245 { | |
215 | 246 qglColor(trolltechGreen.darker(to!(int)(rndtol(250 + (100 * x1))))); |
5 | 247 |
1 | 248 glVertex3d(x1, y1, +0.05); |
249 glVertex3d(x2, y2, +0.05); | |
250 glVertex3d(x2, y2, -0.05); | |
251 glVertex3d(x1, y1, -0.05); | |
252 } | |
5 | 253 |
1 | 254 void normalizeAngle(int *angle) |
255 { | |
256 while (*angle < 0) | |
257 *angle += 360 * 16; | |
258 while (*angle > 360 * 16) | |
259 *angle -= 360 * 16; | |
260 } | |
5 | 261 |
1 | 262 GLuint object; |
263 int xRot; | |
264 int yRot; | |
265 int zRot; | |
266 QPoint lastPos; | |
267 QColor trolltechGreen; | |
268 QColor trolltechPurple; | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
284
diff
changeset
|
269 |
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
284
diff
changeset
|
270 mixin Q_OBJECT; |
1 | 271 } |