Skip to content

Commit

Permalink
[neeeds-docs] Show all color schemes and tools for interacting with t…
Browse files Browse the repository at this point in the history
…he 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)
  • Loading branch information
nyalldawson committed Mar 12, 2018
1 parent 2a0f364 commit 14c8b3c
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 80 deletions.
39 changes: 39 additions & 0 deletions python/gui/qgscompoundcolorwidget.sip.in
Expand Up @@ -65,6 +65,45 @@ be stored in the recent color list.
:param discarded: set to true to avoid adding color to recent color list on widget destruction.

.. versionadded:: 3.0
%End

static QgsUserColorScheme *importUserPaletteFromFile( QWidget *parent );
%Docstring
Triggers a user prompt for importing a new color scheme from an existing GPL file.

The ``parent`` argument must be set to a valid parent widget for the dialog prompts.

.. versionadded:: 3.2

.. seealso:: :py:func:`createNewUserPalette`

.. seealso:: :py:func:`removeUserPalette`
%End

static QgsUserColorScheme *createNewUserPalette( QWidget *parent );
%Docstring
Triggers a user prompt for creating a new user color scheme.

The ``parent`` argument must be set to a valid parent widget for the dialog prompts.

.. versionadded:: 3.2

.. seealso:: :py:func:`importUserPaletteFromFile`

.. seealso:: :py:func:`removeUserPalette`
%End

static bool removeUserPalette( QgsUserColorScheme *scheme, QWidget *parent );
%Docstring
Triggers a user prompt for removing an existing user color ``scheme``.

The ``parent`` argument must be set to a valid parent widget for the dialog prompts.

.. versionadded:: 3.2

.. seealso:: :py:func:`importUserPaletteFromFile`

.. seealso:: :py:func:`createNewUserPalette`
%End

signals:
Expand Down
85 changes: 84 additions & 1 deletion src/app/qgsoptions.cpp
Expand Up @@ -754,6 +754,66 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl, const QList<QgsOpti
connect( mButtonImportColors, &QAbstractButton::clicked, mTreeCustomColors, &QgsColorSchemeList::showImportColorsDialog );
connect( mButtonExportColors, &QAbstractButton::clicked, mTreeCustomColors, &QgsColorSchemeList::showExportColorsDialog );

connect( mActionImportPalette, &QAction::triggered, this, [ = ]
{
if ( QgsCompoundColorWidget::importUserPaletteFromFile( this ) )
{
//refresh combobox
refreshSchemeComboBox();
mColorSchemesComboBox->setCurrentIndex( mColorSchemesComboBox->count() - 1 );
}
} );
connect( mActionRemovePalette, &QAction::triggered, this, [ = ]
{
//get current scheme
QList<QgsColorScheme *> schemeList = QgsApplication::colorSchemeRegistry()->schemes();
int prevIndex = mColorSchemesComboBox->currentIndex();
if ( prevIndex >= schemeList.length() )
{
return;
}

//make user scheme is a user removable scheme
QgsUserColorScheme *userScheme = dynamic_cast<QgsUserColorScheme *>( schemeList.at( prevIndex ) );
if ( !userScheme )
{
return;
}

if ( QgsCompoundColorWidget::removeUserPalette( userScheme, this ) )
{
refreshSchemeComboBox();
prevIndex = std::max( std::min( prevIndex, mColorSchemesComboBox->count() - 1 ), 0 );
mColorSchemesComboBox->setCurrentIndex( prevIndex );
}
} );
connect( mActionNewPalette, &QAction::triggered, this, [ = ]
{
if ( QgsCompoundColorWidget::createNewUserPalette( this ) )
{
//refresh combobox
refreshSchemeComboBox();
mColorSchemesComboBox->setCurrentIndex( mColorSchemesComboBox->count() - 1 );
}
} );

connect( mActionShowInButtons, &QAction::toggled, this, [ = ]( bool state )
{
QgsUserColorScheme *scheme = dynamic_cast< QgsUserColorScheme * >( mTreeCustomColors->scheme() );
if ( scheme )
{
scheme->setShowSchemeInMenu( state );
}
} );

QMenu *schemeMenu = new QMenu( mSchemeToolButton );
schemeMenu->addAction( mActionNewPalette );
schemeMenu->addAction( mActionImportPalette );
schemeMenu->addAction( mActionRemovePalette );
schemeMenu->addSeparator();
schemeMenu->addAction( mActionShowInButtons );
mSchemeToolButton->setMenu( schemeMenu );

