Skip to content
Permalink
Browse files

Add support for removing user palettes from color picker

  • Loading branch information
nyalldawson committed Sep 18, 2014
1 parent 6368adb commit 46c75994a50f9534865aa9212391853f2bb8b27f
@@ -143,6 +143,11 @@ class QgsUserColorScheme : QgsGplColorScheme
*/
void setName( const QString name );

/**Erases the associated gpl palette file from the users "palettes" folder
* @returns true if erase was successful
*/
bool erase();

protected:

virtual QString gplFilePath();
@@ -338,6 +338,18 @@ QgsColorScheme *QgsUserColorScheme::clone() const
return new QgsUserColorScheme( mFilename );
}

bool QgsUserColorScheme::erase()
{
QString filePath = gplFilePath();
if ( filePath.isEmpty() )
{
return false;
}

//try to erase gpl file
return QFile::remove( filePath );
}

QString QgsUserColorScheme::gplFilePath()
{
QString palettesDir = QgsApplication::qgisSettingsDirPath() + "/palettes";
@@ -155,6 +155,11 @@ class CORE_EXPORT QgsUserColorScheme : public QgsGplColorScheme
*/
void setName( const QString name ) { mName = name; }

/**Erases the associated gpl palette file from the users "palettes" folder
* @returns true if erase was successful
*/
bool erase();

protected:

QString mName;
@@ -80,27 +80,26 @@ QgsColorDialogV2::QgsColorDialogV2( QWidget *parent, Qt::WindowFlags fl, const Q
mSchemeList->setColumnWidth( 0, 44 );

//get schemes with ShowInColorDialog set
refreshSchemeComboBox();
QList<QgsColorScheme *> schemeList = QgsColorSchemeRegistry::instance()->schemes( QgsColorScheme::ShowInColorDialog );
QList<QgsColorScheme *>::const_iterator schemeIt = schemeList.constBegin();
for ( ; schemeIt != schemeList.constEnd(); ++schemeIt )
{
mSchemeComboBox->addItem(( *schemeIt )->schemeName() );
}
int activeScheme = qMin( settings.value( "/Windows/ColorDialog/activeScheme", 0 ).toInt(), schemeList.length() - 1 );
if ( activeScheme < schemeList.length() )
int activeScheme = settings.value( "/Windows/ColorDialog/activeScheme", 0 ).toInt();
if ( activeScheme < mSchemeComboBox->count() )
{
mSchemeList->setScheme( schemeList.at( activeScheme ) );
mSchemeComboBox->setCurrentIndex( activeScheme );
mActionImportColors->setEnabled( schemeList.at( activeScheme )->isEditable() );
mActionPasteColors->setEnabled( schemeList.at( activeScheme )->isEditable() );
mAddColorToSchemeButton->setEnabled( schemeList.at( activeScheme )->isEditable() );
mRemoveColorsFromSchemeButton->setEnabled( schemeList.at( activeScheme )->isEditable() );
QgsUserColorScheme* userScheme = dynamic_cast<QgsUserColorScheme*>( schemeList.at( activeScheme ) );
mActionRemovePalette->setEnabled( userScheme ? true : false );
}

connect( mActionPasteColors, SIGNAL( triggered() ), mSchemeList, SLOT( pasteColors() ) );
connect( mActionExportColors, SIGNAL( triggered() ), this, SLOT( exportColors() ) );
connect( mActionImportColors, SIGNAL( triggered() ), this, SLOT( importColors() ) );
connect( mActionImportPalette, SIGNAL( triggered() ), this, SLOT( importPalette() ) );
connect( mActionRemovePalette, SIGNAL( triggered() ), this, SLOT( removePalette() ) );
connect( mRemoveColorsFromSchemeButton, SIGNAL( clicked() ), mSchemeList, SLOT( removeSelection() ) );

QMenu* schemeMenu = new QMenu( mSchemeToolButton );
@@ -109,6 +108,7 @@ QgsColorDialogV2::QgsColorDialogV2( QWidget *parent, Qt::WindowFlags fl, const Q
schemeMenu->addAction( mActionExportColors );
schemeMenu->addSeparator();
schemeMenu->addAction( mActionImportPalette );
schemeMenu->addAction( mActionRemovePalette );
mSchemeToolButton->setMenu( schemeMenu );

connect( mSchemeComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( schemeIndexChanged( int ) ) );
@@ -385,6 +385,19 @@ void QgsColorDialogV2::importColors()
}
}

void QgsColorDialogV2::refreshSchemeComboBox()
{
mSchemeComboBox->blockSignals( true );
mSchemeComboBox->clear();
QList<QgsColorScheme *> schemeList = QgsColorSchemeRegistry::instance()->schemes( QgsColorScheme::ShowInColorDialog );
QList<QgsColorScheme *>::const_iterator schemeIt = schemeList.constBegin();
for ( ; schemeIt != schemeList.constEnd(); ++schemeIt )
{
mSchemeComboBox->addItem(( *schemeIt )->schemeName() );
}
mSchemeComboBox->blockSignals( false );
}

void QgsColorDialogV2::importPalette()
{
QSettings s;
@@ -432,16 +445,47 @@ void QgsColorDialogV2::importPalette()
QgsColorSchemeRegistry::instance()->addColorScheme( importedScheme );

//refresh combobox
mSchemeComboBox->blockSignals( true );
mSchemeComboBox->clear();
refreshSchemeComboBox();
mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );
}

