0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the tools applications of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include "qtgradientwidget.h"
|
|
43 |
#include <QtCore/QMap>
|
|
44 |
#include <QtGui/QImage>
|
|
45 |
#include <QtGui/QPainter>
|
|
46 |
#include <QtGui/QScrollBar>
|
|
47 |
#include <QtGui/QMouseEvent>
|
|
48 |
|
|
49 |
#define _USE_MATH_DEFINES
|
|
50 |
|
|
51 |
|
|
52 |
#include "math.h"
|
|
53 |
|
|
54 |
#ifndef M_PI
|
|
55 |
#define M_PI 3.14159265358979323846
|
|
56 |
#endif
|
|
57 |
|
|
58 |
QT_BEGIN_NAMESPACE
|
|
59 |
|
|
60 |
class QtGradientWidgetPrivate
|
|
61 |
{
|
|
62 |
QtGradientWidget *q_ptr;
|
|
63 |
Q_DECLARE_PUBLIC(QtGradientWidget)
|
|
64 |
public:
|
|
65 |
QPointF fromViewport(const QPointF &point) const;
|
|
66 |
QPointF toViewport(const QPointF &point) const;
|
|
67 |
// void setupDrag(QtGradientStop *stop, int x);
|
|
68 |
|
|
69 |
QPointF checkRange(const QPointF &point) const;
|
|
70 |
QRectF pointRect(const QPointF &point, double size) const;
|
|
71 |
|
|
72 |
double correctAngle(double angle) const;
|
|
73 |
void setAngleConical(double angle);
|
|
74 |
|
|
75 |
void paintPoint(QPainter *painter, const QPointF &point, double size) const;
|
|
76 |
|
|
77 |
double m_handleSize;
|
|
78 |
bool m_backgroundCheckered;
|
|
79 |
|
|
80 |
QGradientStops m_gradientStops;
|
|
81 |
QGradient::Type m_gradientType;
|
|
82 |
QGradient::Spread m_gradientSpread;
|
|
83 |
QPointF m_startLinear;
|
|
84 |
QPointF m_endLinear;
|
|
85 |
QPointF m_centralRadial;
|
|
86 |
QPointF m_focalRadial;
|
|
87 |
qreal m_radiusRadial;
|
|
88 |
QPointF m_centralConical;
|
|
89 |
qreal m_angleConical;
|
|
90 |
|
|
91 |
enum Handle {
|
|
92 |
NoHandle,
|
|
93 |
StartLinearHandle,
|
|
94 |
EndLinearHandle,
|
|
95 |
CentralRadialHandle,
|
|
96 |
FocalRadialHandle,
|
|
97 |
RadiusRadialHandle,
|
|
98 |
CentralConicalHandle,
|
|
99 |
AngleConicalHandle
|
|
100 |
};
|
|
101 |
|
|
102 |
Handle m_dragHandle;
|
|
103 |
QPointF m_dragOffset;
|
|
104 |
//double m_radiusOffset;
|
|
105 |
double m_radiusFactor;
|
|
106 |
double m_dragRadius;
|
|
107 |
double m_angleOffset;
|
|
108 |
double m_dragAngle;
|
|
109 |
};
|
|
110 |
|
|
111 |
double QtGradientWidgetPrivate::correctAngle(double angle) const
|
|
112 |
{
|
|
113 |
double a = angle;
|
|
114 |
while (a >= 360)
|
|
115 |
a -= 360;
|
|
116 |
while (a < 0)
|
|
117 |
a += 360;
|
|
118 |
return a;
|
|
119 |
}
|
|
120 |
|
|
121 |
void QtGradientWidgetPrivate::setAngleConical(double angle)
|
|
122 |
{
|
|
123 |
double a = correctAngle(angle);
|
|
124 |
if (m_angleConical == a)
|
|
125 |
return;
|
|
126 |
m_angleConical = a;
|
|
127 |
emit q_ptr->angleConicalChanged(m_angleConical);
|
|
128 |
}
|
|
129 |
|
|
130 |
QRectF QtGradientWidgetPrivate::pointRect(const QPointF &point, double size) const
|
|
131 |
{
|
|
132 |
return QRectF(point.x() - size / 2, point.y() - size / 2, size, size);
|
|
133 |
}
|
|
134 |
|
|
135 |
QPointF QtGradientWidgetPrivate::checkRange(const QPointF &point) const
|
|
136 |
{
|
|
137 |
QPointF p = point;
|
|
138 |
if (p.x() > 1)
|
|
139 |
p.setX(1);
|
|
140 |
else if (p.x() < 0)
|
|
141 |
p.setX(0);
|
|
142 |
if (p.y() > 1)
|
|
143 |
p.setY(1);
|
|
144 |
else if (p.y() < 0)
|
|
145 |
p.setY(0);
|
|
146 |
return p;
|
|
147 |
}
|
|
148 |
|
|
149 |
QPointF QtGradientWidgetPrivate::fromViewport(const QPointF &point) const
|
|
150 |
{
|
|
151 |
QSize size = q_ptr->size();
|
|
152 |
return QPointF(point.x() / size.width(), point.y() / size.height());
|
|
153 |
}
|
|
154 |
|
|
155 |
QPointF QtGradientWidgetPrivate::toViewport(const QPointF &point) const
|
|
156 |
{
|
|
157 |
QSize size = q_ptr->size();
|
|
158 |
return QPointF(point.x() * size.width(), point.y() * size.height());
|
|
159 |
}
|
|
160 |
|
|
161 |
void QtGradientWidgetPrivate::paintPoint(QPainter *painter, const QPointF &point, double size) const
|
|
162 |
{
|
|
163 |
QPointF pf = toViewport(point);
|
|
164 |
QRectF rf = pointRect(pf, size);
|
|
165 |
|
|
166 |
QPen pen;
|
|
167 |
pen.setWidthF(1);
|
|
168 |
QColor alphaZero = Qt::white;
|
|
169 |
alphaZero.setAlpha(0);
|
|
170 |
|
|
171 |
painter->save();
|
|
172 |
painter->drawEllipse(rf);
|
|
173 |
|
|
174 |
/*
|
|
175 |
painter->save();
|
|
176 |
|
|
177 |
QLinearGradient lgV(0, rf.top(), 0, rf.bottom());
|
|
178 |
lgV.setColorAt(0, alphaZero);
|
|
179 |
lgV.setColorAt(0.25, Qt::white);
|
|
180 |
lgV.setColorAt(0.25, Qt::white);
|
|
181 |
lgV.setColorAt(1, alphaZero);
|
|
182 |
pen.setBrush(lgV);
|
|
183 |
painter->setPen(pen);
|
|
184 |
|
|
185 |
painter->drawLine(QPointF(pf.x(), rf.top()), QPointF(pf.x(), rf.bottom()));
|
|
186 |
|
|
187 |
QLinearGradient lgH(rf.left(), 0, rf.right(), 0);
|
|
188 |
lgH.setColorAt(0, alphaZero);
|
|
189 |
lgH.setColorAt(0.5, Qt::white);
|
|
190 |
lgH.setColorAt(1, alphaZero);
|
|
191 |
pen.setBrush(lgH);
|
|
192 |
painter->setPen(pen);
|
|
193 |
|
|
194 |
painter->drawLine(QPointF(rf.left(), pf.y()), QPointF(rf.right(), pf.y()));
|
|
195 |
|
|
196 |
painter->restore();
|
|
197 |
*/
|
|
198 |
|
|
199 |
painter->restore();
|
|
200 |
}
|
|
201 |
|
|
202 |
/*
|
|
203 |
void QtGradientWidgetPrivate::setupDrag(QtGradientStop *stop, int x)
|
|
204 |
{
|
|
205 |
m_model->setCurrentStop(stop);
|
|
206 |
|
|
207 |
int viewportX = qRound(toViewport(stop->position()));
|
|
208 |
m_dragOffset = x - viewportX;
|
|
209 |
|
|
210 |
QList<QtGradientStop *> stops = m_stops;
|
|
211 |
m_stops.clear();
|
|
212 |
QListIterator<QtGradientStop *> itStop(stops);
|
|
213 |
while (itStop.hasNext()) {
|
|
214 |
QtGradientStop *s = itStop.next();
|
|
215 |
if (m_model->isSelected(s) || s == stop) {
|
|
216 |
m_dragStops[s] = s->position() - stop->position();
|
|
217 |
m_stops.append(s);
|
|
218 |
} else {
|
|
219 |
m_dragOriginal[s->position()] = s->color();
|
|
220 |
}
|
|
221 |
}
|
|
222 |
itStop.toFront();
|
|
223 |
while (itStop.hasNext()) {
|
|
224 |
QtGradientStop *s = itStop.next();
|
|
225 |
if (!m_model->isSelected(s))
|
|
226 |
m_stops.append(s);
|
|
227 |
}
|
|
228 |
m_stops.removeAll(stop);
|
|
229 |
m_stops.prepend(stop);
|
|
230 |
}
|
|
231 |
*/
|
|
232 |
////////////////////////////
|
|
233 |
|
|
234 |
QtGradientWidget::QtGradientWidget(QWidget *parent)
|
|
235 |
: QWidget(parent), d_ptr(new QtGradientWidgetPrivate)
|
|
236 |
{
|
|
237 |
d_ptr->q_ptr = this;
|
|
238 |
d_ptr->m_backgroundCheckered = true;
|
|
239 |
d_ptr->m_handleSize = 20.0;
|
|
240 |
d_ptr->m_gradientType = QGradient::LinearGradient;
|
|
241 |
d_ptr->m_startLinear = QPointF(0, 0);
|
|
242 |
d_ptr->m_endLinear = QPointF(1, 1);
|
|
243 |
d_ptr->m_centralRadial = QPointF(0.5, 0.5);
|
|
244 |
d_ptr->m_focalRadial = QPointF(0.5, 0.5);
|
|
245 |
d_ptr->m_radiusRadial = 0.5;
|
|
246 |
d_ptr->m_centralConical = QPointF(0.5, 0.5);
|
|
247 |
d_ptr->m_angleConical = 0;
|
|
248 |
d_ptr->m_dragHandle = QtGradientWidgetPrivate::NoHandle;
|
|
249 |
|
|
250 |
setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
|
|
251 |
}
|
|
252 |
|
|
253 |
QtGradientWidget::~QtGradientWidget()
|
|
254 |
{
|
|
255 |
}
|
|
256 |
|
|
257 |
QSize QtGradientWidget::sizeHint() const
|
|
258 |
{
|
|
259 |
return QSize(176, 176);
|
|
260 |
}
|
|
261 |
|
|
262 |
QSize QtGradientWidget::minimumSizeHint() const
|
|
263 |
{
|
|
264 |
return QSize(128, 128);
|
|
265 |
}
|
|
266 |
|
|
267 |
int QtGradientWidget::heightForWidth(int w) const
|
|
268 |
{
|
|
269 |
return w;
|
|
270 |
}
|
|
271 |
|
|
272 |
void QtGradientWidget::setBackgroundCheckered(bool checkered)
|
|
273 |
{
|
|
274 |
if (d_ptr->m_backgroundCheckered == checkered)
|
|
275 |
return;
|
|
276 |
d_ptr->m_backgroundCheckered = checkered;
|
|
277 |
update();
|
|
278 |
}
|
|
279 |
|
|
280 |
bool QtGradientWidget::isBackgroundCheckered() const
|
|
281 |
{
|
|
282 |
return d_ptr->m_backgroundCheckered;
|
|
283 |
}
|
|
284 |
|
|
285 |
void QtGradientWidget::mousePressEvent(QMouseEvent *e)
|
|
286 |
{
|
|
287 |
if (e->button() != Qt::LeftButton)
|
|
288 |
return;
|
|
289 |
|
|
290 |
QPoint p = e->pos();
|
|
291 |
if (d_ptr->m_gradientType == QGradient::LinearGradient) {
|
|
292 |
QPointF startPoint = d_ptr->toViewport(d_ptr->m_startLinear);
|
|
293 |
double x = p.x() - startPoint.x();
|
|
294 |
double y = p.y() - startPoint.y();
|
|
295 |
|
|
296 |
if ((d_ptr->m_handleSize * d_ptr->m_handleSize / 4) > (x * x + y * y)) {
|
|
297 |
d_ptr->m_dragHandle = QtGradientWidgetPrivate::StartLinearHandle;
|
|
298 |
d_ptr->m_dragOffset = QPointF(x, y);
|
|
299 |
update();
|
|
300 |
return;
|
|
301 |
}
|
|
302 |
|
|
303 |
QPointF endPoint = d_ptr->toViewport(d_ptr->m_endLinear);
|
|
304 |
x = p.x() - endPoint.x();
|
|
305 |
y = p.y() - endPoint.y();
|
|
306 |
|
|
307 |
if ((d_ptr->m_handleSize * d_ptr->m_handleSize / 4) > (x * x + y * y)) {
|
|
308 |
d_ptr->m_dragHandle = QtGradientWidgetPrivate::EndLinearHandle;
|
|
309 |
d_ptr->m_dragOffset = QPointF(x, y);
|
|
310 |
update();
|
|
311 |
return;
|
|
312 |
}
|
|
313 |
} else if (d_ptr->m_gradientType == QGradient::RadialGradient) {
|
|
314 |
QPointF focalPoint = d_ptr->toViewport(d_ptr->m_focalRadial);
|
|
315 |
double x = p.x() - focalPoint.x();
|
|
316 |
double y = p.y() - focalPoint.y();
|
|
317 |
|
|
318 |
if ((d_ptr->m_handleSize * d_ptr->m_handleSize / 9) > (x * x + y * y)) {
|
|
319 |
d_ptr->m_dragHandle = QtGradientWidgetPrivate::FocalRadialHandle;
|
|
320 |
d_ptr->m_dragOffset = QPointF(x, y);
|
|
321 |
update();
|
|
322 |
return;
|
|
323 |
}
|
|
324 |
|
|
325 |
QPointF centralPoint = d_ptr->toViewport(d_ptr->m_centralRadial);
|
|
326 |
x = p.x() - centralPoint.x();
|
|
327 |
y = p.y() - centralPoint.y();
|
|
328 |
|
|
329 |
if ((d_ptr->m_handleSize * d_ptr->m_handleSize / 4) > (x * x + y * y)) {
|
|
330 |
d_ptr->m_dragHandle = QtGradientWidgetPrivate::CentralRadialHandle;
|
|
331 |
d_ptr->m_dragOffset = QPointF(x, y);
|
|
332 |
update();
|
|
333 |
return;
|
|
334 |
}
|
|
335 |
|
|
336 |
QPointF central = d_ptr->toViewport(d_ptr->m_centralRadial);
|
|
337 |
QRectF r = d_ptr->pointRect(central, 2 * d_ptr->m_handleSize / 3);
|
|
338 |
QRectF r1(0, r.y(), size().width(), r.height());
|
|
339 |
QRectF r2(r.x(), 0, r.width(), r.y());
|
|
340 |
QRectF r3(r.x(), r.y() + r.height(), r.width(), size().height() - r.y() - r.height());
|
|
341 |
QPointF pF(p.x(), p.y());
|
|
342 |
if (r1.contains(pF) || r2.contains(pF) || r3.contains(pF)) {
|
|
343 |
x = pF.x() / size().width() - d_ptr->m_centralRadial.x();
|
|
344 |
y = pF.y() / size().height() - d_ptr->m_centralRadial.y();
|
|
345 |
double clickRadius = sqrt(x * x + y * y);
|
|
346 |
//d_ptr->m_radiusOffset = d_ptr->m_radiusRadial - clickRadius;
|
|
347 |
d_ptr->m_radiusFactor = d_ptr->m_radiusRadial / clickRadius;
|
|
348 |
if (d_ptr->m_radiusFactor == 0)
|
|
349 |
d_ptr->m_radiusFactor = 1;
|
|
350 |
d_ptr->m_dragRadius = d_ptr->m_radiusRadial;
|
|
351 |
d_ptr->m_dragHandle = QtGradientWidgetPrivate::RadiusRadialHandle;
|
|
352 |
mouseMoveEvent(e);
|
|
353 |
update();
|
|
354 |
return;
|
|
355 |
}
|
|
356 |
} else if (d_ptr->m_gradientType == QGradient::ConicalGradient) {
|
|
357 |
QPointF centralPoint = d_ptr->toViewport(d_ptr->m_centralConical);
|
|
358 |
double x = p.x() - centralPoint.x();
|
|
359 |
double y = p.y() - centralPoint.y();
|
|
360 |
|
|
361 |
if ((d_ptr->m_handleSize * d_ptr->m_handleSize / 4) > (x * x + y * y)) {
|
|
362 |
d_ptr->m_dragHandle = QtGradientWidgetPrivate::CentralConicalHandle;
|
|
363 |
d_ptr->m_dragOffset = QPointF(x, y);
|
|
364 |
update();
|
|
365 |
return;
|
|
366 |
}
|
|
367 |
double radius = size().width();
|
|
368 |
if (size().height() < radius)
|
|
369 |
radius = size().height();
|
|
370 |
radius /= 2;
|
|
371 |
double corr = d_ptr->m_handleSize / 3;
|
|
372 |
radius -= corr;
|
|
373 |
QPointF vp = d_ptr->toViewport(d_ptr->m_centralConical);
|
|
374 |
x = p.x() - vp.x();
|
|
375 |
y = p.y() - vp.y();
|
|
376 |
if (((radius - corr) * (radius - corr) < (x * x + y * y)) &&
|
|
377 |
((radius + corr) * (radius + corr) > (x * x + y * y))) {
|
|
378 |
QPointF central = d_ptr->toViewport(d_ptr->m_centralConical);
|
|
379 |
QPointF current(e->pos().x(), e->pos().y());
|
|
380 |
x = current.x() - central.x();
|
|
381 |
y = current.y() - central.y();
|
|
382 |
x /= size().width() / 2;
|
|
383 |
y /= size().height() / 2;
|
|
384 |
double r = sqrt(x * x + y * y);
|
|
385 |
|
|
386 |
double arcSin = asin(y / r);
|
|
387 |
double arcCos = acos(x / r);
|
|
388 |
|
|
389 |
double angle = arcCos * 180 / M_PI;
|
|
390 |
if (arcSin > 0) {
|
|
391 |
angle = -angle;
|
|
392 |
}
|
|
393 |
|
|
394 |
d_ptr->m_angleOffset = d_ptr->m_angleConical - angle;
|
|
395 |
d_ptr->m_dragAngle = d_ptr->m_angleConical;
|
|
396 |
d_ptr->m_dragHandle = QtGradientWidgetPrivate::AngleConicalHandle;
|
|
397 |
update();
|
|
398 |
return;
|
|
399 |
}
|
|
400 |
}
|
|
401 |
}
|
|
402 |
|
|
403 |
void QtGradientWidget::mouseReleaseEvent(QMouseEvent *e)
|
|
404 |
{
|
|
405 |
Q_UNUSED(e)
|
|
406 |
d_ptr->m_dragHandle = QtGradientWidgetPrivate::NoHandle;
|
|
407 |
update();
|
|
408 |
}
|
|
409 |
|
|
410 |
void QtGradientWidget::mouseMoveEvent(QMouseEvent *e)
|
|
411 |
{
|
|
412 |
if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::NoHandle)
|
|
413 |
return;
|
|
414 |
|
|
415 |
QPointF newPos = QPointF((double)e->pos().x() - d_ptr->m_dragOffset.x(),
|
|
416 |
(double)e->pos().y() - d_ptr->m_dragOffset.y());
|
|
417 |
QPointF newPoint = d_ptr->fromViewport(newPos);
|
|
418 |
if (newPoint.x() < 0)
|
|
419 |
newPoint.setX(0);
|
|
420 |
else if (newPoint.x() > 1)
|
|
421 |
newPoint.setX(1);
|
|
422 |
if (newPoint.y() < 0)
|
|
423 |
newPoint.setY(0);
|
|
424 |
else if (newPoint.y() > 1)
|
|
425 |
newPoint.setY(1);
|
|
426 |
|
|
427 |
if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::StartLinearHandle) {
|
|
428 |
d_ptr->m_startLinear = newPoint;
|
|
429 |
emit startLinearChanged(newPoint);
|
|
430 |
} else if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::EndLinearHandle) {
|
|
431 |
d_ptr->m_endLinear = newPoint;
|
|
432 |
emit endLinearChanged(newPoint);
|
|
433 |
} else if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::CentralRadialHandle) {
|
|
434 |
d_ptr->m_centralRadial = newPoint;
|
|
435 |
emit centralRadialChanged(newPoint);
|
|
436 |
} else if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::FocalRadialHandle) {
|
|
437 |
d_ptr->m_focalRadial = newPoint;
|
|
438 |
emit focalRadialChanged(newPoint);
|
|
439 |
} else if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::RadiusRadialHandle) {
|
|
440 |
QPointF centralPoint = d_ptr->toViewport(d_ptr->m_centralRadial);
|
|
441 |
QPointF pF(e->pos().x(), e->pos().y());
|
|
442 |
double x = pF.x() - centralPoint.x();
|
|
443 |
double y = pF.y() - centralPoint.y();
|
|
444 |
|
|
445 |
if ((d_ptr->m_handleSize * d_ptr->m_handleSize / 4) > (x * x + y * y)) {
|
|
446 |
if (d_ptr->m_radiusRadial != d_ptr->m_dragRadius) {
|
|
447 |
d_ptr->m_radiusRadial = d_ptr->m_dragRadius;
|
|
448 |
emit radiusRadialChanged(d_ptr->m_radiusRadial);
|
|
449 |
}
|
|
450 |
} else {
|
|
451 |
x = pF.x() / size().width() - d_ptr->m_centralRadial.x();
|
|
452 |
y = pF.y() / size().height() - d_ptr->m_centralRadial.y();
|
|
453 |
double moveRadius = sqrt(x * x + y * y);
|
|
454 |
//double newRadius = moveRadius + d_ptr->m_radiusOffset;
|
|
455 |
double newRadius = moveRadius * d_ptr->m_radiusFactor;
|
|
456 |
if (newRadius > 2)
|
|
457 |
newRadius = 2;
|
|
458 |
d_ptr->m_radiusRadial = newRadius;
|
|
459 |
emit radiusRadialChanged(d_ptr->m_radiusRadial);
|
|
460 |
}
|
|
461 |
} else if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::CentralConicalHandle) {
|
|
462 |
d_ptr->m_centralConical = newPoint;
|
|
463 |
emit centralConicalChanged(newPoint);
|
|
464 |
} else if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::AngleConicalHandle) {
|
|
465 |
QPointF centralPoint = d_ptr->toViewport(d_ptr->m_centralConical);
|
|
466 |
QPointF pF(e->pos().x(), e->pos().y());
|
|
467 |
double x = pF.x() - centralPoint.x();
|
|
468 |
double y = pF.y() - centralPoint.y();
|
|
469 |
|
|
470 |
if ((d_ptr->m_handleSize * d_ptr->m_handleSize / 4) > (x * x + y * y)) {
|
|
471 |
if (d_ptr->m_angleConical != d_ptr->m_dragAngle) {
|
|
472 |
d_ptr->m_angleConical = d_ptr->m_dragAngle;
|
|
473 |
emit angleConicalChanged(d_ptr->m_angleConical);
|
|
474 |
}
|
|
475 |
} else {
|
|
476 |
QPointF central = d_ptr->toViewport(d_ptr->m_centralConical);
|
|
477 |
QPointF current = pF;
|
|
478 |
x = current.x() - central.x();
|
|
479 |
y = current.y() - central.y();
|
|
480 |
x /= size().width() / 2;
|
|
481 |
y /= size().height() / 2;
|
|
482 |
double r = sqrt(x * x + y * y);
|
|
483 |
|
|
484 |
double arcSin = asin(y / r);
|
|
485 |
double arcCos = acos(x / r);
|
|
486 |
|
|
487 |
double angle = arcCos * 180 / M_PI;
|
|
488 |
if (arcSin > 0) {
|
|
489 |
angle = -angle;
|
|
490 |
}
|
|
491 |
|
|
492 |
angle += d_ptr->m_angleOffset;
|
|
493 |
|
|
494 |
d_ptr->setAngleConical(angle);
|
|
495 |
}
|
|
496 |
}
|
|
497 |
update();
|
|
498 |
}
|
|
499 |
|
|
500 |
void QtGradientWidget::mouseDoubleClickEvent(QMouseEvent *e)
|
|
501 |
{
|
|
502 |
mousePressEvent(e);
|
|
503 |
}
|
|
504 |
|
|
505 |
void QtGradientWidget::paintEvent(QPaintEvent *e)
|
|
506 |
{
|
|
507 |
Q_UNUSED(e)
|
|
508 |
|
|
509 |
QPainter p(this);
|
|
510 |
|
|
511 |
if (d_ptr->m_backgroundCheckered) {
|
|
512 |
int pixSize = 40;
|
|
513 |
QPixmap pm(2 * pixSize, 2 * pixSize);
|
|
514 |
|
|
515 |
QPainter pmp(&pm);
|
|
516 |
pmp.fillRect(0, 0, pixSize, pixSize, Qt::white);
|
|
517 |
pmp.fillRect(pixSize, pixSize, pixSize, pixSize, Qt::white);
|
|
518 |
pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::black);
|
|
519 |
pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::black);
|
|
520 |
|
|
521 |
p.setBrushOrigin((size().width() % pixSize + pixSize) / 2, (size().height() % pixSize + pixSize) / 2);
|
|
522 |
p.fillRect(rect(), pm);
|
|
523 |
p.setBrushOrigin(0, 0);
|
|
524 |
}
|
|
525 |
|
|
526 |
QGradient *gradient = 0;
|
|
527 |
switch (d_ptr->m_gradientType) {
|
|
528 |
case QGradient::LinearGradient:
|
|
529 |
gradient = new QLinearGradient(d_ptr->m_startLinear, d_ptr->m_endLinear);
|
|
530 |
break;
|
|
531 |
case QGradient::RadialGradient:
|
|
532 |
gradient = new QRadialGradient(d_ptr->m_centralRadial, d_ptr->m_radiusRadial, d_ptr->m_focalRadial);
|
|
533 |
break;
|
|
534 |
case QGradient::ConicalGradient:
|
|
535 |
gradient = new QConicalGradient(d_ptr->m_centralConical, d_ptr->m_angleConical);
|
|
536 |
break;
|
|
537 |
default:
|
|
538 |
break;
|
|
539 |
}
|
|
540 |
if (!gradient)
|
|
541 |
return;
|
|
542 |
|
|
543 |
gradient->setStops(d_ptr->m_gradientStops);
|
|
544 |
gradient->setSpread(d_ptr->m_gradientSpread);
|
|
545 |
|
|
546 |
p.save();
|
|
547 |
p.scale(size().width(), size().height());
|
|
548 |
p.fillRect(QRect(0, 0, 1, 1), *gradient);
|
|
549 |
p.restore();
|
|
550 |
|
|
551 |
p.setRenderHint(QPainter::Antialiasing);
|
|
552 |
|
|
553 |
QColor c = QColor::fromRgbF(0.5, 0.5, 0.5, 0.5);
|
|
554 |
QBrush br(c);
|
|
555 |
p.setBrush(br);
|
|
556 |
QPen pen(Qt::white);
|
|
557 |
pen.setWidthF(1);
|
|
558 |
p.setPen(pen);
|
|
559 |
QPen dragPen = pen;
|
|
560 |
dragPen.setWidthF(2);
|
|
561 |
if (d_ptr->m_gradientType == QGradient::LinearGradient) {
|
|
562 |
p.save();
|
|
563 |
if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::StartLinearHandle)
|
|
564 |
p.setPen(dragPen);
|
|
565 |
d_ptr->paintPoint(&p, d_ptr->m_startLinear, d_ptr->m_handleSize);
|
|
566 |
p.restore();
|
|
567 |
|
|
568 |
p.save();
|
|
569 |
if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::EndLinearHandle)
|
|
570 |
p.setPen(dragPen);
|
|
571 |
d_ptr->paintPoint(&p, d_ptr->m_endLinear, d_ptr->m_handleSize);
|
|
572 |
p.restore();
|
|
573 |
} else if (d_ptr->m_gradientType == QGradient::RadialGradient) {
|
|
574 |
QPointF central = d_ptr->toViewport(d_ptr->m_centralRadial);
|
|
575 |
|
|
576 |
p.save();
|
|
577 |
QRectF r = d_ptr->pointRect(central, 2 * d_ptr->m_handleSize / 3);
|
|
578 |
QRectF r1(0, r.y(), size().width(), r.height());
|
|
579 |
QRectF r2(r.x(), 0, r.width(), r.y());
|
|
580 |
QRectF r3(r.x(), r.y() + r.height(), r.width(), size().height() - r.y() - r.height());
|
|
581 |
p.fillRect(r1, c);
|
|
582 |
p.fillRect(r2, c);
|
|
583 |
p.fillRect(r3, c);
|
|
584 |
p.setBrush(Qt::NoBrush);
|
|
585 |
p.save();
|
|
586 |
if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::CentralRadialHandle)
|
|
587 |
p.setPen(dragPen);
|
|
588 |
d_ptr->paintPoint(&p, d_ptr->m_centralRadial, d_ptr->m_handleSize);
|
|
589 |
p.restore();
|
|
590 |
|
|
591 |
QRectF rect = QRectF(central.x() - d_ptr->m_radiusRadial * size().width(),
|
|
592 |
central.y() - d_ptr->m_radiusRadial * size().height(),
|
|
593 |
2 * d_ptr->m_radiusRadial * size().width(),
|
|
594 |
2 * d_ptr->m_radiusRadial * size().height());
|
|
595 |
p.setClipRect(r1);
|
|
596 |
p.setClipRect(r2, Qt::UniteClip);
|
|
597 |
p.setClipRect(r3, Qt::UniteClip);
|
|
598 |
p.drawEllipse(rect);
|
|
599 |
if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::RadiusRadialHandle) {
|
|
600 |
p.save();
|
|
601 |
p.setPen(dragPen);
|
|
602 |
QRectF rect = QRectF(central.x() - d_ptr->m_radiusRadial / d_ptr->m_radiusFactor * size().width(),
|
|
603 |
central.y() - d_ptr->m_radiusRadial / d_ptr->m_radiusFactor * size().height(),
|
|
604 |
2 * d_ptr->m_radiusRadial / d_ptr->m_radiusFactor * size().width(),
|
|
605 |
2 * d_ptr->m_radiusRadial / d_ptr->m_radiusFactor * size().height());
|
|
606 |
p.drawEllipse(rect);
|
|
607 |
|
|
608 |
p.restore();
|
|
609 |
}
|
|
610 |
p.restore();
|
|
611 |
|
|
612 |
p.save();
|
|
613 |
if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::FocalRadialHandle)
|
|
614 |
p.setPen(dragPen);
|
|
615 |
d_ptr->paintPoint(&p, d_ptr->m_focalRadial, 2 * d_ptr->m_handleSize / 3);
|
|
616 |
p.restore();
|
|
617 |
} else if (d_ptr->m_gradientType == QGradient::ConicalGradient) {
|
|
618 |
double radius = size().width();
|
|
619 |
if (size().height() < radius)
|
|
620 |
radius = size().height();
|
|
621 |
radius /= 2;
|
|
622 |
double corr = d_ptr->m_handleSize / 3;
|
|
623 |
radius -= corr;
|
|
624 |
QPointF central = d_ptr->toViewport(d_ptr->m_centralConical);
|
|
625 |
|
|
626 |
p.save();
|
|
627 |
p.setBrush(Qt::NoBrush);
|
|
628 |
QPen pen2(c);
|
|
629 |
pen2.setWidthF(2 * d_ptr->m_handleSize / 3);
|
|
630 |
p.setPen(pen2);
|
|
631 |
p.drawEllipse(d_ptr->pointRect(central, 2 * radius));
|
|
632 |
p.restore();
|
|
633 |
|
|
634 |
p.save();
|
|
635 |
p.setBrush(Qt::NoBrush);
|
|
636 |
int pointCount = 2;
|
|
637 |
for (int i = 0; i < pointCount; i++) {
|
|
638 |
QPointF ang(cos(M_PI * (i * 180.0 / pointCount + d_ptr->m_angleConical) / 180) * size().width() / 2,
|
|
639 |
-sin(M_PI * (i * 180.0 / pointCount + d_ptr->m_angleConical) / 180) * size().height() / 2);
|
|
640 |
double mod = sqrt(ang.x() * ang.x() + ang.y() * ang.y());
|
|
641 |
p.drawLine(QPointF(central.x() + ang.x() * (radius - corr) / mod,
|
|
642 |
central.y() + ang.y() * (radius - corr) / mod),
|
|
643 |
QPointF(central.x() + ang.x() * (radius + corr) / mod,
|
|
644 |
central.y() + ang.y() * (radius + corr) / mod));
|
|
645 |
p.drawLine(QPointF(central.x() - ang.x() * (radius - corr) / mod,
|
|
646 |
central.y() - ang.y() * (radius - corr) / mod),
|
|
647 |
QPointF(central.x() - ang.x() * (radius + corr) / mod,
|
|
648 |
central.y() - ang.y() * (radius + corr) / mod));
|
|
649 |
}
|
|
650 |
if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::AngleConicalHandle) {
|
|
651 |
p.save();
|
|
652 |
p.setPen(dragPen);
|
|
653 |
QPointF ang(cos(M_PI * (d_ptr->m_angleConical - d_ptr->m_angleOffset) / 180) * size().width() / 2,
|
|
654 |
-sin(M_PI * (d_ptr->m_angleConical - d_ptr->m_angleOffset) / 180) * size().height() / 2);
|
|
655 |
double mod = sqrt(ang.x() * ang.x() + ang.y() * ang.y());
|
|
656 |
p.drawLine(QPointF(central.x() + ang.x() * (radius - corr) / mod,
|
|
657 |
central.y() + ang.y() * (radius - corr) / mod),
|
|
658 |
QPointF(central.x() + ang.x() * (radius + corr) / mod,
|
|
659 |
central.y() + ang.y() * (radius + corr) / mod));
|
|
660 |
p.restore();
|
|
661 |
}
|
|
662 |
|
|
663 |
p.restore();
|
|
664 |
|
|
665 |
p.save();
|
|
666 |
if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::CentralConicalHandle)
|
|
667 |
p.setPen(dragPen);
|
|
668 |
d_ptr->paintPoint(&p, d_ptr->m_centralConical, d_ptr->m_handleSize);
|
|
669 |
p.restore();
|
|
670 |
|
|
671 |
}
|
|
672 |
|
|
673 |
delete gradient;
|
|
674 |
}
|
|
675 |
|
|
676 |
void QtGradientWidget::setGradientStops(const QGradientStops &stops)
|
|
677 |
{
|
|
678 |
d_ptr->m_gradientStops = stops;
|
|
679 |
update();
|
|
680 |
}
|
|
681 |
|
|
682 |
QGradientStops QtGradientWidget::gradientStops() const
|
|
683 |
{
|
|
684 |
return d_ptr->m_gradientStops;
|
|
685 |
}
|
|
686 |
|
|
687 |
void QtGradientWidget::setGradientType(QGradient::Type type)
|
|
688 |
{
|
|
689 |
if (type == QGradient::NoGradient)
|
|
690 |
return;
|
|
691 |
if (d_ptr->m_gradientType == type)
|
|
692 |
return;
|
|
693 |
|
|
694 |
d_ptr->m_gradientType = type;
|
|
695 |
update();
|
|
696 |
}
|
|
697 |
|
|
698 |
QGradient::Type QtGradientWidget::gradientType() const
|
|
699 |
{
|
|
700 |
return d_ptr->m_gradientType;
|
|
701 |
}
|
|
702 |
|
|
703 |
void QtGradientWidget::setGradientSpread(QGradient::Spread spread)
|
|
704 |
{
|
|
705 |
if (d_ptr->m_gradientSpread == spread)
|
|
706 |
return;
|
|
707 |
|
|
708 |
d_ptr->m_gradientSpread = spread;
|
|
709 |
update();
|
|
710 |
}
|
|
711 |
|
|
712 |
QGradient::Spread QtGradientWidget::gradientSpread() const
|
|
713 |
{
|
|
714 |
return d_ptr->m_gradientSpread;
|
|
715 |
}
|
|
716 |
|
|
717 |
void QtGradientWidget::setStartLinear(const QPointF &point)
|
|
718 |
{
|
|
719 |
if (d_ptr->m_startLinear == point)
|
|
720 |
return;
|
|
721 |
|
|
722 |
d_ptr->m_startLinear = d_ptr->checkRange(point);
|
|
723 |
update();
|
|
724 |
}
|
|
725 |
|
|
726 |
QPointF QtGradientWidget::startLinear() const
|
|
727 |
{
|
|
728 |
return d_ptr->m_startLinear;
|
|
729 |
}
|
|
730 |
|
|
731 |
void QtGradientWidget::setEndLinear(const QPointF &point)
|
|
732 |
{
|
|
733 |
if (d_ptr->m_endLinear == point)
|
|
734 |
return;
|
|
735 |
|
|
736 |
d_ptr->m_endLinear = d_ptr->checkRange(point);
|
|
737 |
update();
|
|
738 |
}
|
|
739 |
|
|
740 |
QPointF QtGradientWidget::endLinear() const
|
|
741 |
{
|
|
742 |
return d_ptr->m_endLinear;
|
|
743 |
}
|
|
744 |
|
|
745 |
void QtGradientWidget::setCentralRadial(const QPointF &point)
|
|
746 |
{
|
|
747 |
if (d_ptr->m_centralRadial == point)
|
|
748 |
return;
|
|
749 |
|
|
750 |
d_ptr->m_centralRadial = point;
|
|
751 |
update();
|
|
752 |
}
|
|
753 |
|
|
754 |
QPointF QtGradientWidget::centralRadial() const
|
|
755 |
{
|
|
756 |
return d_ptr->m_centralRadial;
|
|
757 |
}
|
|
758 |
|
|
759 |
void QtGradientWidget::setFocalRadial(const QPointF &point)
|
|
760 |
{
|
|
761 |
if (d_ptr->m_focalRadial == point)
|
|
762 |
return;
|
|
763 |
|
|
764 |
d_ptr->m_focalRadial = point;
|
|
765 |
update();
|
|
766 |
}
|
|
767 |
|
|
768 |
QPointF QtGradientWidget::focalRadial() const
|
|
769 |
{
|
|
770 |
return d_ptr->m_focalRadial;
|
|
771 |
}
|
|
772 |
|
|
773 |
void QtGradientWidget::setRadiusRadial(qreal radius)
|
|
774 |
{
|
|
775 |
if (d_ptr->m_radiusRadial == radius)
|
|
776 |
return;
|
|
777 |
|
|
778 |
d_ptr->m_radiusRadial = radius;
|
|
779 |
update();
|
|
780 |
}
|
|
781 |
|
|
782 |
qreal QtGradientWidget::radiusRadial() const
|
|
783 |
{
|
|
784 |
return d_ptr->m_radiusRadial;
|
|
785 |
}
|
|
786 |
|
|
787 |
void QtGradientWidget::setCentralConical(const QPointF &point)
|
|
788 |
{
|
|
789 |
if (d_ptr->m_centralConical == point)
|
|
790 |
return;
|
|
791 |
|
|
792 |
d_ptr->m_centralConical = point;
|
|
793 |
update();
|
|
794 |
}
|
|
795 |
|
|
796 |
QPointF QtGradientWidget::centralConical() const
|
|
797 |
{
|
|
798 |
return d_ptr->m_centralConical;
|
|
799 |
}
|
|
800 |
|
|
801 |
void QtGradientWidget::setAngleConical(qreal angle)
|
|
802 |
{
|
|
803 |
if (d_ptr->m_angleConical == angle)
|
|
804 |
return;
|
|
805 |
|
|
806 |
d_ptr->m_angleConical = angle;
|
|
807 |
update();
|
|
808 |
}
|
|
809 |
|
|
810 |
qreal QtGradientWidget::angleConical() const
|
|
811 |
{
|
|
812 |
return d_ptr->m_angleConical;
|
|
813 |
}
|
|
814 |
|
|
815 |
QT_END_NAMESPACE
|