21 #include <QTextStream> 
   22 #include <QRegularExpression> 
   26   QFile inputFile( path );
 
   27   if ( !inputFile.open( QFile::ReadOnly ) )
 
   29     errors.append( QObject::tr( 
"Read access denied. Adjust the file permissions and try again.\n\n" ) );
 
   35   QTextStream inputStream( &inputFile );
 
   37   const QRegularExpression itemRegex( QStringLiteral( 
"^(.+?),(.+?),(.+?),(.+?),(.+?),(.+)$" ) );
 
   40   while ( !inputStream.atEnd() )
 
   43     const QString inputLine = inputStream.readLine();
 
   44     if ( !inputLine.isEmpty() )
 
   46       if ( !inputLine.simplified().startsWith( 
'#' ) )
 
   48         if ( inputLine.contains( QLatin1String( 
"INTERPOLATION" ), Qt::CaseInsensitive ) )
 
   50           QStringList inputStringComponents = inputLine.split( 
':' );
 
   51           if ( inputStringComponents.size() == 2 )
 
   53             if ( inputStringComponents[1].trimmed().toUpper().compare( QLatin1String( 
"INTERPOLATED" ), Qt::CaseInsensitive ) == 0 )
 
   57             else if ( inputStringComponents[1].trimmed().toUpper().compare( QLatin1String( 
"DISCRETE" ), Qt::CaseInsensitive ) == 0 )
 
   69             errors << QObject::tr( 
"Unknown interpolation type at line %1: %2" ).arg( lineCounter ).arg( inputLine );
 
   74           const QRegularExpressionMatch match = itemRegex.match( inputLine );
 
   75           if ( match.hasMatch() )
 
   78                 QColor::fromRgb( match.captured( 2 ).toInt(), match.captured( 3 ).toInt(), match.captured( 4 ).toInt(), match.captured( 5 ).toInt() ),
 
   79                 match.captured( 6 ) );
 
   80             items.push_back( currentItem );
 
   85             errors << QObject::tr( 
"Invalid entry at line %1: %2" ).arg( lineCounter ).arg( inputLine );
 
   98   QFile outputFile( path );
 
   99   if ( outputFile.open( QFile::WriteOnly | QIODevice::Truncate ) )
 
  101     QTextStream outputStream( &outputFile );
 
  102     outputStream << 
"# " << QObject::tr( 
"QGIS Generated Color Map Export File" ) << 
'\n';
 
  103     outputStream << 
"INTERPOLATION:";
 
  107         outputStream << 
"INTERPOLATED\n";
 
  110         outputStream << 
"DISCRETE\n";
 
  113         outputStream << 
"EXACT\n";
 
  121       outputStream << item.color.red() << 
',' << item.color.green() << 
',' << item.color.blue() << 
',' << item.color.alpha() << 
',';
 
  122       if ( item.label.isEmpty() )
 
  124         outputStream << 
"Color entry " << i + 1 << 
'\n';
 
  128         outputStream << item.label << 
'\n';
 
  132     outputStream.flush();
 
Type
Supported methods for color interpolation.
@ Interpolated
Interpolates the color between two class breaks linearly.
@ Discrete
Assigns the color of the higher class for every pixel between two class breaks.
@ Exact
Assigns the color of the exact matching value in the color ramp item list.
static bool parseColorMapFile(const QString &path, QList< QgsColorRampShader::ColorRampItem > &items, QgsColorRampShader::Type &type, QStringList &errors)
Parses an exported color map file at the specified path and extracts the stored color ramp items and ...
static bool saveColorMapFile(const QString &path, const QList< QgsColorRampShader::ColorRampItem > &items, QgsColorRampShader::Type type)
Exports a list of color ramp items and ramp shader type to a color map file at the specified path.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.