1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the demonstration applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#ifndef PATHDEFORM_H
52#define PATHDEFORM_H
53
54#include "arthurwidgets.h"
55
56#include <QBasicTimer>
57#include <QElapsedTimer>
58#include <QPainterPath>
59
60class PathDeformRenderer : public ArthurFrame
61{
62 Q_OBJECT
63 Q_PROPERTY(bool animated READ animated WRITE setAnimated)
64 Q_PROPERTY(int radius READ radius WRITE setRadius)
65 Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize)
66 Q_PROPERTY(int intensity READ intensity WRITE setIntensity)
67 Q_PROPERTY(QString text READ text WRITE setText)
68
69public:
70 explicit PathDeformRenderer(QWidget *widget, bool smallScreen = false);
71
72 void paint(QPainter *painter) override;
73
74 void mousePressEvent(QMouseEvent *e) override;
75 void mouseReleaseEvent(QMouseEvent *e) override;
76 void mouseMoveEvent(QMouseEvent *e) override;
77 void timerEvent(QTimerEvent *e) override;
78
79 QSize sizeHint() const override { return QSize(600, 500); }
80
81 bool animated() const { return m_animated; }
82 int radius() const { return int(m_radius); }
83 int fontSize() const { return m_fontSize; }
84 int intensity() const { return int(m_intensity); }
85 QString text() const { return m_text; }
86
87public slots:
88 void setRadius(int radius);
89 void setFontSize(int fontSize) { m_fontSize = fontSize; setText(m_text); }
90 void setText(const QString &text);
91 void setIntensity(int intensity);
92
93 void setAnimated(bool animated);
94
95signals:
96 void clicked();
97// void frameRate(double fps);
98
99private:
100 void generateLensPixmap();
101 QPainterPath lensDeform(const QPainterPath &source, const QPointF &offset);
102
103 QBasicTimer m_repaintTimer;
104// QBasicTimer m_fpsTimer;
105// int m_fpsCounter;
106 QElapsedTimer m_repaintTracker;
107
108 QVector<QPainterPath> m_paths;
109 QVector<QPointF> m_advances;
110 QRectF m_pathBounds;
111 QString m_text;
112
113 QPixmap m_lens_pixmap;
114 QImage m_lens_image;
115
116 int m_fontSize;
117 bool m_animated;
118
119 qreal m_intensity;
120 qreal m_radius;
121 QPointF m_pos;
122 QPointF m_offset;
123 QPointF m_direction;
124 QPointF m_mousePress;
125 bool m_mouseDrag;
126 bool m_smallScreen;
127};
128
129class PathDeformControls : public QWidget
130{
131 Q_OBJECT
132public:
133 PathDeformControls(QWidget *parent, PathDeformRenderer* renderer, bool smallScreen);
134signals:
135 void okPressed();
136 void quitPressed();
137private:
138 PathDeformRenderer *m_renderer;
139 void layoutForDesktop();
140 void layoutForSmallScreen();
141};
142
143class PathDeformWidget : public QWidget
144{
145 Q_OBJECT
146public:
147 PathDeformWidget(QWidget *parent, bool smallScreen);
148 void setStyle(QStyle *style);
149
150private:
151 PathDeformRenderer *m_renderer;
152 PathDeformControls *m_controls;
153
154private slots:
155 void showControls();
156 void hideControls();
157};
158
159#endif // PATHDEFORM_H
160

source code of qtbase/examples/widgets/painting/deform/pathdeform.h