Skip to content

Commit 39700e7

Browse files
committed
Option in color picker dialog to import a gpl palette to a new user scheme
1 parent 0181df5 commit 39700e7

File tree

10 files changed

+108
-9
lines changed

10 files changed

+108
-9
lines changed

python/core/qgscolorscheme.sip

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ class QgsUserColorScheme : QgsGplColorScheme
137137
virtual QgsColorScheme* clone() const;
138138

139139
virtual bool isEditable() const;
140+
141+
/**Sets the name for the scheme
142+
* @param name new name
143+
*/
144+
void setName( const QString name );
140145

141146
protected:
142147

python/core/symbology-ng/qgssymbollayerv2utils.sip

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,11 @@ class QgsSymbolLayerV2Utils
259259
* Imports colors from a gpl GIMP palette file
260260
* @param file source gpl file
261261
* @param ok will be true if file was successfully read
262+
* @param name will be set to palette name from gpl file, if present
262263
* @returns list of imported colors
263264
* @see saveColorsToGpl
264265
*/
265-
static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok );
266+
static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok, QString& name );
266267

267268
/**
268269
* Attempts to parse a string as a color using a variety of common formats, including hex

src/core/qgscolorscheme.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,9 @@ QgsNamedColorList QgsGplColorScheme::fetchColors( const QString context, const Q
266266
}
267267

268268
bool ok;
269+
QString name;
269270
QFile sourceFile( sourceFilePath );
270-
return QgsSymbolLayerV2Utils::importColorsFromGpl( sourceFile, ok );
271+
return QgsSymbolLayerV2Utils::importColorsFromGpl( sourceFile, ok, name );
271272
}
272273

273274
bool QgsGplColorScheme::setColors( const QgsNamedColorList colors, const QString context, const QColor baseColor )

src/core/qgscolorscheme.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ class CORE_EXPORT QgsUserColorScheme : public QgsGplColorScheme
150150

151151
virtual bool isEditable() const { return true; }
152152

153+
/**Sets the name for the scheme
154+
* @param name new name
155+
*/
156+
void setName( const QString name ) { mName = name; }
157+
153158
protected:
154159

155160
QString mName;

src/core/symbology-ng/qgssymbollayerv2utils.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3033,7 +3033,7 @@ bool QgsSymbolLayerV2Utils::saveColorsToGpl( QFile &file, const QString paletteN
30333033
return true;
30343034
}
30353035

