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