28#include <QRegularExpression>
32using namespace Qt::StringLiterals;
55 const QList< QVariant > recentColorVariants = settings.
value( u
"colors/recent"_s ).toList();
59 const auto constRecentColorVariants = recentColorVariants;
60 for (
const QVariant &color : constRecentColorVariants )
74 if ( !color.isValid() )
80 QColor opaqueColor = color;
81 opaqueColor.setAlpha( 255 );
84 QList< QVariant > recentColorVariants = settings.
value( u
"colors/recent"_s ).toList();
87 for (
int colorIdx = recentColorVariants.length() - 1; colorIdx >= 0; --colorIdx )
89 if ( ( recentColorVariants.at( colorIdx ).value<QColor>() ).name() == opaqueColor.name() )
91 recentColorVariants.removeAt( colorIdx );
96 const QVariant colorVariant = QVariant( opaqueColor );
97 recentColorVariants.prepend( colorVariant );
100 while ( recentColorVariants.count() > 20 )
102 recentColorVariants.pop_back();
105 settings.
setValue( u
"colors/recent"_s, recentColorVariants );
112 const QList< QVariant > recentColorVariants = settings.
value( u
"colors/recent"_s ).toList();
114 if ( recentColorVariants.isEmpty() )
117 return recentColorVariants.at( 0 ).value<QColor>();
123 Q_UNUSED( baseColor )
130 if ( !settings.
contains( u
"/colors/palettecolors"_s ) )
133 colorList.append( qMakePair( QColor( 0, 0, 0 ), QString() ) );
134 colorList.append( qMakePair( QColor( 255, 255, 255 ), QString() ) );
135 colorList.append( qMakePair( QColor( 166, 206, 227 ), QString() ) );
136 colorList.append( qMakePair( QColor( 31, 120, 180 ), QString() ) );
137 colorList.append( qMakePair( QColor( 178, 223, 138 ), QString() ) );
138 colorList.append( qMakePair( QColor( 51, 160, 44 ), QString() ) );
139 colorList.append( qMakePair( QColor( 251, 154, 153 ), QString() ) );
140 colorList.append( qMakePair( QColor( 227, 26, 28 ), QString() ) );
141 colorList.append( qMakePair( QColor( 253, 191, 111 ), QString() ) );
142 colorList.append( qMakePair( QColor( 255, 127, 0 ), QString() ) );
147 QList< QVariant > customColorVariants = settings.
value( u
"colors/palettecolors"_s ).toList();
148 const QList< QVariant > customColorLabels = settings.
value( u
"colors/palettelabels"_s ).toList();
152 for ( QList< QVariant >::iterator it = customColorVariants.begin();
153 it != customColorVariants.end(); ++it )
155 const QColor color = ( *it ).value<QColor>();
157 if ( customColorLabels.length() > colorIndex )
159 label = customColorLabels.at( colorIndex ).toString();
162 colorList.append( qMakePair( color, label ) );
172 Q_UNUSED( baseColor )
176 QList< QVariant > customColors;
177 QList< QVariant > customColorLabels;
179 QgsNamedColorList::const_iterator colorIt = colors.constBegin();
180 for ( ; colorIt != colors.constEnd(); ++colorIt )
182 const QVariant color = ( *colorIt ).first;
183 const QVariant label = ( *colorIt ).second;
184 customColors.append( color );
185 customColorLabels.append( label );
187 settings.
setValue( u
"colors/palettecolors"_s, customColors );
188 settings.
setValue( u
"colors/palettelabels"_s, customColorLabels );
201 Q_UNUSED( baseColor )
210 for ( QStringList::iterator it = colorStrings.begin();
211 it != colorStrings.end(); ++it )
215 if ( colorLabels.length() > colorIndex )
217 label = colorLabels.at( colorIndex );
220 colorList.append( qMakePair( color, label ) );
230 Q_UNUSED( baseColor )
248 Q_UNUSED( baseColor )
251 if ( sourceFilePath.isEmpty() )
259 QFile sourceFile( sourceFilePath );
266 Q_UNUSED( baseColor )
269 if ( destFilePath.isEmpty() )
274 QFile destFile( destFilePath );
301 if ( sourceFile.open( QIODevice::ReadOnly ) )
303 QTextStream in( &sourceFile );
307 while ( !in.atEnd() && !line.startsWith(
"Name:"_L1 ) )
309 line = in.readLine();
313 const thread_local QRegularExpression rx(
"Name:\\s*(\\S.*)$" );
314 const QRegularExpressionMatch match = rx.match( line );
315 if ( match.hasMatch() )
317 mName = match.captured( 1 );
321 if (
mName.isEmpty() )
330 mEditable = !sourceFileInfo.exists() || sourceFileInfo.isWritable();
348 const QStringList showInMenuSchemes = s.
value( u
"/colors/showInMenuList"_s ).toStringList();
350 if ( showInMenuSchemes.contains(
mName ) )
361 if ( filePath.isEmpty() )
367 if ( ! QFile::exists( filePath ) )
373 return QFile::remove( filePath );
379 QStringList showInMenuSchemes = s.
value( u
"/colors/showInMenuList"_s ).toStringList();
381 if ( show && !showInMenuSchemes.contains(
mName ) )
383 showInMenuSchemes <<
mName;
385 else if ( !show && showInMenuSchemes.contains(
mName ) )
387 showInMenuSchemes.removeAll(
mName );
390 s.
setValue( u
"/colors/showInMenuList"_s, showInMenuSchemes );
398 if ( !localDir.mkpath( palettesDir ) )
403 return QDir( palettesDir ).filePath(
mFilename );
static QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the application's color scheme registry, used for managing color schemes.
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user's home dir.
void setRandomStyleColorScheme(QgsColorScheme *scheme)
Sets the color scheme to use when fetching random colors to use for symbol styles.
@ ShowInColorButtonMenu
Show scheme in color button drop-down menu.
virtual QString schemeName() const =0
Gets the name for the color scheme.
virtual bool setColors(const QgsNamedColorList &colors, const QString &context=QString(), const QColor &baseColor=QColor())
Sets the colors for the scheme.
virtual SchemeFlags flags() const
Returns the current flags for the color scheme.
QFlags< SchemeFlag > SchemeFlags
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor()) override
Gets a list of colors from the scheme.
bool setColors(const QgsNamedColorList &colors, const QString &context=QString(), const QColor &baseColor=QColor()) override
Sets the colors for the scheme.
QgsCustomColorScheme * clone() const override
Clones a color scheme.
QgsCustomColorScheme()=default
virtual QString gplFilePath()=0
Returns the file path for the associated gpl palette file.
QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor()) override
Gets a list of colors from the scheme.
bool setColors(const QgsNamedColorList &colors, const QString &context=QString(), const QColor &baseColor=QColor()) override
Sets the colors for the scheme.
QgsProjectColorScheme * clone() const override
Clones a color scheme.
bool setColors(const QgsNamedColorList &colors, const QString &context=QString(), const QColor &baseColor=QColor()) override
Sets the colors for the scheme.
QgsProjectColorScheme()=default
QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor()) override
Gets a list of colors from the scheme.
static QgsProject * instance()
Returns the QgsProject singleton instance.
void setProjectColors(const QgsNamedColorList &colors)
Sets the colors for the project's color scheme (see QgsProjectColorScheme).
QStringList readListEntry(const QString &scope, const QString &key, const QStringList &def=QStringList(), bool *ok=nullptr) const
Reads a string list from the specified scope and key.
QgsRecentColorScheme()=default
QgsNamedColorList fetchColors(const QString &context=QString(), const QColor &baseColor=QColor()) override
Gets a list of colors from the scheme.
static QColor lastUsedColor()
Returns the most recently used color.
QgsRecentColorScheme * clone() const override
Clones a color scheme.
static void addRecentColor(const QColor &color)
Adds a color to the list of recent colors.
Stores settings for use within QGIS.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
static bool saveColorsToGpl(QFile &file, const QString &paletteName, const QgsNamedColorList &colors)
Exports colors to a gpl GIMP palette file.
static QgsNamedColorList importColorsFromGpl(QFile &file, bool &ok, QString &name)
Imports colors from a gpl GIMP palette file.
static QString colorToName(const QColor &color)
Returns a friendly display name for a color.
QString schemeName() const override
Gets the name for the color scheme.
QgsUserColorScheme(const QString &filename)
Constructs a new user color scheme, using a specified gpl palette file.
QgsColorScheme::SchemeFlags flags() const override
Returns the current flags for the color scheme.
QString gplFilePath() override
Returns the file path for the associated gpl palette file.
bool erase()
Erases the associated gpl palette file from the users "palettes" folder.
void setShowSchemeInMenu(bool show)
Sets whether a this scheme should be shown in color button menus.
QgsUserColorScheme * clone() const override
Clones a color scheme.
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.