29#include <QRegularExpression>
33using namespace Qt::StringLiterals;
68 const auto constRecentColorVariants = recentColorVariants;
69 for (
const QVariant &color : constRecentColorVariants )
83 if ( !color.isValid() )
89 QColor opaqueColor = color;
90 opaqueColor.setAlpha( 255 );
95 for (
int colorIdx = recentColorVariants.length() - 1; colorIdx >= 0; --colorIdx )
97 if ( ( recentColorVariants.at( colorIdx ).value<QColor>() ).name() == opaqueColor.name() )
99 recentColorVariants.removeAt( colorIdx );
104 const QVariant colorVariant = QVariant( opaqueColor );
105 recentColorVariants.prepend( colorVariant );
108 while ( recentColorVariants.count() > 20 )
110 recentColorVariants.pop_back();
121 if ( recentColorVariants.isEmpty() )
124 return recentColorVariants.at( 0 ).value<QColor>();
130 Q_UNUSED( baseColor )
139 colorList.append( qMakePair( QColor( 0, 0, 0 ), QString() ) );
140 colorList.append( qMakePair( QColor( 255, 255, 255 ), QString() ) );
141 colorList.append( qMakePair( QColor( 166, 206, 227 ), QString() ) );
142 colorList.append( qMakePair( QColor( 31, 120, 180 ), QString() ) );
143 colorList.append( qMakePair( QColor( 178, 223, 138 ), QString() ) );
144 colorList.append( qMakePair( QColor( 51, 160, 44 ), QString() ) );
145 colorList.append( qMakePair( QColor( 251, 154, 153 ), QString() ) );
146 colorList.append( qMakePair( QColor( 227, 26, 28 ), QString() ) );
147 colorList.append( qMakePair( QColor( 253, 191, 111 ), QString() ) );
148 colorList.append( qMakePair( QColor( 255, 127, 0 ), QString() ) );
158 for ( QList< QVariant >::iterator it = customColorVariants.begin(); it != customColorVariants.end(); ++it )
160 const QColor color = ( *it ).value<QColor>();
162 if ( customColorLabels.length() > colorIndex )
164 label = customColorLabels.at( colorIndex ).toString();
167 colorList.append( qMakePair( color, label ) );
177 Q_UNUSED( baseColor )
180 QList< QVariant > customColors;
181 QList< QVariant > customColorLabels;
183 QgsNamedColorList::const_iterator colorIt = colors.constBegin();
184 for ( ; colorIt != colors.constEnd(); ++colorIt )
186 const QVariant color = ( *colorIt ).first;
187 const QVariant label = ( *colorIt ).second;
188 customColors.append( color );
189 customColorLabels.append( label );
205 Q_UNUSED( baseColor )
214 for ( QStringList::iterator it = colorStrings.begin(); it != colorStrings.end(); ++it )
218 if ( colorLabels.length() > colorIndex )
220 label = colorLabels.at( colorIndex );
223 colorList.append( qMakePair( color, label ) );
233 Q_UNUSED( baseColor )
251 Q_UNUSED( baseColor )
254 if ( sourceFilePath.isEmpty() )
262 QFile sourceFile( sourceFilePath );
269 Q_UNUSED( baseColor )
272 if ( destFilePath.isEmpty() )
277 QFile destFile( destFilePath );
304 if ( sourceFile.open( QIODevice::ReadOnly ) )
306 QTextStream in( &sourceFile );
310 while ( !in.atEnd() && !line.startsWith(
"Name:"_L1 ) )
312 line = in.readLine();
316 const thread_local QRegularExpression rx(
"Name:\\s*(\\S.*)$" );
317 const QRegularExpressionMatch match = rx.match( line );
318 if ( match.hasMatch() )
320 mName = match.captured( 1 );
324 if (
mName.isEmpty() )
333 mEditable = !sourceFileInfo.exists() || sourceFileInfo.isWritable();
352 if ( showInMenuSchemes.contains(
mName ) )
363 if ( filePath.isEmpty() )
369 if ( !QFile::exists( filePath ) )
375 return QFile::remove( filePath );
382 if ( show && !showInMenuSchemes.contains(
mName ) )
384 showInMenuSchemes <<
mName;
386 else if ( !show && showInMenuSchemes.contains(
mName ) )
388 showInMenuSchemes.removeAll(
mName );
399 if ( !localDir.mkpath( palettesDir ) )
404 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
static const QgsSettingsEntryVariant * settingsPaletteLabels
static const QgsSettingsEntryVariant * settingsPaletteColors
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
static const QgsSettingsEntryVariant * settingsRecentColors
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.
A string list settings entry.
A variant settings entry.
static QgsSettingsTreeNode * sTreeColors
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.
static const QgsSettingsEntryStringList * settingsShowInMenuList
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.