23 #include <QPushButton> 25 #include <QToolButton> 26 #include <QFileDialog> 27 #include <QMessageBox> 28 #include <QDesktopWidget> 29 #include <QMouseEvent> 30 #include <QInputDialog> 31 #include <QVBoxLayout> 36 , mLastCustomColorIndex( 0 )
37 , mPickingColor( false )
57 mSchemeList->header()->hide();
58 mSchemeList->setColumnWidth( 0, 44 );
61 refreshSchemeComboBox();
65 int activeScheme = settings.
value(
"/Windows/ColorDialog/activeScheme", 0 ).
toInt();
66 activeScheme = activeScheme >= mSchemeComboBox->count() ? 0 : activeScheme;
68 mSchemeList->setScheme( schemeList.
at( activeScheme ) );
70 mSchemeComboBox->setCurrentIndex( activeScheme );
71 updateActionsForCurrentScheme();
76 mActionCopyColors->setEnabled(
false );
78 connect( mActionCopyColors, SIGNAL( triggered() ), mSchemeList, SLOT( copyColors() ) );
79 connect( mActionPasteColors, SIGNAL( triggered() ), mSchemeList, SLOT( pasteColors() ) );
80 connect( mActionExportColors, SIGNAL( triggered() ),
this, SLOT( exportColors() ) );
81 connect( mActionImportColors, SIGNAL( triggered() ),
this, SLOT( importColors() ) );
82 connect( mActionImportPalette, SIGNAL( triggered() ),
this, SLOT( importPalette() ) );
83 connect( mActionRemovePalette, SIGNAL( triggered() ),
this, SLOT( removePalette() ) );
84 connect( mActionNewPalette, SIGNAL( triggered() ),
this, SLOT( newPalette() ) );
85 connect( mRemoveColorsFromSchemeButton, SIGNAL( clicked() ), mSchemeList, SLOT( removeSelection() ) );
87 QMenu* schemeMenu =
new QMenu( mSchemeToolButton );
88 schemeMenu->
addAction( mActionCopyColors );
89 schemeMenu->
addAction( mActionPasteColors );
91 schemeMenu->
addAction( mActionImportColors );
92 schemeMenu->
addAction( mActionExportColors );
94 schemeMenu->
addAction( mActionNewPalette );
95 schemeMenu->
addAction( mActionImportPalette );
96 schemeMenu->
addAction( mActionRemovePalette );
97 schemeMenu->
addAction( mActionShowInButtons );
98 mSchemeToolButton->setMenu( schemeMenu );
100 connect( mSchemeComboBox, SIGNAL( currentIndexChanged(
int ) ),
this, SLOT( schemeIndexChanged(
int ) ) );
103 mOldColorLabel->hide();
106 mVerticalRamp->setInteriorMargin( 2 );
107 mVerticalRamp->setShowFrame(
true );
117 mSwatchButton1->setShowMenu(
false );
119 mSwatchButton2->setShowMenu(
false );
121 mSwatchButton3->setShowMenu(
false );
123 mSwatchButton4->setShowMenu(
false );
125 mSwatchButton5->setShowMenu(
false );
127 mSwatchButton6->setShowMenu(
false );
129 mSwatchButton7->setShowMenu(
false );
131 mSwatchButton8->setShowMenu(
false );
133 mSwatchButton9->setShowMenu(
false );
135 mSwatchButton10->setShowMenu(
false );
137 mSwatchButton11->setShowMenu(
false );
139 mSwatchButton12->setShowMenu(
false );
141 mSwatchButton13->setShowMenu(
false );
143 mSwatchButton14->setShowMenu(
false );
145 mSwatchButton15->setShowMenu(
false );
147 mSwatchButton16->setShowMenu(
false );
168 mSpinBoxRadius->setValue( settings.
value(
"/Windows/ColorDialog/sampleRadius", 1 ).
toInt() );
169 mSamplePreview->setColor(
QColor() );
177 int activeRadio = settings.
value(
"/Windows/ColorDialog/activeComponent", 2 ).
toInt();
178 switch ( activeRadio )
181 mHueRadio->setChecked(
true );
184 mSaturationRadio->setChecked(
true );
187 mValueRadio->setChecked(
true );
190 mRedRadio->setChecked(
true );
193 mGreenRadio->setChecked(
true );
196 mBlueRadio->setChecked(
true );
199 int currentTab = settings.
value(
"/Windows/ColorDialog/activeTab", 0 ).
toInt();
200 mTabWidget->setCurrentIndex( currentTab );
206 mTabWidget->removeTab( 3 );
249 return mColorPreview->color();
254 mAllowAlpha = allowAlpha;
255 mAlphaLabel->setVisible( allowAlpha );
256 mAlphaSlider->setVisible( allowAlpha );
259 mAlphaLayout->setContentsMargins( 0, 0, 0, 0 );
260 mAlphaLayout->setSpacing( 0 );
264 void QgsCompoundColorWidget::importColors()
284 QFile file( filePath );
285 bool importOk = mSchemeList->importColorsFromGpl( file );
293 void QgsCompoundColorWidget::refreshSchemeComboBox()
295 mSchemeComboBox->blockSignals(
true );
296 mSchemeComboBox->clear();
299 for ( ; schemeIt != schemeList.
constEnd(); ++schemeIt )
301 mSchemeComboBox->addItem(( *schemeIt )->schemeName() );
303 mSchemeComboBox->blockSignals(
false );
306 void QgsCompoundColorWidget::importPalette()
326 QFile file( filePath );
338 if ( importedColors.
length() == 0 )
347 importedScheme->
setName( paletteName );
348 importedScheme->
setColors( importedColors );
353 refreshSchemeComboBox();
354 mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );
357 void QgsCompoundColorWidget::removePalette()
361 int prevIndex = mSchemeComboBox->currentIndex();
362 if ( prevIndex >= schemeList.
length() )
376 QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
383 if ( !userScheme->
erase() )
391 refreshSchemeComboBox();
392 prevIndex = qMax( qMin( prevIndex, mSchemeComboBox->count() - 1 ), 0 );
393 mSchemeComboBox->setCurrentIndex( prevIndex );
396 void QgsCompoundColorWidget::newPalette()
400 QLineEdit::Normal,
tr(
"New palette" ), &ok );
409 QDir palettePath( gplFilePath() );
410 QRegExp badChars(
"[,^@={}\\[\\]~!?:&*\"|#%<>$\"'();`' /\\\\]" );
414 filename =
tr(
"new_palette" );
418 while ( destFileInfo.exists() )
431 refreshSchemeComboBox();
432 mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );
435 QString QgsCompoundColorWidget::gplFilePath()
440 if ( !localDir.
mkpath( palettesDir ) )
448 void QgsCompoundColorWidget::exportColors()
460 if ( !fileName.
endsWith(
".gpl", Qt::CaseInsensitive ) )
468 QFile file( fileName );
469 bool exportOk = mSchemeList->exportColorsToGpl( file );
477 void QgsCompoundColorWidget::schemeIndexChanged(
int index )
480 if ( mSchemeList->isDirty() )
482 mSchemeList->saveColorsToScheme();
487 if ( index >= schemeList.
length() )
493 mSchemeList->setScheme( scheme );
495 updateActionsForCurrentScheme();
498 mActionCopyColors->setEnabled(
false );
503 Q_UNUSED( deselected );
504 mActionCopyColors->setEnabled( selected.
length() > 0 );
507 void QgsCompoundColorWidget::on_mAddCustomColorButton_clicked()
509 switch ( mLastCustomColorIndex )
512 mSwatchButton1->setColor( mColorPreview->color() );
515 mSwatchButton2->setColor( mColorPreview->color() );
518 mSwatchButton3->setColor( mColorPreview->color() );
521 mSwatchButton4->setColor( mColorPreview->color() );
524 mSwatchButton5->setColor( mColorPreview->color() );
527 mSwatchButton6->setColor( mColorPreview->color() );
530 mSwatchButton7->setColor( mColorPreview->color() );
533 mSwatchButton8->setColor( mColorPreview->color() );
536 mSwatchButton9->setColor( mColorPreview->color() );
539 mSwatchButton10->setColor( mColorPreview->color() );
542 mSwatchButton11->setColor( mColorPreview->color() );
545 mSwatchButton12->setColor( mColorPreview->color() );
548 mSwatchButton13->setColor( mColorPreview->color() );
551 mSwatchButton14->setColor( mColorPreview->color() );
554 mSwatchButton15->setColor( mColorPreview->color() );
557 mSwatchButton16->setColor( mColorPreview->color() );
560 mLastCustomColorIndex++;
561 if ( mLastCustomColorIndex >= 16 )
563 mLastCustomColorIndex = 0;
567 void QgsCompoundColorWidget::on_mSampleButton_clicked()
574 mPickingColor =
true;
578 void QgsCompoundColorWidget::on_mTabWidget_currentChanged(
int index )
582 mRedRadio->setEnabled( enabled );
583 mBlueRadio->setEnabled( enabled );
584 mGreenRadio->setEnabled( enabled );
585 mHueRadio->setEnabled( enabled );
586 mSaturationRadio->setEnabled( enabled );
587 mValueRadio->setEnabled( enabled );
590 void QgsCompoundColorWidget::on_mActionShowInButtons_toggled(
bool state )
599 void QgsCompoundColorWidget::saveSettings()
602 if ( mSchemeList->isDirty() )
604 mSchemeList->saveColorsToScheme();
611 if ( mHueRadio->isChecked() )
613 if ( mSaturationRadio->isChecked() )
615 if ( mValueRadio->isChecked() )
617 if ( mRedRadio->isChecked() )
619 if ( mGreenRadio->isChecked() )
621 if ( mBlueRadio->isChecked() )
623 settings.
setValue(
"/Windows/ColorDialog/activeComponent", activeRadio );
626 settings.
setValue(
"/Windows/ColorDialog/activeScheme", mSchemeComboBox->currentIndex() );
629 settings.
setValue(
"/Windows/ColorDialog/activeTab", mTabWidget->currentIndex() );
632 settings.
setValue(
"/Windows/ColorDialog/customColor1",
QVariant( mSwatchButton1->color() ) );
633 settings.
setValue(
"/Windows/ColorDialog/customColor2",
QVariant( mSwatchButton2->color() ) );
634 settings.
setValue(
"/Windows/ColorDialog/customColor3",
QVariant( mSwatchButton3->color() ) );
635 settings.
setValue(
"/Windows/ColorDialog/customColor4",
QVariant( mSwatchButton4->color() ) );
636 settings.
setValue(
"/Windows/ColorDialog/customColor5",
QVariant( mSwatchButton5->color() ) );
637 settings.
setValue(
"/Windows/ColorDialog/customColor6",
QVariant( mSwatchButton6->color() ) );
638 settings.
setValue(
"/Windows/ColorDialog/customColor7",
QVariant( mSwatchButton7->color() ) );
639 settings.
setValue(
"/Windows/ColorDialog/customColor8",
QVariant( mSwatchButton8->color() ) );
640 settings.
setValue(
"/Windows/ColorDialog/customColor9",
QVariant( mSwatchButton9->color() ) );
641 settings.
setValue(
"/Windows/ColorDialog/customColor10",
QVariant( mSwatchButton10->color() ) );
642 settings.
setValue(
"/Windows/ColorDialog/customColor11",
QVariant( mSwatchButton11->color() ) );
643 settings.
setValue(
"/Windows/ColorDialog/customColor12",
QVariant( mSwatchButton12->color() ) );
644 settings.
setValue(
"/Windows/ColorDialog/customColor13",
QVariant( mSwatchButton13->color() ) );
645 settings.
setValue(
"/Windows/ColorDialog/customColor14",
QVariant( mSwatchButton14->color() ) );
646 settings.
setValue(
"/Windows/ColorDialog/customColor15",
QVariant( mSwatchButton15->color() ) );
647 settings.
setValue(
"/Windows/ColorDialog/customColor16",
QVariant( mSwatchButton16->color() ) );
650 settings.
setValue(
"/Windows/ColorDialog/sampleRadius", mSpinBoxRadius->value() );
653 void QgsCompoundColorWidget::stopPicking(
QPoint eventPos,
const bool takeSample )
660 mPickingColor =
false;
669 QColor snappedColor = sampleColor( eventPos );
670 mSamplePreview->setColor( snappedColor );
671 mColorPreview->setColor( snappedColor,
true );
690 if ( widget == mSamplePreview )
703 mOldColorLabel->setVisible( color.
isValid() );
704 mColorPreview->setColor2( color );
719 QColor QgsCompoundColorWidget::averageColor(
const QImage &image )
const 727 for (
int heightIndex = 0; heightIndex < image.
height(); ++heightIndex )
729 QRgb* scanLine = ( QRgb* )image.
constScanLine( heightIndex );
730 for (
int widthIndex = 0; widthIndex < image.
width(); ++widthIndex )
732 tmpRgb = scanLine[widthIndex];
733 sumRed += qRed( tmpRgb );
734 sumBlue += qBlue( tmpRgb );
735 sumGreen += qGreen( tmpRgb );
740 double avgRed = ( double )sumRed / ( 255.0 * colorCount );
741 double avgGreen = ( double )sumGreen / ( 255.0 * colorCount );
742 double avgBlue = ( double )sumBlue / ( 255.0 * colorCount );
748 QColor QgsCompoundColorWidget::sampleColor(
QPoint point )
const 750 int sampleRadius = mSpinBoxRadius->
value() - 1;
752 1 + sampleRadius * 2, 1 + sampleRadius * 2 );
755 return averageColor( snappedImage );
765 mSamplePreview->setColor( hoverColor );
789 if ( !mPickingColor )
800 void QgsCompoundColorWidget::on_mHueRadio_toggled(
bool checked )
809 void QgsCompoundColorWidget::on_mSaturationRadio_toggled(
bool checked )
818 void QgsCompoundColorWidget::on_mValueRadio_toggled(
bool checked )
827 void QgsCompoundColorWidget::on_mRedRadio_toggled(
bool checked )
836 void QgsCompoundColorWidget::on_mGreenRadio_toggled(
bool checked )
845 void QgsCompoundColorWidget::on_mBlueRadio_toggled(
bool checked )
854 void QgsCompoundColorWidget::on_mAddColorToSchemeButton_clicked()
859 void QgsCompoundColorWidget::updateActionsForCurrentScheme()
863 mActionImportColors->setEnabled( scheme->
isEditable() );
864 mActionPasteColors->setEnabled( scheme->
isEditable() );
865 mAddColorToSchemeButton->setEnabled( scheme->
isEditable() );
866 mRemoveColorsFromSchemeButton->setEnabled( scheme->
isEditable() );
869 mActionRemovePalette->setEnabled( userScheme ?
true :
false );
872 mActionShowInButtons->setEnabled(
true );
878 mActionShowInButtons->setEnabled(
false );
A color scheme which stores its colors in a gpl palette file within the "palettes" subfolder off the ...
void setName(const QString &name)
Sets the name for the scheme.
void setContentsMargins(int left, int top, int right, int bottom)
bool erase()
Erases the associated gpl palette file from the users "palettes" folder.
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user's home dir.
const uchar * constScanLine(int i) const
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.
const T & at(int i) const
QString simplified() const
QString filePath(const QString &fileName) const
virtual QgsColorScheme::SchemeFlags flags() const override
Returns the current flags for the color scheme.
QPixmap grabWindow(WId window, int x, int y, int width, int height)
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.
QString tr(const char *sourceText, const char *disambiguation, int n)
void setValue(const QString &key, const QVariant &value)
const char * name() const
QList< QgsColorScheme * > schemes() const
Returns all color schemes in the registry.
const QPoint & globalPos() const
void setShowSchemeInMenu(bool show)
Sets whether a this scheme should be shown in color button menus.
int toInt(bool *ok) const
StandardButton question(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
static QgsNamedColorList importColorsFromGpl(QFile &file, bool &ok, QString &name)
Imports colors from a gpl GIMP palette file.
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
void setMargin(int margin)
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)
static QgsColorSchemeRegistry * instance()
Returns the global instance pointer, creating the object on the first call.
const char * sampler_cursor[]
QString & replace(int position, int n, QChar after)
QVariant value(const QString &key, const QVariant &defaultValue) const
virtual bool isEditable() const
Returns whether the color scheme is editable.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
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)
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)
bool mkpath(const QString &dirPath) const