Skip to content

Commit 14c8b3c

Browse files
committed
[neeeds-docs] Show all color schemes and tools for interacting with the in the
options->color tab This brings all of QGIS' color scheme handling to a more logical and user-discoverable place. Previously this functionality was only available inside the color dialog itself (i.e. users would have to start changing a color before they could create and edit schemes)
1 parent 2a0f364 commit 14c8b3c

8 files changed

+307
-80
lines changed

python/gui/qgscompoundcolorwidget.sip.in

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,45 @@ be stored in the recent color list.
6565
:param discarded: set to true to avoid adding color to recent color list on widget destruction.
6666

6767
.. versionadded:: 3.0
68+
%End
69+
70+
static QgsUserColorScheme *importUserPaletteFromFile( QWidget *parent );
71+
%Docstring
72+
Triggers a user prompt for importing a new color scheme from an existing GPL file.
73+
74+
The ``parent`` argument must be set to a valid parent widget for the dialog prompts.
75+
76+
.. versionadded:: 3.2
77+
78+
.. seealso:: :py:func:`createNewUserPalette`
79+
80+
.. seealso:: :py:func:`removeUserPalette`
81+
%End
82+
83+
static QgsUserColorScheme *createNewUserPalette( QWidget *parent );
84+
%Docstring
85+
Triggers a user prompt for creating a new user color scheme.
86+
87+
The ``parent`` argument must be set to a valid parent widget for the dialog prompts.
88+
89+
.. versionadded:: 3.2
90+
91+
.. seealso:: :py:func:`importUserPaletteFromFile`
92+
93+
.. seealso:: :py:func:`removeUserPalette`
94+
%End
95+
96+
static bool removeUserPalette( QgsUserColorScheme *scheme, QWidget *parent );
97+
%Docstring
98+
Triggers a user prompt for removing an existing user color ``scheme``.
99+
100+
The ``parent`` argument must be set to a valid parent widget for the dialog prompts.
101+
102+
.. versionadded:: 3.2
103+
104+
.. seealso:: :py:func:`importUserPaletteFromFile`
105+
106+
.. seealso:: :py:func:`createNewUserPalette`
68107
%End
69108

70109
signals:

src/app/qgsoptions.cpp

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,66 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl, const QList<QgsOpti
754754
connect( mButtonImportColors, &QAbstractButton::clicked, mTreeCustomColors, &QgsColorSchemeList::showImportColorsDialog );
755755
connect( mButtonExportColors, &QAbstractButton::clicked, mTreeCustomColors, &QgsColorSchemeList::showExportColorsDialog );
756756

