Skip to content
Permalink
Browse files

Allow creation of new color palettes in color dialog

  • Loading branch information
nyalldawson committed Sep 22, 2014
1 parent 9b00978 commit 1704866b6092dfefd709dfd0de5a13fcb3247692
Showing with 69 additions and 0 deletions.
  1. +56 −0 src/gui/qgscolordialog.cpp
  2. +5 −0 src/gui/qgscolordialog.h
  3. +8 −0 src/ui/qgscolordialog.ui
@@ -19,6 +19,7 @@
#include "qgscolorschemeregistry.h"
#include "qgssymbollayerv2utils.h"
#include "qgscursors.h"
#include "qgsapplication.h"
#include <QSettings>
#include <QPushButton>
#include <QMenu>
@@ -27,6 +28,7 @@
#include <QMessageBox>
#include <QDesktopWidget>
#include <QMouseEvent>
#include <QInputDialog>

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

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

0 comments on commit 1704866

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