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 tools applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qtgradientstopscontroller.h"
41 #include "ui_qtgradienteditor.h"
42 #include "qtgradientstopsmodel.h"
43
44 #include <QtCore/QTimer>
45
46 QT_BEGIN_NAMESPACE
47
48 class QtGradientStopsControllerPrivate
49 {
50 QtGradientStopsController *q_ptr;
51 Q_DECLARE_PUBLIC(QtGradientStopsController)
52 public:
53 typedef QMap<qreal, QColor> PositionColorMap;
54 typedef QMap<qreal, QtGradientStop *> PositionStopMap;
55
56 void slotHsvClicked();
57 void slotRgbClicked();
58
59 void slotCurrentStopChanged(QtGradientStop *stop);
60 void slotStopMoved(QtGradientStop *stop, qreal newPos);
61 void slotStopsSwapped(QtGradientStop *stop1, QtGradientStop *stop2);
62 void slotStopChanged(QtGradientStop *stop, const QColor &newColor);
63 void slotStopSelected(QtGradientStop *stop, bool selected);
64 void slotStopAdded(QtGradientStop *stop);
65 void slotStopRemoved(QtGradientStop *stop);
66 void slotUpdatePositionSpinBox();
67
68 void slotChangeColor(const QColor &color);
69 void slotChangeHue(const QColor &color);
70 void slotChangeSaturation(const QColor &color);
71 void slotChangeValue(const QColor &color);
72 void slotChangeAlpha(const QColor &color);
73 void slotChangeHue(int color);
74 void slotChangeSaturation(int color);
75 void slotChangeValue(int color);
76 void slotChangeAlpha(int color);
77 void slotChangePosition(double value);
78
79 void slotChangeZoom(int value);
80 void slotZoomIn();
81 void slotZoomOut();
82 void slotZoomAll();
83 void slotZoomChanged(double zoom);
84
85 void enableCurrent(bool enable);
86 void setColorSpinBoxes(const QColor &color);
87 PositionColorMap stopsData(const PositionStopMap &stops) const;
88 QGradientStops makeGradientStops(const PositionColorMap &data) const;
89 void updateZoom(double zoom);
90
91 QtGradientStopsModel *m_model;
92 QColor::Spec m_spec;
93
94 Ui::QtGradientEditor *m_ui;
95 };
96
enableCurrent(bool enable)97 void QtGradientStopsControllerPrivate::enableCurrent(bool enable)
98 {
99 m_ui->positionLabel->setEnabled(enable);
100 m_ui->colorLabel->setEnabled(enable);
101 m_ui->hLabel->setEnabled(enable);
102 m_ui->sLabel->setEnabled(enable);
103 m_ui->vLabel->setEnabled(enable);
104 m_ui->aLabel->setEnabled(enable);
105 m_ui->hueLabel->setEnabled(enable);
106 m_ui->saturationLabel->setEnabled(enable);
107 m_ui->valueLabel->setEnabled(enable);
108 m_ui->alphaLabel->setEnabled(enable);
109
110 m_ui->positionSpinBox->setEnabled(enable);
111 m_ui->colorButton->setEnabled(enable);
112
113 m_ui->hueColorLine->setEnabled(enable);
114 m_ui->saturationColorLine->setEnabled(enable);
115 m_ui->valueColorLine->setEnabled(enable);
116 m_ui->alphaColorLine->setEnabled(enable);
117
118 m_ui->hueSpinBox->setEnabled(enable);
119 m_ui->saturationSpinBox->setEnabled(enable);
120 m_ui->valueSpinBox->setEnabled(enable);
121 m_ui->alphaSpinBox->setEnabled(enable);
122 }
123
stopsData(const PositionStopMap & stops) const124 QtGradientStopsControllerPrivate::PositionColorMap QtGradientStopsControllerPrivate::stopsData(const PositionStopMap &stops) const
125 {
126 PositionColorMap data;
127 PositionStopMap::ConstIterator itStop = stops.constBegin();
128 while (itStop != stops.constEnd()) {
129 QtGradientStop *stop = itStop.value();
130 data[stop->position()] = stop->color();
131
132 ++itStop;
133 }
134 return data;
135 }
136
makeGradientStops(const PositionColorMap & data) const137 QGradientStops QtGradientStopsControllerPrivate::makeGradientStops(const PositionColorMap &data) const
138 {
139 QGradientStops stops;
140 PositionColorMap::ConstIterator itData = data.constBegin();
141 while (itData != data.constEnd()) {
142 stops << QPair<qreal, QColor>(itData.key(), itData.value());
143
144 ++itData;
145 }
146 return stops;
147 }
148
updateZoom(double zoom)149 void QtGradientStopsControllerPrivate::updateZoom(double zoom)
150 {
151 m_ui->gradientStopsWidget->setZoom(zoom);
152 m_ui->zoomSpinBox->blockSignals(true);
153 m_ui->zoomSpinBox->setValue(qRound(zoom * 100));
154 m_ui->zoomSpinBox->blockSignals(false);
155 bool zoomInEnabled = true;
156 bool zoomOutEnabled = true;
157 bool zoomAllEnabled = true;
158 if (zoom <= 1) {
159 zoomAllEnabled = false;
160 zoomOutEnabled = false;
161 } else if (zoom >= 100) {
162 zoomInEnabled = false;
163 }
164 m_ui->zoomInButton->setEnabled(zoomInEnabled);
165 m_ui->zoomOutButton->setEnabled(zoomOutEnabled);
166 m_ui->zoomAllButton->setEnabled(zoomAllEnabled);
167 }
168
slotHsvClicked()169 void QtGradientStopsControllerPrivate::slotHsvClicked()
170 {
171 QString h = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "H");
172 QString s = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "S");
173 QString v = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "V");
174
175 m_ui->hLabel->setText(h);
176 m_ui->sLabel->setText(s);
177 m_ui->vLabel->setText(v);
178
179 h = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "Hue");
180 s = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "Sat");
181 v = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "Val");
182
183 const QString hue = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "Hue");
184 const QString saturation = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "Saturation");
185 const QString value = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "Value");
186
187 m_ui->hLabel->setToolTip(hue);
188 m_ui->hueLabel->setText(h);
189 m_ui->hueColorLine->setToolTip(hue);
190 m_ui->hueColorLine->setColorComponent(QtColorLine::Hue);
191
192 m_ui->sLabel->setToolTip(saturation);
193 m_ui->saturationLabel->setText(s);
194 m_ui->saturationColorLine->setToolTip(saturation);
195 m_ui->saturationColorLine->setColorComponent(QtColorLine::Saturation);
196
197 m_ui->vLabel->setToolTip(value);
198 m_ui->valueLabel->setText(v);
199 m_ui->valueColorLine->setToolTip(value);
200 m_ui->valueColorLine->setColorComponent(QtColorLine::Value);
201
202 setColorSpinBoxes(m_ui->colorButton->color());
203 }
204
slotRgbClicked()205 void QtGradientStopsControllerPrivate::slotRgbClicked()
206 {
207 QString r = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "R");
208 QString g = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "G");
209 QString b = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "B");
210
211 m_ui->hLabel->setText(r);
212 m_ui->sLabel->setText(g);
213 m_ui->vLabel->setText(b);
214
215 QString red = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "Red");
216 QString green = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "Green");
217 QString blue = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "Blue");
218
219 m_ui->hLabel->setToolTip(red);
220 m_ui->hueLabel->setText(red);
221 m_ui->hueColorLine->setToolTip(red);
222 m_ui->hueColorLine->setColorComponent(QtColorLine::Red);
223
224 m_ui->sLabel->setToolTip(green);
225 m_ui->saturationLabel->setText(green);
226 m_ui->saturationColorLine->setToolTip(green);
227 m_ui->saturationColorLine->setColorComponent(QtColorLine::Green);
228
229 m_ui->vLabel->setToolTip(blue);
230 m_ui->valueLabel->setText(blue);
231 m_ui->valueColorLine->setToolTip(blue);
232 m_ui->valueColorLine->setColorComponent(QtColorLine::Blue);
233
234 setColorSpinBoxes(m_ui->colorButton->color());
235 }
236
setColorSpinBoxes(const QColor & color)237 void QtGradientStopsControllerPrivate::setColorSpinBoxes(const QColor &color)
238 {
239 m_ui->hueSpinBox->blockSignals(true);
240 m_ui->saturationSpinBox->blockSignals(true);
241 m_ui->valueSpinBox->blockSignals(true);
242 m_ui->alphaSpinBox->blockSignals(true);
243 if (m_ui->hsvRadioButton->isChecked()) {
244 if (m_ui->hueSpinBox->maximum() != 359)
245 m_ui->hueSpinBox->setMaximum(359);
246 if (m_ui->hueSpinBox->value() != color.hue())
247 m_ui->hueSpinBox->setValue(color.hue());
248 if (m_ui->saturationSpinBox->value() != color.saturation())
249 m_ui->saturationSpinBox->setValue(color.saturation());
250 if (m_ui->valueSpinBox->value() != color.value())
251 m_ui->valueSpinBox->setValue(color.value());
252 } else {
253 if (m_ui->hueSpinBox->maximum() != 255)
254 m_ui->hueSpinBox->setMaximum(255);
255 if (m_ui->hueSpinBox->value() != color.red())
256 m_ui->hueSpinBox->setValue(color.red());
257 if (m_ui->saturationSpinBox->value() != color.green())
258 m_ui->saturationSpinBox->setValue(color.green());
259 if (m_ui->valueSpinBox->value() != color.blue())
260 m_ui->valueSpinBox->setValue(color.blue());
261 }
262 m_ui->alphaSpinBox->setValue(color.alpha());
263 m_ui->hueSpinBox->blockSignals(false);
264 m_ui->saturationSpinBox->blockSignals(false);
265 m_ui->valueSpinBox->blockSignals(false);
266 m_ui->alphaSpinBox->blockSignals(false);
267 }
268
slotCurrentStopChanged(QtGradientStop * stop)269 void QtGradientStopsControllerPrivate::slotCurrentStopChanged(QtGradientStop *stop)
270 {
271 if (!stop) {
272 enableCurrent(false);
273 return;
274 }
275 enableCurrent(true);
276
277 QTimer::singleShot(0, q_ptr, SLOT(slotUpdatePositionSpinBox()));
278
279 m_ui->colorButton->setColor(stop->color());
280 m_ui->hueColorLine->setColor(stop->color());
281 m_ui->saturationColorLine->setColor(stop->color());
282 m_ui->valueColorLine->setColor(stop->color());
283 m_ui->alphaColorLine->setColor(stop->color());
284 setColorSpinBoxes(stop->color());
285 }
286
slotStopMoved(QtGradientStop * stop,qreal newPos)287 void QtGradientStopsControllerPrivate::slotStopMoved(QtGradientStop *stop, qreal newPos)
288 {
289 QTimer::singleShot(0, q_ptr, SLOT(slotUpdatePositionSpinBox()));
290
291 PositionColorMap stops = stopsData(m_model->stops());
292 stops.remove(stop->position());
293 stops[newPos] = stop->color();
294
295 QGradientStops gradStops = makeGradientStops(stops);
296 emit q_ptr->gradientStopsChanged(gradStops);
297 }
298
slotStopsSwapped(QtGradientStop * stop1,QtGradientStop * stop2)299 void QtGradientStopsControllerPrivate::slotStopsSwapped(QtGradientStop *stop1, QtGradientStop *stop2)
300 {
301 QTimer::singleShot(0, q_ptr, SLOT(slotUpdatePositionSpinBox()));
302
303 PositionColorMap stops = stopsData(m_model->stops());
304 const qreal pos1 = stop1->position();
305 const qreal pos2 = stop2->position();
306 stops[pos1] = stop2->color();
307 stops[pos2] = stop1->color();
308
309 QGradientStops gradStops = makeGradientStops(stops);
310 emit q_ptr->gradientStopsChanged(gradStops);
311 }
312
slotStopAdded(QtGradientStop * stop)313 void QtGradientStopsControllerPrivate::slotStopAdded(QtGradientStop *stop)
314 {
315 PositionColorMap stops = stopsData(m_model->stops());
316 stops[stop->position()] = stop->color();
317
318 QGradientStops gradStops = makeGradientStops(stops);
319 emit q_ptr->gradientStopsChanged(gradStops);
320 }
321
slotStopRemoved(QtGradientStop * stop)322 void QtGradientStopsControllerPrivate::slotStopRemoved(QtGradientStop *stop)
323 {
324 PositionColorMap stops = stopsData(m_model->stops());
325 stops.remove(stop->position());
326
327 QGradientStops gradStops = makeGradientStops(stops);
328 emit q_ptr->gradientStopsChanged(gradStops);
329 }
330
slotStopChanged(QtGradientStop * stop,const QColor & newColor)331 void QtGradientStopsControllerPrivate::slotStopChanged(QtGradientStop *stop, const QColor &newColor)
332 {
333 if (m_model->currentStop() == stop) {
334 m_ui->colorButton->setColor(newColor);
335 m_ui->hueColorLine->setColor(newColor);
336 m_ui->saturationColorLine->setColor(newColor);
337 m_ui->valueColorLine->setColor(newColor);
338 m_ui->alphaColorLine->setColor(newColor);
339 setColorSpinBoxes(newColor);
340 }
341
342 PositionColorMap stops = stopsData(m_model->stops());
343 stops[stop->position()] = newColor;
344
345 QGradientStops gradStops = makeGradientStops(stops);
346 emit q_ptr->gradientStopsChanged(gradStops);
347 }
348
slotStopSelected(QtGradientStop * stop,bool selected)349 void QtGradientStopsControllerPrivate::slotStopSelected(QtGradientStop *stop, bool selected)
350 {
351 Q_UNUSED(stop);
352 Q_UNUSED(selected);
353 QTimer::singleShot(0, q_ptr, SLOT(slotUpdatePositionSpinBox()));
354 }
355
slotUpdatePositionSpinBox()356 void QtGradientStopsControllerPrivate::slotUpdatePositionSpinBox()
357 {
358 QtGradientStop *current = m_model->currentStop();
359 if (!current)
360 return;
361
362 qreal min = 0.0;
363 qreal max = 1.0;
364 const qreal pos = current->position();
365
366 QtGradientStop *first = m_model->firstSelected();
367 QtGradientStop *last = m_model->lastSelected();
368
369 if (first && last) {
370 const qreal minPos = pos - first->position() - 0.0004999;
371 const qreal maxPos = pos + 1.0 - last->position() + 0.0004999;
372
373 if (max > maxPos)
374 max = maxPos;
375 if (min < minPos)
376 min = minPos;
377
378 if (first->position() == 0.0)
379 min = pos;
380 if (last->position() == 1.0)
381 max = pos;
382 }
383
384 const int spinMin = qRound(m_ui->positionSpinBox->minimum() * 1000);
385 const int spinMax = qRound(m_ui->positionSpinBox->maximum() * 1000);
386
387 const int newMin = qRound(min * 1000);
388 const int newMax = qRound(max * 1000);
389
390 m_ui->positionSpinBox->blockSignals(true);
391 if (spinMin != newMin || spinMax != newMax) {
392 m_ui->positionSpinBox->setRange(double(newMin) / 1000, double(newMax) / 1000);
393 }
394 if (m_ui->positionSpinBox->value() != pos)
395 m_ui->positionSpinBox->setValue(pos);
396 m_ui->positionSpinBox->blockSignals(false);
397 }
398
slotChangeColor(const QColor & color)399 void QtGradientStopsControllerPrivate::slotChangeColor(const QColor &color)
400 {
401 QtGradientStop *stop = m_model->currentStop();
402 if (!stop)
403 return;
404 m_model->changeStop(stop, color);
405 const auto stops = m_model->selectedStops();
406 for (QtGradientStop *s : stops) {
407 if (s != stop)
408 m_model->changeStop(s, color);
409 }
410 }
411
slotChangeHue(const QColor & color)412 void QtGradientStopsControllerPrivate::slotChangeHue(const QColor &color)
413 {
414 QtGradientStop *stop = m_model->currentStop();
415 if (!stop)
416 return;
417 m_model->changeStop(stop, color);
418 const auto stops = m_model->selectedStops();
419 for (QtGradientStop *s : stops) {
420 if (s != stop) {
421 QColor c = s->color();
422 if (m_ui->hsvRadioButton->isChecked())
423 c.setHsvF(color.hueF(), c.saturationF(), c.valueF(), c.alphaF());
424 else
425 c.setRgbF(color.redF(), c.greenF(), c.blueF(), c.alphaF());
426 m_model->changeStop(s, c);
427 }
428 }
429 }
430
slotChangeHue(int color)431 void QtGradientStopsControllerPrivate::slotChangeHue(int color)
432 {
433 QColor c = m_ui->hueColorLine->color();
434 if (m_ui->hsvRadioButton->isChecked())
435 c.setHsvF(qreal(color) / 360.0, c.saturationF(), c.valueF(), c.alphaF());
436 else
437 c.setRed(color);
438 slotChangeHue(c);
439 }
440
slotChangeSaturation(const QColor & color)441 void QtGradientStopsControllerPrivate::slotChangeSaturation(const QColor &color)
442 {
443 QtGradientStop *stop = m_model->currentStop();
444 if (!stop)
445 return;
446 m_model->changeStop(stop, color);
447 const auto stops = m_model->selectedStops();
448 for (QtGradientStop *s : stops) {
449 if (s != stop) {
450 QColor c = s->color();
451 if (m_ui->hsvRadioButton->isChecked()) {
452 c.setHsvF(c.hueF(), color.saturationF(), c.valueF(), c.alphaF());
453 int hue = c.hue();
454 if (hue == 360 || hue == -1)
455 c.setHsvF(0.0, c.saturationF(), c.valueF(), c.alphaF());
456 } else {
457 c.setRgbF(c.redF(), color.greenF(), c.blueF(), c.alphaF());
458 }
459 m_model->changeStop(s, c);
460 }
461 }
462 }
463
slotChangeSaturation(int color)464 void QtGradientStopsControllerPrivate::slotChangeSaturation(int color)
465 {
466 QColor c = m_ui->saturationColorLine->color();
467 if (m_ui->hsvRadioButton->isChecked())
468 c.setHsvF(c.hueF(), qreal(color) / 255, c.valueF(), c.alphaF());
469 else
470 c.setGreen(color);
471 slotChangeSaturation(c);
472 }
473
slotChangeValue(const QColor & color)474 void QtGradientStopsControllerPrivate::slotChangeValue(const QColor &color)
475 {
476 QtGradientStop *stop = m_model->currentStop();
477 if (!stop)
478 return;
479 m_model->changeStop(stop, color);
480 const auto stops = m_model->selectedStops();
481 for (QtGradientStop *s : stops) {
482 if (s != stop) {
483 QColor c = s->color();
484 if (m_ui->hsvRadioButton->isChecked()) {
485 c.setHsvF(c.hueF(), c.saturationF(), color.valueF(), c.alphaF());
486 int hue = c.hue();
487 if (hue == 360 || hue == -1)
488 c.setHsvF(0.0, c.saturationF(), c.valueF(), c.alphaF());
489 } else {
490 c.setRgbF(c.redF(), c.greenF(), color.blueF(), c.alphaF());
491 }
492 m_model->changeStop(s, c);
493 }
494 }
495 }
496
slotChangeValue(int color)497 void QtGradientStopsControllerPrivate::slotChangeValue(int color)
498 {
499 QColor c = m_ui->valueColorLine->color();
500 if (m_ui->hsvRadioButton->isChecked())
501 c.setHsvF(c.hueF(), c.saturationF(), qreal(color) / 255, c.alphaF());
502 else
503 c.setBlue(color);
504 slotChangeValue(c);
505 }
506
slotChangeAlpha(const QColor & color)507 void QtGradientStopsControllerPrivate::slotChangeAlpha(const QColor &color)
508 {
509 QtGradientStop *stop = m_model->currentStop();
510 if (!stop)
511 return;
512 m_model->changeStop(stop, color);
513 const auto stops = m_model->selectedStops();
514 for (QtGradientStop *s : stops) {
515 if (s != stop) {
516 QColor c = s->color();
517 if (m_ui->hsvRadioButton->isChecked()) {
518 c.setHsvF(c.hueF(), c.saturationF(), c.valueF(), color.alphaF());
519 int hue = c.hue();
520 if (hue == 360 || hue == -1)
521 c.setHsvF(0.0, c.saturationF(), c.valueF(), c.alphaF());
522 } else {
523 c.setRgbF(c.redF(), c.greenF(), c.blueF(), color.alphaF());
524 }
525 m_model->changeStop(s, c);
526 }
527 }
528 }
529
slotChangeAlpha(int color)530 void QtGradientStopsControllerPrivate::slotChangeAlpha(int color)
531 {
532 QColor c = m_ui->alphaColorLine->color();
533 if (m_ui->hsvRadioButton->isChecked())
534 c.setHsvF(c.hueF(), c.saturationF(), c.valueF(), qreal(color) / 255);
535 else
536 c.setAlpha(color);
537 slotChangeAlpha(c);
538 }
539
slotChangePosition(double value)540 void QtGradientStopsControllerPrivate::slotChangePosition(double value)
541 {
542 QtGradientStop *stop = m_model->currentStop();
543 if (!stop)
544 return;
545
546 m_model->moveStops(value);
547 }
548
slotChangeZoom(int value)549 void QtGradientStopsControllerPrivate::slotChangeZoom(int value)
550 {
551 updateZoom(value / 100.0);
552 }
553
slotZoomIn()554 void QtGradientStopsControllerPrivate::slotZoomIn()
555 {
556 double newZoom = m_ui->gradientStopsWidget->zoom() * 2;
557 if (newZoom > 100)
558 newZoom = 100;
559 updateZoom(newZoom);
560 }
561
slotZoomOut()562 void QtGradientStopsControllerPrivate::slotZoomOut()
563 {
564 double newZoom = m_ui->gradientStopsWidget->zoom() / 2;
565 if (newZoom < 1)
566 newZoom = 1;
567 updateZoom(newZoom);
568 }
569
slotZoomAll()570 void QtGradientStopsControllerPrivate::slotZoomAll()
571 {
572 updateZoom(1);
573 }
574
slotZoomChanged(double zoom)575 void QtGradientStopsControllerPrivate::slotZoomChanged(double zoom)
576 {
577 updateZoom(zoom);
578 }
579
QtGradientStopsController(QObject * parent)580 QtGradientStopsController::QtGradientStopsController(QObject *parent)
581 : QObject(parent), d_ptr(new QtGradientStopsControllerPrivate())
582 {
583 d_ptr->q_ptr = this;
584
585 d_ptr->m_spec = QColor::Hsv;
586 }
587
setUi(Ui::QtGradientEditor * ui)588 void QtGradientStopsController::setUi(Ui::QtGradientEditor *ui)
589 {
590 d_ptr->m_ui = ui;
591
592 d_ptr->m_ui->hueColorLine->setColorComponent(QtColorLine::Hue);
593 d_ptr->m_ui->saturationColorLine->setColorComponent(QtColorLine::Saturation);
594 d_ptr->m_ui->valueColorLine->setColorComponent(QtColorLine::Value);
595 d_ptr->m_ui->alphaColorLine->setColorComponent(QtColorLine::Alpha);
596
597 d_ptr->m_model = new QtGradientStopsModel(this);
598 d_ptr->m_ui->gradientStopsWidget->setGradientStopsModel(d_ptr->m_model);
599 connect(d_ptr->m_model, SIGNAL(currentStopChanged(QtGradientStop*)),
600 this, SLOT(slotCurrentStopChanged(QtGradientStop*)));
601 connect(d_ptr->m_model, SIGNAL(stopMoved(QtGradientStop*,qreal)),
602 this, SLOT(slotStopMoved(QtGradientStop*,qreal)));
603 connect(d_ptr->m_model, SIGNAL(stopsSwapped(QtGradientStop*,QtGradientStop*)),
604 this, SLOT(slotStopsSwapped(QtGradientStop*,QtGradientStop*)));
605 connect(d_ptr->m_model, SIGNAL(stopChanged(QtGradientStop*,QColor)),
606 this, SLOT(slotStopChanged(QtGradientStop*,QColor)));
607 connect(d_ptr->m_model, SIGNAL(stopSelected(QtGradientStop*,bool)),
608 this, SLOT(slotStopSelected(QtGradientStop*,bool)));
609 connect(d_ptr->m_model, SIGNAL(stopAdded(QtGradientStop*)),
610 this, SLOT(slotStopAdded(QtGradientStop*)));
611 connect(d_ptr->m_model, SIGNAL(stopRemoved(QtGradientStop*)),
612 this, SLOT(slotStopRemoved(QtGradientStop*)));
613
614 connect(d_ptr->m_ui->hueColorLine, SIGNAL(colorChanged(QColor)),
615 this, SLOT(slotChangeHue(QColor)));
616 connect(d_ptr->m_ui->saturationColorLine, SIGNAL(colorChanged(QColor)),
617 this, SLOT(slotChangeSaturation(QColor)));
618 connect(d_ptr->m_ui->valueColorLine, SIGNAL(colorChanged(QColor)),
619 this, SLOT(slotChangeValue(QColor)));
620 connect(d_ptr->m_ui->alphaColorLine, SIGNAL(colorChanged(QColor)),
621 this, SLOT(slotChangeAlpha(QColor)));
622 connect(d_ptr->m_ui->colorButton, SIGNAL(colorChanged(QColor)),
623 this, SLOT(slotChangeColor(QColor)));
624
625 connect(d_ptr->m_ui->hueSpinBox, SIGNAL(valueChanged(int)),
626 this, SLOT(slotChangeHue(int)));
627 connect(d_ptr->m_ui->saturationSpinBox, SIGNAL(valueChanged(int)),
628 this, SLOT(slotChangeSaturation(int)));
629 connect(d_ptr->m_ui->valueSpinBox, SIGNAL(valueChanged(int)),
630 this, SLOT(slotChangeValue(int)));
631 connect(d_ptr->m_ui->alphaSpinBox, SIGNAL(valueChanged(int)),
632 this, SLOT(slotChangeAlpha(int)));
633
634 connect(d_ptr->m_ui->positionSpinBox, SIGNAL(valueChanged(double)),
635 this, SLOT(slotChangePosition(double)));
636
637 connect(d_ptr->m_ui->zoomSpinBox, SIGNAL(valueChanged(int)),
638 this, SLOT(slotChangeZoom(int)));
639 connect(d_ptr->m_ui->zoomInButton, SIGNAL(clicked()),
640 this, SLOT(slotZoomIn()));
641 connect(d_ptr->m_ui->zoomOutButton, SIGNAL(clicked()),
642 this, SLOT(slotZoomOut()));
643 connect(d_ptr->m_ui->zoomAllButton, SIGNAL(clicked()),
644 this, SLOT(slotZoomAll()));
645 connect(d_ptr->m_ui->gradientStopsWidget, SIGNAL(zoomChanged(double)),
646 this, SLOT(slotZoomChanged(double)));
647
648 connect(d_ptr->m_ui->hsvRadioButton, SIGNAL(clicked()),
649 this, SLOT(slotHsvClicked()));
650 connect(d_ptr->m_ui->rgbRadioButton, SIGNAL(clicked()),
651 this, SLOT(slotRgbClicked()));
652
653 d_ptr->enableCurrent(false);
654 d_ptr->m_ui->zoomInButton->setIcon(QIcon(QLatin1String(":/qt-project.org/qtgradienteditor/images/zoomin.png")));
655 d_ptr->m_ui->zoomOutButton->setIcon(QIcon(QLatin1String(":/qt-project.org/qtgradienteditor/images/zoomout.png")));
656 d_ptr->updateZoom(1);
657 }
658
~QtGradientStopsController()659 QtGradientStopsController::~QtGradientStopsController()
660 {
661 }
662
setGradientStops(const QGradientStops & stops)663 void QtGradientStopsController::setGradientStops(const QGradientStops &stops)
664 {
665 d_ptr->m_model->clear();
666 QtGradientStop *first = 0;
667 for (const QPair<qreal, QColor> &pair : stops) {
668 QtGradientStop *stop = d_ptr->m_model->addStop(pair.first, pair.second);
669 if (!first)
670 first = stop;
671 }
672 if (first)
673 d_ptr->m_model->setCurrentStop(first);
674 }
675
gradientStops() const676 QGradientStops QtGradientStopsController::gradientStops() const
677 {
678 QGradientStops stops;
679 const auto stopsList = d_ptr->m_model->stops().values();
680 for (const QtGradientStop *stop : stopsList)
681 stops << QPair<qreal, QColor>(stop->position(), stop->color());
682 return stops;
683 }
684
spec() const685 QColor::Spec QtGradientStopsController::spec() const
686 {
687 return d_ptr->m_spec;
688 }
689
setSpec(QColor::Spec spec)690 void QtGradientStopsController::setSpec(QColor::Spec spec)
691 {
692 if (d_ptr->m_spec == spec)
693 return;
694
695 d_ptr->m_spec = spec;
696 if (d_ptr->m_spec == QColor::Rgb) {
697 d_ptr->m_ui->rgbRadioButton->setChecked(true);
698 d_ptr->slotRgbClicked();
699 } else {
700 d_ptr->m_ui->hsvRadioButton->setChecked(true);
701 d_ptr->slotHsvClicked();
702 }
703 }
704
705 QT_END_NAMESPACE
706
707 #include "moc_qtgradientstopscontroller.cpp"
708