QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
qgsprojectstylesettings.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsprojectstylesettings.h
3 ---------------------------
4 begin : May 2022
5 copyright : (C) 2022 by Mathieu Pellerin
6 email : nirvn dot asia at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15#ifndef QGSPROJECTSTYLESETTINGS_H
16#define QGSPROJECTSTYLESETTINGS_H
17
18#include "qgis_core.h"
19#include "qgstextformat.h"
20#include "qgswkbtypes.h"
21
22#include <memory.h>
23#include <QAbstractListModel>
24#include <QSortFilterProxyModel>
25#include <QPointer>
26
27class QDomElement;
29class QDomDocument;
30class QgsProject;
31class QgsSymbol;
32class QgsColorRamp;
33class QgsStyle;
35
43class CORE_EXPORT QgsProjectStyleSettings : public QObject
44{
45 Q_OBJECT
46
47 public:
48
54 QgsProjectStyleSettings( QgsProject *project = nullptr );
55
56 ~QgsProjectStyleSettings() override;
57
64 QgsSymbol *defaultSymbol( Qgis::SymbolType symbolType ) const SIP_FACTORY;
65
72 void setDefaultSymbol( Qgis::SymbolType symbolType, QgsSymbol *symbol );
73
79 QgsColorRamp *defaultColorRamp() const SIP_FACTORY;
80
86 void setDefaultColorRamp( QgsColorRamp *colorRamp );
87
92 QgsTextFormat defaultTextFormat() const;
93
98 void setDefaultTextFormat( const QgsTextFormat &textFormat );
99
103 bool randomizeDefaultSymbolColor() const { return mRandomizeDefaultSymbolColor; }
104
108 void setRandomizeDefaultSymbolColor( bool randomized ) { mRandomizeDefaultSymbolColor = randomized; }
109
113 double defaultSymbolOpacity() const { return mDefaultSymbolOpacity; }
114
118 void setDefaultSymbolOpacity( double opacity ) { mDefaultSymbolOpacity = opacity; }
119
123 void reset();
124
130 void setProjectStyle( QgsStyle *style SIP_TRANSFER );
131
137 QgsStyle *projectStyle();
138
143 bool readXml( const QDomElement &element, const QgsReadWriteContext &context, Qgis::ProjectReadFlags flags = Qgis::ProjectReadFlags() );
144
149 QDomElement writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const;
150
161 QStringList styleDatabasePaths() const { return mStyleDatabases; }
162
168 QList< QgsStyle * > styles() const;
169
173 QgsStyle *styleAtPath( const QString &path );
174
187 void addStyleDatabasePath( const QString &path );
188
201 void setStyleDatabasePaths( const QStringList &paths );
202
213 QgsCombinedStyleModel *combinedStyleModel();
214
215 signals:
216
221
222#ifndef SIP_RUN
223
229 void styleDatabaseAboutToBeAdded( const QString &path );
230
236 void styleDatabaseAdded( const QString &path );
237
243 void styleDatabaseAboutToBeRemoved( const QString &path );
244
250 void styleDatabaseRemoved( const QString &path );
251
258
259#endif
260 private:
261
262 QgsProject *mProject = nullptr;
263
264 std::unique_ptr< QgsSymbol > mDefaultMarkerSymbol;
265 std::unique_ptr< QgsSymbol > mDefaultLineSymbol;
266 std::unique_ptr< QgsSymbol > mDefaultFillSymbol;
267 std::unique_ptr< QgsColorRamp > mDefaultColorRamp;
268 QgsTextFormat mDefaultTextFormat;
269
270 bool mRandomizeDefaultSymbolColor = true;
271 double mDefaultSymbolOpacity = 1.0;
272
273 QgsStyle *mProjectStyle = nullptr;
274 QStringList mStyleDatabases;
275 QList< QPointer< QgsStyle > > mStyles;
276
277 QgsCombinedStyleModel *mCombinedStyleModel = nullptr;
278
279 void loadStyleAtPath( const QString &path );
280 void clearStyles();
281
282};
283
292class CORE_EXPORT QgsProjectStyleDatabaseModel : public QAbstractListModel
293{
294 Q_OBJECT
295
296 public:
297
299 enum Role
300 {
301 StyleRole = Qt::UserRole + 1,
303 };
304
308 explicit QgsProjectStyleDatabaseModel( QgsProjectStyleSettings *settings, QObject *parent SIP_TRANSFERTHIS = nullptr );
309
310 int rowCount( const QModelIndex &parent ) const override;
311 QVariant data( const QModelIndex &index, int role ) const override;
312
317 QgsStyle *styleFromIndex( const QModelIndex &index ) const;
318
323 QModelIndex indexFromStyle( QgsStyle *style ) const;
324
330 void setShowDefaultStyle( bool show );
331
337 bool showDefaultStyle() const { return mShowDefault; }
338
339 private slots:
340 void styleDatabaseAboutToBeAdded( const QString &path );
341 void styleDatabaseAboutToBeRemoved( const QString &path );
342 void styleDatabaseAdded( const QString &path );
343 void styleDatabaseRemoved( const QString &path );
344
345 void setProjectStyle( QgsStyle *style );
346 void projectStyleAboutToBeDestroyed();
347 void projectStyleDestroyed();
348 void projectStyleChanged();
349
350 private:
351 QgsProjectStyleSettings *mSettings = nullptr;
352 bool mShowDefault = false;
353 QPointer< QgsStyle > mProjectStyle;
354};
355
364class CORE_EXPORT QgsProjectStyleDatabaseProxyModel : public QSortFilterProxyModel
365{
366 Q_OBJECT
367
368 public:
369
371 enum class Filter
372 {
373 FilterHideReadOnly = 1 << 0,
374 };
375 Q_ENUM( Filter )
377 Q_DECLARE_FLAGS( Filters, Filter )
378 Q_FLAG( Filters )
379
380
384
385 bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const override;
386
392 QgsProjectStyleDatabaseProxyModel::Filters filters() const;
393
399 void setFilters( QgsProjectStyleDatabaseProxyModel::Filters filters );
400
401 private:
402
403 QgsProjectStyleDatabaseProxyModel::Filters mFilters;
404
405};
406Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProjectStyleDatabaseProxyModel::Filters )
407
408
409#endif // QGSPROJECTSTYLESETTINGS_H
SymbolType
Symbol types.
Definition: qgis.h:206
Abstract base class for color ramps.
Definition: qgscolorramp.h:30
A model which contains entities from multiple QgsStyle databases.
List model representing the style databases associated with a QgsProject.
bool showDefaultStyle() const
Returns true if the model includes the default style.
A proxy model for filtering QgsProjectStyleDatabaseModel.
Filter
Available filter flags for filtering the model.
Contains settings and properties relating to how a QgsProject should handle styling.
double defaultSymbolOpacity() const
Returns the default symbol opacity.
void projectStyleChanged()
Emitted when the style returned by projectStyle() is changed.
bool randomizeDefaultSymbolColor() const
Returns whether the default symbol fill color is randomized.
QStringList styleDatabasePaths() const
Returns a list of all style databases (file paths) associated with the project.
void styleDatabaseAdded(const QString &path)
Emitted when a style database path is added.
void styleDatabaseAboutToBeRemoved(const QString &path)
Emitted when a style database path is about to be removed.
void setRandomizeDefaultSymbolColor(bool randomized)
Sets whether the default symbol fill color is randomized.
void styleDatabaseRemoved(const QString &path)
Emitted when a style database path is removed.
void setDefaultSymbolOpacity(double opacity)
Sets the default symbol opacity.
void styleDatabaseAboutToBeAdded(const QString &path)
Emitted when a style database path is about to be added.
void styleDatabasesChanged()
Emitted whenever the set of style databases associated with the project is changed.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:104
The class is used as a container of context for various read/write operations on other objects.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:93
Container for all settings relating to text rendering.
Definition: qgstextformat.h:41
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_FACTORY
Definition: qgis_sip.h:76
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.