Skip to content
Permalink
Browse files

Allow importing and exporting to GIMP palette file (gpl) from color list

  • Loading branch information
nyalldawson committed Aug 17, 2014
1 parent b457355 commit b15b014b829e346178013e77e7c7c453b0dbc6ba
@@ -217,6 +217,25 @@ class QgsSymbolLayerV2Utils
*/
static QMimeData* colorListToMimeData( const QgsNamedColorList colorList, const bool allFormats = true );

/**
* Exports colors to a gpl GIMP palette file
* @param file destination file
* @param paletteName name of palette, which is stored in gpl file
* @param colors colors to export
* @returns true if export was successful
* @see importColorsFromGpl
*/
static bool saveColorsToGpl( QFile &file, const QString paletteName, QgsNamedColorList colors );

/**
* Imports colors from a gpl GIMP palette file
* @param file source gpl file
* @param ok will be true if file was successfully read
* @returns list of imported colors
* @see saveColorsToGpl
*/
static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok );

/**
* Attempts to parse a string as a color using a variety of common formats, including hex
* codes, rgb and rgba strings.
@@ -120,14 +120,26 @@ class QgsColorSchemeList: QTreeView
*/
void setScheme( QgsColorScheme* scheme, const QString context = QString(), const QColor baseColor = QColor() );

public slots:

/**Saves the current colors shown in the list back to a color scheme, if supported
* by the color scheme.
* @note this method is only effective if the color scheme is editable
*/
bool saveColorsToScheme();

/**Import colors from a GPL palette file to the list
* @param file file to import
* @see exportColorsToGpl
*/
bool importColorsFromGpl( QFile &file );

/**Export colors to a GPL palette file from the list
* @param file destination file
* @see importColorsFromGpl
*/
bool exportColorsToGpl( QFile &file );

public slots:

/**Removes any selected colors from the list
*/
void removeSelection();
@@ -2089,3 +2089,61 @@ void QgsOptions::on_mButtonAddColor_clicked()

mTreeCustomColors->addColor( newColor );
}

void QgsOptions::on_mButtonImportColors_clicked()
{
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 );
bool importOk = mTreeCustomColors->importColorsFromGpl( file );
if ( !importOk )
{
QMessageBox::critical( 0, tr( "Invalid file" ), tr( "Error, no colors found in palette file" ) );
return;
}
}

void QgsOptions::on_mButtonExportColors_clicked()
{
QSettings s;
QString lastDir = s.value( "/UI/lastGplPaletteDir", "" ).toString();
QString fileName = QFileDialog::getSaveFileName( this, tr( "Palette file" ), lastDir, "GPL (*.gpl)" );
activateWindow();
if ( fileName.isEmpty() )
{
return;
}

// ensure filename contains extension
if ( !fileName.toLower().endsWith( ".gpl" ) )
{
fileName += ".gpl";
}

QFileInfo fileInfo( fileName );
s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );

QFile file( fileName );
bool exportOk = mTreeCustomColors->exportColorsToGpl( file );
if ( !exportOk )
{
QMessageBox::critical( 0, tr( "Error exporting" ), tr( "Error writing palette file" ) );
return;
}
}
@@ -242,6 +242,8 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption
void on_mAddDefaultTransformButton_clicked();

void on_mButtonAddColor_clicked();
void on_mButtonImportColors_clicked();
void on_mButtonExportColors_clicked();

private:
QStringList i18nList();
@@ -1684,3 +1684,61 @@ void QgsProjectProperties::on_mButtonAddColor_clicked()

mTreeProjectColors->addColor( newColor );
}

void QgsProjectProperties::on_mButtonImportColors_clicked()
{
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 );
bool importOk = mTreeProjectColors->importColorsFromGpl( file );
if ( !importOk )
{
QMessageBox::critical( 0, tr( "Invalid file" ), tr( "Error, no colors found in palette file" ) );
return;
}
}

void QgsProjectProperties::on_mButtonExportColors_clicked()
{
QSettings s;
QString lastDir = s.value( "/UI/lastGplPaletteDir", "" ).toString();
QString fileName = QFileDialog::getSaveFileName( this, tr( "Palette file" ), lastDir, "GPL (*.gpl)" );
activateWindow();
if ( fileName.isEmpty() )
{
return;
}

// ensure filename contains extension
if ( !fileName.toLower().endsWith( ".gpl" ) )
{
fileName += ".gpl";
}

QFileInfo fileInfo( fileName );
s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() );

QFile file( fileName );
bool exportOk = mTreeProjectColors->exportColorsToGpl( file );
if ( !exportOk )
{
QMessageBox::critical( 0, tr( "Error exporting" ), tr( "Error writing palette file" ) );
return;
}
}
@@ -167,6 +167,8 @@ class APP_EXPORT QgsProjectProperties : public QgsOptionsDialogBase, private Ui:
void projectionSelectorInitialized();