void QgsColorDialogV2::removePalette()
{
//get current scheme
QList<QgsColorScheme *> schemeList = QgsColorSchemeRegistry::instance()->schemes( QgsColorScheme::ShowInColorDialog );
QList<QgsColorScheme *>::const_iterator schemeIt = schemeList.constBegin();
for ( ; schemeIt != schemeList.constEnd(); ++schemeIt )
int prevIndex = mSchemeComboBox->currentIndex();
if ( prevIndex >= schemeList.length() )
{
mSchemeComboBox->addItem(( *schemeIt )->schemeName() );
return;
}
mSchemeComboBox->blockSignals( false );
mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );

//make user scheme is a user removable scheme
QgsUserColorScheme* userScheme = dynamic_cast<QgsUserColorScheme*>( schemeList.at( prevIndex ) );
if ( !userScheme )
{
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 cancelled
return;
}

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

//remove scheme from registry
QgsColorSchemeRegistry::instance()->removeColorScheme( userScheme );
refreshSchemeComboBox();
prevIndex = qMax( qMin( prevIndex, mSchemeComboBox->count() - 1 ), 0 );
mSchemeComboBox->setCurrentIndex( prevIndex );
}

void QgsColorDialogV2::exportColors()
@@ -494,6 +538,8 @@ void QgsColorDialogV2::schemeIndexChanged( int index )
mActionPasteColors->setEnabled( scheme->isEditable() );
mAddColorToSchemeButton->setEnabled( scheme->isEditable() );
mRemoveColorsFromSchemeButton->setEnabled( scheme->isEditable() );
QgsUserColorScheme* userScheme = dynamic_cast<QgsUserColorScheme*>( scheme );
mActionRemovePalette->setEnabled( userScheme ? true : false );
}

void QgsColorDialogV2::on_mAddCustomColorButton_clicked()
@@ -161,6 +161,7 @@ class GUI_EXPORT QgsColorDialogV2 : public QDialog, private Ui::QgsColorDialogBa
void exportColors();
void importColors();
void importPalette();
void removePalette();

void schemeIndexChanged( int index );

@@ -200,6 +201,10 @@ class GUI_EXPORT QgsColorDialogV2 : public QDialog, private Ui::QgsColorDialogBa
* @returns average color from sampled position
*/
QColor sampleColor( const QPoint &point ) const;

/**Repopulates the scheme combo box with current color schemes
*/
void refreshSchemeComboBox();
};

#endif // #ifndef QGSCOLORDIALOG_H
@@ -819,6 +819,14 @@
<string>Import palette from file</string>
</property>
</action>
<action name="mActionRemovePalette">
<property name="text">
<string>Remove Palette</string>
</property>
<property name="toolTip">
<string>Remove current palette</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>

0 comments on commit 46c7599

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