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 PATHSTROKE_H
52#define PATHSTROKE_H
53
54#include "arthurwidgets.h"
55
56#include <QtWidgets>
57
58class PathStrokeRenderer : public ArthurFrame
59{
60 Q_OBJECT
61 Q_PROPERTY(bool animation READ animation WRITE setAnimation)
62 Q_PROPERTY(qreal penWidth READ realPenWidth WRITE setRealPenWidth)
63public:
64 enum PathMode { CurveMode, LineMode };
65
66 explicit PathStrokeRenderer(QWidget *parent, bool smallScreen = false);
67
68 void paint(QPainter *) override;
69 void mousePressEvent(QMouseEvent *e) override;
70 void mouseMoveEvent(QMouseEvent *e) override;
71 void mouseReleaseEvent(QMouseEvent *e) override;
72 void timerEvent(QTimerEvent *e) override;
73 bool event(QEvent *e) override;
74
75 QSize sizeHint() const override { return QSize(500, 500); }
76
77 bool animation() const { return m_timer.isActive(); }
78
79 qreal realPenWidth() const { return m_penWidth; }
80 void setRealPenWidth(qreal penWidth) { m_penWidth = penWidth; update(); }
81
82signals:
83 void clicked();
84
85public slots:
86 void setPenWidth(int penWidth) { m_penWidth = penWidth / 10.0; update(); }
87 void setAnimation(bool animation);
88
89 void setFlatCap() { m_capStyle = Qt::FlatCap; update(); }
90 void setSquareCap() { m_capStyle = Qt::SquareCap; update(); }
91 void setRoundCap() { m_capStyle = Qt::RoundCap; update(); }
92
93 void setBevelJoin() { m_joinStyle = Qt::BevelJoin; update(); }
94 void setMiterJoin() { m_joinStyle = Qt::MiterJoin; update(); }
95 void setSvgMiterJoin() { m_joinStyle = Qt::SvgMiterJoin; update(); }
96 void setRoundJoin() { m_joinStyle = Qt::RoundJoin; update(); }
97
98 void setCurveMode() { m_pathMode = CurveMode; update(); }
99 void setLineMode() { m_pathMode = LineMode; update(); }
100
101 void setSolidLine() { m_penStyle = Qt::SolidLine; update(); }
102 void setDashLine() { m_penStyle = Qt::DashLine; update(); }
103 void setDotLine() { m_penStyle = Qt::DotLine; update(); }
104 void setDashDotLine() { m_penStyle = Qt::DashDotLine; update(); }
105 void setDashDotDotLine() { m_penStyle = Qt::DashDotDotLine; update(); }
106 void setCustomDashLine() { m_penStyle = Qt::NoPen; update(); }
107
108private:
109 void initializePoints();
110 void updatePoints();
111
112 QBasicTimer m_timer;
113
114 PathMode m_pathMode;
115
116 bool m_wasAnimated;
117
118 qreal m_penWidth;
119 int m_pointCount;
120 int m_pointSize;
121 int m_activePoint;
122 QList<QPointF> m_points;
123 QList<QPointF> m_vectors;
124
125 Qt::PenJoinStyle m_joinStyle;
126 Qt::PenCapStyle m_capStyle;
127
128 Qt::PenStyle m_penStyle;
129
130 bool m_smallScreen;
131 QPoint m_mousePress;
132 bool m_mouseDrag;
133
134 QHash<int, int> m_fingerPointMapping;
135};
136
137class PathStrokeControls : public QWidget
138{
139 Q_OBJECT
140
141public:
142 PathStrokeControls(QWidget* parent, PathStrokeRenderer* renderer, bool smallScreen);
143
144signals:
145 void okPressed();
146 void quitPressed();
147
148private:
149 PathStrokeRenderer* m_renderer;
150
151 QGroupBox *m_capGroup;
152 QGroupBox *m_joinGroup;
153 QGroupBox *m_styleGroup;
154 QGroupBox *m_pathModeGroup;
155
156 void createCommonControls(QWidget* parent);
157 void layoutForDesktop();
158 void layoutForSmallScreens();
159
160private slots:
161 void emitQuitSignal();
162 void emitOkSignal();
163
164};
165
166class PathStrokeWidget : public QWidget
167{
168 Q_OBJECT
169
170public:
171 PathStrokeWidget(bool smallScreen);
172 void setStyle(QStyle *style);
173
174private:
175 PathStrokeRenderer *m_renderer;
176 PathStrokeControls *m_controls;
177
178private slots:
179 void showControls();
180 void hideControls();
181};
182
183#endif // PATHSTROKE_H
184