//find custom color scheme from registry
refreshSchemeComboBox();
QList<QgsCustomColorScheme *> customSchemes;
Expand All @@ -762,6 +822,7 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl, const QList<QgsOpti
{
mTreeCustomColors->setScheme( customSchemes.at( 0 ) );
mColorSchemesComboBox->setCurrentIndex( mColorSchemesComboBox->findText( customSchemes.at( 0 )->schemeName() ) );
updateActionsForCurrentColorScheme( customSchemes.at( 0 ) );
}
connect( mColorSchemesComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]( int index )
{
Expand All @@ -774,8 +835,9 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl, const QList<QgsOpti
QgsColorScheme *scheme = QgsApplication::colorSchemeRegistry()->schemes().value( index );
if ( scheme )
mTreeCustomColors->setScheme( scheme );
} );

updateActionsForCurrentColorScheme( scheme );
} );

//
// Layout settings
Expand Down Expand Up @@ -2272,6 +2334,27 @@ void QgsOptions::refreshSchemeComboBox()
mColorSchemesComboBox->blockSignals( false );
}

void QgsOptions::updateActionsForCurrentColorScheme( QgsColorScheme *scheme )
{
mButtonImportColors->setEnabled( scheme->isEditable() );
mButtonPasteColors->setEnabled( scheme->isEditable() );
mButtonAddColor->setEnabled( scheme->isEditable() );
mButtonRemoveColor->setEnabled( scheme->isEditable() );

QgsUserColorScheme *userScheme = dynamic_cast<QgsUserColorScheme *>( scheme );
mActionRemovePalette->setEnabled( static_cast< bool >( userScheme ) && userScheme->isEditable() );
if ( userScheme )
{
mActionShowInButtons->setEnabled( true );
whileBlocking( mActionShowInButtons )->setChecked( userScheme->flags() & QgsColorScheme::ShowInColorButtonMenu );
}
else
{
whileBlocking( mActionShowInButtons )->setChecked( false );
mActionShowInButtons->setEnabled( false );
}
}

void QgsOptions::scaleItemChanged( QListWidgetItem *changedScaleItem )
{
// Check if the new value is valid, restore the old value if not.
Expand Down
4 changes: 4 additions & 0 deletions src/app/qgsoptions.h
Expand Up @@ -264,6 +264,10 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption

QList< QgsOptionsPageWidget * > mAdditionalOptionWidgets;
QgsLocatorOptionsWidget *mLocatorOptionsWidget = nullptr;

void updateActionsForCurrentColorScheme( QgsColorScheme *scheme );


};

#endif // #ifndef QGSOPTIONS_H
4 changes: 1 addition & 3 deletions src/core/qgscolorscheme.cpp
Expand Up @@ -303,8 +303,6 @@ QgsUserColorScheme::QgsUserColorScheme( const QString &filename )
: mFilename( filename )
{
QFile sourceFile( gplFilePath() );
QFileInfo sourceFileInfo( gplFilePath() );
mEditable = sourceFileInfo.isWritable();

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

QString QgsUserColorScheme::gplFilePath()
{
QString palettesDir = QgsApplication::qgisSettingsDirPath() + "/palettes";
QString palettesDir = QgsApplication::qgisSettingsDirPath() + "palettes";

QDir localDir;
if ( !localDir.mkpath( palettesDir ) )
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgscolorschemeregistry.cpp
Expand Up @@ -64,7 +64,7 @@ void QgsColorSchemeRegistry::initStyleScheme()

void QgsColorSchemeRegistry::addUserSchemes()
{
QString palettesDir = QgsApplication::qgisSettingsDirPath() + "/palettes";
QString palettesDir = QgsApplication::qgisSettingsDirPath() + "palettes";

QDir localDir;
if ( !localDir.mkpath( palettesDir ) )
Expand Down
97 changes: 61 additions & 36 deletions src/gui/qgscompoundcolorwidget.cpp
Expand Up @@ -280,23 +280,25 @@ void QgsCompoundColorWidget::refreshSchemeComboBox()
mSchemeComboBox->blockSignals( false );
}

void QgsCompoundColorWidget::importPalette()

QgsUserColorScheme *QgsCompoundColorWidget::importUserPaletteFromFile( QWidget *parent )
{
QgsSettings s;
QString lastDir = s.value( QStringLiteral( "/UI/lastGplPaletteDir" ), QDir::homePath() ).toString();
QString filePath = QFileDialog::getOpenFileName( this, tr( "Select Palette File" ), lastDir, QStringLiteral( "GPL (*.gpl);;All files (*.*)" ) );
activateWindow();
QString filePath = QFileDialog::getOpenFileName( parent, tr( "Select Palette File" ), lastDir, QStringLiteral( "GPL (*.gpl);;All files (*.*)" ) );
if ( parent )
parent->activateWindow();
if ( filePath.isEmpty() )
{
return;
return nullptr;
}

//check if file exists
QFileInfo fileInfo( filePath );
if ( !fileInfo.exists() || !fileInfo.isReadable() )
{
QMessageBox::critical( nullptr, tr( "Import Color Palette" ), tr( "Error, file does not exist or is not readable." ) );
return;
return nullptr;
}

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

if ( importedColors.length() == 0 )
{
//no imported colors
QMessageBox::critical( nullptr, tr( "Import Color Palette" ), tr( "No colors found in palette file." ) );
return;
return nullptr;
}

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

QgsApplication::colorSchemeRegistry()->addColorScheme( importedScheme );
return importedScheme;
}

//refresh combobox
refreshSchemeComboBox();
mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );
void QgsCompoundColorWidget::importPalette()
{
if ( importUserPaletteFromFile( this ) )
{
//refresh combobox
refreshSchemeComboBox();
mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );
}
}