757+
connect( mActionImportPalette, &QAction::triggered, this, [ = ]
758+
{
759+
if ( QgsCompoundColorWidget::importUserPaletteFromFile( this ) )
760+
{
761+
//refresh combobox
762+
refreshSchemeComboBox();
763+
mColorSchemesComboBox->setCurrentIndex( mColorSchemesComboBox->count() - 1 );
764+
}
765+
} );
766+
connect( mActionRemovePalette, &QAction::triggered, this, [ = ]
767+
{
768+
//get current scheme
769+
QList<QgsColorScheme *> schemeList = QgsApplication::colorSchemeRegistry()->schemes();
770+
int prevIndex = mColorSchemesComboBox->currentIndex();
771+
if ( prevIndex >= schemeList.length() )
772+
{
773+
return;
774+
}
775+
776+
//make user scheme is a user removable scheme
777+
QgsUserColorScheme *userScheme = dynamic_cast<QgsUserColorScheme *>( schemeList.at( prevIndex ) );
778+
if ( !userScheme )
779+
{
780+
return;
781+
}
782+
783+
if ( QgsCompoundColorWidget::removeUserPalette( userScheme, this ) )
784+
{
785+
refreshSchemeComboBox();
786+
prevIndex = std::max( std::min( prevIndex, mColorSchemesComboBox->count() - 1 ), 0 );
787+
mColorSchemesComboBox->setCurrentIndex( prevIndex );
788+
}
789+
} );
790+
connect( mActionNewPalette, &QAction::triggered, this, [ = ]
791+
{
792+
if ( QgsCompoundColorWidget::createNewUserPalette( this ) )
793+
{
794+
//refresh combobox
795+
refreshSchemeComboBox();
796+
mColorSchemesComboBox->setCurrentIndex( mColorSchemesComboBox->count() - 1 );
797+
}
798+
} );
799+
800+
connect( mActionShowInButtons, &QAction::toggled, this, [ = ]( bool state )
801+
{
802+
QgsUserColorScheme *scheme = dynamic_cast< QgsUserColorScheme * >( mTreeCustomColors->scheme() );
803+
if ( scheme )
804+
{
805+
scheme->setShowSchemeInMenu( state );
806+
}
807+
} );
808+
809+
QMenu *schemeMenu = new QMenu( mSchemeToolButton );
810+
schemeMenu->addAction( mActionNewPalette );
811+
schemeMenu->addAction( mActionImportPalette );
812+
schemeMenu->addAction( mActionRemovePalette );
813+
schemeMenu->addSeparator();
814+
schemeMenu->addAction( mActionShowInButtons );
815+
mSchemeToolButton->setMenu( schemeMenu );
816+
757817
//find custom color scheme from registry
758818
refreshSchemeComboBox();
759819
QList<QgsCustomColorScheme *> customSchemes;
@@ -762,6 +822,7 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl, const QList<QgsOpti
762822
{
763823
mTreeCustomColors->setScheme( customSchemes.at( 0 ) );
764824
mColorSchemesComboBox->setCurrentIndex( mColorSchemesComboBox->findText( customSchemes.at( 0 )->schemeName() ) );
825+
updateActionsForCurrentColorScheme( customSchemes.at( 0 ) );
765826
}
766827
connect( mColorSchemesComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]( int index )
767828
{
@@ -774,8 +835,9 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl, const QList<QgsOpti
774835
QgsColorScheme *scheme = QgsApplication::colorSchemeRegistry()->schemes().value( index );
775836
if ( scheme )
776837
mTreeCustomColors->setScheme( scheme );
777-
} );
778838

839+
updateActionsForCurrentColorScheme( scheme );
840+
} );
779841

780842
//
781843
// Layout settings
@@ -2272,6 +2334,27 @@ void QgsOptions::refreshSchemeComboBox()
22722334
mColorSchemesComboBox->blockSignals( false );
22732335
}
22742336

2337+
void QgsOptions::updateActionsForCurrentColorScheme( QgsColorScheme *scheme )
2338+
{
2339+
mButtonImportColors->setEnabled( scheme->isEditable() );
2340+
mButtonPasteColors->setEnabled( scheme->isEditable() );
2341+
mButtonAddColor->setEnabled( scheme->isEditable() );
2342+
mButtonRemoveColor->setEnabled( scheme->isEditable() );
2343+
2344+
QgsUserColorScheme *userScheme = dynamic_cast<QgsUserColorScheme *>( scheme );
2345+
mActionRemovePalette->setEnabled( static_cast< bool >( userScheme ) && userScheme->isEditable() );
2346+
if ( userScheme )
2347+
{
2348+
mActionShowInButtons->setEnabled( true );
2349+
whileBlocking( mActionShowInButtons )->setChecked( userScheme->flags() & QgsColorScheme::ShowInColorButtonMenu );
2350+
}
2351+
else
2352+
{
2353+
whileBlocking( mActionShowInButtons )->setChecked( false );
2354+
mActionShowInButtons->setEnabled( false );
2355+
}
2356+
}
2357+
22752358
void QgsOptions::scaleItemChanged( QListWidgetItem *changedScaleItem )
22762359
{
22772360
// Check if the new value is valid, restore the old value if not.

src/app/qgsoptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption
264264

265265
QList< QgsOptionsPageWidget * > mAdditionalOptionWidgets;
266266
QgsLocatorOptionsWidget *mLocatorOptionsWidget = nullptr;
267+
268+
void updateActionsForCurrentColorScheme( QgsColorScheme *scheme );
269+
270+
267271
};
268272

269273
#endif // #ifndef QGSOPTIONS_H

