Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add support for removing user palettes from color picker
  • Loading branch information
nyalldawson committed Sep 20, 2014
1 parent 6368adb commit 46c7599
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 14 deletions.
5 changes: 5 additions & 0 deletions python/core/qgscolorscheme.sip
Expand Up @@ -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();
Expand Down
12 changes: 12 additions & 0 deletions src/core/qgscolorscheme.cpp
Expand Up @@ -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";
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgscolorscheme.h
Expand Up @@ -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;
Expand Down
74 changes: 60 additions & 14 deletions src/gui/qgscolordialog.cpp
Expand Up @@ -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 );
Expand All @@ -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 ) ) );
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions src/gui/qgscolordialog.h
Expand Up @@ -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 );

Expand Down Expand Up @@ -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
8 changes: 8 additions & 0 deletions src/ui/qgscolordialog.ui
Expand Up @@ -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>
Expand Down

0 comments on commit 46c7599

Please sign in to comment.