QGIS API Documentation 4.3.0-Master (91d4a333d5d)
Loading...
Searching...
No Matches
qgscompoundcolorwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscompoundcolorwidget.cpp
3 --------------------------
4 begin : April 2016
5 copyright : (C) 2016 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include "qgsapplication.h"
20#include "qgscolorscheme.h"
22#include "qgsguiutils.h"
23#include "qgsscreenhelper.h"
24#include "qgssettings.h"
25#include "qgssymbollayerutils.h"
26
27#include <QButtonGroup>
28#include <QFileDialog>
29#include <QHeaderView>
30#include <QInputDialog>
31#include <QMenu>
32#include <QMessageBox>
33#include <QMouseEvent>
34#include <QPushButton>
35#include <QRegularExpression>
36#include <QScreen>
37#include <QString>
38#include <QToolButton>
39#include <QVBoxLayout>
40
41#include "moc_qgscompoundcolorwidget.cpp"
42
43using namespace Qt::StringLiterals;
44
45QgsCompoundColorWidget::QgsCompoundColorWidget( QWidget *parent, const QColor &color, Layout widgetLayout )
46 : QgsPanelWidget( parent )
47 , mPickerHelper( new QgsColorPickerHelper( this ) )
48{
49 setupUi( this );
50
51 mScreenHelper = new QgsScreenHelper( this );
52
53 mRgbRadios = {
60 };
61
62 mCmykRadios
64
65 mRgbGroup = new QButtonGroup( this );
66 int i = 0;
67 for ( auto colorRadio : mRgbRadios )
68 mRgbGroup->addButton( colorRadio.first, i++ );
69
70 mCmykGroup = new QButtonGroup( this );
71 i = 0;
72 for ( auto colorRadio : mCmykRadios )
73 mCmykGroup->addButton( colorRadio.first, i++ );
74
75 connect( mRgbGroup, &QButtonGroup::idToggled, this, &QgsCompoundColorWidget::onColorButtonGroupToggled );
76 connect( mCmykGroup, &QButtonGroup::idToggled, this, &QgsCompoundColorWidget::onColorButtonGroupToggled );
77 connect( mAddColorToSchemeButton, &QPushButton::clicked, this, &QgsCompoundColorWidget::mAddColorToSchemeButton_clicked );
78 connect( mAddCustomColorButton, &QPushButton::clicked, this, &QgsCompoundColorWidget::mAddCustomColorButton_clicked );
79 connect( mSampleButton, &QPushButton::clicked, this, &QgsCompoundColorWidget::mSampleButton_clicked );
80 connect( mTabWidget, &QTabWidget::currentChanged, this, &QgsCompoundColorWidget::mTabWidget_currentChanged );
81 connect( mActionShowInButtons, &QAction::toggled, this, &QgsCompoundColorWidget::mActionShowInButtons_toggled );
82
83 mColorModel->addItem( tr( "RGB" ), QColor::Spec::Rgb );
84 mColorModel->addItem( tr( "CMYK" ), QColor::Spec::Cmyk );
85 connect( mColorModel, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [this]( int ) {
86 const QColor::Spec spec = static_cast<QColor::Spec>( mColorModel->currentData().toInt() );
87 if ( spec == QColor::Spec::Cmyk )
88 setColor( this->color().toCmyk() );
89 else
90 setColor( this->color().toRgb() );
91
92 updateComponent();
93 } );
94
95 if ( widgetLayout == LayoutVertical )
96 {
97 // shuffle stuff around
98 QVBoxLayout *newLayout = new QVBoxLayout();
99 newLayout->setContentsMargins( 0, 0, 0, 0 );
100 newLayout->addWidget( mTabWidget );
101 newLayout->addWidget( mSlidersWidget );
102 newLayout->addWidget( mPreviewWidget );
103 newLayout->addWidget( mSwatchesWidget );
104 delete layout();
105 setLayout( newLayout );
106 }
107
108 const QgsSettings settings;
109
110 mSchemeList->header()->hide();
111 mSchemeList->setColumnWidth( 0, static_cast<int>( Qgis::UI_SCALE_FACTOR * fontMetrics().horizontalAdvance( 'X' ) * 6 ) );
112
113 //get schemes with ShowInColorDialog set
114 refreshSchemeComboBox();
115 const QList<QgsColorScheme *> schemeList = QgsApplication::colorSchemeRegistry()->schemes( QgsColorScheme::ShowInColorDialog );
116
117 //choose a reasonable starting scheme
118 int activeScheme = settings.value( u"Windows/ColorDialog/activeScheme"_s, 0 ).toInt();
119 activeScheme = activeScheme >= mSchemeComboBox->count() ? 0 : activeScheme;
120
121 mSchemeList->setScheme( schemeList.at( activeScheme ) );
122
123 mSchemeComboBox->setCurrentIndex( activeScheme );
124 updateActionsForCurrentScheme();
125
126 //listen out for selection changes in list, so we can enable/disable the copy colors option
127 connect( mSchemeList->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsCompoundColorWidget::listSelectionChanged );
128 //copy action defaults to disabled
129 mActionCopyColors->setEnabled( false );
130
131 connect( mActionCopyColors, &QAction::triggered, mSchemeList, &QgsColorSchemeList::copyColors );
132 connect( mActionPasteColors, &QAction::triggered, mSchemeList, &QgsColorSchemeList::pasteColors );
133 connect( mActionExportColors, &QAction::triggered, mSchemeList, &QgsColorSchemeList::showExportColorsDialog );
134 connect( mActionImportColors, &QAction::triggered, mSchemeList, &QgsColorSchemeList::showImportColorsDialog );
135 connect( mActionImportPalette, &QAction::triggered, this, &QgsCompoundColorWidget::importPalette );
136 connect( mActionRemovePalette, &QAction::triggered, this, &QgsCompoundColorWidget::removePalette );
137 connect( mActionNewPalette, &QAction::triggered, this, &QgsCompoundColorWidget::newPalette );
138 connect( mRemoveColorsFromSchemeButton, &QAbstractButton::clicked, mSchemeList, &QgsColorSchemeList::removeSelection );
139
140 QMenu *schemeMenu = new QMenu( mSchemeToolButton );
141 schemeMenu->addAction( mActionCopyColors );
142 schemeMenu->addAction( mActionPasteColors );
143 schemeMenu->addSeparator();
144 schemeMenu->addAction( mActionImportColors );
145 schemeMenu->addAction( mActionExportColors );
146 schemeMenu->addSeparator();
147 schemeMenu->addAction( mActionNewPalette );
148 schemeMenu->addAction( mActionImportPalette );
149 schemeMenu->addAction( mActionRemovePalette );
150 schemeMenu->addAction( mActionShowInButtons );
151 mSchemeToolButton->setMenu( schemeMenu );
152
153 connect( mSchemeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsCompoundColorWidget::schemeIndexChanged );
154 connect( mSchemeList, &QgsColorSchemeList::colorSelected, this, &QgsCompoundColorWidget::_setColor );
155
156 mOldColorLabel->hide();
157
158 mVerticalRamp->setOrientation( QgsColorRampWidget::Vertical );
159 mVerticalRamp->setInteriorMargin( 2 );
160 mVerticalRamp->setShowFrame( true );
161
162 mRedSlider->setComponent( QgsColorWidget::Red );
163 mGreenSlider->setComponent( QgsColorWidget::Green );
164 mBlueSlider->setComponent( QgsColorWidget::Blue );
165 mHueSlider->setComponent( QgsColorWidget::Hue );
166 mSaturationSlider->setComponent( QgsColorWidget::Saturation );
167 mValueSlider->setComponent( QgsColorWidget::Value );
168 mAlphaSlider->setComponent( QgsColorWidget::Alpha );
169 mCyanSlider->setComponent( QgsColorWidget::Cyan );
170 mMagentaSlider->setComponent( QgsColorWidget::Magenta );
171 mYellowSlider->setComponent( QgsColorWidget::Yellow );
172 mBlackSlider->setComponent( QgsColorWidget::Black );
173
174 mSwatchButton1->setShowMenu( false );
175 mSwatchButton1->setBehavior( QgsColorButton::SignalOnly );
176 mSwatchButton2->setShowMenu( false );
177 mSwatchButton2->setBehavior( QgsColorButton::SignalOnly );
178 mSwatchButton3->setShowMenu( false );
179 mSwatchButton3->setBehavior( QgsColorButton::SignalOnly );
180 mSwatchButton4->setShowMenu( false );
181 mSwatchButton4->setBehavior( QgsColorButton::SignalOnly );
182 mSwatchButton5->setShowMenu( false );
183 mSwatchButton5->setBehavior( QgsColorButton::SignalOnly );
184 mSwatchButton6->setShowMenu( false );
185 mSwatchButton6->setBehavior( QgsColorButton::SignalOnly );
186 mSwatchButton7->setShowMenu( false );
187 mSwatchButton7->setBehavior( QgsColorButton::SignalOnly );
188 mSwatchButton8->setShowMenu( false );
189 mSwatchButton8->setBehavior( QgsColorButton::SignalOnly );
190 mSwatchButton9->setShowMenu( false );
191 mSwatchButton9->setBehavior( QgsColorButton::SignalOnly );
192 mSwatchButton10->setShowMenu( false );
193 mSwatchButton10->setBehavior( QgsColorButton::SignalOnly );
194 mSwatchButton11->setShowMenu( false );
195 mSwatchButton11->setBehavior( QgsColorButton::SignalOnly );
196 mSwatchButton12->setShowMenu( false );
197 mSwatchButton12->setBehavior( QgsColorButton::SignalOnly );
198 mSwatchButton13->setShowMenu( false );
199 mSwatchButton13->setBehavior( QgsColorButton::SignalOnly );
200 mSwatchButton14->setShowMenu( false );
201 mSwatchButton14->setBehavior( QgsColorButton::SignalOnly );
202 mSwatchButton15->setShowMenu( false );
203 mSwatchButton15->setBehavior( QgsColorButton::SignalOnly );
204 mSwatchButton16->setShowMenu( false );
205 mSwatchButton16->setBehavior( QgsColorButton::SignalOnly );
206 //restore custom colors
207 mSwatchButton1->setColor( settings.value( u"Windows/ColorDialog/customColor1"_s, QVariant( QColor() ) ).value<QColor>() );
208 mSwatchButton2->setColor( settings.value( u"Windows/ColorDialog/customColor2"_s, QVariant( QColor() ) ).value<QColor>() );
209 mSwatchButton3->setColor( settings.value( u"Windows/ColorDialog/customColor3"_s, QVariant( QColor() ) ).value<QColor>() );
210 mSwatchButton4->setColor( settings.value( u"Windows/ColorDialog/customColor4"_s, QVariant( QColor() ) ).value<QColor>() );
211 mSwatchButton5->setColor( settings.value( u"Windows/ColorDialog/customColor5"_s, QVariant( QColor() ) ).value<QColor>() );
212 mSwatchButton6->setColor( settings.value( u"Windows/ColorDialog/customColor6"_s, QVariant( QColor() ) ).value<QColor>() );
213 mSwatchButton7->setColor( settings.value( u"Windows/ColorDialog/customColor7"_s, QVariant( QColor() ) ).value<QColor>() );
214 mSwatchButton8->setColor( settings.value( u"Windows/ColorDialog/customColor8"_s, QVariant( QColor() ) ).value<QColor>() );
215 mSwatchButton9->setColor( settings.value( u"Windows/ColorDialog/customColor9"_s, QVariant( QColor() ) ).value<QColor>() );
216 mSwatchButton10->setColor( settings.value( u"Windows/ColorDialog/customColor10"_s, QVariant( QColor() ) ).value<QColor>() );
217 mSwatchButton11->setColor( settings.value( u"Windows/ColorDialog/customColor11"_s, QVariant( QColor() ) ).value<QColor>() );
218 mSwatchButton12->setColor( settings.value( u"Windows/ColorDialog/customColor12"_s, QVariant( QColor() ) ).value<QColor>() );
219 mSwatchButton13->setColor( settings.value( u"Windows/ColorDialog/customColor13"_s, QVariant( QColor() ) ).value<QColor>() );
220 mSwatchButton14->setColor( settings.value( u"Windows/ColorDialog/customColor14"_s, QVariant( QColor() ) ).value<QColor>() );
221 mSwatchButton15->setColor( settings.value( u"Windows/ColorDialog/customColor15"_s, QVariant( QColor() ) ).value<QColor>() );
222 mSwatchButton16->setColor( settings.value( u"Windows/ColorDialog/customColor16"_s, QVariant( QColor() ) ).value<QColor>() );
223
224 //restore sample radius
225 mSpinBoxRadius->setValue( settings.value( u"Windows/ColorDialog/sampleRadius"_s, 1 ).toInt() );
226 mSamplePreview->setColor( QColor() );
227
228 // hidpi friendly sizes
229 const int swatchWidth = static_cast<int>( std::round( std::max( Qgis::UI_SCALE_FACTOR * 1.9 * mSwatchButton1->fontMetrics().height(), 38.0 ) ) );
230 const int swatchHeight = static_cast<int>( std::round( std::max( Qgis::UI_SCALE_FACTOR * 1.5 * mSwatchButton1->fontMetrics().height(), 30.0 ) ) );
231 mSwatchButton1->setMinimumSize( swatchWidth, swatchHeight );
232 mSwatchButton1->setMaximumSize( swatchWidth, swatchHeight );
233 mSwatchButton2->setMinimumSize( swatchWidth, swatchHeight );
234 mSwatchButton2->setMaximumSize( swatchWidth, swatchHeight );
235 mSwatchButton3->setMinimumSize( swatchWidth, swatchHeight );
236 mSwatchButton3->setMaximumSize( swatchWidth, swatchHeight );
237 mSwatchButton4->setMinimumSize( swatchWidth, swatchHeight );
238 mSwatchButton4->setMaximumSize( swatchWidth, swatchHeight );
239 mSwatchButton5->setMinimumSize( swatchWidth, swatchHeight );
240 mSwatchButton5->setMaximumSize( swatchWidth, swatchHeight );
241 mSwatchButton6->setMinimumSize( swatchWidth, swatchHeight );
242 mSwatchButton6->setMaximumSize( swatchWidth, swatchHeight );
243 mSwatchButton7->setMinimumSize( swatchWidth, swatchHeight );
244 mSwatchButton7->setMaximumSize( swatchWidth, swatchHeight );
245 mSwatchButton8->setMinimumSize( swatchWidth, swatchHeight );
246 mSwatchButton8->setMaximumSize( swatchWidth, swatchHeight );
247 mSwatchButton9->setMinimumSize( swatchWidth, swatchHeight );
248 mSwatchButton9->setMaximumSize( swatchWidth, swatchHeight );
249 mSwatchButton10->setMinimumSize( swatchWidth, swatchHeight );
250 mSwatchButton10->setMaximumSize( swatchWidth, swatchHeight );
251 mSwatchButton11->setMinimumSize( swatchWidth, swatchHeight );
252 mSwatchButton11->setMaximumSize( swatchWidth, swatchHeight );
253 mSwatchButton12->setMinimumSize( swatchWidth, swatchHeight );
254 mSwatchButton12->setMaximumSize( swatchWidth, swatchHeight );
255 mSwatchButton13->setMinimumSize( swatchWidth, swatchHeight );
256 mSwatchButton13->setMaximumSize( swatchWidth, swatchHeight );
257 mSwatchButton14->setMinimumSize( swatchWidth, swatchHeight );
258 mSwatchButton14->setMaximumSize( swatchWidth, swatchHeight );
259 mSwatchButton15->setMinimumSize( swatchWidth, swatchHeight );
260 mSwatchButton15->setMaximumSize( swatchWidth, swatchHeight );
261 mSwatchButton16->setMinimumSize( swatchWidth, swatchHeight );
262 mSwatchButton16->setMaximumSize( swatchWidth, swatchHeight );
263 const int previewHeight = static_cast<int>( std::round( std::max( Qgis::UI_SCALE_FACTOR * 2.0 * mSwatchButton1->fontMetrics().height(), 40.0 ) ) );
264 mColorPreview->setMinimumSize( 0, previewHeight );
265 mPreviewWidget->setMaximumHeight( previewHeight * 2 );
266 const int swatchAddSize = static_cast<int>( std::round( std::max( Qgis::UI_SCALE_FACTOR * 1.4 * mSwatchButton1->fontMetrics().height(), 28.0 ) ) );
267 mAddCustomColorButton->setMinimumWidth( swatchAddSize );
268 mAddCustomColorButton->setMaximumWidth( swatchAddSize );
269
270 const int iconSize = QgsGuiUtils::scaleIconSize( 16 );
271 mTabWidget->setIconSize( QSize( iconSize, iconSize ) );
272
273 setColor( color );
274
275 // restore active Rgb/Cmyk component radio button
276 const int activeRgbRadio = settings.value( u"Windows/ColorDialog/activeComponent"_s, 2 ).toInt();
277 if ( QAbstractButton *rgbRadio = mRgbGroup->button( activeRgbRadio ) )
278 rgbRadio->setChecked( true );
279
280 const int activeCmykRadio = settings.value( u"Windows/ColorDialog/activeCmykComponent"_s, 0 ).toInt();
281 if ( QAbstractButton *cmykRadio = mCmykGroup->button( activeCmykRadio ) )
282 cmykRadio->setChecked( true );
283
284 const int currentTab = settings.value( u"Windows/ColorDialog/activeTab"_s, 0 ).toInt();
285 mTabWidget->setCurrentIndex( currentTab );
286
287 //setup connections
288 connect( mColorBox, &QgsColorWidget::colorChanged, this, &QgsCompoundColorWidget::_setColor );
289 connect( mColorWheel, &QgsColorWidget::colorChanged, this, &QgsCompoundColorWidget::_setColor );
290 connect( mColorText, &QgsColorWidget::colorChanged, this, &QgsCompoundColorWidget::_setColor );
291 connect( mVerticalRamp, &QgsColorWidget::colorChanged, this, &QgsCompoundColorWidget::_setColor );
292 connect( mRedSlider, &QgsColorWidget::colorChanged, this, &QgsCompoundColorWidget::_setColor );
293 connect( mGreenSlider, &QgsColorWidget::colorChanged, this, &QgsCompoundColorWidget::_setColor );
294 connect( mBlueSlider, &QgsColorWidget::colorChanged, this, &QgsCompoundColorWidget::_setColor );
295 connect( mHueSlider, &QgsColorWidget::colorChanged, this, &QgsCompoundColorWidget::_setColor );
296 connect( mValueSlider, &QgsColorWidget::colorChanged, this, &QgsCompoundColorWidget::_setColor );
297 connect( mCyanSlider, &QgsColorWidget::colorChanged, this, &QgsCompoundColorWidget::_setColor );
298 connect( mMagentaSlider, &QgsColorWidget::colorChanged, this, &QgsCompoundColorWidget::_setColor );
299 connect( mYellowSlider, &QgsColorWidget::colorChanged, this, &QgsCompoundColorWidget::_setColor );
300 connect( mBlackSlider, &QgsColorWidget::colorChanged, this, &QgsCompoundColorWidget::_setColor );
301 connect( mSaturationSlider, &QgsColorWidget::colorChanged, this, &QgsCompoundColorWidget::_setColor );
302 connect( mAlphaSlider, &QgsColorWidget::colorChanged, this, &QgsCompoundColorWidget::_setColor );
303 connect( mColorPreview, &QgsColorWidget::colorChanged, this, &QgsCompoundColorWidget::_setColor );
304 connect( mSwatchButton1, &QgsColorButton::colorClicked, this, &QgsCompoundColorWidget::_setColor );
305 connect( mSwatchButton2, &QgsColorButton::colorClicked, this, &QgsCompoundColorWidget::_setColor );
306 connect( mSwatchButton3, &QgsColorButton::colorClicked, this, &QgsCompoundColorWidget::_setColor );
307 connect( mSwatchButton4, &QgsColorButton::colorClicked, this, &QgsCompoundColorWidget::_setColor );
308 connect( mSwatchButton5, &QgsColorButton::colorClicked, this, &QgsCompoundColorWidget::_setColor );
309 connect( mSwatchButton6, &QgsColorButton::colorClicked, this, &QgsCompoundColorWidget::_setColor );
310 connect( mSwatchButton7, &QgsColorButton::colorClicked, this, &QgsCompoundColorWidget::_setColor );
311 connect( mSwatchButton8, &QgsColorButton::colorClicked, this, &QgsCompoundColorWidget::_setColor );
312 connect( mSwatchButton9, &QgsColorButton::colorClicked, this, &QgsCompoundColorWidget::_setColor );
313 connect( mSwatchButton10, &QgsColorButton::colorClicked, this, &QgsCompoundColorWidget::_setColor );
314 connect( mSwatchButton11, &QgsColorButton::colorClicked, this, &QgsCompoundColorWidget::_setColor );
315 connect( mSwatchButton12, &QgsColorButton::colorClicked, this, &QgsCompoundColorWidget::_setColor );
316 connect( mSwatchButton13, &QgsColorButton::colorClicked, this, &QgsCompoundColorWidget::_setColor );
317 connect( mSwatchButton14, &QgsColorButton::colorClicked, this, &QgsCompoundColorWidget::_setColor );
318 connect( mSwatchButton15, &QgsColorButton::colorClicked, this, &QgsCompoundColorWidget::_setColor );
319 connect( mSwatchButton16, &QgsColorButton::colorClicked, this, &QgsCompoundColorWidget::_setColor );
320
321 connect( mPickerHelper, &QgsColorPickerHelper::colorPicked, this, &QgsCompoundColorWidget::onColorPicked );
322 connect( mPickerHelper, &QgsColorPickerHelper::colorHovered, this, &QgsCompoundColorWidget::onColorHovered );
323}
324
332
334{
335 //all widgets should have the same color, so it shouldn't matter
336 //which we fetch it from
337 return mColorPreview->color();
338}
339
340void QgsCompoundColorWidget::setAllowOpacity( const bool allowOpacity )
341{
342 mAllowAlpha = allowOpacity;
343 mAlphaLabel->setVisible( allowOpacity );
344 mAlphaSlider->setVisible( allowOpacity );
345 mColorText->setAllowOpacity( allowOpacity );
346 if ( !allowOpacity )
347 {
348 mAlphaLayout->setContentsMargins( 0, 0, 0, 0 );
349 mAlphaLayout->setSpacing( 0 );
350 }
351}
352
354{
355 mColorModel->setVisible( colorModelEditable );
356}
357
358void QgsCompoundColorWidget::refreshSchemeComboBox()
359{
360 mSchemeComboBox->blockSignals( true );
361 mSchemeComboBox->clear();
362 const QList<QgsColorScheme *> schemeList = QgsApplication::colorSchemeRegistry()->schemes( QgsColorScheme::ShowInColorDialog );
363 QList<QgsColorScheme *>::const_iterator schemeIt = schemeList.constBegin();
364 for ( ; schemeIt != schemeList.constEnd(); ++schemeIt )
365 {
366 mSchemeComboBox->addItem( ( *schemeIt )->schemeName() );
367 }
368 mSchemeComboBox->blockSignals( false );
369}
370
371
373{
374 QgsSettings s;
375 const QString lastDir = s.value( u"/UI/lastGplPaletteDir"_s, QDir::homePath() ).toString();
376 const QString filePath = QFileDialog::getOpenFileName( parent, tr( "Select Palette File" ), lastDir, u"GPL (*.gpl);;All files (*.*)"_s );
377 if ( parent )
378 parent->activateWindow();
379 if ( filePath.isEmpty() )
380 {
381 return nullptr;
382 }
383
384 //check if file exists
385 const QFileInfo fileInfo( filePath );
386 if ( !fileInfo.exists() || !fileInfo.isReadable() )
387 {
388 QMessageBox::critical( nullptr, tr( "Import Color Palette" ), tr( "Error, file does not exist or is not readable." ) );
389 return nullptr;
390 }
391
392 s.setValue( u"/UI/lastGplPaletteDir"_s, fileInfo.absolutePath() );
393 QFile file( filePath );
394
395 QgsNamedColorList importedColors;
396 bool ok = false;
397 QString paletteName;
398 importedColors = QgsSymbolLayerUtils::importColorsFromGpl( file, ok, paletteName );
399 if ( !ok )
400 {
401 QMessageBox::critical( nullptr, tr( "Import Color Palette" ), tr( "Palette file is not readable." ) );
402 return nullptr;
403 }
404
405 if ( importedColors.length() == 0 )
406 {
407 //no imported colors
408 QMessageBox::critical( nullptr, tr( "Import Color Palette" ), tr( "No colors found in palette file." ) );
409 return nullptr;
410 }
411
412 //TODO - handle conflicting file names, name for new palette
413 QgsUserColorScheme *importedScheme = new QgsUserColorScheme( fileInfo.fileName() );
414 importedScheme->setName( paletteName );
415 importedScheme->setColors( importedColors );
416
418 return importedScheme;
419}
420
421void QgsCompoundColorWidget::importPalette()
422{
423 if ( importUserPaletteFromFile( this ) )
424 {
425 //refresh combobox
426 refreshSchemeComboBox();
427 mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );
428 }
429}
430
431
433{
434 if ( QMessageBox::question( parent, tr( "Remove Color Palette" ), tr( "Are you sure you want to remove %1?" ).arg( scheme->schemeName() ), QMessageBox::Yes | QMessageBox::No, QMessageBox::No )
435 != QMessageBox::Yes )
436 {
437 //user canceled
438 return false;
439 }
440
441 //remove palette and associated gpl file
442 if ( !scheme->erase() )
443 {
444 //something went wrong
445 return false;
446 }
447
448 //remove scheme from registry
450 return true;
451}
452
453void QgsCompoundColorWidget::removePalette()
454{
455 //get current scheme
456 const QList<QgsColorScheme *> schemeList = QgsApplication::colorSchemeRegistry()->schemes( QgsColorScheme::ShowInColorDialog );
457 int prevIndex = mSchemeComboBox->currentIndex();
458 if ( prevIndex >= schemeList.length() )
459 {
460 return;
461 }
462
463 //make user scheme is a user removable scheme
464 QgsUserColorScheme *userScheme = dynamic_cast<QgsUserColorScheme *>( schemeList.at( prevIndex ) );
465 if ( !userScheme )
466 {
467 return;
468 }
469
470 if ( removeUserPalette( userScheme, this ) )
471 {
472 refreshSchemeComboBox();
473 prevIndex = std::max( std::min( prevIndex, mSchemeComboBox->count() - 1 ), 0 );
474 mSchemeComboBox->setCurrentIndex( prevIndex );
475 }
476}
477
479{
480 bool ok = false;
481 const QString name = QInputDialog::getText( parent, tr( "Create New Palette" ), tr( "Enter a name for the new palette:" ), QLineEdit::Normal, tr( "New palette" ), &ok );
482
483 if ( !ok || name.isEmpty() )
484 {
485 //user canceled
486 return nullptr;
487 }
488
489 //generate file name for new palette
490 const QDir palettePath( gplFilePath() );
491 const thread_local QRegularExpression badChars( "[,^@={}\\[\\]~!?:&*\"|#%<>$\"'();`' /\\\\]" );
492 QString filename = name.simplified().toLower().replace( badChars, u"_"_s );
493 if ( filename.isEmpty() )
494 {
495 filename = tr( "new_palette" );
496 }
497 QFileInfo destFileInfo( palettePath.filePath( filename + ".gpl" ) );
498 int fileNumber = 1;
499 while ( destFileInfo.exists() )
500 {
501 //try to generate a unique file name
502 destFileInfo = QFileInfo( palettePath.filePath( filename + u"%1.gpl"_s.arg( fileNumber ) ) );
503 fileNumber++;
504 }
505
506 QgsUserColorScheme *newScheme = new QgsUserColorScheme( destFileInfo.fileName() );
507 newScheme->setName( name );
508
510 return newScheme;
511}
512
513void QgsCompoundColorWidget::newPalette()
514{
515 if ( createNewUserPalette( this ) )
516 {
517 //refresh combobox and set new scheme as active
518 refreshSchemeComboBox();
519 mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );
520 }
521}
522
523QString QgsCompoundColorWidget::gplFilePath()
524{
525 QString palettesDir = QgsApplication::qgisSettingsDirPath() + "palettes";
526
527 const QDir localDir;
528 if ( !localDir.mkpath( palettesDir ) )
529 {
530 return QString();
531 }
532
533 return palettesDir;
534}
535
536void QgsCompoundColorWidget::schemeIndexChanged( int index )
537{
538 //save changes to scheme
539 if ( mSchemeList->isDirty() )
540 {
541 mSchemeList->saveColorsToScheme();
542 }
543
544 //get schemes with ShowInColorDialog set
545 const QList<QgsColorScheme *> schemeList = QgsApplication::colorSchemeRegistry()->schemes( QgsColorScheme::ShowInColorDialog );
546 if ( index >= schemeList.length() )
547 {
548 return;
549 }
550
551 QgsColorScheme *scheme = schemeList.at( index );
552 mSchemeList->setScheme( scheme );
553
554 updateActionsForCurrentScheme();
555
556 //copy action defaults to disabled
557 mActionCopyColors->setEnabled( false );
558}
559
560void QgsCompoundColorWidget::listSelectionChanged( const QItemSelection &selected, const QItemSelection &deselected )
561{
562 Q_UNUSED( deselected )
563 mActionCopyColors->setEnabled( selected.length() > 0 );
564}
565
566void QgsCompoundColorWidget::mAddCustomColorButton_clicked()
567{
568 switch ( mLastCustomColorIndex )
569 {
570 case 0:
571 mSwatchButton1->setColor( mColorPreview->color() );
572 break;
573 case 1:
574 mSwatchButton2->setColor( mColorPreview->color() );
575 break;
576 case 2:
577 mSwatchButton3->setColor( mColorPreview->color() );
578 break;
579 case 3:
580 mSwatchButton4->setColor( mColorPreview->color() );
581 break;
582 case 4:
583 mSwatchButton5->setColor( mColorPreview->color() );
584 break;
585 case 5:
586 mSwatchButton6->setColor( mColorPreview->color() );
587 break;
588 case 6:
589 mSwatchButton7->setColor( mColorPreview->color() );
590 break;
591 case 7:
592 mSwatchButton8->setColor( mColorPreview->color() );
593 break;
594 case 8:
595 mSwatchButton9->setColor( mColorPreview->color() );
596 break;
597 case 9:
598 mSwatchButton10->setColor( mColorPreview->color() );
599 break;
600 case 10:
601 mSwatchButton11->setColor( mColorPreview->color() );
602 break;
603 case 11:
604 mSwatchButton12->setColor( mColorPreview->color() );
605 break;
606 case 12:
607 mSwatchButton13->setColor( mColorPreview->color() );
608 break;
609 case 13:
610 mSwatchButton14->setColor( mColorPreview->color() );
611 break;
612 case 14:
613 mSwatchButton15->setColor( mColorPreview->color() );
614 break;
615 case 15:
616 mSwatchButton16->setColor( mColorPreview->color() );
617 break;
618 default:
619 return;
620 }
621 mLastCustomColorIndex++;
622 if ( mLastCustomColorIndex >= 16 )
623 {
624 mLastCustomColorIndex = 0;
625 }
626}
627
628void QgsCompoundColorWidget::mSampleButton_clicked()
629{
630 mPickerHelper->activate( mSpinBoxRadius->value() - 1 );
631}
632
633void QgsCompoundColorWidget::mTabWidget_currentChanged( int index )
634{
635 //disable radio buttons if not using the first tab, as they have no meaning for other tabs
636 const bool enabled = index == 0;
637 const QList<QRadioButton *> colorRadios { mHueRadio, mSaturationRadio, mValueRadio, mRedRadio, mGreenRadio, mBlueRadio, mCyanRadio, mMagentaRadio, mYellowRadio, mBlackRadio };
638 for ( QRadioButton *colorRadio : colorRadios )
639 colorRadio->setEnabled( enabled );
640}
641
642void QgsCompoundColorWidget::mActionShowInButtons_toggled( bool state )
643{
644 QgsUserColorScheme *scheme = dynamic_cast<QgsUserColorScheme *>( mSchemeList->scheme() );
645 if ( scheme )
646 {
647 scheme->setShowSchemeInMenu( state );
648 }
649}
650
651QScreen *QgsCompoundColorWidget::findScreenAt( QPoint pos )
652{
653 const QList<QScreen *> screens = QGuiApplication::screens();
654 for ( QScreen *screen : screens )
655 {
656 if ( screen->geometry().contains( pos ) )
657 {
658 return screen;
659 }
660 }
661 return nullptr;
662}
663
664void QgsCompoundColorWidget::saveSettings()
665{
666 //save changes to scheme
667 if ( mSchemeList->isDirty() )
668 {
669 mSchemeList->saveColorsToScheme();
670 }
671
672 QgsSettings settings;
673
674 // record active component
675 settings.setValue( u"Windows/ColorDialog/activeComponent"_s, mRgbGroup->checkedId() );
676 settings.setValue( u"Windows/ColorDialog/activeCmykComponent"_s, mCmykGroup->checkedId() );
677
678 //record current scheme
679 settings.setValue( u"Windows/ColorDialog/activeScheme"_s, mSchemeComboBox->currentIndex() );
680
681 //record current tab
682 settings.setValue( u"Windows/ColorDialog/activeTab"_s, mTabWidget->currentIndex() );
683
684 //record custom colors
685 settings.setValue( u"Windows/ColorDialog/customColor1"_s, QVariant( mSwatchButton1->color() ) );
686 settings.setValue( u"Windows/ColorDialog/customColor2"_s, QVariant( mSwatchButton2->color() ) );
687 settings.setValue( u"Windows/ColorDialog/customColor3"_s, QVariant( mSwatchButton3->color() ) );
688 settings.setValue( u"Windows/ColorDialog/customColor4"_s, QVariant( mSwatchButton4->color() ) );
689 settings.setValue( u"Windows/ColorDialog/customColor5"_s, QVariant( mSwatchButton5->color() ) );
690 settings.setValue( u"Windows/ColorDialog/customColor6"_s, QVariant( mSwatchButton6->color() ) );
691 settings.setValue( u"Windows/ColorDialog/customColor7"_s, QVariant( mSwatchButton7->color() ) );
692 settings.setValue( u"Windows/ColorDialog/customColor8"_s, QVariant( mSwatchButton8->color() ) );
693 settings.setValue( u"Windows/ColorDialog/customColor9"_s, QVariant( mSwatchButton9->color() ) );
694 settings.setValue( u"Windows/ColorDialog/customColor10"_s, QVariant( mSwatchButton10->color() ) );
695 settings.setValue( u"Windows/ColorDialog/customColor11"_s, QVariant( mSwatchButton11->color() ) );
696 settings.setValue( u"Windows/ColorDialog/customColor12"_s, QVariant( mSwatchButton12->color() ) );
697 settings.setValue( u"Windows/ColorDialog/customColor13"_s, QVariant( mSwatchButton13->color() ) );
698 settings.setValue( u"Windows/ColorDialog/customColor14"_s, QVariant( mSwatchButton14->color() ) );
699 settings.setValue( u"Windows/ColorDialog/customColor15"_s, QVariant( mSwatchButton15->color() ) );
700 settings.setValue( u"Windows/ColorDialog/customColor16"_s, QVariant( mSwatchButton16->color() ) );
701
702 //sample radius
703 settings.setValue( u"Windows/ColorDialog/sampleRadius"_s, mSpinBoxRadius->value() );
704}
705
706void QgsCompoundColorWidget::onColorPicked( const QColor &color )
707{
708 mSamplePreview->setColor( color );
709 mColorPreview->setColor( color, true );
710}
711
712void QgsCompoundColorWidget::onColorHovered( const QColor &color )
713{
714 mSamplePreview->setColor( color );
715}
716
718{
719 const QColor::Spec colorSpec = color.spec() == QColor::Cmyk ? QColor::Cmyk : QColor::Rgb;
720 mColorModel->setCurrentIndex( mColorModel->findData( colorSpec ) );
721 mRGB->setVisible( colorSpec != QColor::Cmyk );
722 mCMYK->setVisible( colorSpec == QColor::Cmyk );
723 _setColor( color );
724}
725
726void QgsCompoundColorWidget::_setColor( const QColor &color )
727{
728 if ( !color.isValid() )
729 {
730 return;
731 }
732
733 QColor fixedColor = QColor( color );
734 if ( !mAllowAlpha )
735 {
736 //opacity disallowed, so don't permit transparent colors
737 fixedColor.setAlpha( 255 );
738 }
739
740 if ( mColorModel->currentIndex() && fixedColor.spec() != QColor::Cmyk )
741 {
742 fixedColor = fixedColor.toCmyk();
743 }
744
745 const QList<QgsColorWidget *> colorWidgets = this->findChildren<QgsColorWidget *>();
746 const auto constColorWidgets = colorWidgets;
747 for ( QgsColorWidget *widget : constColorWidgets )
748 {
749 if ( widget == mSamplePreview )
750 {
751 continue;
752 }
753 widget->blockSignals( true );
754 widget->setColor( fixedColor );
755 widget->blockSignals( false );
756 }
757
758
759 emit currentColorChanged( fixedColor );
760}
761
763{
764 mOldColorLabel->setVisible( color.isValid() );
765 mColorPreview->setColor2( color );
766}
767
769{
770 saveSettings();
771 QWidget::hideEvent( e );
772}
773
774void QgsCompoundColorWidget::updateComponent()
775{
776 const bool isCmyk = mColorModel->currentData().toInt() == QColor::Spec::Cmyk;
777 const auto radios = isCmyk ? mCmykRadios : mRgbRadios;
778 const QButtonGroup *group = isCmyk ? mCmykGroup : mRgbGroup;
779
780 const int id = group->checkedId();
781 if ( id >= 0 && id < radios.count() )
782 {
783 const QgsColorWidget::ColorComponent component = radios.at( group->checkedId() ).second;
784 mColorBox->setComponent( component );
785 mVerticalRamp->setComponent( component );
786 }
787}
788
789void QgsCompoundColorWidget::onColorButtonGroupToggled( int, bool checked )
790{
791 if ( checked )
792 updateComponent();
793}
794
795void QgsCompoundColorWidget::mAddColorToSchemeButton_clicked()
796{
797 mSchemeList->addColor( mColorPreview->color(), QgsSymbolLayerUtils::colorToName( mColorPreview->color() ) );
798}
799
800void QgsCompoundColorWidget::updateActionsForCurrentScheme()
801{
802 QgsColorScheme *scheme = mSchemeList->scheme();
803
804 mActionImportColors->setEnabled( scheme->isEditable() );
805 mActionPasteColors->setEnabled( scheme->isEditable() );
806 mAddColorToSchemeButton->setEnabled( scheme->isEditable() );
807 mRemoveColorsFromSchemeButton->setEnabled( scheme->isEditable() );
808
809 QgsUserColorScheme *userScheme = dynamic_cast<QgsUserColorScheme *>( scheme );
810 mActionRemovePalette->setEnabled( static_cast<bool>( userScheme ) );
811 if ( userScheme )
812 {
813 mActionShowInButtons->setEnabled( true );
814 whileBlocking( mActionShowInButtons )->setChecked( userScheme->flags() & QgsColorScheme::ShowInColorButtonMenu );
815 }
816 else
817 {
818 whileBlocking( mActionShowInButtons )->setChecked( false );
819 mActionShowInButtons->setEnabled( false );
820 }
821}
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition qgis.h:7405
static QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the application's color scheme registry, used for managing color schemes.
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user's home dir.
@ SignalOnly
Emit colorClicked signal only, no dialog.
void colorClicked(const QColor &color)
Emitted when the button is clicked, if the button's behavior is set to SignalOnly.
A utility class for sampling colors from the screen.
void colorPicked(const QColor &color)
Emitted when the user has finished picking a color from the screen.
void colorHovered(const QColor &color)
Emitted when the user hovers over a color from the screen.
@ Vertical
Vertical ramp.
void pasteColors()
Pastes colors from clipboard to the list.
void removeSelection()
Removes any selected colors from the list.
void copyColors()
Copies colors from the list to the clipboard.
void showExportColorsDialog()
Displays a file picker dialog allowing users to export colors from the list into a file.
void colorSelected(const QColor &color)
Emitted when a color is selected from the list.
void showImportColorsDialog()
Displays a file picker dialog allowing users to import colors into the list from a file.
void addColorScheme(QgsColorScheme *scheme)
Adds a color scheme to the registry.
QList< QgsColorScheme * > schemes() const
Returns all color schemes in the registry.
bool removeColorScheme(QgsColorScheme *scheme)
Removes all matching color schemes from the registry.
@ ShowInColorButtonMenu
Show scheme in color button drop-down menu.
@ ShowInColorDialog
Show scheme in color picker dialog.
virtual bool isEditable() const
Returns whether the color scheme is editable.
void colorChanged(const QColor &color)
Emitted when the widget's color changes.
ColorComponent
Specifies the color component which the widget alters.
@ Hue
Hue component of color (based on HSV model).
@ Alpha
Alpha component (opacity) of color.
@ Green
Green component of color.
@ Red
Red component of color.
@ Saturation
Saturation component of color (based on HSV model).
@ Magenta
Magenta component (based on CMYK model) of color.
@ Yellow
Yellow component (based on CMYK model) of color.
@ Black
Black component (based on CMYK model) of color.
@ Cyan
Cyan component (based on CMYK model) of color.
@ Blue
Blue component of color.
@ Value
Value component of color (based on HSV model).
@ LayoutVertical
Use a narrower, vertically stacked layout.
void currentColorChanged(const QColor &color)
Emitted when the dialog's color changes.
void hideEvent(QHideEvent *e) override
QgsCompoundColorWidget(QWidget *parent=nullptr, const QColor &color=QColor(), Layout layout=LayoutDefault)
Constructor for QgsCompoundColorWidget.
void setColorModelEditable(bool colorModelEditable)
Sets whether color model is editable or not.
static QgsUserColorScheme * importUserPaletteFromFile(QWidget *parent)
Triggers a user prompt for importing a new color scheme from an existing GPL file.
void setPreviousColor(const QColor &color)
Sets the color to show in an optional "previous color" section.
static bool removeUserPalette(QgsUserColorScheme *scheme, QWidget *parent)
Triggers a user prompt for removing an existing user color scheme.
static QgsUserColorScheme * createNewUserPalette(QWidget *parent)
Triggers a user prompt for creating a new user color scheme.
void setAllowOpacity(bool allowOpacity)
Sets whether opacity modification (transparency) is permitted for the color dialog.
void setColor(const QColor &color)
Sets the current color for the dialog.
QColor color() const
Returns the current color for the dialog.
bool setColors(const QgsNamedColorList &colors, const QString &context=QString(), const QColor &baseColor=QColor()) override
Sets the colors for the scheme.
QgsPanelWidget(QWidget *parent=nullptr)
Base class for any widget that can be shown as an inline panel.
static void addRecentColor(const QColor &color)
Adds a color to the list of recent colors.
A utility class for dynamic handling of changes to screen properties.
Stores settings for use within QGIS.
Definition qgssettings.h:68
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
static QgsNamedColorList importColorsFromGpl(QFile &file, bool &ok, QString &name)
Imports colors from a gpl GIMP palette file.
static QString colorToName(const QColor &color)
Returns a friendly display name for a color.
A color scheme which stores its colors in a gpl palette file within the "palettes" subfolder off the ...
QString schemeName() const override
Gets the name for the color scheme.
void setName(const QString &name)
Sets the name for the scheme.
QgsColorScheme::SchemeFlags flags() const override
Returns the current flags for the color scheme.
bool erase()
Erases the associated gpl palette file from the users "palettes" folder.
void setShowSchemeInMenu(bool show)
Sets whether a this scheme should be shown in color button menus.
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:7705