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