Skip to content

Commit b15b014

Browse files
committed
Allow importing and exporting to GIMP palette file (gpl) from color list
1 parent b457355 commit b15b014

13 files changed

+575
-42
lines changed

python/core/symbology-ng/qgssymbollayerv2utils.sip

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,25 @@ class QgsSymbolLayerV2Utils
217217
*/
218218
static QMimeData* colorListToMimeData( const QgsNamedColorList colorList, const bool allFormats = true );
219219

220+
/**
221+
* Exports colors to a gpl GIMP palette file
222+
* @param file destination file
223+
* @param paletteName name of palette, which is stored in gpl file
224+
* @param colors colors to export
225+
* @returns true if export was successful
226+
* @see importColorsFromGpl
227+
*/
228+
static bool saveColorsToGpl( QFile &file, const QString paletteName, QgsNamedColorList colors );
229+
230+
/**
231+
* Imports colors from a gpl GIMP palette file
232+
* @param file source gpl file
233+
* @param ok will be true if file was successfully read
234+
* @returns list of imported colors
235+
* @see saveColorsToGpl
236+
*/
237+
static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok );
238+
220239
/**
221240
* Attempts to parse a string as a color using a variety of common formats, including hex
222241
* codes, rgb and rgba strings.

python/gui/qgscolorschemelist.sip

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,26 @@ class QgsColorSchemeList: QTreeView
120120
*/
121121
void setScheme( QgsColorScheme* scheme, const QString context = QString(), const QColor baseColor = QColor() );
122122

123-
public slots:
124-
125123
/**Saves the current colors shown in the list back to a color scheme, if supported
126124
* by the color scheme.
127125
* @note this method is only effective if the color scheme is editable
128126
*/
129127
bool saveColorsToScheme();
130128

129+
/**Import colors from a GPL palette file to the list
130+
* @param file file to import
131+
* @see exportColorsToGpl
132+
*/
133+
bool importColorsFromGpl( QFile &file );
134+
135+
/**Export colors to a GPL palette file from the list
136+
* @param file destination file
137+
* @see importColorsFromGpl
138+
*/
139+
bool exportColorsToGpl( QFile &file );
140+
141+
public slots:
142+
131143
/**Removes any selected colors from the list
132144
*/
133145
void removeSelection();

src/app/qgsoptions.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,3 +2089,61 @@ void QgsOptions::on_mButtonAddColor_clicked()
20892089

20902090
mTreeCustomColors->addColor( newColor );
20912091
}
2092+
2093+
void QgsOptions::on_mButtonImportColors_clicked()
2094+
{
2095+
QSettings s;
2096+
QString lastDir = s.value( "/UI/lastGplPaletteDir", "" ).toString();
2097+
QString filePath = QFileDialog::getOpenFileName( this, tr( "Select palette file" ), lastDir, "GPL (*.gpl);;All files (*.*)" );
2098+
activateWindow();
2099+
if ( filePath.isEmpty() )
2100+
{
2101+
return;
2102+
}
2103+
2104+
//check if file exists
2105+
QFileInfo fileInfo( filePath );
2106+
if ( !fileInfo.exists() || !fileInfo.isReadable() )
2107+
{
2108+
QMessageBox::critical( 0, tr( "Invalid file" ), tr( "Error, file does not exist or is not readable" ) );
2109+
return;
2110+
}
2111+
2112+
s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );
2113+
QFile file( filePath );
2114+
bool importOk = mTreeCustomColors->importColorsFromGpl( file );
2115+
if ( !importOk )
2116+
{
2117+
QMessageBox::critical( 0, tr( "Invalid file" ), tr( "Error, no colors found in palette file" ) );
2118+
return;
2119+
}
2120+
}
2121+
2122+
void QgsOptions::on_mButtonExportColors_clicked()
2123+
{
2124+
QSettings s;
2125+
QString lastDir = s.value( "/UI/lastGplPaletteDir", "" ).toString();
2126+
QString fileName = QFileDialog::getSaveFileName( this, tr( "Palette file" ), lastDir, "GPL (*.gpl)" );
2127+
activateWindow();
2128+
if ( fileName.isEmpty() )
2129+
{
2130+
return;
2131+
}
2132+
2133+
// ensure filename contains extension
2134+
if ( !fileName.toLower().endsWith( ".gpl" ) )
2135+
{
2136+
fileName += ".gpl";
2137+
}
2138+
2139+
QFileInfo fileInfo( fileName );
2140+
s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );
2141+
2142+
QFile file( fileName );
2143+
bool exportOk = mTreeCustomColors->exportColorsToGpl( file );
2144+
if ( !exportOk )
2145+
{
2146+
QMessageBox::critical( 0, tr( "Error exporting" ), tr( "Error writing palette file" ) );
2147+
return;
2148+
}
2149+
}

