Skip to content
Permalink
Browse files

[neeeds-docs] Show all color schemes and tools for interacting with t…

…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 Feb 26, 2018
1 parent 2a0f364 commit 14c8b3c75d38e755cccdc0508937ce5fabf38c4b
@@ -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:
@@ -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;
@@ -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 )
{
@@ -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
@@ -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.
@@ -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
@@ -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 ) )
@@ -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 ) )
@@ -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 ) )
@@ -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() );
@@ -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
@@ -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()
@@ -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( "_" ) );
@@ -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 ) )

0 comments on commit 14c8b3c

Please sign in to comment.
You can’t perform that action at this time.