source: trunk/tools/shared/qtgradienteditor/qtgradientwidget.cpp@ 447

Last change on this file since 447 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 25.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the tools 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 [email protected].
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
58QT_BEGIN_NAMESPACE
59
60class QtGradientWidgetPrivate
61{
62 QtGradientWidget *q_ptr;
63 Q_DECLARE_PUBLIC(QtGradientWidget)
64public:
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
111double 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
121void 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
130QRectF QtGradientWidgetPrivate::pointRect(const QPointF &point, double size) const
131{
132 return QRectF(point.x() - size / 2, point.y() - size / 2, size, size);
133}
134
135QPointF 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
149QPointF 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
155QPointF 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
161void 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/*
203void 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
234QtGradientWidget::QtGradientWidget(QWidget *parent)
235 : QWidget(parent)
236{
237 d_ptr = new QtGradientWidgetPrivate;
238 d_ptr->q_ptr = this;
239 d_ptr->m_backgroundCheckered = true;
240 d_ptr->m_handleSize = 20.0;
241 d_ptr->m_gradientType = QGradient::LinearGradient;
242 d_ptr->m_startLinear = QPointF(0, 0);
243 d_ptr->m_endLinear = QPointF(1, 1);
244 d_ptr->m_centralRadial = QPointF(0.5, 0.5);
245 d_ptr->m_focalRadial = QPointF(0.5, 0.5);
246 d_ptr->m_radiusRadial = 0.5;
247 d_ptr->m_centralConical = QPointF(0.5, 0.5);
248 d_ptr->m_angleConical = 0;
249 d_ptr->m_dragHandle = QtGradientWidgetPrivate::NoHandle;
250
251 setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
252}
253
254QtGradientWidget::~QtGradientWidget()
255{
256 delete d_ptr;
257}
258
259QSize QtGradientWidget::sizeHint() const
260{
261 return QSize(176, 176);
262}
263
264QSize QtGradientWidget::minimumSizeHint() const
265{
266 return QSize(128, 128);
267}
268
269int QtGradientWidget::heightForWidth(int w) const
270{
271 return w;
272}
273
274void QtGradientWidget::setBackgroundCheckered(bool checkered)
275{
276 if (d_ptr->m_backgroundCheckered == checkered)
277 return;
278 d_ptr->m_backgroundCheckered = checkered;
279 update();
280}
281
282bool QtGradientWidget::isBackgroundCheckered() const
283{
284 return d_ptr->m_backgroundCheckered;
285}
286
287void QtGradientWidget::mousePressEvent(QMouseEvent *e)
288{
289 if (e->button() != Qt::LeftButton)
290 return;
291
292 QPoint p = e->pos();
293 if (d_ptr->m_gradientType == QGradient::LinearGradient) {
294 QPointF startPoint = d_ptr->toViewport(d_ptr->m_startLinear);
295 double x = p.x() - startPoint.x();
296 double y = p.y() - startPoint.y();
297
298 if ((d_ptr->m_handleSize * d_ptr->m_handleSize / 4) > (x * x + y * y)) {
299 d_ptr->m_dragHandle = QtGradientWidgetPrivate::StartLinearHandle;
300 d_ptr->m_dragOffset = QPointF(x, y);
301 update();
302 return;
303 }
304
305 QPointF endPoint = d_ptr->toViewport(d_ptr->m_endLinear);
306 x = p.x() - endPoint.x();
307 y = p.y() - endPoint.y();
308
309 if ((d_ptr->m_handleSize * d_ptr->m_handleSize / 4) > (x * x + y * y)) {
310 d_ptr->m_dragHandle = QtGradientWidgetPrivate::EndLinearHandle;
311 d_ptr->m_dragOffset = QPointF(x, y);
312 update();
313 return;
314 }
315 } else if (d_ptr->m_gradientType == QGradient::RadialGradient) {
316 QPointF focalPoint = d_ptr->toViewport(d_ptr->m_focalRadial);
317 double x = p.x() - focalPoint.x();
318 double y = p.y() - focalPoint.y();
319
320 if ((d_ptr->m_handleSize * d_ptr->m_handleSize / 9) > (x * x + y * y)) {
321 d_ptr->m_dragHandle = QtGradientWidgetPrivate::FocalRadialHandle;
322 d_ptr->m_dragOffset = QPointF(x, y);
323 update();
324 return;
325 }
326
327 QPointF centralPoint = d_ptr->toViewport(d_ptr->m_centralRadial);
328 x = p.x() - centralPoint.x();
329 y = p.y() - centralPoint.y();
330
331 if ((d_ptr->m_handleSize * d_ptr->m_handleSize / 4) > (x * x + y * y)) {
332 d_ptr->m_dragHandle = QtGradientWidgetPrivate::CentralRadialHandle;
333 d_ptr->m_dragOffset = QPointF(x, y);
334 update();
335 return;
336 }
337
338 QPointF central = d_ptr->toViewport(d_ptr->m_centralRadial);
339 QRectF r = d_ptr->pointRect(central, 2 * d_ptr->m_handleSize / 3);
340 QRectF r1(0, r.y(), size().width(), r.height());
341 QRectF r2(r.x(), 0, r.width(), r.y());
342 QRectF r3(r.x(), r.y() + r.height(), r.width(), size().height() - r.y() - r.height());
343 QPointF pF(p.x(), p.y());
344 if (r1.contains(pF) || r2.contains(pF) || r3.contains(pF)) {
345 x = pF.x() / size().width() - d_ptr->m_centralRadial.x();
346 y = pF.y() / size().height() - d_ptr->m_centralRadial.y();
347 double clickRadius = sqrt(x * x + y * y);
348 //d_ptr->m_radiusOffset = d_ptr->m_radiusRadial - clickRadius;
349 d_ptr->m_radiusFactor = d_ptr->m_radiusRadial / clickRadius;
350 if (d_ptr->m_radiusFactor == 0)
351 d_ptr->m_radiusFactor = 1;
352 d_ptr->m_dragRadius = d_ptr->m_radiusRadial;
353 d_ptr->m_dragHandle = QtGradientWidgetPrivate::RadiusRadialHandle;
354 mouseMoveEvent(e);
355 update();
356 return;
357 }
358 } else if (d_ptr->m_gradientType == QGradient::ConicalGradient) {
359 QPointF centralPoint = d_ptr->toViewport(d_ptr->m_centralConical);
360 double x = p.x() - centralPoint.x();
361 double y = p.y() - centralPoint.y();
362
363 if ((d_ptr->m_handleSize * d_ptr->m_handleSize / 4) > (x * x + y * y)) {
364 d_ptr->m_dragHandle = QtGradientWidgetPrivate::CentralConicalHandle;
365 d_ptr->m_dragOffset = QPointF(x, y);
366 update();
367 return;
368 }
369 double radius = size().width();
370 if (size().height() < radius)
371 radius = size().height();
372 radius /= 2;
373 double corr = d_ptr->m_handleSize / 3;
374 radius -= corr;
375 QPointF vp = d_ptr->toViewport(d_ptr->m_centralConical);
376 x = p.x() - vp.x();
377 y = p.y() - vp.y();
378 if (((radius - corr) * (radius - corr) < (x * x + y * y)) &&
379 ((radius + corr) * (radius + corr) > (x * x + y * y))) {
380 QPointF central = d_ptr->toViewport(d_ptr->m_centralConical);
381 QPointF current(e->pos().x(), e->pos().y());
382 x = current.x() - central.x();
383 y = current.y() - central.y();
384 x /= size().width() / 2;
385 y /= size().height() / 2;
386 double r = sqrt(x * x + y * y);
387
388 double arcSin = asin(y / r);
389 double arcCos = acos(x / r);
390
391 double angle = arcCos * 180 / M_PI;
392 if (arcSin > 0) {
393 angle = -angle;
394 }
395
396 d_ptr->m_angleOffset = d_ptr->m_angleConical - angle;
397 d_ptr->m_dragAngle = d_ptr->m_angleConical;
398 d_ptr->m_dragHandle = QtGradientWidgetPrivate::AngleConicalHandle;
399 update();
400 return;
401 }
402 }
403}
404
405void QtGradientWidget::mouseReleaseEvent(QMouseEvent *e)
406{
407 Q_UNUSED(e)
408 d_ptr->m_dragHandle = QtGradientWidgetPrivate::NoHandle;
409 update();
410}
411
412void QtGradientWidget::mouseMoveEvent(QMouseEvent *e)
413{
414 if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::NoHandle)
415 return;
416
417 QPointF newPos = QPointF((double)e->pos().x() - d_ptr->m_dragOffset.x(),
418 (double)e->pos().y() - d_ptr->m_dragOffset.y());
419 QPointF newPoint = d_ptr->fromViewport(newPos);
420 if (newPoint.x() < 0)
421 newPoint.setX(0);
422 else if (newPoint.x() > 1)
423 newPoint.setX(1);
424 if (newPoint.y() < 0)
425 newPoint.setY(0);
426 else if (newPoint.y() > 1)
427 newPoint.setY(1);
428
429 if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::StartLinearHandle) {
430 d_ptr->m_startLinear = newPoint;
431 emit startLinearChanged(newPoint);
432 } else if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::EndLinearHandle) {
433 d_ptr->m_endLinear = newPoint;
434 emit endLinearChanged(newPoint);
435 } else if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::CentralRadialHandle) {
436 d_ptr->m_centralRadial = newPoint;
437 emit centralRadialChanged(newPoint);
438 } else if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::FocalRadialHandle) {
439 d_ptr->m_focalRadial = newPoint;
440 emit focalRadialChanged(newPoint);
441 } else if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::RadiusRadialHandle) {
442 QPointF centralPoint = d_ptr->toViewport(d_ptr->m_centralRadial);
443 QPointF pF(e->pos().x(), e->pos().y());
444 double x = pF.x() - centralPoint.x();
445 double y = pF.y() - centralPoint.y();
446
447 if ((d_ptr->m_handleSize * d_ptr->m_handleSize / 4) > (x * x + y * y)) {
448 if (d_ptr->m_radiusRadial != d_ptr->m_dragRadius) {
449 d_ptr->m_radiusRadial = d_ptr->m_dragRadius;
450 emit radiusRadialChanged(d_ptr->m_radiusRadial);
451 }
452 } else {
453 x = pF.x() / size().width() - d_ptr->m_centralRadial.x();
454 y = pF.y() / size().height() - d_ptr->m_centralRadial.y();
455 double moveRadius = sqrt(x * x + y * y);
456 //double newRadius = moveRadius + d_ptr->m_radiusOffset;
457 double newRadius = moveRadius * d_ptr->m_radiusFactor;
458 if (newRadius > 2)
459 newRadius = 2;
460 d_ptr->m_radiusRadial = newRadius;
461 emit radiusRadialChanged(d_ptr->m_radiusRadial);
462 }
463 } else if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::CentralConicalHandle) {
464 d_ptr->m_centralConical = newPoint;
465 emit centralConicalChanged(newPoint);
466 } else if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::AngleConicalHandle) {
467 QPointF centralPoint = d_ptr->toViewport(d_ptr->m_centralConical);
468 QPointF pF(e->pos().x(), e->pos().y());
469 double x = pF.x() - centralPoint.x();
470 double y = pF.y() - centralPoint.y();
471
472 if ((d_ptr->m_handleSize * d_ptr->m_handleSize / 4) > (x * x + y * y)) {
473 if (d_ptr->m_angleConical != d_ptr->m_dragAngle) {
474 d_ptr->m_angleConical = d_ptr->m_dragAngle;
475 emit angleConicalChanged(d_ptr->m_angleConical);
476 }
477 } else {
478 QPointF central = d_ptr->toViewport(d_ptr->m_centralConical);
479 QPointF current = pF;
480 x = current.x() - central.x();
481 y = current.y() - central.y();
482 x /= size().width() / 2;
483 y /= size().height() / 2;
484 double r = sqrt(x * x + y * y);
485
486 double arcSin = asin(y / r);
487 double arcCos = acos(x / r);
488
489 double angle = arcCos * 180 / M_PI;
490 if (arcSin > 0) {
491 angle = -angle;
492 }
493
494 angle += d_ptr->m_angleOffset;
495
496 d_ptr->setAngleConical(angle);
497 }
498 }
499 update();
500}
501
502void QtGradientWidget::mouseDoubleClickEvent(QMouseEvent *e)
503{
504 mousePressEvent(e);
505}
506
507void QtGradientWidget::paintEvent(QPaintEvent *e)
508{
509 Q_UNUSED(e)
510
511 QPainter p(this);
512
513 if (d_ptr->m_backgroundCheckered) {
514 int pixSize = 40;
515 QPixmap pm(2 * pixSize, 2 * pixSize);
516
517 QPainter pmp(&pm);
518 pmp.fillRect(0, 0, pixSize, pixSize, Qt::white);
519 pmp.fillRect(pixSize, pixSize, pixSize, pixSize, Qt::white);
520 pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::black);
521 pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::black);
522
523 p.setBrushOrigin((size().width() % pixSize + pixSize) / 2, (size().height() % pixSize + pixSize) / 2);
524 p.fillRect(rect(), pm);
525 p.setBrushOrigin(0, 0);
526 }
527
528 QGradient *gradient = 0;
529 switch (d_ptr->m_gradientType) {
530 case QGradient::LinearGradient:
531 gradient = new QLinearGradient(d_ptr->m_startLinear, d_ptr->m_endLinear);
532 break;
533 case QGradient::RadialGradient:
534 gradient = new QRadialGradient(d_ptr->m_centralRadial, d_ptr->m_radiusRadial, d_ptr->m_focalRadial);
535 break;
536 case QGradient::ConicalGradient:
537 gradient = new QConicalGradient(d_ptr->m_centralConical, d_ptr->m_angleConical);
538 break;
539 default:
540 break;
541 }
542 if (!gradient)
543 return;
544
545 gradient->setStops(d_ptr->m_gradientStops);
546 gradient->setSpread(d_ptr->m_gradientSpread);
547
548 p.save();
549 p.scale(size().width(), size().height());
550 p.fillRect(QRect(0, 0, 1, 1), *gradient);
551 p.restore();
552
553 p.setRenderHint(QPainter::Antialiasing);
554
555 QColor c = QColor::fromRgbF(0.5, 0.5, 0.5, 0.5);
556 QBrush br(c);
557 p.setBrush(br);
558 QPen pen(Qt::white);
559 pen.setWidthF(1);
560 p.setPen(pen);
561 QPen dragPen = pen;
562 dragPen.setWidthF(2);
563 if (d_ptr->m_gradientType == QGradient::LinearGradient) {
564 p.save();
565 if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::StartLinearHandle)
566 p.setPen(dragPen);
567 d_ptr->paintPoint(&p, d_ptr->m_startLinear, d_ptr->m_handleSize);
568 p.restore();
569
570 p.save();
571 if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::EndLinearHandle)
572 p.setPen(dragPen);
573 d_ptr->paintPoint(&p, d_ptr->m_endLinear, d_ptr->m_handleSize);
574 p.restore();
575 } else if (d_ptr->m_gradientType == QGradient::RadialGradient) {
576 QPointF central = d_ptr->toViewport(d_ptr->m_centralRadial);
577
578 p.save();
579 QRectF r = d_ptr->pointRect(central, 2 * d_ptr->m_handleSize / 3);
580 QRectF r1(0, r.y(), size().width(), r.height());
581 QRectF r2(r.x(), 0, r.width(), r.y());
582 QRectF r3(r.x(), r.y() + r.height(), r.width(), size().height() - r.y() - r.height());
583 p.fillRect(r1, c);
584 p.fillRect(r2, c);
585 p.fillRect(r3, c);
586 p.setBrush(Qt::NoBrush);
587 p.save();
588 if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::CentralRadialHandle)
589 p.setPen(dragPen);
590 d_ptr->paintPoint(&p, d_ptr->m_centralRadial, d_ptr->m_handleSize);
591 p.restore();
592
593 QRectF rect = QRectF(central.x() - d_ptr->m_radiusRadial * size().width(),
594 central.y() - d_ptr->m_radiusRadial * size().height(),
595 2 * d_ptr->m_radiusRadial * size().width(),
596 2 * d_ptr->m_radiusRadial * size().height());
597 p.setClipRect(r1);
598 p.setClipRect(r2, Qt::UniteClip);
599 p.setClipRect(r3, Qt::UniteClip);
600 p.drawEllipse(rect);
601 if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::RadiusRadialHandle) {
602 p.save();
603 p.setPen(dragPen);
604 QRectF rect = QRectF(central.x() - d_ptr->m_radiusRadial / d_ptr->m_radiusFactor * size().width(),
605 central.y() - d_ptr->m_radiusRadial / d_ptr->m_radiusFactor * size().height(),
606 2 * d_ptr->m_radiusRadial / d_ptr->m_radiusFactor * size().width(),
607 2 * d_ptr->m_radiusRadial / d_ptr->m_radiusFactor * size().height());
608 p.drawEllipse(rect);
609
610 p.restore();
611 }
612 p.restore();
613
614 p.save();
615 if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::FocalRadialHandle)
616 p.setPen(dragPen);
617 d_ptr->paintPoint(&p, d_ptr->m_focalRadial, 2 * d_ptr->m_handleSize / 3);
618 p.restore();
619 } else if (d_ptr->m_gradientType == QGradient::ConicalGradient) {
620 double radius = size().width();
621 if (size().height() < radius)
622 radius = size().height();
623 radius /= 2;
624 double corr = d_ptr->m_handleSize / 3;
625 radius -= corr;
626 QPointF central = d_ptr->toViewport(d_ptr->m_centralConical);
627
628 p.save();
629 p.setBrush(Qt::NoBrush);
630 QPen pen2(c);
631 pen2.setWidthF(2 * d_ptr->m_handleSize / 3);
632 p.setPen(pen2);
633 p.drawEllipse(d_ptr->pointRect(central, 2 * radius));
634 p.restore();
635
636 p.save();
637 p.setBrush(Qt::NoBrush);
638 int pointCount = 2;
639 for (int i = 0; i < pointCount; i++) {
640 QPointF ang(cos(M_PI * (i * 180.0 / pointCount + d_ptr->m_angleConical) / 180) * size().width() / 2,
641 -sin(M_PI * (i * 180.0 / pointCount + d_ptr->m_angleConical) / 180) * size().height() / 2);
642 double mod = sqrt(ang.x() * ang.x() + ang.y() * ang.y());
643 p.drawLine(QPointF(central.x() + ang.x() * (radius - corr) / mod,
644 central.y() + ang.y() * (radius - corr) / mod),
645 QPointF(central.x() + ang.x() * (radius + corr) / mod,
646 central.y() + ang.y() * (radius + corr) / mod));
647 p.drawLine(QPointF(central.x() - ang.x() * (radius - corr) / mod,
648 central.y() - ang.y() * (radius - corr) / mod),
649 QPointF(central.x() - ang.x() * (radius + corr) / mod,
650 central.y() - ang.y() * (radius + corr) / mod));
651 }
652 if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::AngleConicalHandle) {
653 p.save();
654 p.setPen(dragPen);
655 QPointF ang(cos(M_PI * (d_ptr->m_angleConical - d_ptr->m_angleOffset) / 180) * size().width() / 2,
656 -sin(M_PI * (d_ptr->m_angleConical - d_ptr->m_angleOffset) / 180) * size().height() / 2);
657 double mod = sqrt(ang.x() * ang.x() + ang.y() * ang.y());
658 p.drawLine(QPointF(central.x() + ang.x() * (radius - corr) / mod,
659 central.y() + ang.y() * (radius - corr) / mod),
660 QPointF(central.x() + ang.x() * (radius + corr) / mod,
661 central.y() + ang.y() * (radius + corr) / mod));
662 p.restore();
663 }
664
665 p.restore();
666
667 p.save();
668 if (d_ptr->m_dragHandle == QtGradientWidgetPrivate::CentralConicalHandle)
669 p.setPen(dragPen);
670 d_ptr->paintPoint(&p, d_ptr->m_centralConical, d_ptr->m_handleSize);
671 p.restore();
672
673 }
674
675 delete gradient;
676}
677
678void QtGradientWidget::setGradientStops(const QGradientStops &stops)
679{
680 d_ptr->m_gradientStops = stops;
681 update();
682}
683
684QGradientStops QtGradientWidget::gradientStops() const
685{
686 return d_ptr->m_gradientStops;
687}
688
689void QtGradientWidget::setGradientType(QGradient::Type type)
690{
691 if (type == QGradient::NoGradient)
692 return;
693 if (d_ptr->m_gradientType == type)
694 return;
695
696 d_ptr->m_gradientType = type;
697 update();
698}
699
700QGradient::Type QtGradientWidget::gradientType() const
701{
702 return d_ptr->m_gradientType;
703}
704
705void QtGradientWidget::setGradientSpread(QGradient::Spread spread)
706{
707 if (d_ptr->m_gradientSpread == spread)
708 return;
709
710 d_ptr->m_gradientSpread = spread;
711 update();
712}
713
714QGradient::Spread QtGradientWidget::gradientSpread() const
715{
716 return d_ptr->m_gradientSpread;
717}
718
719void QtGradientWidget::setStartLinear(const QPointF &point)
720{
721 if (d_ptr->m_startLinear == point)
722 return;
723
724 d_ptr->m_startLinear = d_ptr->checkRange(point);
725 update();
726}
727
728QPointF QtGradientWidget::startLinear() const
729{
730 return d_ptr->m_startLinear;
731}
732
733void QtGradientWidget::setEndLinear(const QPointF &point)
734{
735 if (d_ptr->m_endLinear == point)
736 return;
737
738 d_ptr->m_endLinear = d_ptr->checkRange(point);
739 update();
740}
741
742QPointF QtGradientWidget::endLinear() const
743{
744 return d_ptr->m_endLinear;
745}
746
747void QtGradientWidget::setCentralRadial(const QPointF &point)
748{
749 if (d_ptr->m_centralRadial == point)
750 return;
751
752 d_ptr->m_centralRadial = point;
753 update();
754}
755
756QPointF QtGradientWidget::centralRadial() const
757{
758 return d_ptr->m_centralRadial;
759}
760
761void QtGradientWidget::setFocalRadial(const QPointF &point)
762{
763 if (d_ptr->m_focalRadial == point)
764 return;
765
766 d_ptr->m_focalRadial = point;
767 update();
768}
769
770QPointF QtGradientWidget::focalRadial() const
771{
772 return d_ptr->m_focalRadial;
773}
774
775void QtGradientWidget::setRadiusRadial(qreal radius)
776{
777 if (d_ptr->m_radiusRadial == radius)
778 return;
779
780 d_ptr->m_radiusRadial = radius;
781 update();
782}
783
784qreal QtGradientWidget::radiusRadial() const
785{
786 return d_ptr->m_radiusRadial;
787}
788
789void QtGradientWidget::setCentralConical(const QPointF &point)
790{
791 if (d_ptr->m_centralConical == point)
792 return;
793
794 d_ptr->m_centralConical = point;
795 update();
796}
797
798QPointF QtGradientWidget::centralConical() const
799{
800 return d_ptr->m_centralConical;
801}
802
803void QtGradientWidget::setAngleConical(qreal angle)
804{
805 if (d_ptr->m_angleConical == angle)
806 return;
807
808 d_ptr->m_angleConical = angle;
809 update();
810}
811
812qreal QtGradientWidget::angleConical() const
813{
814 return d_ptr->m_angleConical;
815}
816
817QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.