src/core/qgscolorscheme.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,6 @@ QgsUserColorScheme::QgsUserColorScheme( const QString &filename )
303303
: mFilename( filename )
304304
{
305305
QFile sourceFile( gplFilePath() );
306-
QFileInfo sourceFileInfo( gplFilePath() );
307-
mEditable = sourceFileInfo.isWritable();
308306

309307
//read in name
310308
if ( sourceFile.open( QIODevice::ReadOnly ) )
@@ -394,7 +392,7 @@ void QgsUserColorScheme::setShowSchemeInMenu( bool show )
394392

395393
QString QgsUserColorScheme::gplFilePath()
396394
{
397-
QString palettesDir = QgsApplication::qgisSettingsDirPath() + "/palettes";
395+
QString palettesDir = QgsApplication::qgisSettingsDirPath() + "palettes";
398396

399397
QDir localDir;
400398
if ( !localDir.mkpath( palettesDir ) )

src/core/qgscolorschemeregistry.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void QgsColorSchemeRegistry::initStyleScheme()
6464

6565
void QgsColorSchemeRegistry::addUserSchemes()
6666
{
67-
QString palettesDir = QgsApplication::qgisSettingsDirPath() + "/palettes";
67+
QString palettesDir = QgsApplication::qgisSettingsDirPath() + "palettes";
6868

6969
QDir localDir;
7070
if ( !localDir.mkpath( palettesDir ) )

src/gui/qgscompoundcolorwidget.cpp

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -280,23 +280,25 @@ void QgsCompoundColorWidget::refreshSchemeComboBox()
280280
mSchemeComboBox->blockSignals( false );
281281
}
282282

283-
void QgsCompoundColorWidget::importPalette()
283+
284+
QgsUserColorScheme *QgsCompoundColorWidget::importUserPaletteFromFile( QWidget *parent )
284285
{
285286
QgsSettings s;
286287
QString lastDir = s.value( QStringLiteral( "/UI/lastGplPaletteDir" ), QDir::homePath() ).toString();
287-
QString filePath = QFileDialog::getOpenFileName( this, tr( "Select Palette File" ), lastDir, QStringLiteral( "GPL (*.gpl);;All files (*.*)" ) );
288-
activateWindow();
288+
QString filePath = QFileDialog::getOpenFileName( parent, tr( "Select Palette File" ), lastDir, QStringLiteral( "GPL (*.gpl);;All files (*.*)" ) );
289+
if ( parent )
290+
parent->activateWindow();
289291
if ( filePath.isEmpty() )
290292
{
291-
return;
293+
return nullptr;
292294
}
293295

294296
//check if file exists
295297
QFileInfo fileInfo( filePath );
296298
if ( !fileInfo.exists() || !fileInfo.isReadable() )
297299
{
298300
QMessageBox::critical( nullptr, tr( "Import Color Palette" ), tr( "Error, file does not exist or is not readable." ) );
299-
return;
301+
return nullptr;
300302
}
301303

302304
s.setValue( QStringLiteral( "/UI/lastGplPaletteDir" ), fileInfo.absolutePath() );
@@ -309,14 +311,14 @@ void QgsCompoundColorWidget::importPalette()
309311
if ( !ok )
310312
{
311313
QMessageBox::critical( nullptr, tr( "Import Color Palette" ), tr( "Palette file is not readable." ) );
312-
return;
314+
return nullptr;
313315
}
314316

315317
if ( importedColors.length() == 0 )
316318
{
317319
//no imported colors
318320
QMessageBox::critical( nullptr, tr( "Import Color Palette" ), tr( "No colors found in palette file." ) );
319-
return;
321+
return nullptr;
320322
}
321323

322324
//TODO - handle conflicting file names, name for new palette
@@ -325,10 +327,40 @@ void QgsCompoundColorWidget::importPalette()
325327
importedScheme->setColors( importedColors );
326328

327329
QgsApplication::colorSchemeRegistry()->addColorScheme( importedScheme );
330+
return importedScheme;
331+
}
328332

329-
//refresh combobox
330-
refreshSchemeComboBox();
331-
mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );
333+
void QgsCompoundColorWidget::importPalette()
334+
{
335+
if ( importUserPaletteFromFile( this ) )
336+
{
337+
//refresh combobox
338+
refreshSchemeComboBox();
339+
mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );
340+
}
341+
}
342+
343+
344+
bool QgsCompoundColorWidget::removeUserPalette( QgsUserColorScheme *scheme, QWidget *parent )
345+
{
346+
if ( QMessageBox::question( parent, tr( "Remove Color Palette" ),
347+
QString( tr( "Are you sure you want to remove %1?" ) ).arg( scheme->schemeName() ),
348+
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
349+
{
350+
//user canceled
351+
return false;
352+
}
353+
354+
//remove palette and associated gpl file
355+
if ( !scheme->erase() )
356+
{
357+
//something went wrong
358+
return false;
359+
}
360+
361+
//remove scheme from registry
362+
QgsApplication::colorSchemeRegistry()->removeColorScheme( scheme );
363+
return true;
332364
}
333365

334366
void QgsCompoundColorWidget::removePalette()
@@ -348,41 +380,27 @@ void QgsCompoundColorWidget::removePalette()
348380
return;
349381
}
350382

351-
if ( QMessageBox::question( this, tr( "Remove Color Palette" ),
352-
QString( tr( "Are you sure you want to remove %1?" ) ).arg( userScheme->schemeName() ),
353-
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
354-
{
355-
//user canceled
356-
return;
357-
}
358-
359-
//remove palette and associated gpl file
360-
if ( !userScheme->erase() )
383+
if ( removeUserPalette( userScheme, this ) )
361384
{
362-
//something went wrong
363-
return;
385+
refreshSchemeComboBox();
386+
prevIndex = std::max( std::min( prevIndex, mSchemeComboBox->count() - 1 ), 0 );
387+
mSchemeComboBox->setCurrentIndex( prevIndex );
364388
}
365-
366-
//remove scheme from registry
367-
QgsApplication::colorSchemeRegistry()->removeColorScheme( userScheme );
368-
refreshSchemeComboBox();
369-
prevIndex = std::max( std::min( prevIndex, mSchemeComboBox->count() - 1 ), 0 );
370-
mSchemeComboBox->setCurrentIndex( prevIndex );
371389
}
372390

373-
void QgsCompoundColorWidget::newPalette()
391+
QgsUserColorScheme *QgsCompoundColorWidget::createNewUserPalette( QWidget *parent )
374392
{
375393
bool ok = false;
376-
QString name = QInputDialog::getText( this, tr( "Create New Palette" ), tr( "Enter a name for the new palette:" ),
394+
QString name = QInputDialog::getText( parent, tr( "Create New Palette" ), tr( "Enter a name for the new palette:" ),
377395
QLineEdit::Normal, tr( "New palette" ), &ok );
378396

379397
if ( !ok || name.isEmpty() )
380398
{
381399
//user canceled
382-
return;
400+
return nullptr;
383401
}
384402

385-
//generate file name for new palette
403+
//generate file name for new palette
386404
QDir palettePath( gplFilePath() );
387405
QRegExp badChars( "[,^@={}\\[\\]~!?:&*\"|#%<>$\"'();`' /\\\\]" );
388406
QString filename = name.simplified().toLower().replace( badChars, QStringLiteral( "_" ) );
@@ -403,15 +421,22 @@ void QgsCompoundColorWidget::newPalette()
403421
newScheme->setName( name );
404422

405423
QgsApplication::colorSchemeRegistry()->addColorScheme( newScheme );
424+
return newScheme;
425+
}
406426

407-
//refresh combobox and set new scheme as active
408-
refreshSchemeComboBox();
409-
mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );
427+
void QgsCompoundColorWidget::newPalette()
428+
{
429+
if ( createNewUserPalette( this ) )
430+
{
431+
//refresh combobox and set new scheme as active
432+
refreshSchemeComboBox();
433+
mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );
434+
}
410435
}
411436

412437
QString QgsCompoundColorWidget::gplFilePath()
413438
{
414-
QString palettesDir = QgsApplication::qgisSettingsDirPath() + "/palettes";
439+
QString palettesDir = QgsApplication::qgisSettingsDirPath() + "palettes";
415440

416441
QDir localDir;
417442
if ( !localDir.mkpath( palettesDir ) )

0 commit comments

Comments
 (0)