41
|
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 ****************************************************************************/
|
161
|
41 module pathdeform;
|
|
42
|
41
|
43
|
|
44 import arthurwidgets,
|
|
45 qt.gui.QPainterPath,
|
|
46
|
|
47 qt.gui.QApplication,
|
|
48 qt.gui.QMouseEvent,
|
|
49 qt.core.QDateTime,
|
|
50 qt.core.QTimerEvent,
|
|
51 qt.core.QBasicTimer,
|
|
52 qt.gui.QLayout,
|
|
53 qt.gui.QLineEdit,
|
|
54 qt.gui.QPainter,
|
|
55 qt.gui.QSlider,
|
|
56 qt.gui.QLabel,
|
|
57 qt.gui.QDesktopWidget,
|
|
58 qt.gui.QGroupBox,
|
|
59 qt.gui.QPushButton,
|
|
60 qt.gui.QVBoxLayout,
|
|
61 qt.gui.QGridLayout,
|
|
62 qt.gui.QHBoxLayout,
|
|
63 qt.gui.QRadialGradient,
|
|
64 qt.opengl.QGLFormat,
|
|
65 tango.math.Math;
|
|
66
|
161
|
67
|
41
|
68 class PathDeformControls : QWidget
|
|
69 {
|
|
70 private PathDeformRenderer m_renderer;
|
|
71
|
|
72 mixin Signal!("okPressed");
|
|
73 mixin Signal!("quitPressed");
|
|
74
|
|
75 this(QWidget parent, PathDeformRenderer renderer, bool smallScreen)
|
|
76 {
|
|
77 super(parent);
|
|
78 m_renderer = renderer;
|
|
79
|
|
80 if (smallScreen)
|
|
81 layoutForSmallScreen();
|
|
82 else
|
|
83 layoutForDesktop();
|
|
84 }
|
|
85
|
|
86 void layoutForDesktop()
|
|
87 {
|
|
88 QGroupBox mainGroup = new QGroupBox(this);
|
|
89 mainGroup.setTitle(tr("Controls"));
|
|
90
|
|
91 QGroupBox radiusGroup = new QGroupBox(mainGroup);
|
|
92 radiusGroup.setTitle(tr("Lens Radius"));
|
|
93 QSlider radiusSlider = new QSlider(Qt.Horizontal, radiusGroup);
|
|
94 radiusSlider.setRange(15, 150);
|
|
95 radiusSlider.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed);
|
|
96
|
|
97 QGroupBox deformGroup = new QGroupBox(mainGroup);
|
|
98 deformGroup.setTitle(tr("Deformation"));
|
|
99 QSlider deformSlider = new QSlider(Qt.Horizontal, deformGroup);
|
|
100 deformSlider.setRange(-100, 100);
|
|
101 deformSlider.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed);
|
|
102
|
|
103 QGroupBox fontSizeGroup = new QGroupBox(mainGroup);
|
|
104 fontSizeGroup.setTitle(tr("Font Size"));
|
|
105 QSlider fontSizeSlider = new QSlider(Qt.Horizontal, fontSizeGroup);
|
|
106 fontSizeSlider.setRange(16, 200);
|
|
107 fontSizeSlider.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed);
|
|
108
|
|
109 QGroupBox textGroup = new QGroupBox(mainGroup);
|
|
110 textGroup.setTitle(tr("Text"));
|
|
111 QLineEdit textInput = new QLineEdit(textGroup);
|
|
112
|
|
113 QPushButton animateButton = new QPushButton(mainGroup);
|
|
114 animateButton.setText(tr("Animated"));
|
|
115 animateButton.setCheckable(true);
|
|
116
|
|
117 QPushButton showSourceButton = new QPushButton(mainGroup);
|
|
118 showSourceButton.setText(tr("Show Source"));
|
|
119
|
|
120 version (QT_OPENGL_SUPPORT)
|
|
121 {
|
|
122 QPushButton enableOpenGLButton = new QPushButton(mainGroup);
|
|
123 enableOpenGLButton.setText(tr("Use OpenGL"));
|
|
124 enableOpenGLButton.setCheckable(true);
|
|
125 enableOpenGLButton.setChecked(m_renderer.usesOpenGL());
|
|
126 if (!QGLFormat.hasOpenGL())
|
|
127 enableOpenGLButton.hide();
|
|
128 }
|
|
129
|
|
130 QPushButton whatsThisButton = new QPushButton(mainGroup);
|
|
131 whatsThisButton.setText(tr("What's This?"));
|
|
132 whatsThisButton.setCheckable(true);
|
|
133
|
|
134 mainGroup.setFixedWidth(180);
|
|
135
|
|
136 QVBoxLayout mainGroupLayout = new QVBoxLayout(mainGroup);
|
|
137 mainGroupLayout.addWidget(radiusGroup);
|
|
138 mainGroupLayout.addWidget(deformGroup);
|
|
139 mainGroupLayout.addWidget(fontSizeGroup);
|
|
140 mainGroupLayout.addWidget(textGroup);
|
|
141 mainGroupLayout.addWidget(animateButton);
|
|
142 mainGroupLayout.addStretch(1);
|
|
143 version (QT_OPENGL_SUPPORT)
|
|
144 {
|
|
145 mainGroupLayout.addWidget(enableOpenGLButton);
|
|
146 }
|
|
147 mainGroupLayout.addWidget(showSourceButton);
|
|
148 mainGroupLayout.addWidget(whatsThisButton);
|
|
149
|
|
150 QVBoxLayout radiusGroupLayout = new QVBoxLayout(radiusGroup);
|
|
151 radiusGroupLayout.addWidget(radiusSlider);
|
|
152
|
|
153 QVBoxLayout deformGroupLayout = new QVBoxLayout(deformGroup);
|
|
154 deformGroupLayout.addWidget(deformSlider);
|
|
155
|
|
156 QVBoxLayout fontSizeGroupLayout = new QVBoxLayout(fontSizeGroup);
|
|
157 fontSizeGroupLayout.addWidget(fontSizeSlider);
|
|
158
|
|
159 QVBoxLayout textGroupLayout = new QVBoxLayout(textGroup);
|
|
160 textGroupLayout.addWidget(textInput);
|
|
161
|
|
162 QVBoxLayout mainLayout = new QVBoxLayout(this);
|
|
163 mainLayout.addWidget(mainGroup);
|
|
164 mainLayout.setMargin(0);
|
|
165
|
|
166 radiusSlider.valueChanged.connect(&m_renderer.setRadius);
|
|
167 deformSlider.valueChanged.connect(&m_renderer.setIntensity);
|
|
168 fontSizeSlider.valueChanged.connect(&m_renderer.setFontSize);
|
|
169 animateButton.clicked.connect(&m_renderer.setAnimated);
|
|
170 version (QT_OPENGL_SUPPORT)
|
|
171 {
|
|
172 enableOpenGLButton.clicked.connect(&m_renderer.enableOpenGL);
|
|
173 }
|
|
174
|
|
175 textInput.textChanged.connect(&m_renderer.setText);
|
|
176 m_renderer.descriptionEnabledChanged.connect(&whatsThisButton.setChecked);
|
|
177 whatsThisButton.clicked.connect(&m_renderer.setDescriptionEnabled);
|
|
178 showSourceButton.clicked.connect(&m_renderer.showSource);
|
|
179
|
|
180 animateButton.animateClick();
|
|
181 deformSlider.setValue(80);
|
|
182 fontSizeSlider.setValue(120);
|
|
183 radiusSlider.setValue(100);
|
|
184 textInput.setText(tr("Qt"));
|
|
185 }
|
|
186
|
|
187 void layoutForSmallScreen()
|
|
188 {
|
|
189 QGroupBox mainGroup = new QGroupBox(this);
|
|
190 mainGroup.setTitle(tr("Controls"));
|
|
191
|
|
192 QLabel radiusLabel = new QLabel(mainGroup);
|
|
193 radiusLabel.setText(tr("Lens Radius:"));
|
|
194 QSlider radiusSlider = new QSlider(Qt.Horizontal, mainGroup);
|
|
195 radiusSlider.setRange(15, 150);
|
|
196 radiusSlider.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed);
|
|
197
|
|
198 QLabel deformLabel = new QLabel(mainGroup);
|
|
199 deformLabel.setText(tr("Deformation:"));
|
|
200 QSlider deformSlider = new QSlider(Qt.Horizontal, mainGroup);
|
|
201 deformSlider.setRange(-100, 100);
|
|
202 deformSlider.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed);
|
|
203
|
|
204 QLabel fontSizeLabel = new QLabel(mainGroup);
|
|
205 fontSizeLabel.setText(tr("Font Size:"));
|
|
206 QSlider fontSizeSlider = new QSlider(Qt.Horizontal, mainGroup);
|
|
207 fontSizeSlider.setRange(16, 200);
|
|
208 fontSizeSlider.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed);
|
|
209
|
|
210 QPushButton animateButton = new QPushButton(tr("Animated"), mainGroup);
|
|
211 animateButton.setCheckable(true);
|
|
212
|
|
213 version (QT_OPENGL_SUPPORT)
|
|
214 {
|
|
215 QPushButton enableOpenGLButton = new QPushButton(mainGroup);
|
|
216 enableOpenGLButton.setText(tr("Use OpenGL"));
|
|
217 enableOpenGLButton.setCheckable(true);
|
|
218 enableOpenGLButton.setChecked(m_renderer.usesOpenGL());
|
|
219 if (!QGLFormat.hasOpenGL())
|
|
220 enableOpenGLButton.hide();
|
|
221 }
|
|
222
|
|
223 QPushButton quitButton = new QPushButton(tr("Quit"), mainGroup);
|
|
224 QPushButton okButton = new QPushButton(tr("OK"), mainGroup);
|
|
225
|
|
226
|
|
227 QGridLayout mainGroupLayout = new QGridLayout(mainGroup);
|
|
228 mainGroupLayout.setMargin(0);
|
|
229 mainGroupLayout.addWidget(radiusLabel, 0, 0, Qt.AlignRight);
|
|
230 mainGroupLayout.addWidget(radiusSlider, 0, 1);
|
|
231 mainGroupLayout.addWidget(deformLabel, 1, 0, Qt.AlignRight);
|
|
232 mainGroupLayout.addWidget(deformSlider, 1, 1);
|
|
233 mainGroupLayout.addWidget(fontSizeLabel, 2, 0, Qt.AlignRight);
|
|
234 mainGroupLayout.addWidget(fontSizeSlider, 2, 1);
|
|
235 mainGroupLayout.addWidget(animateButton, 3,0, 1,2);
|
|
236 version (QT_OPENGL_SUPPORT)
|
|
237 {
|
|
238 mainGroupLayout.addWidget(enableOpenGLButton, 4,0, 1,2);
|
|
239 }
|
|
240
|
|
241 QVBoxLayout mainLayout = new QVBoxLayout(this);
|
|
242 mainLayout.addWidget(mainGroup);
|
|
243 mainLayout.addStretch(1);
|
|
244 mainLayout.addWidget(okButton);
|
|
245 mainLayout.addWidget(quitButton);
|
|
246
|
|
247 quitButton.clicked.connect(&emitQuitSignal);
|
|
248 okButton.clicked.connect(&emitOkSignal);
|
|
249 radiusSlider.valueChanged.connect(&m_renderer.setRadius);
|
|
250 deformSlider.valueChanged.connect(&m_renderer.setIntensity);
|
|
251 fontSizeSlider.valueChanged.connect(&m_renderer.setFontSize);
|
|
252 animateButton.clicked.connect(&m_renderer.setAnimated);
|
|
253 version (QT_OPENGL_SUPPORT)
|
|
254 {
|
|
255 enableOpenGLButton.clicked.connect(&m_renderer.enableOpenGL);
|
|
256 }
|
|
257
|
|
258
|
|
259 animateButton.animateClick();
|
|
260 deformSlider.setValue(80);
|
|
261 fontSizeSlider.setValue(120);
|
|
262
|
|
263 QRect screen_size = QApplication.desktop().screenGeometry();
|
|
264 radiusSlider.setValue(qMin(screen_size.width(), screen_size.height())/5);
|
|
265 m_renderer.setText(tr("Qt"));
|
|
266 }
|
|
267
|
|
268
|
|
269 void emitQuitSignal()
|
|
270 { quitPressed.emit; }
|
|
271
|
|
272 void emitOkSignal()
|
|
273 { okPressed.emit; }
|
|
274 }
|
|
275
|
|
276
|
|
277 class PathDeformWidget : QWidget
|
|
278 {
|
|
279 private:
|
|
280 PathDeformRenderer m_renderer;
|
|
281 PathDeformControls m_controls;
|
|
282
|
|
283 public:
|
|
284 this(QWidget parent, bool smallScreen)
|
|
285 {
|
|
286 super(parent);
|
|
287
|
|
288 setWindowTitle(tr("Vector Deformation"));
|
|
289
|
|
290 m_renderer = new PathDeformRenderer(this, smallScreen);
|
|
291 m_renderer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding);
|
|
292
|
|
293 // Layouts
|
|
294 QHBoxLayout mainLayout = new QHBoxLayout(this);
|
|
295 mainLayout.addWidget(m_renderer);
|
|
296
|
|
297 m_controls = new PathDeformControls(null, m_renderer, smallScreen);
|
|
298 m_controls.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Minimum);
|
|
299
|
|
300 if (!smallScreen)
|
|
301 mainLayout.addWidget(m_controls);
|
|
302
|
|
303 m_renderer.loadSourceFile(":res/deform/pathdeform.d");
|
|
304 m_renderer.loadDescription(":res/deform/pathdeform.html");
|
|
305 m_renderer.setDescriptionEnabled(false);
|
|
306
|
|
307 m_renderer.clicked.connect(&showControls);
|
|
308 m_controls.okPressed.connect(&hideControls);
|
|
309
|
|
310 m_controls.quitPressed.connect(&QApplication.quit);
|
|
311 }
|
|
312
|
|
313 void showControls()
|
|
314 {
|
|
315 m_controls.showFullScreen;
|
|
316 }
|
|
317
|
|
318 void hideControls()
|
|
319 {
|
|
320 m_controls.hide;
|
|
321 }
|
|
322
|
|
323 void setWidgetStyle(QStyle style) // TODO: QWidget.setStyle is not virtual
|
|
324 {
|
|
325 super.setStyle(style);
|
|
326 if (m_controls)
|
|
327 {
|
|
328 m_controls.setStyle(style);
|
|
329
|
161
|
330 QWidget[] widgets; // = qFindChildren!(QWidget)(m_controls); //TODO
|
41
|
331 foreach (w; widgets)
|
|
332 w.setStyle(style);
|
|
333 }
|
|
334 }
|
|
335 }
|
|
336
|
|
337 private QRect circle_bounds(QPointF center, qreal radius, qreal compensation)
|
|
338 {
|
161
|
339 return QRect(qRound(center.x() - radius - compensation),
|
41
|
340 qRound(center.y() - radius - compensation),
|
|
341 qRound((radius + compensation) * 2),
|
|
342 qRound((radius + compensation) * 2));
|
|
343 }
|
|
344
|
|
345 enum
|
|
346 {
|
|
347 LENS_EXTENT = 10
|
|
348 }
|
|
349
|
|
350 class PathDeformRenderer : ArthurFrame
|
|
351 {
|
|
352 private:
|
|
353 QBasicTimer m_repaintTimer;
|
|
354 // QBasicTimer m_fpsTimer;
|
|
355 // int m_fpsCounter;
|
|
356 QTime m_repaintTracker;
|
|
357
|
|
358 QPainterPath[] m_paths;
|
|
359 QPointF[] m_advances;
|
|
360 QRectF m_pathBounds;
|
|
361 string m_text;
|
|
362
|
|
363 QPixmap m_lens_pixmap;
|
|
364 QImage m_lens_image;
|
|
365
|
|
366 int m_fontSize;
|
|
367 bool m_animated;
|
|
368
|
|
369 qreal m_intensity;
|
|
370 qreal m_radius;
|
|
371 QPointF m_pos;
|
|
372 QPointF m_offset;
|
|
373 QPointF m_direction;
|
|
374 QPointF m_mousePress;
|
|
375 bool m_mouseDrag;
|
|
376 bool m_smallScreen;
|
|
377
|
|
378 public:
|
|
379 mixin Signal!("clicked");
|
|
380
|
|
381 this(QWidget widget, bool smallScreen)
|
|
382 {
|
|
383 super(widget);
|
|
384 m_radius = 100;
|
|
385 m_pos = QPointF(m_radius, m_radius);
|
|
386 m_direction = QPointF(1, 1);
|
|
387 m_fontSize = 24;
|
|
388 m_animated = true;
|
|
389 m_repaintTimer.start(25, this);
|
|
390 m_repaintTracker.start();
|
|
391 m_intensity = 100;
|
|
392 m_smallScreen = smallScreen;
|
|
393
|
|
394 // m_fpsTimer.start(1000, this);
|
|
395 // m_fpsCounter = 0;
|
|
396
|
|
397 generateLensPixmap();
|
|
398 }
|
|
399
|
|
400 void setFontSize(int fontSize) { m_fontSize = fontSize; setText(m_text); }
|
|
401
|
|
402 override QSize sizeHint() { return QSize(600, 500); }
|
|
403
|
|
404 bool animated() { return m_animated; }
|
|
405 int radius() { return cast(int)m_radius; }
|
|
406 int fontSize() { return m_fontSize; }
|
|
407 int intensity() { return cast(int)m_intensity; }
|
|
408 string text() { return m_text; }
|
|
409
|
|
410
|
|
411 void setText(string text)
|
|
412 {
|
|
413 m_text = text;
|
|
414
|
|
415 auto f = new QFont("times new roman,utopia");
|
|
416 f.setStyleStrategy(QFont.ForceOutline);
|
|
417 f.setPointSize(m_fontSize);
|
|
418 f.setStyleHint(QFont.Times);
|
|
419
|
|
420 m_paths = null;
|
161
|
421 m_pathBounds = QRectF();
|
41
|
422
|
|
423 QPointF advance;
|
|
424
|
|
425 auto path = new QPainterPath;
|
|
426 path.addText(advance, f, text);
|
|
427 m_pathBounds = m_pathBounds.united(path.boundingRect);
|
|
428 m_paths ~= path;
|
|
429
|
|
430 foreach (ref p; m_paths)
|
|
431 p = (new QMatrix(1, 0, 0, 1, -m_pathBounds.x(), -m_pathBounds.y())).map(path);
|
|
432
|
|
433 update;
|
|
434 }
|
|
435
|
|
436
|
|
437 void generateLensPixmap()
|
|
438 {
|
|
439 qreal rad = m_radius + LENS_EXTENT;
|
|
440
|
|
441 QRect bounds = circle_bounds(QPointF(), rad, 0);
|
|
442
|
|
443 QPainter painter = new QPainter;
|
|
444
|
|
445 if (preferImage()) {
|
|
446 m_lens_image = new QImage(bounds.size(), QImage.Format_ARGB32_Premultiplied);
|
|
447 m_lens_image.fill(0);
|
|
448 painter.begin(m_lens_image);
|
|
449 } else {
|
|
450 m_lens_pixmap = new QPixmap(bounds.size());
|
|
451 m_lens_pixmap.fill(new QColor(Qt.transparent));
|
|
452 painter.begin(m_lens_pixmap);
|
|
453 }
|
|
454
|
|
455 auto gr = new QRadialGradient(rad, rad, rad, 3 * rad / 5, 3 * rad / 5);
|
|
456 gr.setColorAt(0.0, new QColor(255, 255, 255, 191));
|
|
457 gr.setColorAt(0.2, new QColor(255, 255, 127, 191));
|
|
458 gr.setColorAt(0.9, new QColor(150, 150, 200, 63));
|
|
459 gr.setColorAt(0.95, new QColor(0, 0, 0, 127));
|
|
460 gr.setColorAt(1, new QColor(0, 0, 0, 0));
|
|
461 painter.setRenderHint(QPainter.Antialiasing);
|
|
462 painter.setBrush(gr);
|
|
463 painter.setPen(Qt.NoPen);
|
|
464 painter.drawEllipse(0, 0, bounds.width(), bounds.height());
|
|
465 }
|
|
466
|
|
467 void setAnimated(bool animated)
|
|
468 {
|
|
469 m_animated = animated;
|
|
470
|
|
471 if (m_animated) {
|
|
472 // m_fpsTimer.start(1000, this);
|
|
473 // m_fpsCounter = 0;
|
|
474 m_repaintTimer.start(25, this);
|
|
475 m_repaintTracker.start();
|
|
476 } else {
|
|
477 // m_fpsTimer.stop();
|
|
478 m_repaintTimer.stop();
|
|
479 }
|
|
480 }
|
|
481
|
|
482 override void timerEvent(QTimerEvent e)
|
|
483 {
|
|
484
|
|
485 if (e.timerId == m_repaintTimer.timerId) {
|
|
486
|
161
|
487 if ((QLineF(QPointF(0,0), m_direction)).length() > 1)
|
41
|
488 m_direction *= 0.995;
|
|
489 qreal time = m_repaintTracker.restart();
|
|
490
|
|
491 QRect rectBefore = circle_bounds(m_pos, m_radius, m_fontSize);
|
|
492
|
|
493 qreal dx = m_direction.x();
|
|
494 qreal dy = m_direction.y();
|
|
495 if (time > 0) {
|
|
496 dx = dx * time * .1;
|
|
497 dy = dy * time * .1;
|
|
498 }
|
|
499
|
|
500 m_pos += QPointF(dx, dy);
|
|
501
|
|
502 if (m_pos.x() - m_radius < 0) {
|
|
503 m_direction.x = -m_direction.x;
|
|
504 m_pos.x = m_radius;
|
|
505 } else if (m_pos.x + m_radius > width) {
|
|
506 m_direction.x = -m_direction.x;
|
|
507 m_pos.x = width - m_radius;
|
|
508 }
|
|
509
|
|
510 if (m_pos.y - m_radius < 0) {
|
|
511 m_direction.y = -m_direction.y;
|
|
512 m_pos.y = m_radius;
|
|
513 } else if (m_pos.y + m_radius > height) {
|
|
514 m_direction.y = -m_direction.y;
|
|
515 m_pos.y = height - m_radius;
|
|
516 }
|
|
517
|
|
518 void noGLUpdate()
|
|
519 {
|
|
520 QRect rectAfter = circle_bounds(m_pos, m_radius, m_fontSize);
|
|
521 update(rectAfter.united(rectBefore));
|
|
522 QApplication.syncX();
|
|
523 }
|
|
524
|
|
525 version (QT_OPENGL_SUPPORT)
|
|
526 {
|
|
527 if (usesOpenGL()) {
|
|
528 update;
|
|
529 }
|
|
530 else
|
|
531 noGLUpdate;
|
|
532 }
|
|
533 else
|
|
534 noGLUpdate;
|
|
535 }
|
|
536 // else if (e.timerId() == m_fpsTimer.timerId()) {
|
|
537 // printf("fps: %d\n", m_fpsCounter);
|
|
538 // emit frameRate(m_fpsCounter);
|
|
539 // m_fpsCounter = 0;
|
|
540
|
|
541 // }
|
|
542 }
|
|
543
|
|
544 override void mousePressEvent(QMouseEvent e)
|
|
545 {
|
|
546 setDescriptionEnabled(false);
|
|
547
|
|
548 m_repaintTimer.stop();
|
|
549 m_offset = QPointF();
|
161
|
550 if ((QLineF(m_pos, QPointF(e.pos))).length <= m_radius)
|
41
|
551 m_offset = m_pos - QPointF(e.pos);
|
|
552
|
161
|
553 m_mousePress = QPointF(e.pos);
|
41
|
554
|
|
555 // If we're not running in small screen mode, always assume we're dragging
|
|
556 m_mouseDrag = !m_smallScreen;
|
|
557
|
|
558 mouseMoveEvent(e);
|
|
559 }
|
|
560
|
|
561 override void mouseReleaseEvent(QMouseEvent e)
|
|
562 {
|
|
563 if (e.buttons() == Qt.NoButton && m_animated) {
|
|
564 m_repaintTimer.start(10, this);
|
|
565 m_repaintTracker.start();
|
|
566 }
|
|
567
|
|
568 if (!m_mouseDrag && m_smallScreen)
|
|
569 clicked.emit;
|
|
570 }
|
|
571
|
|
572 override void mouseMoveEvent(QMouseEvent e)
|
|
573 {
|
|
574 auto epos = QPointF(e.pos);
|
|
575
|
161
|
576 if (!m_mouseDrag && (QLineF(m_mousePress, QPointF(e.pos))).length() > 25.0)
|
41
|
577 m_mouseDrag = true;
|
|
578
|
|
579 if (m_mouseDrag) {
|
|
580 QRect rectBefore = circle_bounds(m_pos, m_radius, m_fontSize);
|
|
581 if (e.type() == QEvent.MouseMove) {
|
161
|
582 QLineF line = QLineF(m_pos, epos + m_offset);
|
41
|
583 line.setLength(line.length() * .1);
|
|
584 auto dir = QPointF(line.dx(), line.dy());
|
|
585 m_direction = (m_direction + dir) / 2;
|
|
586 }
|
|
587 m_pos = epos + m_offset;
|
|
588
|
|
589 void noGLUpdate()
|
|
590 {
|
|
591 QRect rectAfter = circle_bounds(m_pos, m_radius, m_fontSize);
|
|
592 update(rectBefore.united(rectAfter));
|
|
593 }
|
|
594
|
|
595 version (QT_OPENGL_SUPPORT)
|
|
596 {
|
|
597 if (usesOpenGL()) {
|
|
598 update;
|
|
599 } else
|
|
600 noGLUpdate;
|
|
601 }
|
|
602 else
|
|
603 noGLUpdate;
|
|
604 }
|
|
605 }
|
|
606
|
|
607 QPainterPath lensDeform(QPainterPath source, QPointF offset)
|
|
608 {
|
|
609 auto path = new QPainterPath;
|
|
610 path.addPath(source);
|
|
611
|
|
612 qreal flip = m_intensity / 100.0;
|
|
613
|
|
614 for (int i=0; i<path.elementCount; ++i) {
|
|
615 auto e = path.elementAt(i);
|
|
616
|
|
617 qreal x = e.x + offset.x();
|
|
618 qreal y = e.y + offset.y();
|
|
619
|
|
620 qreal dx = x - m_pos.x();
|
|
621 qreal dy = y - m_pos.y();
|
|
622 qreal len = m_radius - sqrt(dx * dx + dy * dy);
|
|
623
|
|
624 if (len > 0) {
|
|
625 path.setElementPositionAt(i,
|
|
626 x + flip * dx * len / m_radius,
|
|
627 y + flip * dy * len / m_radius);
|
|
628 } else {
|
|
629 path.setElementPositionAt(i, x, y);
|
|
630 }
|
|
631
|
|
632 }
|
|
633
|
|
634 return path;
|
|
635 }
|
|
636
|
|
637
|
|
638 override void paint(QPainter painter)
|
|
639 {
|
|
640 int pad_x = 5;
|
|
641 int pad_y = 5;
|
|
642
|
|
643 int skip_x = qRound(m_pathBounds.width() + pad_x + m_fontSize/2);
|
|
644 int skip_y = qRound(m_pathBounds.height() + pad_y);
|
|
645
|
|
646 painter.setPen(Qt.NoPen);
|
|
647 painter.setBrush(new QColor(Qt.black));
|
|
648
|
|
649 auto clip = painter.clipPath().boundingRect();
|
|
650
|
|
651 int overlap = pad_x / 2;
|
|
652
|
|
653 for (int start_y=0; start_y < height(); start_y += skip_y) {
|
|
654
|
|
655 if (start_y > clip.bottom())
|
|
656 break;
|
|
657
|
|
658 int start_x = -overlap;
|
|
659 for (; start_x < width(); start_x += skip_x) {
|
|
660
|
|
661 if (start_y + skip_y >= clip.top() &&
|
|
662 start_x + skip_x >= clip.left() &&
|
|
663 start_x <= clip.right()) {
|
|
664 for (int i=0; i<m_paths.length; ++i) {
|
|
665 QPainterPath path = lensDeform(m_paths[i], QPointF(start_x, start_y));
|
|
666 painter.drawPath(path);
|
|
667 }
|
|
668 }
|
|
669 }
|
|
670 overlap = skip_x - (start_x - width());
|
|
671
|
|
672 }
|
|
673
|
|
674 if (preferImage) {
|
|
675 painter.drawImage(m_pos - QPointF(m_radius + LENS_EXTENT, m_radius + LENS_EXTENT),
|
|
676 m_lens_image);
|
|
677 } else {
|
|
678 painter.drawPixmap(m_pos - QPointF(m_radius + LENS_EXTENT, m_radius + LENS_EXTENT),
|
|
679 m_lens_pixmap);
|
|
680 }
|
|
681 }
|
|
682
|
|
683
|
|
684 void setRadius(int radius)
|
|
685 {
|
|
686 qreal max = max(m_radius, cast(qreal)radius);
|
|
687 m_radius = radius;
|
|
688 generateLensPixmap();
|
|
689 if (!m_animated || m_radius < max) {
|
|
690
|
|
691 auto noGLUpdate = (){ update(circle_bounds(m_pos, max, m_fontSize)); };
|
|
692
|
|
693 version (QT_OPENGL_SUPPORT)
|
|
694 {
|
|
695 if (usesOpenGL())
|
|
696 update();
|
|
697 else
|
|
698 noGLUpdate();
|
|
699 }
|
|
700 else
|
|
701 noGLUpdate();
|
|
702 }
|
|
703 }
|
|
704
|
|
705 void setIntensity(int intensity)
|
|
706 {
|
|
707 m_intensity = intensity;
|
|
708 if (!m_animated) {
|
|
709
|
|
710 auto noGLUpdate = (){ update(circle_bounds(m_pos, m_radius, m_fontSize)); };
|
|
711
|
|
712 version (QT_OPENGL_SUPPORT)
|
|
713 {
|
|
714
|
|
715
|
|
716 if (usesOpenGL()) {
|
|
717 update();
|
|
718 } else
|
|
719 noGLUpdate();
|
|
720 }
|
|
721 else
|
|
722 noGLUpdate();
|
|
723 }
|
|
724 }
|
|
725 }
|