| 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 | |
| 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 | |
| 124 | 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 | |
| 137 | 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 | |
| 149 | void QtGradientStopsControllerPrivate::updateZoom(double zoom) |
| 150 | { |
| 151 | m_ui->gradientStopsWidget->setZoom(zoom); |
| 152 | m_ui->zoomSpinBox->blockSignals(b: true); |
| 153 | m_ui->zoomSpinBox->setValue(qRound(d: zoom * 100)); |
| 154 | m_ui->zoomSpinBox->blockSignals(b: 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 | |
| 169 | void QtGradientStopsControllerPrivate::slotHsvClicked() |
| 170 | { |
| 171 | QString h = QCoreApplication::translate(context: "qdesigner_internal::QtGradientStopsController" , key: "H" ); |
| 172 | QString s = QCoreApplication::translate(context: "qdesigner_internal::QtGradientStopsController" , key: "S" ); |
| 173 | QString v = QCoreApplication::translate(context: "qdesigner_internal::QtGradientStopsController" , key: "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(context: "qdesigner_internal::QtGradientStopsController" , key: "Hue" ); |
| 180 | s = QCoreApplication::translate(context: "qdesigner_internal::QtGradientStopsController" , key: "Sat" ); |
| 181 | v = QCoreApplication::translate(context: "qdesigner_internal::QtGradientStopsController" , key: "Val" ); |
| 182 | |
| 183 | const QString hue = QCoreApplication::translate(context: "qdesigner_internal::QtGradientStopsController" , key: "Hue" ); |
| 184 | const QString saturation = QCoreApplication::translate(context: "qdesigner_internal::QtGradientStopsController" , key: "Saturation" ); |
| 185 | const QString value = QCoreApplication::translate(context: "qdesigner_internal::QtGradientStopsController" , key: "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 | |
| 205 | void QtGradientStopsControllerPrivate::slotRgbClicked() |
| 206 | { |
| 207 | QString r = QCoreApplication::translate(context: "qdesigner_internal::QtGradientStopsController" , key: "R" ); |
| 208 | QString g = QCoreApplication::translate(context: "qdesigner_internal::QtGradientStopsController" , key: "G" ); |
| 209 | QString b = QCoreApplication::translate(context: "qdesigner_internal::QtGradientStopsController" , key: "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(context: "qdesigner_internal::QtGradientStopsController" , key: "Red" ); |
| 216 | QString green = QCoreApplication::translate(context: "qdesigner_internal::QtGradientStopsController" , key: "Green" ); |
| 217 | QString blue = QCoreApplication::translate(context: "qdesigner_internal::QtGradientStopsController" , key: "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 | |
| 237 | void QtGradientStopsControllerPrivate::setColorSpinBoxes(const QColor &color) |
| 238 | { |
| 239 | m_ui->hueSpinBox->blockSignals(b: true); |
| 240 | m_ui->saturationSpinBox->blockSignals(b: true); |
| 241 | m_ui->valueSpinBox->blockSignals(b: true); |
| 242 | m_ui->alphaSpinBox->blockSignals(b: 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(b: false); |
| 264 | m_ui->saturationSpinBox->blockSignals(b: false); |
| 265 | m_ui->valueSpinBox->blockSignals(b: false); |
| 266 | m_ui->alphaSpinBox->blockSignals(b: false); |
| 267 | } |
| 268 | |
| 269 | void QtGradientStopsControllerPrivate::slotCurrentStopChanged(QtGradientStop *stop) |
| 270 | { |
| 271 | if (!stop) { |
| 272 | enableCurrent(enable: false); |
| 273 | return; |
| 274 | } |
| 275 | enableCurrent(enable: true); |
| 276 | |
| 277 | QTimer::singleShot(msec: 0, receiver: 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 | |
| 287 | void QtGradientStopsControllerPrivate::slotStopMoved(QtGradientStop *stop, qreal newPos) |
| 288 | { |
| 289 | QTimer::singleShot(msec: 0, receiver: q_ptr, SLOT(slotUpdatePositionSpinBox())); |
| 290 | |
| 291 | PositionColorMap stops = stopsData(stops: m_model->stops()); |
| 292 | stops.remove(akey: stop->position()); |
| 293 | stops[newPos] = stop->color(); |
| 294 | |
| 295 | QGradientStops gradStops = makeGradientStops(data: stops); |
| 296 | emit q_ptr->gradientStopsChanged(stops: gradStops); |
| 297 | } |
| 298 | |
| 299 | void QtGradientStopsControllerPrivate::slotStopsSwapped(QtGradientStop *stop1, QtGradientStop *stop2) |
| 300 | { |
| 301 | QTimer::singleShot(msec: 0, receiver: q_ptr, SLOT(slotUpdatePositionSpinBox())); |
| 302 | |
| 303 | PositionColorMap stops = stopsData(stops: 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(data: stops); |
| 310 | emit q_ptr->gradientStopsChanged(stops: gradStops); |
| 311 | } |
| 312 | |
| 313 | void QtGradientStopsControllerPrivate::slotStopAdded(QtGradientStop *stop) |
| 314 | { |
| 315 | PositionColorMap stops = stopsData(stops: m_model->stops()); |
| 316 | stops[stop->position()] = stop->color(); |
| 317 | |
| 318 | QGradientStops gradStops = makeGradientStops(data: stops); |
| 319 | emit q_ptr->gradientStopsChanged(stops: gradStops); |
| 320 | } |
| 321 | |
| 322 | void QtGradientStopsControllerPrivate::slotStopRemoved(QtGradientStop *stop) |
| 323 | { |
| 324 | PositionColorMap stops = stopsData(stops: m_model->stops()); |
| 325 | stops.remove(akey: stop->position()); |
| 326 | |
| 327 | QGradientStops gradStops = makeGradientStops(data: stops); |
| 328 | emit q_ptr->gradientStopsChanged(stops: gradStops); |
| 329 | } |
| 330 | |
| 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(stops: m_model->stops()); |
| 343 | stops[stop->position()] = newColor; |
| 344 | |
| 345 | QGradientStops gradStops = makeGradientStops(data: stops); |
| 346 | emit q_ptr->gradientStopsChanged(stops: gradStops); |
| 347 | } |
| 348 | |
| 349 | void QtGradientStopsControllerPrivate::slotStopSelected(QtGradientStop *stop, bool selected) |
| 350 | { |
| 351 | Q_UNUSED(stop); |
| 352 | Q_UNUSED(selected); |
| 353 | QTimer::singleShot(msec: 0, receiver: q_ptr, SLOT(slotUpdatePositionSpinBox())); |
| 354 | } |
| 355 | |
| 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(d: m_ui->positionSpinBox->minimum() * 1000); |
| 385 | const int spinMax = qRound(d: m_ui->positionSpinBox->maximum() * 1000); |
| 386 | |
| 387 | const int newMin = qRound(d: min * 1000); |
| 388 | const int newMax = qRound(d: max * 1000); |
| 389 | |
| 390 | m_ui->positionSpinBox->blockSignals(b: true); |
| 391 | if (spinMin != newMin || spinMax != newMax) { |
| 392 | m_ui->positionSpinBox->setRange(min: double(newMin) / 1000, max: double(newMax) / 1000); |
| 393 | } |
| 394 | if (m_ui->positionSpinBox->value() != pos) |
| 395 | m_ui->positionSpinBox->setValue(pos); |
| 396 | m_ui->positionSpinBox->blockSignals(b: false); |
| 397 | } |
| 398 | |
| 399 | void QtGradientStopsControllerPrivate::slotChangeColor(const QColor &color) |
| 400 | { |
| 401 | QtGradientStop *stop = m_model->currentStop(); |
| 402 | if (!stop) |
| 403 | return; |
| 404 | m_model->changeStop(stop, newColor: color); |
| 405 | const auto stops = m_model->selectedStops(); |
| 406 | for (QtGradientStop *s : stops) { |
| 407 | if (s != stop) |
| 408 | m_model->changeStop(stop: s, newColor: color); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | void QtGradientStopsControllerPrivate::slotChangeHue(const QColor &color) |
| 413 | { |
| 414 | QtGradientStop *stop = m_model->currentStop(); |
| 415 | if (!stop) |
| 416 | return; |
| 417 | m_model->changeStop(stop, newColor: 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(h: color.hueF(), s: c.saturationF(), v: c.valueF(), a: c.alphaF()); |
| 424 | else |
| 425 | c.setRgbF(r: color.redF(), g: c.greenF(), b: c.blueF(), a: c.alphaF()); |
| 426 | m_model->changeStop(stop: s, newColor: c); |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | void QtGradientStopsControllerPrivate::slotChangeHue(int color) |
| 432 | { |
| 433 | QColor c = m_ui->hueColorLine->color(); |
| 434 | if (m_ui->hsvRadioButton->isChecked()) |
| 435 | c.setHsvF(h: qreal(color) / 360.0, s: c.saturationF(), v: c.valueF(), a: c.alphaF()); |
| 436 | else |
| 437 | c.setRed(color); |
| 438 | slotChangeHue(color: c); |
| 439 | } |
| 440 | |
| 441 | void QtGradientStopsControllerPrivate::slotChangeSaturation(const QColor &color) |
| 442 | { |
| 443 | QtGradientStop *stop = m_model->currentStop(); |
| 444 | if (!stop) |
| 445 | return; |
| 446 | m_model->changeStop(stop, newColor: 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(h: c.hueF(), s: color.saturationF(), v: c.valueF(), a: c.alphaF()); |
| 453 | int hue = c.hue(); |
| 454 | if (hue == 360 || hue == -1) |
| 455 | c.setHsvF(h: 0.0, s: c.saturationF(), v: c.valueF(), a: c.alphaF()); |
| 456 | } else { |
| 457 | c.setRgbF(r: c.redF(), g: color.greenF(), b: c.blueF(), a: c.alphaF()); |
| 458 | } |
| 459 | m_model->changeStop(stop: s, newColor: c); |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | void QtGradientStopsControllerPrivate::slotChangeSaturation(int color) |
| 465 | { |
| 466 | QColor c = m_ui->saturationColorLine->color(); |
| 467 | if (m_ui->hsvRadioButton->isChecked()) |
| 468 | c.setHsvF(h: c.hueF(), s: qreal(color) / 255, v: c.valueF(), a: c.alphaF()); |
| 469 | else |
| 470 | c.setGreen(color); |
| 471 | slotChangeSaturation(color: c); |
| 472 | } |
| 473 | |
| 474 | void QtGradientStopsControllerPrivate::slotChangeValue(const QColor &color) |
| 475 | { |
| 476 | QtGradientStop *stop = m_model->currentStop(); |
| 477 | if (!stop) |
| 478 | return; |
| 479 | m_model->changeStop(stop, newColor: 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(h: c.hueF(), s: c.saturationF(), v: color.valueF(), a: c.alphaF()); |
| 486 | int hue = c.hue(); |
| 487 | if (hue == 360 || hue == -1) |
| 488 | c.setHsvF(h: 0.0, s: c.saturationF(), v: c.valueF(), a: c.alphaF()); |
| 489 | } else { |
| 490 | c.setRgbF(r: c.redF(), g: c.greenF(), b: color.blueF(), a: c.alphaF()); |
| 491 | } |
| 492 | m_model->changeStop(stop: s, newColor: c); |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | void QtGradientStopsControllerPrivate::slotChangeValue(int color) |
| 498 | { |
| 499 | QColor c = m_ui->valueColorLine->color(); |
| 500 | if (m_ui->hsvRadioButton->isChecked()) |
| 501 | c.setHsvF(h: c.hueF(), s: c.saturationF(), v: qreal(color) / 255, a: c.alphaF()); |
| 502 | else |
| 503 | c.setBlue(color); |
| 504 | slotChangeValue(color: c); |
| 505 | } |
| 506 | |
| 507 | void QtGradientStopsControllerPrivate::slotChangeAlpha(const QColor &color) |
| 508 | { |
| 509 | QtGradientStop *stop = m_model->currentStop(); |
| 510 | if (!stop) |
| 511 | return; |
| 512 | m_model->changeStop(stop, newColor: 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(h: c.hueF(), s: c.saturationF(), v: c.valueF(), a: color.alphaF()); |
| 519 | int hue = c.hue(); |
| 520 | if (hue == 360 || hue == -1) |
| 521 | c.setHsvF(h: 0.0, s: c.saturationF(), v: c.valueF(), a: c.alphaF()); |
| 522 | } else { |
| 523 | c.setRgbF(r: c.redF(), g: c.greenF(), b: c.blueF(), a: color.alphaF()); |
| 524 | } |
| 525 | m_model->changeStop(stop: s, newColor: c); |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | void QtGradientStopsControllerPrivate::slotChangeAlpha(int color) |
| 531 | { |
| 532 | QColor c = m_ui->alphaColorLine->color(); |
| 533 | if (m_ui->hsvRadioButton->isChecked()) |
| 534 | c.setHsvF(h: c.hueF(), s: c.saturationF(), v: c.valueF(), a: qreal(color) / 255); |
| 535 | else |
| 536 | c.setAlpha(color); |
| 537 | slotChangeAlpha(color: c); |
| 538 | } |
| 539 | |
| 540 | void QtGradientStopsControllerPrivate::slotChangePosition(double value) |
| 541 | { |
| 542 | QtGradientStop *stop = m_model->currentStop(); |
| 543 | if (!stop) |
| 544 | return; |
| 545 | |
| 546 | m_model->moveStops(newPosition: value); |
| 547 | } |
| 548 | |
| 549 | void QtGradientStopsControllerPrivate::slotChangeZoom(int value) |
| 550 | { |
| 551 | updateZoom(zoom: value / 100.0); |
| 552 | } |
| 553 | |
| 554 | void QtGradientStopsControllerPrivate::slotZoomIn() |
| 555 | { |
| 556 | double newZoom = m_ui->gradientStopsWidget->zoom() * 2; |
| 557 | if (newZoom > 100) |
| 558 | newZoom = 100; |
| 559 | updateZoom(zoom: newZoom); |
| 560 | } |
| 561 | |
| 562 | void QtGradientStopsControllerPrivate::slotZoomOut() |
| 563 | { |
| 564 | double newZoom = m_ui->gradientStopsWidget->zoom() / 2; |
| 565 | if (newZoom < 1) |
| 566 | newZoom = 1; |
| 567 | updateZoom(zoom: newZoom); |
| 568 | } |
| 569 | |
| 570 | void QtGradientStopsControllerPrivate::slotZoomAll() |
| 571 | { |
| 572 | updateZoom(zoom: 1); |
| 573 | } |
| 574 | |
| 575 | void QtGradientStopsControllerPrivate::slotZoomChanged(double zoom) |
| 576 | { |
| 577 | updateZoom(zoom); |
| 578 | } |
| 579 | |
| 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 | |
| 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(sender: d_ptr->m_model, SIGNAL(currentStopChanged(QtGradientStop*)), |
| 600 | receiver: this, SLOT(slotCurrentStopChanged(QtGradientStop*))); |
| 601 | connect(sender: d_ptr->m_model, SIGNAL(stopMoved(QtGradientStop*,qreal)), |
| 602 | receiver: this, SLOT(slotStopMoved(QtGradientStop*,qreal))); |
| 603 | connect(sender: d_ptr->m_model, SIGNAL(stopsSwapped(QtGradientStop*,QtGradientStop*)), |
| 604 | receiver: this, SLOT(slotStopsSwapped(QtGradientStop*,QtGradientStop*))); |
| 605 | connect(sender: d_ptr->m_model, SIGNAL(stopChanged(QtGradientStop*,QColor)), |
| 606 | receiver: this, SLOT(slotStopChanged(QtGradientStop*,QColor))); |
| 607 | connect(sender: d_ptr->m_model, SIGNAL(stopSelected(QtGradientStop*,bool)), |
| 608 | receiver: this, SLOT(slotStopSelected(QtGradientStop*,bool))); |
| 609 | connect(sender: d_ptr->m_model, SIGNAL(stopAdded(QtGradientStop*)), |
| 610 | receiver: this, SLOT(slotStopAdded(QtGradientStop*))); |
| 611 | connect(sender: d_ptr->m_model, SIGNAL(stopRemoved(QtGradientStop*)), |
| 612 | receiver: this, SLOT(slotStopRemoved(QtGradientStop*))); |
| 613 | |
| 614 | connect(sender: d_ptr->m_ui->hueColorLine, SIGNAL(colorChanged(QColor)), |
| 615 | receiver: this, SLOT(slotChangeHue(QColor))); |
| 616 | connect(sender: d_ptr->m_ui->saturationColorLine, SIGNAL(colorChanged(QColor)), |
| 617 | receiver: this, SLOT(slotChangeSaturation(QColor))); |
| 618 | connect(sender: d_ptr->m_ui->valueColorLine, SIGNAL(colorChanged(QColor)), |
| 619 | receiver: this, SLOT(slotChangeValue(QColor))); |
| 620 | connect(sender: d_ptr->m_ui->alphaColorLine, SIGNAL(colorChanged(QColor)), |
| 621 | receiver: this, SLOT(slotChangeAlpha(QColor))); |
| 622 | connect(sender: d_ptr->m_ui->colorButton, SIGNAL(colorChanged(QColor)), |
| 623 | receiver: this, SLOT(slotChangeColor(QColor))); |
| 624 | |
| 625 | connect(sender: d_ptr->m_ui->hueSpinBox, SIGNAL(valueChanged(int)), |
| 626 | receiver: this, SLOT(slotChangeHue(int))); |
| 627 | connect(sender: d_ptr->m_ui->saturationSpinBox, SIGNAL(valueChanged(int)), |
| 628 | receiver: this, SLOT(slotChangeSaturation(int))); |
| 629 | connect(sender: d_ptr->m_ui->valueSpinBox, SIGNAL(valueChanged(int)), |
| 630 | receiver: this, SLOT(slotChangeValue(int))); |
| 631 | connect(sender: d_ptr->m_ui->alphaSpinBox, SIGNAL(valueChanged(int)), |
| 632 | receiver: this, SLOT(slotChangeAlpha(int))); |
| 633 | |
| 634 | connect(sender: d_ptr->m_ui->positionSpinBox, SIGNAL(valueChanged(double)), |
| 635 | receiver: this, SLOT(slotChangePosition(double))); |
| 636 | |
| 637 | connect(sender: d_ptr->m_ui->zoomSpinBox, SIGNAL(valueChanged(int)), |
| 638 | receiver: this, SLOT(slotChangeZoom(int))); |
| 639 | connect(sender: d_ptr->m_ui->zoomInButton, SIGNAL(clicked()), |
| 640 | receiver: this, SLOT(slotZoomIn())); |
| 641 | connect(sender: d_ptr->m_ui->zoomOutButton, SIGNAL(clicked()), |
| 642 | receiver: this, SLOT(slotZoomOut())); |
| 643 | connect(sender: d_ptr->m_ui->zoomAllButton, SIGNAL(clicked()), |
| 644 | receiver: this, SLOT(slotZoomAll())); |
| 645 | connect(sender: d_ptr->m_ui->gradientStopsWidget, SIGNAL(zoomChanged(double)), |
| 646 | receiver: this, SLOT(slotZoomChanged(double))); |
| 647 | |
| 648 | connect(sender: d_ptr->m_ui->hsvRadioButton, SIGNAL(clicked()), |
| 649 | receiver: this, SLOT(slotHsvClicked())); |
| 650 | connect(sender: d_ptr->m_ui->rgbRadioButton, SIGNAL(clicked()), |
| 651 | receiver: this, SLOT(slotRgbClicked())); |
| 652 | |
| 653 | d_ptr->enableCurrent(enable: 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(zoom: 1); |
| 657 | } |
| 658 | |
| 659 | QtGradientStopsController::~QtGradientStopsController() |
| 660 | { |
| 661 | } |
| 662 | |
| 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(pos: pair.first, color: pair.second); |
| 669 | if (!first) |
| 670 | first = stop; |
| 671 | } |
| 672 | if (first) |
| 673 | d_ptr->m_model->setCurrentStop(first); |
| 674 | } |
| 675 | |
| 676 | 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 | |
| 685 | QColor::Spec QtGradientStopsController::spec() const |
| 686 | { |
| 687 | return d_ptr->m_spec; |
| 688 | } |
| 689 | |
| 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 | |