Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Option in color picker dialog to import a gpl palette to a new user s…
…cheme
  • Loading branch information
nyalldawson committed Sep 17, 2014
1 parent 0181df5 commit 39700e7
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 9 deletions.
5 changes: 5 additions & 0 deletions python/core/qgscolorscheme.sip
Expand Up @@ -137,6 +137,11 @@ class QgsUserColorScheme : QgsGplColorScheme
virtual QgsColorScheme* clone() const;

virtual bool isEditable() const;

/**Sets the name for the scheme
* @param name new name
*/
void setName( const QString name );

protected:

Expand Down
3 changes: 2 additions & 1 deletion python/core/symbology-ng/qgssymbollayerv2utils.sip
Expand Up @@ -259,10 +259,11 @@ class QgsSymbolLayerV2Utils
* Imports colors from a gpl GIMP palette file
* @param file source gpl file
* @param ok will be true if file was successfully read
* @param name will be set to palette name from gpl file, if present
* @returns list of imported colors
* @see saveColorsToGpl
*/
static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok );
static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok, QString& name );

/**
* Attempts to parse a string as a color using a variety of common formats, including hex
Expand Down
3 changes: 2 additions & 1 deletion src/core/qgscolorscheme.cpp
Expand Up @@ -266,8 +266,9 @@ QgsNamedColorList QgsGplColorScheme::fetchColors( const QString context, const Q
}

bool ok;
QString name;
QFile sourceFile( sourceFilePath );
return QgsSymbolLayerV2Utils::importColorsFromGpl( sourceFile, ok );
return QgsSymbolLayerV2Utils::importColorsFromGpl( sourceFile, ok, name );
}

bool QgsGplColorScheme::setColors( const QgsNamedColorList colors, const QString context, const QColor baseColor )
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgscolorscheme.h
Expand Up @@ -150,6 +150,11 @@ class CORE_EXPORT QgsUserColorScheme : public QgsGplColorScheme

virtual bool isEditable() const { return true; }

/**Sets the name for the scheme
* @param name new name
*/
void setName( const QString name ) { mName = name; }

protected:

QString mName;
Expand Down
18 changes: 16 additions & 2 deletions src/core/symbology-ng/qgssymbollayerv2utils.cpp
Expand Up @@ -3033,7 +3033,7 @@ bool QgsSymbolLayerV2Utils::saveColorsToGpl( QFile &file, const QString paletteN
return true;
}

