| 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 | |
| 4 | #include "qtgradientstopscontroller_p.h" |
| 5 | #include "ui_qtgradienteditor.h" |
| 6 | #include "qtgradientstopsmodel_p.h" |
| 7 | |
| 8 | #include <QtCore/QTimer> |
| 9 | |
| 10 | QT_BEGIN_NAMESPACE |
| 11 | |
| 12 | using namespace Qt::StringLiterals; |
| 13 | |
| 14 | class QtGradientStopsControllerPrivate : public QObject |
| 15 | { |
| 16 | Q_OBJECT |
| 17 | QtGradientStopsController *q_ptr = nullptr; |
| 18 | Q_DECLARE_PUBLIC(QtGradientStopsController) |
| 19 | public: |
| 20 | using PositionColorMap = QMap<qreal, QColor>; |
| 21 | using PositionStopMap = QMap<qreal, QtGradientStop *>; |
| 22 | |
| 23 | void setUi(Ui::QtGradientEditor *ui); |
| 24 | |
| 25 | void slotHsvClicked(); |
| 26 | void slotRgbClicked(); |
| 27 | |
| 28 | void slotCurrentStopChanged(QtGradientStop *stop); |
| 29 | void slotStopMoved(QtGradientStop *stop, qreal newPos); |
| 30 | void slotStopsSwapped(QtGradientStop *stop1, QtGradientStop *stop2); |
| 31 | void slotStopChanged(QtGradientStop *stop, QColor newColor); |
| 32 | void slotStopSelected(QtGradientStop *stop, bool selected); |
| 33 | void slotStopAdded(QtGradientStop *stop); |
| 34 | void slotStopRemoved(QtGradientStop *stop); |
| 35 | void slotUpdatePositionSpinBox(); |
| 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(); |
| 50 | void slotZoomOut(); |
| 51 | void slotZoomAll(); |
| 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 | |
| 60 | QtGradientStopsModel *m_model = nullptr; |
| 61 | QColor::Spec m_spec = QColor::Hsv; |
| 62 | |
| 63 | Ui::QtGradientEditor *m_ui = nullptr; |
| 64 | }; |
| 65 | |
| 66 | void 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); |
| 77 | connect(sender: m_model, signal: &QtGradientStopsModel::currentStopChanged, |
| 78 | context: this, slot: &QtGradientStopsControllerPrivate::slotCurrentStopChanged); |
| 79 | connect(sender: m_model, signal: &QtGradientStopsModel::stopMoved, |
| 80 | context: this, slot: &QtGradientStopsControllerPrivate::slotStopMoved); |
| 81 | connect(sender: m_model, signal: &QtGradientStopsModel::stopsSwapped, |
| 82 | context: this, slot: &QtGradientStopsControllerPrivate::slotStopsSwapped); |
| 83 | connect(sender: m_model, signal: &QtGradientStopsModel::stopChanged, |
| 84 | context: this, slot: &QtGradientStopsControllerPrivate::slotStopChanged); |
| 85 | connect(sender: m_model, signal: &QtGradientStopsModel::stopSelected, |
| 86 | context: this, slot: &QtGradientStopsControllerPrivate::slotStopSelected); |
| 87 | connect(sender: m_model, signal: &QtGradientStopsModel::stopAdded, |
| 88 | context: this, slot: &QtGradientStopsControllerPrivate::slotStopAdded); |
| 89 | connect(sender: m_model, signal: &QtGradientStopsModel::stopRemoved, |
| 90 | context: this, slot: &QtGradientStopsControllerPrivate::slotStopRemoved); |
| 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(enable: 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 )); |
| 134 | updateZoom(zoom: 1); |
| 135 | } |
| 136 | |
| 137 | void QtGradientStopsControllerPrivate::enableCurrent(bool enable) |
| 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 | |
| 164 | QtGradientStopsControllerPrivate::PositionColorMap QtGradientStopsControllerPrivate::stopsData(const PositionStopMap &stops) const |
| 165 | { |
| 166 | PositionColorMap data; |
| 167 | for (QtGradientStop *stop : stops) |
| 168 | data[stop->position()] = stop->color(); |
| 169 | return data; |
| 170 | } |
| 171 | |
| 172 | QGradientStops QtGradientStopsControllerPrivate::makeGradientStops(const PositionColorMap &data) const |
| 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 | |
| 180 | void QtGradientStopsControllerPrivate::updateZoom(double zoom) |
| 181 | { |
| 182 | m_ui->gradientStopsWidget->setZoom(zoom); |
| 183 | m_ui->zoomSpinBox->blockSignals(true); |
| 184 | m_ui->zoomSpinBox->setValue(qRound(d: 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 | |
| 200 | void QtGradientStopsControllerPrivate::slotHsvClicked() |
| 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 | |
| 236 | void QtGradientStopsControllerPrivate::slotRgbClicked() |
| 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 | |
| 268 | void QtGradientStopsControllerPrivate::setColorSpinBoxes(QColor color) |
| 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 | |
| 300 | void QtGradientStopsControllerPrivate::slotCurrentStopChanged(QtGradientStop *stop) |
| 301 | { |
| 302 | if (!stop) { |
| 303 | enableCurrent(enable: false); |
| 304 | return; |
| 305 | } |
| 306 | enableCurrent(enable: true); |
| 307 | |
| 308 | QTimer::singleShot(interval: 0, receiver: this, slot: &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()); |
| 315 | setColorSpinBoxes(stop->color()); |
| 316 | } |
| 317 | |
| 318 | void QtGradientStopsControllerPrivate::slotStopMoved(QtGradientStop *stop, qreal newPos) |
| 319 | { |
| 320 | QTimer::singleShot(interval: 0, receiver: this, slot: &QtGradientStopsControllerPrivate::slotUpdatePositionSpinBox); |
| 321 | |
| 322 | PositionColorMap stops = stopsData(stops: m_model->stops()); |
| 323 | stops.remove(key: stop->position()); |
| 324 | stops[newPos] = stop->color(); |
| 325 | |
| 326 | QGradientStops gradStops = makeGradientStops(data: stops); |
| 327 | emit q_ptr->gradientStopsChanged(stops: gradStops); |
| 328 | } |
| 329 | |
| 330 | void QtGradientStopsControllerPrivate::slotStopsSwapped(QtGradientStop *stop1, QtGradientStop *stop2) |
| 331 | { |
| 332 | QTimer::singleShot(interval: 0, receiver: this, slot: &QtGradientStopsControllerPrivate::slotUpdatePositionSpinBox); |
| 333 | |
| 334 | PositionColorMap stops = stopsData(stops: 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(data: stops); |
| 341 | emit q_ptr->gradientStopsChanged(stops: gradStops); |
| 342 | } |
| 343 | |
| 344 | void QtGradientStopsControllerPrivate::slotStopAdded(QtGradientStop *stop) |
| 345 | { |
| 346 | PositionColorMap stops = stopsData(stops: m_model->stops()); |
| 347 | stops[stop->position()] = stop->color(); |
| 348 | |
| 349 | QGradientStops gradStops = makeGradientStops(data: stops); |
| 350 | emit q_ptr->gradientStopsChanged(stops: gradStops); |
| 351 | } |
| 352 | |
| 353 | void QtGradientStopsControllerPrivate::slotStopRemoved(QtGradientStop *stop) |
| 354 | { |
| 355 | PositionColorMap stops = stopsData(stops: m_model->stops()); |
| 356 | stops.remove(key: stop->position()); |
| 357 | |
| 358 | QGradientStops gradStops = makeGradientStops(data: stops); |
| 359 | emit q_ptr->gradientStopsChanged(stops: gradStops); |
| 360 | } |
| 361 | |
| 362 | void QtGradientStopsControllerPrivate::slotStopChanged(QtGradientStop *stop, |
| 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(stops: m_model->stops()); |
| 375 | stops[stop->position()] = newColor; |
| 376 | |
| 377 | QGradientStops gradStops = makeGradientStops(data: stops); |
| 378 | emit q_ptr->gradientStopsChanged(stops: gradStops); |
| 379 | } |
| 380 | |
| 381 | void QtGradientStopsControllerPrivate::slotStopSelected(QtGradientStop *stop, bool selected) |
| 382 | { |
| 383 | Q_UNUSED(stop); |
| 384 | Q_UNUSED(selected); |
| 385 | QTimer::singleShot(interval: 0, receiver: this, slot: &QtGradientStopsControllerPrivate::slotUpdatePositionSpinBox); |
| 386 | } |
| 387 | |
| 388 | void QtGradientStopsControllerPrivate::slotUpdatePositionSpinBox() |
| 389 | { |
| 390 | QtGradientStop *current = m_model->currentStop(); |
| 391 | if (!current) |
| 392 | return; |
| 393 | |
| 394 | qreal min = 0.0; |
| 395 | qreal max = 1.0; |
| 396 | const qreal pos = current->position(); |
| 397 | |
| 398 | QtGradientStop *first = m_model->firstSelected(); |
| 399 | QtGradientStop *last = m_model->lastSelected(); |
| 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(d: min * 1000); |
| 420 | const int newMax = qRound(d: 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 | |
| 431 | void QtGradientStopsControllerPrivate::slotChangeColor(QColor color) |
| 432 | { |
| 433 | QtGradientStop *stop = m_model->currentStop(); |
| 434 | if (!stop) |
| 435 | return; |
| 436 | m_model->changeStop(stop, newColor: color); |
| 437 | const auto stops = m_model->selectedStops(); |
| 438 | for (QtGradientStop *s : stops) { |
| 439 | if (s != stop) |
| 440 | m_model->changeStop(stop: s, newColor: color); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | void QtGradientStopsControllerPrivate::slotChangeHueColor(QColor color) |
| 445 | { |
| 446 | QtGradientStop *stop = m_model->currentStop(); |
| 447 | if (!stop) |
| 448 | return; |
| 449 | m_model->changeStop(stop, newColor: 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(h: color.hueF(), s: c.saturationF(), v: c.valueF(), a: c.alphaF()); |
| 456 | else |
| 457 | c.setRgbF(r: color.redF(), g: c.greenF(), b: c.blueF(), a: c.alphaF()); |
| 458 | m_model->changeStop(stop: s, newColor: c); |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | void QtGradientStopsControllerPrivate::slotChangeHue(int color) |
| 464 | { |
| 465 | QColor c = m_ui->hueColorLine->color(); |
| 466 | if (m_ui->hsvRadioButton->isChecked()) |
| 467 | c.setHsvF(h: qreal(color) / 360.0, s: c.saturationF(), v: c.valueF(), a: c.alphaF()); |
| 468 | else |
| 469 | c.setRed(color); |
| 470 | slotChangeHueColor(color: c); |
| 471 | } |
| 472 | |
| 473 | void QtGradientStopsControllerPrivate::slotChangeSaturationColor(QColor color) |
| 474 | { |
| 475 | QtGradientStop *stop = m_model->currentStop(); |
| 476 | if (!stop) |
| 477 | return; |
| 478 | m_model->changeStop(stop, newColor: 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(h: c.hueF(), s: color.saturationF(), v: c.valueF(), a: c.alphaF()); |
| 485 | int hue = c.hue(); |
| 486 | if (hue == 360 || hue == -1) |
| 487 | c.setHsvF(h: 0.0, s: c.saturationF(), v: c.valueF(), a: c.alphaF()); |
| 488 | } else { |
| 489 | c.setRgbF(r: c.redF(), g: color.greenF(), b: c.blueF(), a: c.alphaF()); |
| 490 | } |
| 491 | m_model->changeStop(stop: s, newColor: c); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | void QtGradientStopsControllerPrivate::slotChangeSaturation(int color) |
| 497 | { |
| 498 | QColor c = m_ui->saturationColorLine->color(); |
| 499 | if (m_ui->hsvRadioButton->isChecked()) |
| 500 | c.setHsvF(h: c.hueF(), s: qreal(color) / 255, v: c.valueF(), a: c.alphaF()); |
| 501 | else |
| 502 | c.setGreen(color); |
| 503 | slotChangeSaturationColor(color: c); |
| 504 | } |
| 505 | |
| 506 | void QtGradientStopsControllerPrivate::slotChangeValueColor(QColor color) |
| 507 | { |
| 508 | QtGradientStop *stop = m_model->currentStop(); |
| 509 | if (!stop) |
| 510 | return; |
| 511 | m_model->changeStop(stop, newColor: 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(h: c.hueF(), s: c.saturationF(), v: color.valueF(), a: c.alphaF()); |
| 518 | int hue = c.hue(); |
| 519 | if (hue == 360 || hue == -1) |
| 520 | c.setHsvF(h: 0.0, s: c.saturationF(), v: c.valueF(), a: c.alphaF()); |
| 521 | } else { |
| 522 | c.setRgbF(r: c.redF(), g: c.greenF(), b: color.blueF(), a: c.alphaF()); |
| 523 | } |
| 524 | m_model->changeStop(stop: s, newColor: c); |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | void QtGradientStopsControllerPrivate::slotChangeValue(int color) |
| 530 | { |
| 531 | QColor c = m_ui->valueColorLine->color(); |
| 532 | if (m_ui->hsvRadioButton->isChecked()) |
| 533 | c.setHsvF(h: c.hueF(), s: c.saturationF(), v: qreal(color) / 255, a: c.alphaF()); |
| 534 | else |
| 535 | c.setBlue(color); |
| 536 | slotChangeValueColor(color: c); |
| 537 | } |
| 538 | |
| 539 | void QtGradientStopsControllerPrivate::slotChangeAlphaColor(QColor color) |
| 540 | { |
| 541 | QtGradientStop *stop = m_model->currentStop(); |
| 542 | if (!stop) |
| 543 | return; |
| 544 | m_model->changeStop(stop, newColor: 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(h: c.hueF(), s: c.saturationF(), v: c.valueF(), a: color.alphaF()); |
| 551 | int hue = c.hue(); |
| 552 | if (hue == 360 || hue == -1) |
| 553 | c.setHsvF(h: 0.0, s: c.saturationF(), v: c.valueF(), a: c.alphaF()); |
| 554 | } else { |
| 555 | c.setRgbF(r: c.redF(), g: c.greenF(), b: c.blueF(), a: color.alphaF()); |
| 556 | } |
| 557 | m_model->changeStop(stop: s, newColor: c); |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | void QtGradientStopsControllerPrivate::slotChangeAlpha(int color) |
| 563 | { |
| 564 | QColor c = m_ui->alphaColorLine->color(); |
| 565 | if (m_ui->hsvRadioButton->isChecked()) |
| 566 | c.setHsvF(h: c.hueF(), s: c.saturationF(), v: c.valueF(), a: qreal(color) / 255); |
| 567 | else |
| 568 | c.setAlpha(color); |
| 569 | slotChangeAlphaColor(color: c); |
| 570 | } |
| 571 | |
| 572 | void QtGradientStopsControllerPrivate::slotChangePosition(double value) |
| 573 | { |
| 574 | QtGradientStop *stop = m_model->currentStop(); |
| 575 | if (!stop) |
| 576 | return; |
| 577 | |
| 578 | m_model->moveStops(newPosition: value); |
| 579 | } |
| 580 | |
| 581 | void QtGradientStopsControllerPrivate::slotChangeZoom(int value) |
| 582 | { |
| 583 | updateZoom(zoom: value / 100.0); |
| 584 | } |
| 585 | |
| 586 | void QtGradientStopsControllerPrivate::slotZoomIn() |
| 587 | { |
| 588 | double newZoom = m_ui->gradientStopsWidget->zoom() * 2; |
| 589 | if (newZoom > 100) |
| 590 | newZoom = 100; |
| 591 | updateZoom(zoom: newZoom); |
| 592 | } |
| 593 | |
| 594 | void QtGradientStopsControllerPrivate::slotZoomOut() |
| 595 | { |
| 596 | double newZoom = m_ui->gradientStopsWidget->zoom() / 2; |
| 597 | if (newZoom < 1) |
| 598 | newZoom = 1; |
| 599 | updateZoom(zoom: newZoom); |
| 600 | } |
| 601 | |
| 602 | void QtGradientStopsControllerPrivate::slotZoomAll() |
| 603 | { |
| 604 | updateZoom(zoom: 1); |
| 605 | } |
| 606 | |
| 607 | void QtGradientStopsControllerPrivate::slotZoomChanged(double zoom) |
| 608 | { |
| 609 | updateZoom(zoom); |
| 610 | } |
| 611 | |
| 612 | QtGradientStopsController::QtGradientStopsController(QObject *parent) |
| 613 | : QObject(parent), d_ptr(new QtGradientStopsControllerPrivate()) |
| 614 | { |
| 615 | d_ptr->q_ptr = this; |
| 616 | } |
| 617 | |
| 618 | void QtGradientStopsController::setUi(Ui::QtGradientEditor *ui) |
| 619 | { |
| 620 | d_ptr->setUi(ui); |
| 621 | } |
| 622 | |
| 623 | QtGradientStopsController::~QtGradientStopsController() |
| 624 | { |
| 625 | } |
| 626 | |
| 627 | void 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(pos: pair.first, color: pair.second); |
| 633 | if (!first) |
| 634 | first = stop; |
| 635 | } |
| 636 | if (first) |
| 637 | d_ptr->m_model->setCurrentStop(first); |
| 638 | } |
| 639 | |
| 640 | QGradientStops QtGradientStopsController::gradientStops() const |
| 641 | { |
| 642 | QGradientStops stops; |
| 643 | const auto stopsList = d_ptr->m_model->stops().values(); |
| 644 | for (const QtGradientStop *stop : stopsList) |
| 645 | stops.append(t: {stop->position(), stop->color()}); |
| 646 | return stops; |
| 647 | } |
| 648 | |
| 649 | QColor::Spec QtGradientStopsController::spec() const |
| 650 | { |
| 651 | return d_ptr->m_spec; |
| 652 | } |
| 653 | |
| 654 | void 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 | |
| 669 | QT_END_NAMESPACE |
| 670 | |
| 671 | #include "qtgradientstopscontroller.moc" |
| 672 | |