Add support for removing user palettes from color picker

This commit is contained in:
Nyall Dawson 2014-09-18 20:29:00 +10:00
parent 6368adbe24
commit 46c75994a5
6 changed files with 97 additions and 16 deletions

View File

@ -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();

View File

@ -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";

View File

@ -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;

View File

@ -80,14 +80,10 @@ 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 );
@ -95,12 +91,15 @@ QgsColorDialogV2::QgsColorDialogV2( QWidget *parent, Qt::WindowFlags fl, const Q
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,18 +445,49 @@ void QgsColorDialogV2::importPalette()
QgsColorSchemeRegistry::instance()->addColorScheme( importedScheme );
//refresh combobox
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 );
refreshSchemeComboBox();
mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );
}
void QgsColorDialogV2::removePalette()
{
//get current scheme
QList<QgsColorScheme *> schemeList = QgsColorSchemeRegistry::instance()->schemes( QgsColorScheme::ShowInColorDialog );
int prevIndex = mSchemeComboBox->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 ( 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()
{
QSettings s;
@ -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()

View File

@ -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

View File

@ -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>