void on_mButtonAddColor_clicked();
void on_mButtonImportColors_clicked();
void on_mButtonExportColors_clicked();

signals:
//! Signal used to inform listeners that the mouse display precision may have changed
@@ -81,7 +81,6 @@ class CORE_EXPORT QgsColorScheme
* @returns copy of color scheme
*/
virtual QgsColorScheme* clone() const = 0;

};

/** \ingroup core
@@ -2953,6 +2953,101 @@ QMimeData* QgsSymbolLayerV2Utils::colorListToMimeData( const QgsNamedColorList c
return mimeData;
}

bool QgsSymbolLayerV2Utils::saveColorsToGpl( QFile &file, const QString paletteName, QgsNamedColorList colors )
{
if ( !file.open( QIODevice::ReadWrite ) )
{
return false;
}

QTextStream stream( &file );
stream << "GIMP Palette" << endl;
if ( paletteName.isEmpty() )
{
stream << "Name: QGIS Palette" << endl;
}
else
{
stream << "Name: " << paletteName << endl;
}
stream << "Columns: 4" << endl;
stream << "#" << endl;

for ( QgsNamedColorList::ConstIterator colorIt = colors.constBegin(); colorIt != colors.constEnd(); ++colorIt )
{
QColor color = ( *colorIt ).first;
if ( !color.isValid() )
{
continue;
}
stream << QString( "%1 %2 %3" ).arg( color.red(), 3 ).arg( color.green(), 3 ).arg( color.blue(), 3 );
stream << "\t" << (( *colorIt ).second.isEmpty() ? color.name() : ( *colorIt ).second ) << endl;
}
file.close();

return true;
}

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

if ( !file.open( QIODevice::ReadOnly ) )
{
ok = false;
return importedColors;
}

QTextStream in( &file );

QString line = in.readLine();
if ( !line.startsWith( "GIMP Palette" ) )
{
ok = false;
return importedColors;
}

//ignore lines until after "#"
while ( !in.atEnd() && !line.startsWith( "#" ) )
{
line = in.readLine();
}
if ( in.atEnd() )
{
ok = false;
return importedColors;
}

//ready to start reading colors
while ( !in.atEnd() )
{
line = in.readLine();
QStringList parts = line.simplified().split( " " );
if ( parts.length() < 3 )
{
continue;
}
int red = parts.at( 0 ).toInt();
int green = parts.at( 1 ).toInt();
int blue = parts.at( 2 ).toInt();
QColor color = QColor( red, green, blue );

//try to read color name
QString label;
parts = line.split( "\t" );
if ( parts.length() > 1 )
{
label = parts.at( parts.length() - 1 );
}

importedColors << qMakePair( color, label );
}

file.close();
ok = true;
return importedColors;
}

QColor QgsSymbolLayerV2Utils::parseColor( QString colorStr , bool strictEval )
{
bool hasAlpha;
@@ -255,6 +255,25 @@ class CORE_EXPORT QgsSymbolLayerV2Utils
*/
static QMimeData* colorListToMimeData( const QgsNamedColorList colorList, const bool allFormats = true );

/**
* Exports colors to a gpl GIMP palette file
* @param file destination file
* @param paletteName name of palette, which is stored in gpl file
* @param colors colors to export
* @returns true if export was successful
* @see importColorsFromGpl
*/
static bool saveColorsToGpl( QFile &file, const QString paletteName, QgsNamedColorList colors );

/**
* Imports colors from a gpl GIMP palette file
* @param file source gpl file
* @param ok will be true if file was successfully read
* @returns list of imported colors
* @see saveColorsToGpl
*/
static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok );

/**
* Attempts to parse a string as a color using a variety of common formats, including hex
* codes, rgb and rgba strings.
@@ -131,6 +131,37 @@ void QgsColorSchemeList::copyColors()
QApplication::clipboard()->setMimeData( mimeData );
}

bool QgsColorSchemeList::importColorsFromGpl( QFile &file )
{
QgsNamedColorList importedColors;
bool ok = false;
importedColors = QgsSymbolLayerV2Utils::importColorsFromGpl( file, ok );
if ( !ok )
{
return false;
}

if ( importedColors.length() == 0 )
{
//no imported colors
return false;
}

//insert imported colors
QgsNamedColorList::const_iterator colorIt = importedColors.constBegin();
for ( ; colorIt != importedColors.constEnd(); ++colorIt )
{
mModel->addColor(( *colorIt ).first, ( *colorIt ).second );
}

return true;
}

bool QgsColorSchemeList::exportColorsToGpl( QFile &file )
{
return QgsSymbolLayerV2Utils::saveColorsToGpl( file, QString(), mModel->colors() );
}

//
// QgsColorSchemeModel
//

0 comments on commit b15b014

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