Option in color picker dialog to import a gpl palette to a new user scheme

This commit is contained in:
Nyall Dawson 2014-09-17 22:52:23 +10:00
parent 0181df5553
commit 39700e7d97
10 changed files with 108 additions and 9 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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;
@ -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( "#" ) )
{
@ -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;

View File

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

View File

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

View File

@ -160,6 +160,7 @@ class GUI_EXPORT QgsColorDialogV2 : public QDialog, private Ui::QgsColorDialogBa
void exportColors();
void importColors();
void importPalette();
void schemeIndexChanged( int index );

View File

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

View File

@ -789,7 +789,7 @@
</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>
@ -797,7 +797,7 @@
</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>
@ -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>