35 QFile inputFile( path );
36 if ( !inputFile.open( QFile::ReadOnly ) )
38 errors.append( QObject::tr(
"Read access denied. Adjust the file permissions and try again.\n\n" ) );
44 QTextStream inputStream( &inputFile );
46 const thread_local QRegularExpression itemRegex( u
"^(.+?),(.+?),(.+?),(.+?),(.+?),(.+)$"_s );
49 while ( !inputStream.atEnd() )
52 const QString inputLine = inputStream.readLine();
53 if ( !inputLine.isEmpty() )
55 if ( !inputLine.simplified().startsWith(
'#' ) )
57 if ( inputLine.contains(
"INTERPOLATION"_L1, Qt::CaseInsensitive ) )
59 QStringList inputStringComponents = inputLine.split(
':' );
60 if ( inputStringComponents.size() == 2 )
62 if ( inputStringComponents[1].trimmed().toUpper().compare(
"INTERPOLATED"_L1, Qt::CaseInsensitive ) == 0 )
66 else if ( inputStringComponents[1].trimmed().toUpper().compare(
"DISCRETE"_L1, Qt::CaseInsensitive ) == 0 )
78 errors << QObject::tr(
"Unknown interpolation type at line %1: %2" ).arg( lineCounter ).arg( inputLine );
83 const QRegularExpressionMatch match = itemRegex.match( inputLine );
84 if ( match.hasMatch() )
87 QColor::fromRgb( match.captured( 2 ).toInt(), match.captured( 3 ).toInt(), match.captured( 4 ).toInt(), match.captured( 5 ).toInt() ),
88 match.captured( 6 ) );
89 items.push_back( currentItem );
94 errors << QObject::tr(
"Invalid entry at line %1: %2" ).arg( lineCounter ).arg( inputLine );
107 QFile outputFile( path );
108 if ( outputFile.open( QFile::WriteOnly | QIODevice::Truncate ) )
110 QTextStream outputStream( &outputFile );
111 outputStream <<
"# " << QObject::tr(
"QGIS Generated Color Map Export File" ) <<
'\n';
112 outputStream <<
"INTERPOLATION:";
116 outputStream <<
"INTERPOLATED\n";
119 outputStream <<
"DISCRETE\n";
122 outputStream <<
"EXACT\n";
130 outputStream << item.color.red() <<
',' << item.color.green() <<
',' << item.color.blue() <<
',' << item.color.alpha() <<
',';
131 if ( item.label.isEmpty() )
133 outputStream <<
"Color entry " << i + 1 <<
'\n';
137 outputStream << item.label <<
'\n';
141 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 ...