QgsNamedColorList QgsSymbolLayerV2Utils::importColorsFromGpl( QFile &file, bool &ok )
QgsNamedColorList QgsSymbolLayerV2Utils::importColorsFromGpl( QFile &file, bool &ok, QString &name )
{
QgsNamedColorList importedColors;

Expand All @@ -3052,6 +3052,20 @@ QgsNamedColorList QgsSymbolLayerV2Utils::importColorsFromGpl( QFile &file, bool
return importedColors;
}

//find name line
while ( !in.atEnd() && !line.startsWith( "Name:" ) && !line.startsWith( "#" ) )
{
line = in.readLine();
}
if ( line.startsWith( "Name:" ) )
{
QRegExp nameRx( "Name:\\s*(\\S.*)$" );
if ( nameRx.indexIn( line ) != -1 )
{
name = nameRx.cap( 1 );
}
}

//ignore lines until after "#"
while ( !in.atEnd() && !line.startsWith( "#" ) )
{
Expand All @@ -3064,10 +3078,10 @@ QgsNamedColorList QgsSymbolLayerV2Utils::importColorsFromGpl( QFile &file, bool
}

//ready to start reading colors
QRegExp rx( "^\\s*(\\d+)\\s+(\\d+)\\s+(\\d+)(\\s.*)?$" );
while ( !in.atEnd() )
{
line = in.readLine();
QRegExp rx( "^\\s*(\\d+)\\s+(\\d+)\\s+(\\d+)(\\s.*)?$" );
if ( rx.indexIn( line ) == -1 )
{
continue;
Expand Down
3 changes: 2 additions & 1 deletion src/core/symbology-ng/qgssymbollayerv2utils.h
Expand Up @@ -297,10 +297,11 @@ class CORE_EXPORT QgsSymbolLayerV2Utils
* Imports colors from a gpl GIMP palette file
* @param file source gpl file
* @param ok will be true if file was successfully read
* @param name will be set to palette name from gpl file, if present
* @returns list of imported colors
* @see saveColorsToGpl
*/
static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok );
static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok, QString& name );

/**
* Attempts to parse a string as a color using a variety of common formats, including hex
Expand Down
64 changes: 63 additions & 1 deletion src/gui/qgscolordialog.cpp
Expand Up @@ -86,7 +86,7 @@ QgsColorDialogV2::QgsColorDialogV2( QWidget *parent, Qt::WindowFlags fl, const Q
{
mSchemeComboBox->addItem(( *schemeIt )->schemeName() );
}
int activeScheme = settings.value( "/Windows/ColorDialog/activeScheme", 0 ).toInt();
int activeScheme = qMin( settings.value( "/Windows/ColorDialog/activeScheme", 0 ).toInt(), schemeList.length() - 1 );
if ( activeScheme < schemeList.length() )
{
mSchemeList->setScheme( schemeList.at( activeScheme ) );
Expand All @@ -100,12 +100,15 @@ QgsColorDialogV2::QgsColorDialogV2( QWidget *parent, Qt::WindowFlags fl, const Q
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( mRemoveColorsFromSchemeButton, SIGNAL( clicked() ), mSchemeList, SLOT( removeSelection() ) );

QMenu* schemeMenu = new QMenu( mSchemeToolButton );
schemeMenu->addAction( mActionPasteColors );
schemeMenu->addAction( mActionImportColors );
schemeMenu->addAction( mActionExportColors );
schemeMenu->addSeparator();
schemeMenu->addAction( mActionImportPalette );
mSchemeToolButton->setMenu( schemeMenu );

connect( mSchemeComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( schemeIndexChanged( int ) ) );
Expand Down Expand Up @@ -382,6 +385,65 @@ void QgsColorDialogV2::importColors()
}
}

void QgsColorDialogV2::importPalette()
{
QSettings s;
QString lastDir = s.value( "/UI/lastGplPaletteDir", "" ).toString();
QString filePath = QFileDialog::getOpenFileName( this, tr( "Select palette file" ), lastDir, "GPL (*.gpl);;All files (*.*)" );
activateWindow();
if ( filePath.isEmpty() )
{
return;
}

//check if file exists
QFileInfo fileInfo( filePath );
if ( !fileInfo.exists() || !fileInfo.isReadable() )
{
QMessageBox::critical( 0, tr( "Invalid file" ), tr( "Error, file does not exist or is not readable" ) );
return;
}

s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );
QFile file( filePath );

QgsNamedColorList importedColors;
bool ok = false;
QString paletteName;
importedColors = QgsSymbolLayerV2Utils::importColorsFromGpl( file, ok, paletteName );
if ( !ok )
{
QMessageBox::critical( 0, tr( "Invalid file" ), tr( "Palette file is not readable" ) );
return;
}

if ( importedColors.length() == 0 )
{
//no imported colors
QMessageBox::critical( 0, tr( "Invalid file" ), tr( "No colors found in palette file" ) );
return;
}

//TODO - handle conflicting file names, name for new palette
QgsUserColorScheme* importedScheme = new QgsUserColorScheme( fileInfo.fileName() );
importedScheme->setName( paletteName );
importedScheme->setColors( importedColors );

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 );
mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );
}

void QgsColorDialogV2::exportColors()
{
QSettings s;
Expand Down
1 change: 1 addition & 0 deletions src/gui/qgscolordialog.h
Expand Up @@ -160,6 +160,7 @@ class GUI_EXPORT QgsColorDialogV2 : public QDialog, private Ui::QgsColorDialogBa

void exportColors();
void importColors();
void importPalette();

void schemeIndexChanged( int index );

Expand Down
3 changes: 2 additions & 1 deletion src/gui/qgscolorschemelist.cpp
Expand Up @@ -191,7 +191,8 @@ bool QgsColorSchemeList::importColorsFromGpl( QFile &file )
{
QgsNamedColorList importedColors;
bool ok = false;
importedColors = QgsSymbolLayerV2Utils::importColorsFromGpl( file, ok );
QString name;
importedColors = QgsSymbolLayerV2Utils::importColorsFromGpl( file, ok, name );
if ( !ok )
{
return false;
Expand Down
12 changes: 10 additions & 2 deletions src/ui/qgscolordialog.ui
Expand Up @@ -789,15 +789,15 @@
</layout>
<action name="mActionImportColors">
<property name="text">
<string>Import Colors</string>
<string>Import Colors...</string>
</property>
<property name="toolTip">
<string>Import colors from file</string>
</property>
</action>
<action name="mActionExportColors">
<property name="text">
<string>Export Colors</string>
<string>Export Colors...</string>
</property>
<property name="toolTip">
<string>Export colors to file</string>
Expand All @@ -811,6 +811,14 @@
<string>Paste colors from clipboard</string>
</property>
</action>
<action name="mActionImportPalette">
<property name="text">
<string>Import Palette...</string>
</property>
<property name="toolTip">
<string>Import palette from file</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
Expand Down

0 comments on commit 39700e7

Please sign in to comment.