Skip to content

Commit

Permalink
Allow creation of new color palettes in color dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 22, 2014
1 parent 9b00978 commit 1704866
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/gui/qgscolordialog.cpp
Expand Up @@ -19,6 +19,7 @@
#include "qgscolorschemeregistry.h"
#include "qgssymbollayerv2utils.h"
#include "qgscursors.h"
#include "qgsapplication.h"
#include <QSettings>
#include <QPushButton>
#include <QMenu>
Expand All @@ -27,6 +28,7 @@
#include <QMessageBox>
#include <QDesktopWidget>
#include <QMouseEvent>
#include <QInputDialog>

QgsColorDialog::QgsColorDialog()
{
Expand Down Expand Up @@ -100,13 +102,15 @@ QgsColorDialogV2::QgsColorDialogV2( QWidget *parent, Qt::WindowFlags fl, const Q
connect( mActionImportColors, SIGNAL( triggered() ), this, SLOT( importColors() ) );
connect( mActionImportPalette, SIGNAL( triggered() ), this, SLOT( importPalette() ) );
connect( mActionRemovePalette, SIGNAL( triggered() ), this, SLOT( removePalette() ) );
connect( mActionNewPalette, SIGNAL( triggered() ), this, SLOT( newPalette() ) );
connect( mRemoveColorsFromSchemeButton, SIGNAL( clicked() ), mSchemeList, SLOT( removeSelection() ) );

QMenu* schemeMenu = new QMenu( mSchemeToolButton );
schemeMenu->addAction( mActionPasteColors );
schemeMenu->addAction( mActionImportColors );
schemeMenu->addAction( mActionExportColors );
schemeMenu->addSeparator();
schemeMenu->addAction( mActionNewPalette );
schemeMenu->addAction( mActionImportPalette );
schemeMenu->addAction( mActionRemovePalette );
mSchemeToolButton->setMenu( schemeMenu );
Expand Down Expand Up @@ -488,6 +492,58 @@ void QgsColorDialogV2::removePalette()
mSchemeComboBox->setCurrentIndex( prevIndex );
}

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

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

//generate file name for new palette
QDir palettePath( gplFilePath() );
QRegExp badChars( "[,^@={}\\[\\]~!?:&*\"|#%<>$\"'();`' /\\\\]" );
QString filename = name.simplified().toLower().replace( badChars, QString( "_" ) );
if ( filename.isEmpty() )
{
filename = tr( "new_palette" );
}
QFileInfo destFileInfo( palettePath.filePath( filename + ".gpl" ) );
int fileNumber = 1;
while ( destFileInfo.exists() )
{
//try to generate a unique file name
destFileInfo = QFileInfo( palettePath.filePath( filename + QString( "%1.gpl" ).arg( fileNumber ) ) );
fileNumber++;
}

QgsUserColorScheme* newScheme = new QgsUserColorScheme( destFileInfo.fileName() );
newScheme->setName( name );

QgsColorSchemeRegistry::instance()->addColorScheme( newScheme );

//refresh combobox and set new scheme as active
refreshSchemeComboBox();
mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );
}

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

QDir localDir;
if ( !localDir.mkpath( palettesDir ) )
{
return QString();
}

return palettesDir;
}

void QgsColorDialogV2::exportColors()
{
QSettings s;
Expand Down
5 changes: 5 additions & 0 deletions src/gui/qgscolordialog.h
Expand Up @@ -162,6 +162,7 @@ class GUI_EXPORT QgsColorDialogV2 : public QDialog, private Ui::QgsColorDialogBa
void importColors();
void importPalette();
void removePalette();
void newPalette();

void schemeIndexChanged( int index );

Expand Down Expand Up @@ -205,6 +206,10 @@ class GUI_EXPORT QgsColorDialogV2 : public QDialog, private Ui::QgsColorDialogBa
/**Repopulates the scheme combo box with current color schemes
*/
void refreshSchemeComboBox();

/**Returns the path to the user's palette folder
*/
QString gplFilePath();
};

#endif // #ifndef QGSCOLORDIALOG_H
8 changes: 8 additions & 0 deletions src/ui/qgscolordialog.ui
Expand Up @@ -827,6 +827,14 @@
<string>Remove current palette</string>
</property>
</action>
<action name="mActionNewPalette">
<property name="text">
<string>New Palette...</string>
</property>
<property name="toolTip">
<string>Create a new palette</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
Expand Down

0 comments on commit 1704866

Please sign in to comment.