30 QFile inputFile( path );
31 if ( !inputFile.open( QFile::ReadOnly ) )
33 errors.append( QObject::tr(
"Read access denied. Adjust the file permissions and try again.\n\n" ) );
39 QTextStream inputStream( &inputFile );
41 const thread_local QRegularExpression itemRegex( QStringLiteral(
"^(.+?),(.+?),(.+?),(.+?),(.+?),(.+)$" ) );
44 while ( !inputStream.atEnd() )
47 const QString inputLine = inputStream.readLine();
48 if ( !inputLine.isEmpty() )
50 if ( !inputLine.simplified().startsWith(
'#' ) )
52 if ( inputLine.contains( QLatin1String(
"INTERPOLATION" ), Qt::CaseInsensitive ) )
54 QStringList inputStringComponents = inputLine.split(
':' );
55 if ( inputStringComponents.size() == 2 )
57 if ( inputStringComponents[1].trimmed().toUpper().compare( QLatin1String(
"INTERPOLATED" ), Qt::CaseInsensitive ) == 0 )
61 else if ( inputStringComponents[1].trimmed().toUpper().compare( QLatin1String(
"DISCRETE" ), Qt::CaseInsensitive ) == 0 )
73 errors << QObject::tr(
"Unknown interpolation type at line %1: %2" ).arg( lineCounter ).arg( inputLine );
78 const QRegularExpressionMatch match = itemRegex.match( inputLine );
79 if ( match.hasMatch() )
82 QColor::fromRgb( match.captured( 2 ).toInt(), match.captured( 3 ).toInt(), match.captured( 4 ).toInt(), match.captured( 5 ).toInt() ),
83 match.captured( 6 ) );
84 items.push_back( currentItem );
89 errors << QObject::tr(
"Invalid entry at line %1: %2" ).arg( lineCounter ).arg( inputLine );
102 QFile outputFile( path );
103 if ( outputFile.open( QFile::WriteOnly | QIODevice::Truncate ) )
105 QTextStream outputStream( &outputFile );
106 outputStream <<
"# " << QObject::tr(
"QGIS Generated Color Map Export File" ) <<
'\n';
107 outputStream <<
"INTERPOLATION:";
111 outputStream <<
"INTERPOLATED\n";
114 outputStream <<
"DISCRETE\n";
117 outputStream <<
"EXACT\n";
125 outputStream << item.color.red() <<
',' << item.color.green() <<
',' << item.color.blue() <<
',' << item.color.alpha() <<
',';
126 if ( item.label.isEmpty() )
128 outputStream <<
"Color entry " << i + 1 <<
'\n';
132 outputStream << item.label <<
'\n';
136 outputStream.flush();
static bool saveColorMapFile(const QString &path, const QList< QgsColorRampShader::ColorRampItem > &items, Qgis::ShaderInterpolationMethod type)
Exports a list of color ramp items and ramp shader type to a color map file at the specified path.
static bool parseColorMapFile(const QString &path, QList< QgsColorRampShader::ColorRampItem > &items, Qgis::ShaderInterpolationMethod &type, QStringList &errors)
Parses an exported color map file at the specified path and extracts the stored color ramp items and ...