src/app/qgsoptions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption
242242
void on_mAddDefaultTransformButton_clicked();
243243

244244
void on_mButtonAddColor_clicked();
245+
void on_mButtonImportColors_clicked();
246+
void on_mButtonExportColors_clicked();
245247

246248
private:
247249
QStringList i18nList();

src/app/qgsprojectproperties.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,3 +1684,61 @@ void QgsProjectProperties::on_mButtonAddColor_clicked()
16841684

16851685
mTreeProjectColors->addColor( newColor );
16861686
}
1687+
1688+
void QgsProjectProperties::on_mButtonImportColors_clicked()
1689+
{
1690+
QSettings s;
1691+
QString lastDir = s.value( "/UI/lastGplPaletteDir", "" ).toString();
1692+
QString filePath = QFileDialog::getOpenFileName( this, tr( "Select palette file" ), lastDir, "GPL (*.gpl);;All files (*.*)" );
1693+
activateWindow();
1694+
if ( filePath.isEmpty() )
1695+
{
1696+
return;
1697+
}
1698+
1699+
//check if file exists
1700+
QFileInfo fileInfo( filePath );
1701+
if ( !fileInfo.exists() || !fileInfo.isReadable() )
1702+
{
1703+
QMessageBox::critical( 0, tr( "Invalid file" ), tr( "Error, file does not exist or is not readable" ) );
1704+
return;
1705+
}
1706+
1707+
s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );
1708+
QFile file( filePath );
1709+
bool importOk = mTreeProjectColors->importColorsFromGpl( file );
1710+
if ( !importOk )
1711+
{
1712+
QMessageBox::critical( 0, tr( "Invalid file" ), tr( "Error, no colors found in palette file" ) );
1713+
return;
1714+
}
1715+
}
1716+
1717+
void QgsProjectProperties::on_mButtonExportColors_clicked()
1718+
{
1719+
QSettings s;
1720+
QString lastDir = s.value( "/UI/lastGplPaletteDir", "" ).toString();
1721+
QString fileName = QFileDialog::getSaveFileName( this, tr( "Palette file" ), lastDir, "GPL (*.gpl)" );
1722+
activateWindow();
1723+
if ( fileName.isEmpty() )
1724+
{
1725+
return;
1726+
}
1727+
1728+
// ensure filename contains extension
1729+
if ( !fileName.toLower().endsWith( ".gpl" ) )
1730+
{
1731+
fileName += ".gpl";
1732+
}
1733+
1734+
QFileInfo fileInfo( fileName );
1735+
s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );
1736+
1737+
QFile file( fileName );
1738+
bool exportOk = mTreeProjectColors->exportColorsToGpl( file );
1739+
if ( !exportOk )
1740+
{
1741+
QMessageBox::critical( 0, tr( "Error exporting" ), tr( "Error writing palette file" ) );
1742+
return;
1743+
}
1744+
}

src/app/qgsprojectproperties.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ class APP_EXPORT QgsProjectProperties : public QgsOptionsDialogBase, private Ui:
167167
void projectionSelectorInitialized();
168168

169169
void on_mButtonAddColor_clicked();
170+
void on_mButtonImportColors_clicked();
171+
void on_mButtonExportColors_clicked();
170172

171173
signals:
172174
//! Signal used to inform listeners that the mouse display precision may have changed

src/core/qgscolorscheme.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ class CORE_EXPORT QgsColorScheme
8181
* @returns copy of color scheme
8282
*/
8383
virtual QgsColorScheme* clone() const = 0;
84-
8584
};
8685