3036-
QgsNamedColorList QgsSymbolLayerV2Utils::importColorsFromGpl( QFile &file, bool &ok )
3036+
QgsNamedColorList QgsSymbolLayerV2Utils::importColorsFromGpl( QFile &file, bool &ok, QString &name )
30373037
{
30383038
QgsNamedColorList importedColors;
30393039

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

3055+
//find name line
3056+
while ( !in.atEnd() && !line.startsWith( "Name:" ) && !line.startsWith( "#" ) )
3057+
{
3058+
line = in.readLine();
3059+
}
3060+
if ( line.startsWith( "Name:" ) )
3061+
{
3062+
QRegExp nameRx( "Name:\\s*(\\S.*)$" );
3063+
if ( nameRx.indexIn( line ) != -1 )
3064+
{
3065+
name = nameRx.cap( 1 );
3066+
}
3067+
}
3068+
30553069
//ignore lines until after "#"
30563070
while ( !in.atEnd() && !line.startsWith( "#" ) )
30573071
{
@@ -3064,10 +3078,10 @@ QgsNamedColorList QgsSymbolLayerV2Utils::importColorsFromGpl( QFile &file, bool
30643078
}
30653079

30663080
//ready to start reading colors
3081+
QRegExp rx( "^\\s*(\\d+)\\s+(\\d+)\\s+(\\d+)(\\s.*)?$" );
30673082
while ( !in.atEnd() )
30683083
{
30693084
line = in.readLine();
3070-
QRegExp rx( "^\\s*(\\d+)\\s+(\\d+)\\s+(\\d+)(\\s.*)?$" );
30713085
if ( rx.indexIn( line ) == -1 )
30723086
{
30733087
continue;

src/core/symbology-ng/qgssymbollayerv2utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,11 @@ class CORE_EXPORT QgsSymbolLayerV2Utils
297297
* Imports colors from a gpl GIMP palette file
298298
* @param file source gpl file
299299
* @param ok will be true if file was successfully read
300+
* @param name will be set to palette name from gpl file, if present
300301
* @returns list of imported colors
301302
* @see saveColorsToGpl
302303
*/
303-
static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok );
304+
static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok, QString& name );
304305

305306
/**
306307
* Attempts to parse a string as a color using a variety of common formats, including hex

src/gui/qgscolordialog.cpp

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ QgsColorDialogV2::QgsColorDialogV2( QWidget *parent, Qt::WindowFlags fl, const Q
8686
{
8787
mSchemeComboBox->addItem(( *schemeIt )->schemeName() );
8888
}
89-
int activeScheme = settings.value( "/Windows/ColorDialog/activeScheme", 0 ).toInt();
89+
int activeScheme = qMin( settings.value( "/Windows/ColorDialog/activeScheme", 0 ).toInt(), schemeList.length() - 1 );
9090
if ( activeScheme < schemeList.length() )
9191
{
9292
mSchemeList->setScheme( schemeList.at( activeScheme ) );
@@ -100,12 +100,15 @@ QgsColorDialogV2::QgsColorDialogV2( QWidget *parent, Qt::WindowFlags fl, const Q
100100
connect( mActionPasteColors, SIGNAL( triggered() ), mSchemeList, SLOT( pasteColors() ) );
101101
connect( mActionExportColors, SIGNAL( triggered() ), this, SLOT( exportColors() ) );
102102
connect( mActionImportColors, SIGNAL( triggered() ), this, SLOT( importColors() ) );
103+
connect( mActionImportPalette, SIGNAL( triggered() ), this, SLOT( importPalette() ) );
103104
connect( mRemoveColorsFromSchemeButton, SIGNAL( clicked() ), mSchemeList, SLOT( removeSelection() ) );
104105

105106
QMenu* schemeMenu = new QMenu( mSchemeToolButton );
106107
schemeMenu->addAction( mActionPasteColors );
107108
schemeMenu->addAction( mActionImportColors );
108109
schemeMenu->addAction( mActionExportColors );
110+
schemeMenu->addSeparator();
111+
schemeMenu->addAction( mActionImportPalette );
109112
mSchemeToolButton->setMenu( schemeMenu );
110113

111114
connect( mSchemeComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( schemeIndexChanged( int ) ) );
@@ -382,6 +385,65 @@ void QgsColorDialogV2::importColors()
382385
}
383386
}
384387

388+
void QgsColorDialogV2::importPalette()
389+
{
390+
QSettings s;
391+
QString lastDir = s.value( "/UI/lastGplPaletteDir", "" ).toString();
392+
QString filePath = QFileDialog::getOpenFileName( this, tr( "Select palette file" ), lastDir, "GPL (*.gpl);;All files (*.*)" );
393+
activateWindow();
394+
if ( filePath.isEmpty() )
395+
{
396+
return;
397+
}
398+
399+
//check if file exists
400+
QFileInfo fileInfo( filePath );
401+
if ( !fileInfo.exists() || !fileInfo.isReadable() )
402+
{
403+
QMessageBox::critical( 0, tr( "Invalid file" ), tr( "Error, file does not exist or is not readable" ) );
404+
return;
405+
}
406+
407+
s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );
408+
QFile file( filePath );
409+
410+
QgsNamedColorList importedColors;
411+
bool ok = false;
412+
QString paletteName;
413+
importedColors = QgsSymbolLayerV2Utils::importColorsFromGpl( file, ok, paletteName );
414+
if ( !ok )
415+
{
416+
QMessageBox::critical( 0, tr( "Invalid file" ), tr( "Palette file is not readable" ) );
417+
return;
418+
}
419+
420+
if ( importedColors.length() == 0 )
421+
{
422+
//no imported colors
423+
QMessageBox::critical( 0, tr( "Invalid file" ), tr( "No colors found in palette file" ) );
424+
return;
425+
}
426+
427+
//TODO - handle conflicting file names, name for new palette
428+
QgsUserColorScheme* importedScheme = new QgsUserColorScheme( fileInfo.fileName() );
429+
importedScheme->setName( paletteName );
430+
importedScheme->setColors( importedColors );
431+
432+
QgsColorSchemeRegistry::instance()->addColorScheme( importedScheme );
433+
434+
//refresh combobox
435+
mSchemeComboBox->blockSignals( true );
436+
mSchemeComboBox->clear();
437+
QList<QgsColorScheme *> schemeList = QgsColorSchemeRegistry::instance()->schemes( QgsColorScheme::ShowInColorDialog );
438+
QList<QgsColorScheme *>::const_iterator schemeIt = schemeList.constBegin();
439+
for ( ; schemeIt != schemeList.constEnd(); ++schemeIt )
440+
{
441+
mSchemeComboBox->addItem(( *schemeIt )->schemeName() );
442+
}
443+
mSchemeComboBox->blockSignals( false );
444+
mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );
445+
}
446+
385447
void QgsColorDialogV2::exportColors()
386448
{
387449
QSettings s;

src/gui/qgscolordialog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ class GUI_EXPORT QgsColorDialogV2 : public QDialog, private Ui::QgsColorDialogBa
160160

161161
void exportColors();
162162
void importColors();
163+
void importPalette();
163164

164165
void schemeIndexChanged( int index );
165166

src/gui/qgscolorschemelist.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ bool QgsColorSchemeList::importColorsFromGpl( QFile &file )
191191
{
192192
QgsNamedColorList importedColors;
193193
bool ok = false;
194-
importedColors = QgsSymbolLayerV2Utils::importColorsFromGpl( file, ok );
194+
QString name;
195+
importedColors = QgsSymbolLayerV2Utils::importColorsFromGpl( file, ok, name );
195196
if ( !ok )
196197
{
197198
return false;

src/ui/qgscolordialog.ui

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,15 +789,15 @@
789789
</layout>
790790
<action name="mActionImportColors">
791791
<property name="text">
792-
<string>Import Colors</string>
792+
<string>Import Colors...</string>
793793
</property>
794794
<property name="toolTip">
795795
<string>Import colors from file</string>
796796
</property>
797797
</action>
798798
<action name="mActionExportColors">
799799
<property name="text">
800-
<string>Export Colors</string>
800+
<string>Export Colors...</string>
801801
</property>
802802
<property name="toolTip">
803803
<string>Export colors to file</string>
@@ -811,6 +811,14 @@
811811
<string>Paste colors from clipboard</string>
812812
</property>
813813
</action>
814+
<action name="mActionImportPalette">
815+
<property name="text">
816+
<string>Import Palette...</string>
817+
</property>
818+
<property name="toolTip">
819+
<string>Import palette from file</string>
820+
</property>
821+
</action>
814822
</widget>
815823
<customwidgets>
816824
<customwidget>

0 commit comments

Comments
 (0)