24 #include <QPushButton> 26 #include <QToolButton> 27 #include <QFileDialog> 28 #include <QMessageBox> 29 #include <QDesktopWidget> 30 #include <QMouseEvent> 31 #include <QInputDialog> 46 QColor returnColor( initialColor );
51 connect( liveDialog, SIGNAL( currentColorChanged(
const QColor& ) ),
52 updateObject, updateSlot );
54 if ( liveDialog->
exec() )
71 , mPreviousColor( color )
73 , mLastCustomColorIndex( 0 )
74 , mPickingColor( false )
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 );
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 );
119 schemeMenu->
addAction( mActionImportColors );
120 schemeMenu->
addAction( mActionExportColors );
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 ) ) );
130 if ( mPreviousColor.
isValid() )
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 );
203 mSpinBoxRadius->setValue( settings.
value(
"/Windows/ColorDialog/sampleRadius", 1 ).
toInt() );
204 mSamplePreview->setColor(
QColor() );
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 );
285 return mColorPreview->color();
295 mAllowAlpha = allowAlpha;
296 mAlphaLabel->setVisible( allowAlpha );
297 mAlphaSlider->setVisible( allowAlpha );
300 mAlphaLayout->setContentsMargins( 0, 0, 0, 0 );
301 mAlphaLayout->setSpacing( 0 );
307 QColor returnColor( initialColor );
316 updateObject, updateSlot );
318 if ( liveDialog->
exec() )
320 returnColor = liveDialog->
color();
323 liveDialog =
nullptr;
334 bool useNative = settings.
value(
"/qgis/native_color_dialogs",
false ).
toBool();
337 return QColorDialog::getColor( initialColor, parent, dialogTitle, allowAlpha ? QColorDialog::ShowAlphaChannel : ( QColorDialog::ColorDialogOption )0 );
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()
399 QFile file( filePath );
400 bool importOk = mSchemeList->importColorsFromGpl( file );
408 void QgsColorDialogV2::refreshSchemeComboBox()
410 mSchemeComboBox->blockSignals(
true );
411 mSchemeComboBox->clear();
414 for ( ; schemeIt != schemeList.
constEnd(); ++schemeIt )
416 mSchemeComboBox->addItem(( *schemeIt )->schemeName() );
418 mSchemeComboBox->blockSignals(
false );
421 void QgsColorDialogV2::importPalette()
441 QFile file( filePath );
453 if ( importedColors.
length() == 0 )
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() )
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()
515 QLineEdit::Normal,
tr(
"New palette" ), &ok );
524 QDir palettePath( gplFilePath() );
525 QRegExp badChars(
"[,^@={}\\[\\]~!?:&*\"|#%<>$\"'();`' /\\\\]" );
529 filename =
tr(
"new_palette" );
533 while ( destFileInfo.exists() )
546 refreshSchemeComboBox();
547 mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );
550 QString QgsColorDialogV2::gplFilePath()
555 if ( !localDir.
mkpath( palettesDir ) )
563 void QgsColorDialogV2::exportColors()
575 if ( !fileName.
endsWith(
".gpl", Qt::CaseInsensitive ) )
583 QFile file( fileName );
584 bool exportOk = mSchemeList->exportColorsToGpl( 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 );
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()
693 mPickingColor =
true;
697 void QgsColorDialogV2::on_mTabWidget_currentChanged(
int index )
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();
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(
QPoint eventPos,
const bool takeSample )
771 mPickingColor =
false;
780 QColor snappedColor = sampleColor( eventPos );
781 mSamplePreview->setColor( snappedColor );
782 mColorPreview->setColor( snappedColor,
true );
801 if ( widget == mSamplePreview )
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 );
859 QColor QgsColorDialogV2::sampleColor(
QPoint point )
const 861 int sampleRadius = mSpinBoxRadius->
value() - 1;
863 1 + sampleRadius * 2, 1 + sampleRadius * 2 );
866 return averageColor( snappedImage );
876 mSamplePreview->setColor( hoverColor );
900 if ( !mPickingColor )
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()
A color scheme which stores its colors in a gpl palette file within the "palettes" subfolder off the ...
void setOptions(QFlags< QColorDialog::ColorDialogOption > options)
void setName(const QString &name)
Sets the name for the scheme.
QByteArray toByteArray() const
virtual void closeEvent(QCloseEvent *e)
QgsColorDialogV2(QWidget *parent=nullptr, const Qt::WindowFlags &fl=QgisGui::ModalDialogFlags, const QColor &color=QColor())
Create a new color picker dialog.
bool erase()
Erases the associated gpl palette file from the users "palettes" folder.
void keyPressEvent(QKeyEvent *e) override
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user's home dir.
const uchar * constScanLine(int i) const
A custom QGIS dialog for selecting a color.
Abstract base class for color schemes.
virtual bool setColors(const QgsNamedColorList &colors, const QString &context=QString(), const QColor &baseColor=QColor()) override
Sets the colors for the scheme.
void currentColorChanged(const QColor &color)
Emitted when the dialog's color changes.
const T & at(int i) const
QString simplified() const
QString filePath(const QString &fileName) const
QPixmap grabWindow(WId window, int x, int y, int width, int height)
void mousePressEvent(QMouseEvent *e) override
static QString colorToName(const QColor &color)
Returns a friendly display name for a color.
bool removeColorScheme(QgsColorScheme *scheme)
Removes all matching color schemes from the registry.
void mouseReleaseEvent(QMouseEvent *e) override
QString tr(const char *sourceText, const char *disambiguation, int n)
void setValue(const QString &key, const QVariant &value)
void setAllowAlpha(const bool allowAlpha)
Sets whether alpha modification (transparency) is permitted for the color dialog. ...
const char * name() const
const QPoint & globalPos() const
virtual bool isEditable() const
Returns whether the color scheme is editable.
int toInt(bool *ok) const
StandardButton question(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
QColor color() const
Returns the current color for the dialog.
static QgsNamedColorList importColorsFromGpl(QFile &file, bool &ok, QString &name)
Imports colors from a gpl GIMP palette file.
typedef ColorDialogOptions
void setTitle(const QString &title)
Sets the title for the color dialog.
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
void addColorScheme(QgsColorScheme *scheme)
Adds a color scheme to the registry.
QString getText(QWidget *parent, const QString &title, const QString &label, QLineEdit::EchoMode mode, const QString &text, bool *ok, QFlags< Qt::WindowType > flags, QFlags< Qt::InputMethodHint > inputMethodHints)
bool blockSignals(bool block)
QColor fromRgbF(qreal r, qreal g, qreal b, qreal a)
QColor getColor(const QColor &initial, QWidget *parent, const QString &title, QFlags< QColorDialog::ColorDialogOption > options)
void closeEvent(QCloseEvent *e) override
static QgsColorSchemeRegistry * instance()
Returns the global instance pointer, creating the object on the first call.
virtual void keyPressEvent(QKeyEvent *e)
const char * sampler_cursor[]
QString & replace(int position, int n, QChar after)
QVariant value(const QString &key, const QVariant &defaultValue) const
static QColor getColor(const QColor &initialColor, QWidget *parent, const QString &title=QString(), const bool allowAlpha=false)
Return a color selection from a color dialog.
QDesktopWidget * desktop()
virtual QString schemeName() const override
Gets the name for the color scheme.
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
StandardButton critical(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
void mouseMoveEvent(QMouseEvent *e) override
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFlags< QFileDialog::Option > options)
const_iterator constEnd() const
const_iterator constBegin() const
QString absolutePath() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setColor(const QColor &color)
Sets the current color for the dialog.
QList< QgsColorScheme * > schemes() const
Returns all color schemes in the registry.
static QColor getLiveColor(const QColor &initialColor, QObject *updateObject, const char *updateSlot, QWidget *parent=nullptr, const QString &title="", const QColorDialog::ColorDialogOptions &options=nullptr)
Return a color selection from a QColorDialog, with live updating of interim selections.
static QColor getLiveColor(const QColor &initialColor, QObject *updateObject, const char *updateSlot, QWidget *parent=nullptr, const QString &title=QString(), const bool allowAlpha=true)
Return a color selection from a color dialog, with live updating of interim selections.
bool mkpath(const QString &dirPath) const