Be a bit more forgiving when reading gpl files which don't exactly follow the specifications

This commit is contained in:
Nyall Dawson 2014-09-17 21:09:49 +10:00
parent 35e3eac73b
commit 66e6820ba1

View File

@ -37,6 +37,7 @@
#include <QIcon> #include <QIcon>
#include <QPainter> #include <QPainter>
#include <QSettings> #include <QSettings>
#include <QRegExp>
QString QgsSymbolLayerV2Utils::encodeColor( QColor color ) QString QgsSymbolLayerV2Utils::encodeColor( QColor color )
{ {
@ -3066,22 +3067,29 @@ QgsNamedColorList QgsSymbolLayerV2Utils::importColorsFromGpl( QFile &file, bool
while ( !in.atEnd() ) while ( !in.atEnd() )
{ {
line = in.readLine(); line = in.readLine();
QStringList parts = line.simplified().split( " " ); QRegExp rx( "^\\s*(\\d+)\\s+(\\d+)\\s+(\\d+)(\\s.*)?$" );
if ( parts.length() < 3 ) if ( rx.indexIn( line ) == -1 )
{ {
continue; continue;
} }
int red = parts.at( 0 ).toInt(); int red = rx.cap( 1 ).toInt();
int green = parts.at( 1 ).toInt(); int green = rx.cap( 2 ).toInt();
int blue = parts.at( 2 ).toInt(); int blue = rx.cap( 3 ).toInt();
QColor color = QColor( red, green, blue ); QColor color = QColor( red, green, blue );
if ( !color.isValid() )
{
continue;
}
//try to read color name //try to read color name
QString label; QString label;
parts = line.split( "\t" ); if ( rx.captureCount() > 3 )
if ( parts.length() > 1 )
{ {
label = parts.at( parts.length() - 1 ); label = rx.cap( 4 ).simplified();
}
else
{
label = colorToName( color );
} }
importedColors << qMakePair( color, label ); importedColors << qMakePair( color, label );