24 #include <QPushButton>
26 #include <QToolButton>
27 #include <QFileDialog>
28 #include <QMessageBox>
29 #include <QDesktopWidget>
30 #include <QMouseEvent>
31 #include <QInputDialog>
44 QColorDialog::ColorDialogOptions options )
46 QColor returnColor( initialColor );
47 QColorDialog* liveDialog =
new QColorDialog( initialColor, parent );
48 liveDialog->setWindowTitle( title.isEmpty() ?
tr(
"Select Color" ) : title );
49 liveDialog->setOptions( options );
51 connect( liveDialog, SIGNAL( currentColorChanged(
const QColor& ) ),
52 updateObject, updateSlot );
54 if ( liveDialog->exec() )
56 returnColor = liveDialog->currentColor();
70 : QDialog( parent, fl )
71 , mPreviousColor( color )
73 , mLastCustomColorIndex( 0 )
74 , mPickingColor( false )
79 restoreGeometry( settings.value(
"/Windows/ColorDialog/geometry" ).toByteArray() );
81 mSchemeList->header()->hide();
82 mSchemeList->setColumnWidth( 0, 44 );
85 refreshSchemeComboBox();
89 int activeScheme = settings.value(
"/Windows/ColorDialog/activeScheme", 0 ).toInt();
90 activeScheme = activeScheme >= mSchemeComboBox->count() ? 0 : activeScheme;
92 mSchemeList->setScheme( schemeList.at( activeScheme ) );
93 mSchemeComboBox->setCurrentIndex( activeScheme );
94 mActionImportColors->setEnabled( schemeList.at( activeScheme )->isEditable() );
95 mActionPasteColors->setEnabled( schemeList.at( activeScheme )->isEditable() );
96 mAddColorToSchemeButton->setEnabled( schemeList.at( activeScheme )->isEditable() );
97 mRemoveColorsFromSchemeButton->setEnabled( schemeList.at( activeScheme )->isEditable() );
99 mActionRemovePalette->setEnabled( userScheme ?
true :
false );
102 connect( mSchemeList->selectionModel(), SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ),
this, SLOT( listSelectionChanged( QItemSelection, QItemSelection ) ) );
104 mActionCopyColors->setEnabled(
false );
106 connect( mActionCopyColors, SIGNAL( triggered() ), mSchemeList, SLOT( copyColors() ) );
107 connect( mActionPasteColors, SIGNAL( triggered() ), mSchemeList, SLOT( pasteColors() ) );
108 connect( mActionExportColors, SIGNAL( triggered() ),
this, SLOT( exportColors() ) );
109 connect( mActionImportColors, SIGNAL( triggered() ),
this, SLOT( importColors() ) );
110 connect( mActionImportPalette, SIGNAL( triggered() ),
this, SLOT( importPalette() ) );
111 connect( mActionRemovePalette, SIGNAL( triggered() ),
this, SLOT( removePalette() ) );
112 connect( mActionNewPalette, SIGNAL( triggered() ),
this, SLOT( newPalette() ) );
113 connect( mRemoveColorsFromSchemeButton, SIGNAL( clicked() ), mSchemeList, SLOT( removeSelection() ) );
115 QMenu* schemeMenu =
new QMenu( mSchemeToolButton );
116 schemeMenu->addAction( mActionCopyColors );
117 schemeMenu->addAction( mActionPasteColors );
118 schemeMenu->addSeparator();
119 schemeMenu->addAction( mActionImportColors );
120 schemeMenu->addAction( mActionExportColors );
121 schemeMenu->addSeparator();
122 schemeMenu->addAction( mActionNewPalette );
123 schemeMenu->addAction( mActionImportPalette );
124 schemeMenu->addAction( mActionRemovePalette );
125 mSchemeToolButton->setMenu( schemeMenu );
127 connect( mSchemeComboBox, SIGNAL( currentIndexChanged(
int ) ),
this, SLOT( schemeIndexChanged(
int ) ) );
128 connect( mSchemeList, SIGNAL( colorSelected( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
130 if ( mPreviousColor.isValid() )
132 QPushButton* resetButton =
new QPushButton(
tr(
"Reset" ) );
133 mButtonBox->addButton( resetButton, QDialogButtonBox::ResetRole );
137 mOldColorLabel->hide();
141 mVerticalRamp->setInteriorMargin( 2 );
142 mVerticalRamp->setShowFrame(
true );
152 mSwatchButton1->setShowMenu(
false );
154 mSwatchButton2->setShowMenu(
false );
156 mSwatchButton3->setShowMenu(
false );
158 mSwatchButton4->setShowMenu(
false );
160 mSwatchButton5->setShowMenu(
false );
162 mSwatchButton6->setShowMenu(
false );
164 mSwatchButton7->setShowMenu(
false );
166 mSwatchButton8->setShowMenu(
false );
168 mSwatchButton9->setShowMenu(
false );
170 mSwatchButton10->setShowMenu(
false );
172 mSwatchButton11->setShowMenu(
false );
174 mSwatchButton12->setShowMenu(
false );
176 mSwatchButton13->setShowMenu(
false );
178 mSwatchButton14->setShowMenu(
false );
180 mSwatchButton15->setShowMenu(
false );
182 mSwatchButton16->setShowMenu(
false );
185 mSwatchButton1->setColor( settings.value(
"/Windows/ColorDialog/customColor1", QVariant( QColor() ) ).value<QColor>() );
186 mSwatchButton2->setColor( settings.value(
"/Windows/ColorDialog/customColor2", QVariant( QColor() ) ).value<QColor>() );
187 mSwatchButton3->setColor( settings.value(
"/Windows/ColorDialog/customColor3", QVariant( QColor() ) ).value<QColor>() );
188 mSwatchButton4->setColor( settings.value(
"/Windows/ColorDialog/customColor4", QVariant( QColor() ) ).value<QColor>() );
189 mSwatchButton5->setColor( settings.value(
"/Windows/ColorDialog/customColor5", QVariant( QColor() ) ).value<QColor>() );
190 mSwatchButton6->setColor( settings.value(
"/Windows/ColorDialog/customColor6", QVariant( QColor() ) ).value<QColor>() );
191 mSwatchButton7->setColor( settings.value(
"/Windows/ColorDialog/customColor7", QVariant( QColor() ) ).value<QColor>() );
192 mSwatchButton8->setColor( settings.value(
"/Windows/ColorDialog/customColor8", QVariant( QColor() ) ).value<QColor>() );
193 mSwatchButton9->setColor( settings.value(
"/Windows/ColorDialog/customColor9", QVariant( QColor() ) ).value<QColor>() );
194 mSwatchButton10->setColor( settings.value(
"/Windows/ColorDialog/customColor10", QVariant( QColor() ) ).value<QColor>() );
195 mSwatchButton11->setColor( settings.value(
"/Windows/ColorDialog/customColor11", QVariant( QColor() ) ).value<QColor>() );
196 mSwatchButton12->setColor( settings.value(
"/Windows/ColorDialog/customColor12", QVariant( QColor() ) ).value<QColor>() );
197 mSwatchButton13->setColor( settings.value(
"/Windows/ColorDialog/customColor13", QVariant( QColor() ) ).value<QColor>() );
198 mSwatchButton14->setColor( settings.value(
"/Windows/ColorDialog/customColor14", QVariant( QColor() ) ).value<QColor>() );
199 mSwatchButton15->setColor( settings.value(
"/Windows/ColorDialog/customColor15", QVariant( QColor() ) ).value<QColor>() );
200 mSwatchButton16->setColor( settings.value(
"/Windows/ColorDialog/customColor16", QVariant( QColor() ) ).value<QColor>() );
203 mSpinBoxRadius->setValue( settings.value(
"/Windows/ColorDialog/sampleRadius", 1 ).toInt() );
204 mSamplePreview->setColor( QColor() );
206 if ( color.isValid() )
209 mColorPreview->setColor2( color );
213 int activeRadio = settings.value(
"/Windows/ColorDialog/activeComponent", 2 ).toInt();
214 switch ( activeRadio )
217 mHueRadio->setChecked(
true );
220 mSaturationRadio->setChecked(
true );
223 mValueRadio->setChecked(
true );
226 mRedRadio->setChecked(
true );
229 mGreenRadio->setChecked(
true );
232 mBlueRadio->setChecked(
true );
235 int currentTab = settings.value(
"/Windows/ColorDialog/activeTab", 0 ).toInt();
236 mTabWidget->setCurrentIndex( currentTab );
242 mTabWidget->removeTab( 3 );
246 connect( mColorBox, SIGNAL( colorChanged( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
247 connect( mColorWheel, SIGNAL( colorChanged( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
248 connect( mColorText, SIGNAL( colorChanged( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
249 connect( mVerticalRamp, SIGNAL( colorChanged( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
250 connect( mRedSlider, SIGNAL( colorChanged( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
251 connect( mGreenSlider, SIGNAL( colorChanged( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
252 connect( mBlueSlider, SIGNAL( colorChanged( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
253 connect( mHueSlider, SIGNAL( colorChanged( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
254 connect( mValueSlider, SIGNAL( colorChanged( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
255 connect( mSaturationSlider, SIGNAL( colorChanged( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
256 connect( mAlphaSlider, SIGNAL( colorChanged( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
257 connect( mColorPreview, SIGNAL( colorChanged( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
258 connect( mSwatchButton1, SIGNAL( colorClicked( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
259 connect( mSwatchButton2, SIGNAL( colorClicked( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
260 connect( mSwatchButton3, SIGNAL( colorClicked( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
261 connect( mSwatchButton4, SIGNAL( colorClicked( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
262 connect( mSwatchButton5, SIGNAL( colorClicked( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
263 connect( mSwatchButton6, SIGNAL( colorClicked( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
264 connect( mSwatchButton7, SIGNAL( colorClicked( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
265 connect( mSwatchButton8, SIGNAL( colorClicked( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
266 connect( mSwatchButton9, SIGNAL( colorClicked( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
267 connect( mSwatchButton10, SIGNAL( colorClicked( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
268 connect( mSwatchButton11, SIGNAL( colorClicked( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
269 connect( mSwatchButton12, SIGNAL( colorClicked( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
270 connect( mSwatchButton13, SIGNAL( colorClicked( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
271 connect( mSwatchButton14, SIGNAL( colorClicked( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
272 connect( mSwatchButton15, SIGNAL( colorClicked( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
273 connect( mSwatchButton16, SIGNAL( colorClicked( QColor ) ),
this, SLOT(
setColor( QColor ) ) );
285 return mColorPreview->color();
290 setWindowTitle( title.isEmpty() ?
tr(
"Select Color" ) : title );
295 mAllowAlpha = allowAlpha;
296 mAlphaLabel->setVisible( allowAlpha );
297 mAlphaSlider->setVisible( allowAlpha );
300 mAlphaLayout->setContentsMargins( 0, 0, 0, 0 );
301 mAlphaLayout->setSpacing( 0 );
305 QColor
QgsColorDialogV2::getLiveColor(
const QColor &initialColor, QObject *updateObject,
const char *updateSlot, QWidget *parent,
const QString &title,
const bool allowAlpha )
307 QColor returnColor( initialColor );
309 liveDialog->setWindowTitle( title.isEmpty() ?
tr(
"Select Color" ) : title );
316 updateObject, updateSlot );
318 if ( liveDialog->exec() )
320 returnColor = liveDialog->
color();
330 QString dialogTitle = title.isEmpty() ?
tr(
"Select Color" ) : title;
334 bool useNative = settings.value(
"/qgis/native_color_dialogs",
false ).toBool();
337 return QColorDialog::getColor( initialColor, parent, dialogTitle, allowAlpha ? QColorDialog::ShowAlphaChannel : ( QColorDialog::ColorDialogOption )0 );
342 dialog->setWindowTitle( dialogTitle );
346 if ( dialog->exec() )
348 result = dialog->
color();
359 void QgsColorDialogV2::on_mButtonBox_accepted()
365 void QgsColorDialogV2::on_mButtonBox_rejected()
371 void QgsColorDialogV2::on_mButtonBox_clicked( QAbstractButton * button )
373 if ( mButtonBox->buttonRole( button ) == QDialogButtonBox::ResetRole && mPreviousColor.isValid() )
379 void QgsColorDialogV2::importColors()
382 QString lastDir = s.value(
"/UI/lastGplPaletteDir",
"" ).toString();
383 QString filePath = QFileDialog::getOpenFileName(
this,
tr(
"Select palette file" ), lastDir,
"GPL (*.gpl);;All files (*.*)" );
385 if ( filePath.isEmpty() )
391 QFileInfo fileInfo( filePath );
392 if ( !fileInfo.exists() || !fileInfo.isReadable() )
394 QMessageBox::critical( 0,
tr(
"Invalid file" ),
tr(
"Error, file does not exist or is not readable" ) );
398 s.setValue(
"/UI/lastGplPaletteDir", fileInfo.absolutePath() );
399 QFile
file( filePath );
400 bool importOk = mSchemeList->importColorsFromGpl(
file );
403 QMessageBox::critical( 0,
tr(
"Invalid file" ),
tr(
"Error, no colors found in palette file" ) );
408 void QgsColorDialogV2::refreshSchemeComboBox()
410 mSchemeComboBox->blockSignals(
true );
411 mSchemeComboBox->clear();
413 QList<QgsColorScheme *>::const_iterator schemeIt = schemeList.constBegin();
414 for ( ; schemeIt != schemeList.constEnd(); ++schemeIt )
416 mSchemeComboBox->addItem(( *schemeIt )->schemeName() );
418 mSchemeComboBox->blockSignals(
false );
421 void QgsColorDialogV2::importPalette()
424 QString lastDir = s.value(
"/UI/lastGplPaletteDir",
"" ).toString();
425 QString filePath = QFileDialog::getOpenFileName(
this,
tr(
"Select palette file" ), lastDir,
"GPL (*.gpl);;All files (*.*)" );
427 if ( filePath.isEmpty() )
433 QFileInfo fileInfo( filePath );
434 if ( !fileInfo.exists() || !fileInfo.isReadable() )
436 QMessageBox::critical( 0,
tr(
"Invalid file" ),
tr(
"Error, file does not exist or is not readable" ) );
440 s.setValue(
"/UI/lastGplPaletteDir", fileInfo.absolutePath() );
441 QFile
file( filePath );
449 QMessageBox::critical( 0,
tr(
"Invalid file" ),
tr(
"Palette file is not readable" ) );
453 if ( importedColors.length() == 0 )
456 QMessageBox::critical( 0,
tr(
"Invalid file" ),
tr(
"No colors found in palette file" ) );
462 importedScheme->
setName( paletteName );
463 importedScheme->
setColors( importedColors );
468 refreshSchemeComboBox();
469 mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );
472 void QgsColorDialogV2::removePalette()
476 int prevIndex = mSchemeComboBox->currentIndex();
477 if ( prevIndex >= schemeList.length() )
489 if ( QMessageBox::question(
this,
tr(
"Remove Color Palette" ),
490 QString(
tr(
"Are you sure you want to remove %1?" ) ).arg( userScheme->
schemeName() ),
491 QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
498 if ( !userScheme->
erase() )
506 refreshSchemeComboBox();
507 prevIndex = qMax( qMin( prevIndex, mSchemeComboBox->count() - 1 ), 0 );
508 mSchemeComboBox->setCurrentIndex( prevIndex );
511 void QgsColorDialogV2::newPalette()
514 QString name = QInputDialog::getText(
this,
tr(
"Create New Palette" ),
tr(
"Enter a name for the new palette:" ),
515 QLineEdit::Normal,
tr(
"New palette" ), &ok );
517 if ( !ok || name.isEmpty() )
524 QDir palettePath( gplFilePath() );
525 QRegExp badChars(
"[,^@={}\\[\\]~!?:&*\"|#%<>$\"'();`' /\\\\]" );
526 QString filename = name.simplified().toLower().replace( badChars, QString(
"_" ) );
527 if ( filename.isEmpty() )
529 filename =
tr(
"new_palette" );
531 QFileInfo destFileInfo( palettePath.filePath( filename +
".gpl" ) );
533 while ( destFileInfo.exists() )
536 destFileInfo = QFileInfo( palettePath.filePath( filename + QString(
"%1.gpl" ).arg( fileNumber ) ) );
546 refreshSchemeComboBox();
547 mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );
550 QString QgsColorDialogV2::gplFilePath()
555 if ( !localDir.mkpath( palettesDir ) )
563 void QgsColorDialogV2::exportColors()
566 QString lastDir = s.value(
"/UI/lastGplPaletteDir",
"" ).toString();
567 QString fileName = QFileDialog::getSaveFileName(
this,
tr(
"Palette file" ), lastDir,
"GPL (*.gpl)" );
569 if ( fileName.isEmpty() )
575 if ( !fileName.toLower().endsWith(
".gpl" ) )
580 QFileInfo fileInfo( fileName );
581 s.setValue(
"/UI/lastGplPaletteDir", fileInfo.absolutePath() );
583 QFile
file( fileName );
584 bool exportOk = mSchemeList->exportColorsToGpl(
file );
587 QMessageBox::critical( 0,
tr(
"Error exporting" ),
tr(
"Error writing palette file" ) );
592 void QgsColorDialogV2::schemeIndexChanged(
int index )
595 if ( mSchemeList->isDirty() )
597 mSchemeList->saveColorsToScheme();
602 if ( index >= schemeList.length() )
608 mSchemeList->setScheme( scheme );
609 mActionImportColors->setEnabled( scheme->
isEditable() );
610 mActionPasteColors->setEnabled( scheme->
isEditable() );
611 mAddColorToSchemeButton->setEnabled( scheme->
isEditable() );
612 mRemoveColorsFromSchemeButton->setEnabled( scheme->
isEditable() );
614 mActionRemovePalette->setEnabled( userScheme ?
true :
false );
617 mActionCopyColors->setEnabled(
false );
620 void QgsColorDialogV2::listSelectionChanged(
const QItemSelection &selected,
const QItemSelection &deselected )
622 Q_UNUSED( deselected );
623 mActionCopyColors->setEnabled( selected.length() > 0 );
626 void QgsColorDialogV2::on_mAddCustomColorButton_clicked()
628 switch ( mLastCustomColorIndex )
631 mSwatchButton1->setColor( mColorPreview->color() );
634 mSwatchButton2->setColor( mColorPreview->color() );
637 mSwatchButton3->setColor( mColorPreview->color() );
640 mSwatchButton4->setColor( mColorPreview->color() );
643 mSwatchButton5->setColor( mColorPreview->color() );
646 mSwatchButton6->setColor( mColorPreview->color() );
649 mSwatchButton7->setColor( mColorPreview->color() );
652 mSwatchButton8->setColor( mColorPreview->color() );
655 mSwatchButton9->setColor( mColorPreview->color() );
658 mSwatchButton10->setColor( mColorPreview->color() );
661 mSwatchButton11->setColor( mColorPreview->color() );
664 mSwatchButton12->setColor( mColorPreview->color() );
667 mSwatchButton13->setColor( mColorPreview->color() );
670 mSwatchButton14->setColor( mColorPreview->color() );
673 mSwatchButton15->setColor( mColorPreview->color() );
676 mSwatchButton16->setColor( mColorPreview->color() );
679 mLastCustomColorIndex++;
680 if ( mLastCustomColorIndex >= 16 )
682 mLastCustomColorIndex = 0;
686 void QgsColorDialogV2::on_mSampleButton_clicked()
689 QPixmap samplerPixmap = QPixmap((
const char ** )
sampler_cursor );
690 setCursor( QCursor( samplerPixmap, 0, 0 ) );
693 mPickingColor =
true;
694 setMouseTracking(
true );
697 void QgsColorDialogV2::on_mTabWidget_currentChanged(
int index )
700 bool enabled = index == 0;
701 mRedRadio->setEnabled( enabled );
702 mBlueRadio->setEnabled( enabled );
703 mGreenRadio->setEnabled( enabled );
704 mHueRadio->setEnabled( enabled );
705 mSaturationRadio->setEnabled( enabled );
706 mValueRadio->setEnabled( enabled );
709 void QgsColorDialogV2::saveSettings()
712 if ( mSchemeList->isDirty() )
714 mSchemeList->saveColorsToScheme();
718 settings.setValue(
"/Windows/ColorDialog/geometry", saveGeometry() );
722 if ( mHueRadio->isChecked() )
724 if ( mSaturationRadio->isChecked() )
726 if ( mValueRadio->isChecked() )
728 if ( mRedRadio->isChecked() )
730 if ( mGreenRadio->isChecked() )
732 if ( mBlueRadio->isChecked() )
734 settings.setValue(
"/Windows/ColorDialog/activeComponent", activeRadio );
737 settings.setValue(
"/Windows/ColorDialog/activeScheme", mSchemeComboBox->currentIndex() );
740 settings.setValue(
"/Windows/ColorDialog/activeTab", mTabWidget->currentIndex() );
743 settings.setValue(
"/Windows/ColorDialog/customColor1", QVariant( mSwatchButton1->color() ) );
744 settings.setValue(
"/Windows/ColorDialog/customColor2", QVariant( mSwatchButton2->color() ) );
745 settings.setValue(
"/Windows/ColorDialog/customColor3", QVariant( mSwatchButton3->color() ) );
746 settings.setValue(
"/Windows/ColorDialog/customColor4", QVariant( mSwatchButton4->color() ) );
747 settings.setValue(
"/Windows/ColorDialog/customColor5", QVariant( mSwatchButton5->color() ) );
748 settings.setValue(
"/Windows/ColorDialog/customColor6", QVariant( mSwatchButton6->color() ) );
749 settings.setValue(
"/Windows/ColorDialog/customColor7", QVariant( mSwatchButton7->color() ) );
750 settings.setValue(
"/Windows/ColorDialog/customColor8", QVariant( mSwatchButton8->color() ) );
751 settings.setValue(
"/Windows/ColorDialog/customColor9", QVariant( mSwatchButton9->color() ) );
752 settings.setValue(
"/Windows/ColorDialog/customColor10", QVariant( mSwatchButton10->color() ) );
753 settings.setValue(
"/Windows/ColorDialog/customColor11", QVariant( mSwatchButton11->color() ) );
754 settings.setValue(
"/Windows/ColorDialog/customColor12", QVariant( mSwatchButton12->color() ) );
755 settings.setValue(
"/Windows/ColorDialog/customColor13", QVariant( mSwatchButton13->color() ) );
756 settings.setValue(
"/Windows/ColorDialog/customColor14", QVariant( mSwatchButton14->color() ) );
757 settings.setValue(
"/Windows/ColorDialog/customColor15", QVariant( mSwatchButton15->color() ) );
758 settings.setValue(
"/Windows/ColorDialog/customColor16", QVariant( mSwatchButton16->color() ) );
761 settings.setValue(
"/Windows/ColorDialog/sampleRadius", mSpinBoxRadius->value() );
764 void QgsColorDialogV2::stopPicking(
const QPoint &eventPos,
const bool takeSample )
770 setMouseTracking(
false );
771 mPickingColor =
false;
780 QColor snappedColor = sampleColor( eventPos );
781 mSamplePreview->setColor( snappedColor );
782 mColorPreview->setColor( snappedColor,
true );
787 if ( !color.isValid() )
792 QColor fixedColor = QColor( color );
796 fixedColor.setAlpha( 255 );
798 QList<QgsColorWidget*> colorWidgets = this->findChildren<QgsColorWidget *>();
801 if ( widget == mSamplePreview )
805 widget->blockSignals(
true );
807 widget->blockSignals(
false );
830 QColor QgsColorDialogV2::averageColor(
const QImage &image )
const
838 for (
int heightIndex = 0; heightIndex < image.height(); ++heightIndex )
840 QRgb* scanLine = ( QRgb* )image.constScanLine( heightIndex );
841 for (
int widthIndex = 0; widthIndex < image.width(); ++widthIndex )
843 tmpRgb = scanLine[widthIndex];
844 sumRed += qRed( tmpRgb );
845 sumBlue += qBlue( tmpRgb );
846 sumGreen += qGreen( tmpRgb );
851 double avgRed = ( double )sumRed / ( 255.0 * colorCount );
852 double avgGreen = ( double )sumGreen / ( 255.0 * colorCount );
853 double avgBlue = ( double )sumBlue / ( 255.0 * colorCount );
856 return QColor::fromRgbF( avgRed, avgGreen, avgBlue );
859 QColor QgsColorDialogV2::sampleColor(
const QPoint &point )
const
861 int sampleRadius = mSpinBoxRadius->value() - 1;
862 QPixmap snappedPixmap = QPixmap::grabWindow( QApplication::desktop()->winId(), point.x() - sampleRadius, point.y() - sampleRadius,
863 1 + sampleRadius * 2, 1 + sampleRadius * 2 );
864 QImage snappedImage = snappedPixmap.toImage();
866 return averageColor( snappedImage );
875 QColor hoverColor = sampleColor( e->globalPos() );
876 mSamplePreview->setColor( hoverColor );
890 stopPicking( e->globalPos() );
900 if ( !mPickingColor )
908 stopPicking( QCursor::pos(), e->key() == Qt::Key_Space );
911 void QgsColorDialogV2::on_mHueRadio_toggled(
bool checked )
920 void QgsColorDialogV2::on_mSaturationRadio_toggled(
bool checked )
929 void QgsColorDialogV2::on_mValueRadio_toggled(
bool checked )
938 void QgsColorDialogV2::on_mRedRadio_toggled(
bool checked )
947 void QgsColorDialogV2::on_mGreenRadio_toggled(
bool checked )
956 void QgsColorDialogV2::on_mBlueRadio_toggled(
bool checked )
965 void QgsColorDialogV2::on_mAddColorToSchemeButton_clicked()