Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtgradientstopscontroller.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include "ui_qtgradienteditor.h"
7
8#include <QtCore/QTimer>
9
11
12using namespace Qt::StringLiterals;
13
15{
16 Q_OBJECT
17 QtGradientStopsController *q_ptr = nullptr;
19public:
22
23 void setUi(Ui::QtGradientEditor *ui);
24
27
29 void slotStopMoved(QtGradientStop *stop, qreal newPos);
31 void slotStopChanged(QtGradientStop *stop, QColor newColor);
32 void slotStopSelected(QtGradientStop *stop, bool selected);
36
37 void slotChangeColor(QColor color);
38 void slotChangeHueColor(QColor color);
39 void slotChangeSaturationColor(QColor color);
40 void slotChangeValueColor(QColor color);
41 void slotChangeAlphaColor(QColor color);
42 void slotChangeHue(int color);
43 void slotChangeSaturation(int color);
44 void slotChangeValue(int color);
45 void slotChangeAlpha(int color);
46 void slotChangePosition(double value);
47
48 void slotChangeZoom(int value);
49 void slotZoomIn();
52 void slotZoomChanged(double zoom);
53
54 void enableCurrent(bool enable);
55 void setColorSpinBoxes(QColor color);
56 PositionColorMap stopsData(const PositionStopMap &stops) const;
57 QGradientStops makeGradientStops(const PositionColorMap &data) const;
58 void updateZoom(double zoom);
59
62
63 Ui::QtGradientEditor *m_ui = nullptr;
64};
65
66void QtGradientStopsControllerPrivate::setUi(Ui::QtGradientEditor *ui)
67{
68 m_ui = ui;
69
70 m_ui->hueColorLine->setColorComponent(QtColorLine::Hue);
71 m_ui->saturationColorLine->setColorComponent(QtColorLine::Saturation);
72 m_ui->valueColorLine->setColorComponent(QtColorLine::Value);
73 m_ui->alphaColorLine->setColorComponent(QtColorLine::Alpha);
74
75 m_model = new QtGradientStopsModel(this);
76 m_ui->gradientStopsWidget->setGradientStopsModel(m_model);
79 connect(m_model, &QtGradientStopsModel::stopMoved,
80 this, &QtGradientStopsControllerPrivate::slotStopMoved);
87 connect(m_model, &QtGradientStopsModel::stopAdded,
88 this, &QtGradientStopsControllerPrivate::slotStopAdded);
91
92 connect(m_ui->hueColorLine, &QtColorLine::colorChanged,
93 this, &QtGradientStopsControllerPrivate::slotChangeHueColor);
94 connect(m_ui->saturationColorLine, &QtColorLine::colorChanged,
95 this, &QtGradientStopsControllerPrivate::slotChangeSaturationColor);
96 connect(m_ui->valueColorLine, &QtColorLine::colorChanged,
97 this, &QtGradientStopsControllerPrivate::slotChangeValueColor);
98 connect(m_ui->alphaColorLine, &QtColorLine::colorChanged,
99 this, &QtGradientStopsControllerPrivate::slotChangeAlphaColor);
100 connect(m_ui->colorButton, &QtColorButton::colorChanged,
101 this, &QtGradientStopsControllerPrivate::slotChangeColor);
102
103 connect(m_ui->hueSpinBox, &QSpinBox::valueChanged,
104 this, &QtGradientStopsControllerPrivate::slotChangeHue);
105 connect(m_ui->saturationSpinBox, &QSpinBox::valueChanged,
106 this, &QtGradientStopsControllerPrivate::slotChangeSaturation);
107 connect(m_ui->valueSpinBox, &QSpinBox::valueChanged,
108 this, &QtGradientStopsControllerPrivate::slotChangeValue);
109 connect(m_ui->alphaSpinBox, &QSpinBox::valueChanged,
110 this, &QtGradientStopsControllerPrivate::slotChangeAlpha);
111
112 connect(m_ui->positionSpinBox, &QDoubleSpinBox::valueChanged,
113 this, &QtGradientStopsControllerPrivate::slotChangePosition);
114
115 connect(m_ui->zoomSpinBox, &QSpinBox::valueChanged,
116 this, &QtGradientStopsControllerPrivate::slotChangeZoom);
117 connect(m_ui->zoomInButton, &QToolButton::clicked,
118 this, &QtGradientStopsControllerPrivate::slotZoomIn);
119 connect(m_ui->zoomOutButton, &QToolButton::clicked,
120 this, &QtGradientStopsControllerPrivate::slotZoomOut);
121 connect(m_ui->zoomAllButton, &QToolButton::clicked,
122 this, &QtGradientStopsControllerPrivate::slotZoomAll);
123 connect(m_ui->gradientStopsWidget, &QtGradientStopsWidget::zoomChanged,
124 this, &QtGradientStopsControllerPrivate::slotZoomChanged);
125
126 connect(m_ui->hsvRadioButton, &QRadioButton::clicked,
127 this, &QtGradientStopsControllerPrivate::slotHsvClicked);
128 connect(m_ui->rgbRadioButton, &QRadioButton::clicked,
129 this, &QtGradientStopsControllerPrivate::slotRgbClicked);
130
131 enableCurrent(false);
132 m_ui->zoomInButton->setIcon(QIcon(":/qt-project.org/qtgradienteditor/images/zoomin.png"_L1));
133 m_ui->zoomOutButton->setIcon(QIcon(":/qt-project.org/qtgradienteditor/images/zoomout.png"_L1));
135}
136
138{
139 m_ui->positionLabel->setEnabled(enable);
140 m_ui->colorLabel->setEnabled(enable);
141 m_ui->hLabel->setEnabled(enable);
142 m_ui->sLabel->setEnabled(enable);
143 m_ui->vLabel->setEnabled(enable);
144 m_ui->aLabel->setEnabled(enable);
145 m_ui->hueLabel->setEnabled(enable);
146 m_ui->saturationLabel->setEnabled(enable);
147 m_ui->valueLabel->setEnabled(enable);
148 m_ui->alphaLabel->setEnabled(enable);
149
150 m_ui->positionSpinBox->setEnabled(enable);
151 m_ui->colorButton->setEnabled(enable);
152
153 m_ui->hueColorLine->setEnabled(enable);
154 m_ui->saturationColorLine->setEnabled(enable);
155 m_ui->valueColorLine->setEnabled(enable);
156 m_ui->alphaColorLine->setEnabled(enable);
157
158 m_ui->hueSpinBox->setEnabled(enable);
159 m_ui->saturationSpinBox->setEnabled(enable);
160 m_ui->valueSpinBox->setEnabled(enable);
161 m_ui->alphaSpinBox->setEnabled(enable);
162}
163
165{
166 PositionColorMap data;
167 for (QtGradientStop *stop : stops)
168 data[stop->position()] = stop->color();
169 return data;
170}
171
173{
174 QGradientStops stops;
175 for (auto itData = data.cbegin(), cend = data.cend(); itData != cend; ++itData)
176 stops << std::pair<qreal, QColor>(itData.key(), itData.value());
177 return stops;
178}
179
181{
182 m_ui->gradientStopsWidget->setZoom(zoom);
183 m_ui->zoomSpinBox->blockSignals(true);
184 m_ui->zoomSpinBox->setValue(qRound(zoom * 100));
185 m_ui->zoomSpinBox->blockSignals(false);
186 bool zoomInEnabled = true;
187 bool zoomOutEnabled = true;
188 bool zoomAllEnabled = true;
189 if (zoom <= 1) {
190 zoomAllEnabled = false;
191 zoomOutEnabled = false;
192 } else if (zoom >= 100) {
193 zoomInEnabled = false;
194 }
195 m_ui->zoomInButton->setEnabled(zoomInEnabled);
196 m_ui->zoomOutButton->setEnabled(zoomOutEnabled);
197 m_ui->zoomAllButton->setEnabled(zoomAllEnabled);
198}
199
201{
202 QString h = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "H");
203 QString s = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "S");
204 QString v = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "V");
205
206 m_ui->hLabel->setText(h);
207 m_ui->sLabel->setText(s);
208 m_ui->vLabel->setText(v);
209
210 h = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "Hue");
211 s = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "Sat");
212 v = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "Val");
213
214 const QString hue = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "Hue");
215 const QString saturation = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "Saturation");
216 const QString value = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "Value");
217
218 m_ui->hLabel->setToolTip(hue);
219 m_ui->hueLabel->setText(h);
220 m_ui->hueColorLine->setToolTip(hue);
221 m_ui->hueColorLine->setColorComponent(QtColorLine::Hue);
222
223 m_ui->sLabel->setToolTip(saturation);
224 m_ui->saturationLabel->setText(s);
225 m_ui->saturationColorLine->setToolTip(saturation);
226 m_ui->saturationColorLine->setColorComponent(QtColorLine::Saturation);
227
228 m_ui->vLabel->setToolTip(value);
229 m_ui->valueLabel->setText(v);
230 m_ui->valueColorLine->setToolTip(value);
231 m_ui->valueColorLine->setColorComponent(QtColorLine::Value);
232
233 setColorSpinBoxes(m_ui->colorButton->color());
234}
235
237{
238 QString r = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "R");
239 QString g = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "G");
240 QString b = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "B");
241
242 m_ui->hLabel->setText(r);
243 m_ui->sLabel->setText(g);
244 m_ui->vLabel->setText(b);
245
246 QString red = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "Red");
247 QString green = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "Green");
248 QString blue = QCoreApplication::translate("qdesigner_internal::QtGradientStopsController", "Blue");
249
250 m_ui->hLabel->setToolTip(red);
251 m_ui->hueLabel->setText(red);
252 m_ui->hueColorLine->setToolTip(red);
253 m_ui->hueColorLine->setColorComponent(QtColorLine::Red);
254
255 m_ui->sLabel->setToolTip(green);
256 m_ui->saturationLabel->setText(green);
257 m_ui->saturationColorLine->setToolTip(green);
258 m_ui->saturationColorLine->setColorComponent(QtColorLine::Green);
259
260 m_ui->vLabel->setToolTip(blue);
261 m_ui->valueLabel->setText(blue);
262 m_ui->valueColorLine->setToolTip(blue);
263 m_ui->valueColorLine->setColorComponent(QtColorLine::Blue);
264
265 setColorSpinBoxes(m_ui->colorButton->color());
266}
267
269{
270 m_ui->hueSpinBox->blockSignals(true);
271 m_ui->saturationSpinBox->blockSignals(true);
272 m_ui->valueSpinBox->blockSignals(true);
273 m_ui->alphaSpinBox->blockSignals(true);
274 if (m_ui->hsvRadioButton->isChecked()) {
275 if (m_ui->hueSpinBox->maximum() != 359)
276 m_ui->hueSpinBox->setMaximum(359);
277 if (m_ui->hueSpinBox->value() != color.hue())
278 m_ui->hueSpinBox->setValue(color.hue());
279 if (m_ui->saturationSpinBox->value() != color.saturation())
280 m_ui->saturationSpinBox->setValue(color.saturation());
281 if (m_ui->valueSpinBox->value() != color.value())
282 m_ui->valueSpinBox->setValue(color.value());
283 } else {
284 if (m_ui->hueSpinBox->maximum() != 255)
285 m_ui->hueSpinBox->setMaximum(255);
286 if (m_ui->hueSpinBox->value() != color.red())
287 m_ui->hueSpinBox->setValue(color.red());
288 if (m_ui->saturationSpinBox->value() != color.green())
289 m_ui->saturationSpinBox->setValue(color.green());
290 if (m_ui->valueSpinBox->value() != color.blue())
291 m_ui->valueSpinBox->setValue(color.blue());
292 }
293 m_ui->alphaSpinBox->setValue(color.alpha());
294 m_ui->hueSpinBox->blockSignals(false);
295 m_ui->saturationSpinBox->blockSignals(false);
296 m_ui->valueSpinBox->blockSignals(false);
297 m_ui->alphaSpinBox->blockSignals(false);
298}
299
301{
302 if (!stop) {
303 enableCurrent(false);
304 return;
305 }
306 enableCurrent(true);
307
308 QTimer::singleShot(0, this, &QtGradientStopsControllerPrivate::slotUpdatePositionSpinBox);
309
310 m_ui->colorButton->setColor(stop->color());
311 m_ui->hueColorLine->setColor(stop->color());
312 m_ui->saturationColorLine->setColor(stop->color());
313 m_ui->valueColorLine->setColor(stop->color());
314 m_ui->alphaColorLine->setColor(stop->color());
316}
317
319{
320 QTimer::singleShot(0, this, &QtGradientStopsControllerPrivate::slotUpdatePositionSpinBox);
321
322 PositionColorMap stops = stopsData(m_model->stops());
323 stops.remove(stop->position());
324 stops[newPos] = stop->color();
325
326 QGradientStops gradStops = makeGradientStops(stops);
327 emit q_ptr->gradientStopsChanged(gradStops);
328}
329
331{
332 QTimer::singleShot(0, this, &QtGradientStopsControllerPrivate::slotUpdatePositionSpinBox);
333
334 PositionColorMap stops = stopsData(m_model->stops());
335 const qreal pos1 = stop1->position();
336 const qreal pos2 = stop2->position();
337 stops[pos1] = stop2->color();
338 stops[pos2] = stop1->color();
339
340 QGradientStops gradStops = makeGradientStops(stops);
341 emit q_ptr->gradientStopsChanged(gradStops);
342}
343
345{
346 PositionColorMap stops = stopsData(m_model->stops());
347 stops[stop->position()] = stop->color();
348
349 QGradientStops gradStops = makeGradientStops(stops);
350 emit q_ptr->gradientStopsChanged(gradStops);
351}
352
354{
355 PositionColorMap stops = stopsData(m_model->stops());
356 stops.remove(stop->position());
357
358 QGradientStops gradStops = makeGradientStops(stops);
359 emit q_ptr->gradientStopsChanged(gradStops);
360}
361
363 QColor newColor)
364{
365 if (m_model->currentStop() == stop) {
366 m_ui->colorButton->setColor(newColor);
367 m_ui->hueColorLine->setColor(newColor);
368 m_ui->saturationColorLine->setColor(newColor);
369 m_ui->valueColorLine->setColor(newColor);
370 m_ui->alphaColorLine->setColor(newColor);
371 setColorSpinBoxes(newColor);
372 }
373
374 PositionColorMap stops = stopsData(m_model->stops());
375 stops[stop->position()] = newColor;
376
377 QGradientStops gradStops = makeGradientStops(stops);
378 emit q_ptr->gradientStopsChanged(gradStops);
379}
380
382{
383 Q_UNUSED(stop);
384 Q_UNUSED(selected);
385 QTimer::singleShot(0, this, &QtGradientStopsControllerPrivate::slotUpdatePositionSpinBox);
386}
387
389{
391 if (!current)
392 return;
393
394 qreal min = 0.0;
395 qreal max = 1.0;
396 const qreal pos = current->position();
397
400
401 if (first && last) {
402 const qreal minPos = pos - first->position() - 0.0004999;
403 const qreal maxPos = pos + 1.0 - last->position() + 0.0004999;
404
405 if (max > maxPos)
406 max = maxPos;
407 if (min < minPos)
408 min = minPos;
409
410 if (first->position() == 0.0)
411 min = pos;
412 if (last->position() == 1.0)
413 max = pos;
414 }
415
416 const int spinMin = qRound(m_ui->positionSpinBox->minimum() * 1000);
417 const int spinMax = qRound(m_ui->positionSpinBox->maximum() * 1000);
418
419 const int newMin = qRound(min * 1000);
420 const int newMax = qRound(max * 1000);
421
422 m_ui->positionSpinBox->blockSignals(true);
423 if (spinMin != newMin || spinMax != newMax) {
424 m_ui->positionSpinBox->setRange(double(newMin) / 1000, double(newMax) / 1000);
425 }
426 if (m_ui->positionSpinBox->value() != pos)
427 m_ui->positionSpinBox->setValue(pos);
428 m_ui->positionSpinBox->blockSignals(false);
429}
430
432{
434 if (!stop)
435 return;
436 m_model->changeStop(stop, color);
437 const auto stops = m_model->selectedStops();
438 for (QtGradientStop *s : stops) {
439 if (s != stop)
440 m_model->changeStop(s, color);
441 }
442}
443
445{
447 if (!stop)
448 return;
449 m_model->changeStop(stop, color);
450 const auto stops = m_model->selectedStops();
451 for (QtGradientStop *s : stops) {
452 if (s != stop) {
453 QColor c = s->color();
454 if (m_ui->hsvRadioButton->isChecked())
455 c.setHsvF(color.hueF(), c.saturationF(), c.valueF(), c.alphaF());
456 else
457 c.setRgbF(color.redF(), c.greenF(), c.blueF(), c.alphaF());
458 m_model->changeStop(s, c);
459 }
460 }
461}
462
464{
465 QColor c = m_ui->hueColorLine->color();
466 if (m_ui->hsvRadioButton->isChecked())
467 c.setHsvF(qreal(color) / 360.0, c.saturationF(), c.valueF(), c.alphaF());
468 else
469 c.setRed(color);
471}
472
474{
476 if (!stop)
477 return;
478 m_model->changeStop(stop, color);
479 const auto stops = m_model->selectedStops();
480 for (QtGradientStop *s : stops) {
481 if (s != stop) {
482 QColor c = s->color();
483 if (m_ui->hsvRadioButton->isChecked()) {
484 c.setHsvF(c.hueF(), color.saturationF(), c.valueF(), c.alphaF());
485 int hue = c.hue();
486 if (hue == 360 || hue == -1)
487 c.setHsvF(0.0, c.saturationF(), c.valueF(), c.alphaF());
488 } else {
489 c.setRgbF(c.redF(), color.greenF(), c.blueF(), c.alphaF());
490 }
491 m_model->changeStop(s, c);
492 }
493 }
494}
495
497{
498 QColor c = m_ui->saturationColorLine->color();
499 if (m_ui->hsvRadioButton->isChecked())
500 c.setHsvF(c.hueF(), qreal(color) / 255, c.valueF(), c.alphaF());
501 else
502 c.setGreen(color);
504}
505
507{
509 if (!stop)
510 return;
511 m_model->changeStop(stop, color);
512 const auto stops = m_model->selectedStops();
513 for (QtGradientStop *s : stops) {
514 if (s != stop) {
515 QColor c = s->color();
516 if (m_ui->hsvRadioButton->isChecked()) {
517 c.setHsvF(c.hueF(), c.saturationF(), color.valueF(), c.alphaF());
518 int hue = c.hue();
519 if (hue == 360 || hue == -1)
520 c.setHsvF(0.0, c.saturationF(), c.valueF(), c.alphaF());
521 } else {
522 c.setRgbF(c.redF(), c.greenF(), color.blueF(), c.alphaF());
523 }
524 m_model->changeStop(s, c);
525 }
526 }
527}
528
530{
531 QColor c = m_ui->valueColorLine->color();
532 if (m_ui->hsvRadioButton->isChecked())
533 c.setHsvF(c.hueF(), c.saturationF(), qreal(color) / 255, c.alphaF());
534 else
535 c.setBlue(color);
537}
538
540{
542 if (!stop)
543 return;
544 m_model->changeStop(stop, color);
545 const auto stops = m_model->selectedStops();
546 for (QtGradientStop *s : stops) {
547 if (s != stop) {
548 QColor c = s->color();
549 if (m_ui->hsvRadioButton->isChecked()) {
550 c.setHsvF(c.hueF(), c.saturationF(), c.valueF(), color.alphaF());
551 int hue = c.hue();
552 if (hue == 360 || hue == -1)
553 c.setHsvF(0.0, c.saturationF(), c.valueF(), c.alphaF());
554 } else {
555 c.setRgbF(c.redF(), c.greenF(), c.blueF(), color.alphaF());
556 }
557 m_model->changeStop(s, c);
558 }
559 }
560}
561
563{
564 QColor c = m_ui->alphaColorLine->color();
565 if (m_ui->hsvRadioButton->isChecked())
566 c.setHsvF(c.hueF(), c.saturationF(), c.valueF(), qreal(color) / 255);
567 else
568 c.setAlpha(color);
570}
571
573{
575 if (!stop)
576 return;
577
579}
580
582{
583 updateZoom(value / 100.0);
584}
585
587{
588 double newZoom = m_ui->gradientStopsWidget->zoom() * 2;
589 if (newZoom > 100)
590 newZoom = 100;
591 updateZoom(newZoom);
592}
593
595{
596 double newZoom = m_ui->gradientStopsWidget->zoom() / 2;
597 if (newZoom < 1)
598 newZoom = 1;
599 updateZoom(newZoom);
600}
601
606
611
612QtGradientStopsController::QtGradientStopsController(QObject *parent)
613 : QObject(parent), d_ptr(new QtGradientStopsControllerPrivate())
614{
615 d_ptr->q_ptr = this;
616}
617
618void QtGradientStopsController::setUi(Ui::QtGradientEditor *ui)
619{
620 d_ptr->setUi(ui);
621}
622
626
627void QtGradientStopsController::setGradientStops(const QGradientStops &stops)
628{
629 d_ptr->m_model->clear();
630 QtGradientStop *first = nullptr;
631 for (const std::pair<qreal, QColor> &pair : stops) {
632 QtGradientStop *stop = d_ptr->m_model->addStop(pair.first, pair.second);
633 if (!first)
634 first = stop;
635 }
636 if (first)
637 d_ptr->m_model->setCurrentStop(first);
638}
639
641{
642 QGradientStops stops;
643 const auto stopsList = d_ptr->m_model->stops().values();
644 for (const QtGradientStop *stop : stopsList)
645 stops.append({stop->position(), stop->color()});
646 return stops;
647}
648
650{
651 return d_ptr->m_spec;
652}
653
654void QtGradientStopsController::setSpec(QColor::Spec spec)
655{
656 if (d_ptr->m_spec == spec)
657 return;
658
659 d_ptr->m_spec = spec;
660 if (d_ptr->m_spec == QColor::Rgb) {
661 d_ptr->m_ui->rgbRadioButton->setChecked(true);
662 d_ptr->slotRgbClicked();
663 } else {
664 d_ptr->m_ui->hsvRadioButton->setChecked(true);
665 d_ptr->slotHsvClicked();
666 }
667}
668
669QT_END_NAMESPACE
670
671#include "qtgradientstopscontroller.moc"
QGradientStops makeGradientStops(const PositionColorMap &data) const
PositionColorMap stopsData(const PositionStopMap &stops) const
void slotStopChanged(QtGradientStop *stop, QColor newColor)
void slotCurrentStopChanged(QtGradientStop *stop)
void slotStopsSwapped(QtGradientStop *stop1, QtGradientStop *stop2)
void slotStopSelected(QtGradientStop *stop, bool selected)
void slotStopMoved(QtGradientStop *stop, qreal newPos)
void setUi(Ui::QtGradientEditor *editor)
void setGradientStops(const QGradientStops &stops)
void stopSelected(QtGradientStop *stop, bool selected)
void changeStop(QtGradientStop *stop, QColor newColor)
QtGradientStop * currentStop() const
void stopChanged(QtGradientStop *stop, const QColor &newColor)
QtGradientStop * firstSelected() const
void moveStops(double newPosition)
void stopRemoved(QtGradientStop *stop)
void currentStopChanged(QtGradientStop *stop)
void stopsSwapped(QtGradientStop *stop1, QtGradientStop *stop2)
QtGradientStop * lastSelected() const
Combined button and popup list for selecting options.