bool QgsCompoundColorWidget::removeUserPalette( QgsUserColorScheme *scheme, QWidget *parent )
{
if ( QMessageBox::question( parent, tr( "Remove Color Palette" ),
QString( tr( "Are you sure you want to remove %1?" ) ).arg( scheme->schemeName() ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
{
//user canceled
return false;
}

//remove palette and associated gpl file
if ( !scheme->erase() )
{
//something went wrong
return false;
}

//remove scheme from registry
QgsApplication::colorSchemeRegistry()->removeColorScheme( scheme );
return true;
}

void QgsCompoundColorWidget::removePalette()
Expand All @@ -348,41 +380,27 @@ void QgsCompoundColorWidget::removePalette()
return;
}

if ( QMessageBox::question( this, tr( "Remove Color Palette" ),
QString( tr( "Are you sure you want to remove %1?" ) ).arg( userScheme->schemeName() ),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
{
//user canceled
return;
}

//remove palette and associated gpl file
if ( !userScheme->erase() )
if ( removeUserPalette( userScheme, this ) )
{
//something went wrong
return;
refreshSchemeComboBox();
prevIndex = std::max( std::min( prevIndex, mSchemeComboBox->count() - 1 ), 0 );
mSchemeComboBox->setCurrentIndex( prevIndex );
}

//remove scheme from registry
QgsApplication::colorSchemeRegistry()->removeColorScheme( userScheme );
refreshSchemeComboBox();
prevIndex = std::max( std::min( prevIndex, mSchemeComboBox->count() - 1 ), 0 );
mSchemeComboBox->setCurrentIndex( prevIndex );
}

void QgsCompoundColorWidget::newPalette()
QgsUserColorScheme *QgsCompoundColorWidget::createNewUserPalette( QWidget *parent )
{
bool ok = false;
QString name = QInputDialog::getText( this, tr( "Create New Palette" ), tr( "Enter a name for the new palette:" ),
QString name = QInputDialog::getText( parent, tr( "Create New Palette" ), tr( "Enter a name for the new palette:" ),
QLineEdit::Normal, tr( "New palette" ), &ok );

if ( !ok || name.isEmpty() )
{
//user canceled
return;
return nullptr;
}

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

QgsApplication::colorSchemeRegistry()->addColorScheme( newScheme );
return newScheme;
}

//refresh combobox and set new scheme as active
refreshSchemeComboBox();
mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );
void QgsCompoundColorWidget::newPalette()
{
if ( createNewUserPalette( this ) )
{
//refresh combobox and set new scheme as active
refreshSchemeComboBox();
mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );
}
}

QString QgsCompoundColorWidget::gplFilePath()
{
QString palettesDir = QgsApplication::qgisSettingsDirPath() + "/palettes";
QString palettesDir = QgsApplication::qgisSettingsDirPath() + "palettes";

QDir localDir;
if ( !localDir.mkpath( palettesDir ) )
Expand Down

0 comments on commit 14c8b3c

Please sign in to comment.