8786
/** \ingroup core

src/core/symbology-ng/qgssymbollayerv2utils.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2953,6 +2953,101 @@ QMimeData* QgsSymbolLayerV2Utils::colorListToMimeData( const QgsNamedColorList c
29532953
return mimeData;
29542954
}
29552955

2956+
bool QgsSymbolLayerV2Utils::saveColorsToGpl( QFile &file, const QString paletteName, QgsNamedColorList colors )
2957+
{
2958+
if ( !file.open( QIODevice::ReadWrite ) )
2959+
{
2960+
return false;
2961+
}
2962+
2963+
QTextStream stream( &file );
2964+
stream << "GIMP Palette" << endl;
2965+
if ( paletteName.isEmpty() )
2966+
{
2967+
stream << "Name: QGIS Palette" << endl;
2968+
}
2969+
else
2970+
{
2971+
stream << "Name: " << paletteName << endl;
2972+
}
2973+
stream << "Columns: 4" << endl;
2974+
stream << "#" << endl;
2975+
2976+
for ( QgsNamedColorList::ConstIterator colorIt = colors.constBegin(); colorIt != colors.constEnd(); ++colorIt )
2977+
{
2978+
QColor color = ( *colorIt ).first;
2979+
if ( !color.isValid() )
2980+
{
2981+
continue;
2982+
}
2983+
stream << QString( "%1 %2 %3" ).arg( color.red(), 3 ).arg( color.green(), 3 ).arg( color.blue(), 3 );
2984+
stream << "\t" << (( *colorIt ).second.isEmpty() ? color.name() : ( *colorIt ).second ) << endl;
2985+
}
2986+
file.close();
2987+
2988+
return true;
2989+
}
2990+
2991+
QgsNamedColorList QgsSymbolLayerV2Utils::importColorsFromGpl( QFile &file, bool &ok )
2992+
{
2993+
QgsNamedColorList importedColors;
2994+
2995+
if ( !file.open( QIODevice::ReadOnly ) )
2996+
{
2997+
ok = false;
2998+
return importedColors;
2999+
}
3000+
3001+
QTextStream in( &file );
3002+
3003+
QString line = in.readLine();
3004+
if ( !line.startsWith( "GIMP Palette" ) )
3005+
{
3006+
ok = false;
3007+
return importedColors;
3008+
}
3009+
3010+
//ignore lines until after "#"
3011+
while ( !in.atEnd() && !line.startsWith( "#" ) )
3012+
{
3013+
line = in.readLine();
3014+
}
3015+
if ( in.atEnd() )
3016+
{
3017+
ok = false;
3018+
return importedColors;
3019+
}
3020+
3021+
//ready to start reading colors
3022+
while ( !in.atEnd() )
3023+
{
3024+
line = in.readLine();
3025+
QStringList parts = line.simplified().split( " " );
3026+
if ( parts.length() < 3 )
3027+
{
3028+
continue;
3029+
}
3030+
int red = parts.at( 0 ).toInt();
3031+
int green = parts.at( 1 ).toInt();
3032+
int blue = parts.at( 2 ).toInt();
3033+
QColor color = QColor( red, green, blue );
3034+
3035+
//try to read color name
3036+
QString label;
3037+
parts = line.split( "\t" );
3038+
if ( parts.length() > 1 )
3039+
{
3040+
label = parts.at( parts.length() - 1 );
3041+
}
3042+
3043+
importedColors << qMakePair( color, label );
3044+
}
3045+
3046+
file.close();
3047+
ok = true;
3048+
return importedColors;
3049+
}
3050+
29563051
QColor QgsSymbolLayerV2Utils::parseColor( QString colorStr , bool strictEval )
29573052
{
29583053
bool hasAlpha;

src/core/symbology-ng/qgssymbollayerv2utils.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,25 @@ class CORE_EXPORT QgsSymbolLayerV2Utils
255255
*/
256256
static QMimeData* colorListToMimeData( const QgsNamedColorList colorList, const bool allFormats = true );
257257

258+
/**
259+
* Exports colors to a gpl GIMP palette file
260+
* @param file destination file
261+
* @param paletteName name of palette, which is stored in gpl file
262+
* @param colors colors to export
263+
* @returns true if export was successful
264+
* @see importColorsFromGpl
265+
*/
266+
static bool saveColorsToGpl( QFile &file, const QString paletteName, QgsNamedColorList colors );
267+
268+
/**
269+
* Imports colors from a gpl GIMP palette file
270+
* @param file source gpl file
271+
* @param ok will be true if file was successfully read
272+
* @returns list of imported colors
273+
* @see saveColorsToGpl
274+
*/
275+
static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok );
276+
258277
/**
259278
* Attempts to parse a string as a color using a variety of common formats, including hex
260279
* codes, rgb and rgba strings.

src/gui/qgscolorschemelist.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,37 @@ void QgsColorSchemeList::copyColors()
131131
QApplication::clipboard()->setMimeData( mimeData );
132132
}
133133

134+
bool QgsColorSchemeList::importColorsFromGpl( QFile &file )
135+
{
136+
QgsNamedColorList importedColors;
137+
bool ok = false;
138+
importedColors = QgsSymbolLayerV2Utils::importColorsFromGpl( file, ok );
139+
if ( !ok )
140+
{
141+
return false;
142+
}
143+
144+
if ( importedColors.length() == 0 )
145+
{
146+
//no imported colors
147+
return false;
148+
}
149+
150+
//insert imported colors
151+
QgsNamedColorList::const_iterator colorIt = importedColors.constBegin();
152+
for ( ; colorIt != importedColors.constEnd(); ++colorIt )
153+
{
154+
mModel->addColor(( *colorIt ).first, ( *colorIt ).second );
155+
}
156+
157+
return true;
158+
}
159+
160+
bool QgsColorSchemeList::exportColorsToGpl( QFile &file )
161+
{
162+
return QgsSymbolLayerV2Utils::saveColorsToGpl( file, QString(), mModel->colors() );
163+
}
164+
134165
//
135166
// QgsColorSchemeModel
136167
//

0 commit comments

Comments
 (0)