Warning, file /graphics/kdiagram/examples/demo/gradientdialog.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /** 0002 * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved. 0003 * 0004 * This file is part of the KD Chart library. 0005 * 0006 * This program is free software; you can redistribute it and/or 0007 * modify it under the terms of the GNU General Public License as 0008 * published by the Free Software Foundation; either version 2 of 0009 * the License, or (at your option) any later version. 0010 * 0011 * This program is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 * GNU General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU General Public License 0017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 0018 */ 0019 0020 #include "gradientdialog.h" 0021 #include "ui_gradientdialog.h" 0022 0023 #include "colorslider.h" 0024 0025 #include <QHBoxLayout> 0026 #include <QVBoxLayout> 0027 #include <QSpinBox> 0028 0029 #include <QLabel> 0030 0031 0032 class GradientDialog::Private : public QObject 0033 { 0034 Q_OBJECT 0035 public: 0036 Private( GradientDialog *qq ); 0037 ~Private(); 0038 void init(); 0039 ColorSlider *redSlider; 0040 ColorSlider *greenSlider; 0041 ColorSlider *blueSlider; 0042 QVector< QGradientStop > m_gradient; 0043 int m_currStopNr; 0044 Ui::GradientDialog *ui; 0045 GradientDialog *q; 0046 public Q_SLOTS: 0047 void changedIndex( int index ); 0048 void changeStopPosition( double value ); 0049 void insertItem(); 0050 void deleteItem(); 0051 void resetColors(); 0052 void updateGradientDisplay(); 0053 }; 0054 0055 GradientDialog::Private::Private( GradientDialog *qq ) 0056 : m_currStopNr( 2 ) 0057 , ui( new Ui::GradientDialog ) 0058 , q( qq ) 0059 { 0060 m_gradient << qMakePair( 0.0, QColor( Qt::red ) ); 0061 m_gradient << qMakePair( 1.0, QColor( Qt::blue ) ); 0062 } 0063 0064 GradientDialog::Private::~Private() 0065 { 0066 delete ui; 0067 } 0068 0069 void GradientDialog::Private::changeStopPosition( double value ) 0070 { 0071 m_gradient[ ui->stopSelector->currentIndex() ].first = value; 0072 } 0073 0074 void GradientDialog::Private::changedIndex( int index ) 0075 { 0076 if ( index >= 0 ) 0077 { 0078 ui->stopPosition->setValue( m_gradient[ index ].first ); 0079 QColor stopColor = m_gradient[ index ].second; 0080 redSlider->setValue( stopColor.red() ); 0081 greenSlider->setValue( stopColor.green() ); 0082 blueSlider->setValue( stopColor.blue() ); 0083 } 0084 } 0085 0086 void GradientDialog::Private::insertItem() 0087 { 0088 QGradientStop newStop; 0089 newStop.first = 0.5; 0090 newStop.second = Qt::white; 0091 const int index = ui->stopSelector->currentIndex() + 1; 0092 ui->stopSelector->setCurrentIndex( - 1 ); 0093 m_gradient.insert( index, newStop ); 0094 0095 0096 QStringList newItems; 0097 newItems << tr( "stop %1" ).arg( m_currStopNr ); 0098 ui->stopSelector->insertItems( index, newItems ); 0099 0100 ui->stopSelector->setCurrentIndex( index ); 0101 0102 updateGradientDisplay(); 0103 0104 } 0105 0106 void GradientDialog::Private::deleteItem() 0107 { 0108 0109 } 0110 0111 void GradientDialog::Private::updateGradientDisplay() 0112 { 0113 QLinearGradient gradient; 0114 gradient.setStart( 0, 0 ); 0115 gradient.setStart( 1, 0 ); 0116 gradient.setCoordinateMode( QGradient::ObjectBoundingMode ); 0117 for ( const QGradientStop &stop : m_gradient ) 0118 gradient.setColorAt( stop.first, stop.second ); 0119 QPalette palette = ui->gradientDisplay->palette(); 0120 palette.setBrush( QPalette::Window, gradient ); 0121 ui->gradientDisplay->setPalette( palette ); 0122 } 0123 0124 void GradientDialog::Private::init() 0125 { 0126 ui->setupUi( q ); 0127 QStringList list; list << tr( "stop1" ) << tr( "stop2" ); 0128 ui->stopSelector->addItems( list ); 0129 QHBoxLayout *redLayout = new QHBoxLayout; 0130 dynamic_cast< QVBoxLayout* >( ui->gradientStopBox->layout() )->addLayout( redLayout ); 0131 QLabel *redLabel = new QLabel( "R" ); 0132 QFont redFont( redLabel->font() ); 0133 redFont.setUnderline( true ); 0134 redLabel->setFont( redFont ); 0135 redLayout->addWidget( redLabel ); 0136 redSlider = new ColorSlider( q ); 0137 redSlider->setStartColor( Qt::black ); 0138 redSlider->setEndColor( Qt::red ); 0139 QSpinBox *redSpin = new QSpinBox( q ); 0140 redSpin->setMinimum( 0 ); 0141 redSpin->setMaximum( 255 ); 0142 redSpin->setAccelerated( true ); 0143 redSpin->setValue( redSlider->value() ); 0144 connect( redSpin, SIGNAL(valueChanged(int)), redSlider, SLOT(setValue(int)) ); 0145 connect( redSlider, SIGNAL(valueChanged(int)), redSpin, SLOT(setValue(int)) ); 0146 redLayout->addWidget( redSlider ); 0147 redLayout->addWidget( redSpin ); 0148 0149 QHBoxLayout *greenLayout = new QHBoxLayout; 0150 dynamic_cast< QVBoxLayout* >( ui->gradientStopBox->layout() )->addLayout( greenLayout ); 0151 QLabel *greenLabel = new QLabel( "G" ); 0152 QFont greenFont( greenLabel->font() ); 0153 greenFont.setUnderline( true ); 0154 greenLabel->setFont( greenFont ); 0155 greenLayout->addWidget( greenLabel ); 0156 greenSlider = new ColorSlider( q ); 0157 greenSlider->setStartColor( Qt::black ); 0158 greenSlider->setEndColor( Qt::green ); 0159 QSpinBox *greenSpin = new QSpinBox( q ); 0160 greenSpin->setMinimum( 0 ); 0161 greenSpin->setMaximum( 255 ); 0162 greenSpin->setAccelerated( true ); 0163 greenSpin->setValue( greenSlider->value() ); 0164 connect( greenSpin, SIGNAL(valueChanged(int)), greenSlider, SLOT(setValue(int)) ); 0165 connect( greenSlider, SIGNAL(valueChanged(int)), greenSpin, SLOT(setValue(int)) ); 0166 greenLayout->addWidget( greenSlider ); 0167 greenLayout->addWidget( greenSpin ); 0168 0169 QHBoxLayout *blueLayout = new QHBoxLayout; 0170 dynamic_cast< QVBoxLayout* >( ui->gradientStopBox->layout() )->addLayout( blueLayout ); 0171 QLabel *blueLabel = new QLabel( "B" ); 0172 QFont blueFont( blueLabel->font() ); 0173 blueFont.setUnderline( true ); 0174 blueLabel->setFont( blueFont ); 0175 blueLayout->addWidget( blueLabel ); 0176 blueSlider = new ColorSlider( q ); 0177 blueSlider->setStartColor( Qt::black ); 0178 blueSlider->setEndColor( Qt::blue ); 0179 QSpinBox *blueSpin = new QSpinBox( q ); 0180 blueSpin->setMinimum( 0 ); 0181 blueSpin->setMaximum( 255 ); 0182 blueSpin->setAccelerated( true ); 0183 blueSpin->setValue( blueSlider->value() ); 0184 connect( blueSpin, SIGNAL(valueChanged(int)), blueSlider, SLOT(setValue(int)) ); 0185 connect( blueSlider, SIGNAL(valueChanged(int)), blueSpin, SLOT(setValue(int)) ); 0186 blueLayout->addWidget( blueSlider ); 0187 blueLayout->addWidget( blueSpin ); 0188 0189 updateGradientDisplay(); 0190 0191 connect( redSlider, SIGNAL(valueChanged(int)), this, SLOT(resetColors()) ); 0192 connect( greenSlider, SIGNAL(valueChanged(int)), this, SLOT(resetColors()) ); 0193 connect( blueSlider, SIGNAL(valueChanged(int)), this, SLOT(resetColors()) ); 0194 0195 connect( ui->newStop, SIGNAL(clicked()), this, SLOT(insertItem()) ); 0196 connect( ui->deleteStop, SIGNAL(clicked()), this, SLOT(deleteItem()) ); 0197 connect( ui->stopSelector, SIGNAL(currentIndexChanged(int)), this, SLOT(changedIndex(int)) ); 0198 0199 connect( ui->stopPosition, SIGNAL(valueChanged(double)), this, SLOT(changeStopPosition(double)) ); 0200 } 0201 0202 GradientDialog::GradientDialog( QWidget *parent ) 0203 : QDialog( parent ) 0204 , d( new Private( this ) ) 0205 { 0206 d->init(); 0207 } 0208 0209 GradientDialog::~GradientDialog() 0210 { 0211 delete d; 0212 } 0213 0214 void GradientDialog::Private::resetColors() 0215 { 0216 const int red = redSlider->value(); 0217 const int green = greenSlider->value(); 0218 const int blue = blueSlider->value(); 0219 QColor redSliderStartColor( 0, green, blue ); 0220 QColor redSliderStopColor( 255, green, blue ); 0221 redSlider->setStartColor( redSliderStartColor ); 0222 redSlider->setEndColor( redSliderStopColor ); 0223 m_gradient[ ui->stopSelector->currentIndex() ].second.setRed( red ); 0224 0225 QColor greenSliderStartColor( red, 0, blue ); 0226 QColor greenSliderStopColor( red, 255, blue ); 0227 greenSlider->setStartColor( greenSliderStartColor ); 0228 greenSlider->setEndColor( greenSliderStopColor ); 0229 m_gradient[ ui->stopSelector->currentIndex() ].second.setGreen( green ); 0230 0231 QColor blueSliderStartColor( red, green, 0 ); 0232 QColor blueSliderStopColor( red, green, 255 ); 0233 blueSlider->setStartColor( blueSliderStartColor ); 0234 blueSlider->setEndColor( blueSliderStopColor ); 0235 m_gradient[ ui->stopSelector->currentIndex() ].second.setBlue( blue ); 0236 0237 updateGradientDisplay(); 0238 0239 } 0240 0241 QGradient GradientDialog::gradient() const 0242 { 0243 QLinearGradient gradient; 0244 gradient.setStart( 0, 0 ); 0245 gradient.setStart( 1, 0 ); 0246 gradient.setCoordinateMode( QGradient::ObjectBoundingMode ); 0247 for ( const QGradientStop &stop : d->m_gradient ) 0248 gradient.setColorAt( stop.first, stop.second ); 0249 return gradient; 0250 } 0251 void GradientDialog::setGradient( const QGradient &gradient ) 0252 { 0253 d->m_gradient.clear(); 0254 d->ui->stopSelector->clear(); 0255 const QGradientStops stops = gradient.stops(); 0256 for ( const QGradientStop &stop : stops ) 0257 { 0258 d->m_gradient.append( stop ); 0259 } 0260 QStringList newEntries; 0261 for ( int i = 0; i < stops.count(); ++i ) 0262 newEntries << tr( "stop %1" ).arg( i ); 0263 d->ui->stopSelector->addItems( newEntries ); 0264 d->m_currStopNr = newEntries.count(); 0265 d->updateGradientDisplay(); 0266 } 0267 0268 QGradient GradientDialog::getGradient( const QGradient &gradient, QWidget *parent, const QString &title ) 0269 { 0270 GradientDialog dialog( parent ); 0271 dialog.setGradient( gradient ); 0272 dialog.setWindowTitle( title ); 0273 dialog.exec(); 0274 return dialog.gradient(); 0275 } 0276 0277 #include "gradientdialog.moc"