QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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 "qgis_sip.h"
20#include "qgstextformat.h"
21#include "qgswkbtypes.h"
22
23#include <memory.h>
24#include <QAbstractListModel>
25#include <QSortFilterProxyModel>
26#include <QPointer>
27
28class QDomElement;
30class QDomDocument;
31class QgsProject;
32class QgsSymbol;
33class QgsColorRamp;
34class QgsStyle;
36
44class CORE_EXPORT QgsProjectStyleSettings : public QObject
45{
46 Q_OBJECT
47
48 public:
49
55 QgsProjectStyleSettings( QgsProject *project = nullptr );
56
57 ~QgsProjectStyleSettings() override;
58
65 QgsSymbol *defaultSymbol( Qgis::SymbolType symbolType ) const SIP_FACTORY;
66
73 void setDefaultSymbol( Qgis::SymbolType symbolType, QgsSymbol *symbol );
74
80 QgsColorRamp *defaultColorRamp() const SIP_FACTORY;
81
87 void setDefaultColorRamp( QgsColorRamp *colorRamp );
88
93 QgsTextFormat defaultTextFormat() const;
94
99 void setDefaultTextFormat( const QgsTextFormat &textFormat );
100
104 bool randomizeDefaultSymbolColor() const { return mRandomizeDefaultSymbolColor; }
105
109 void setRandomizeDefaultSymbolColor( bool randomized ) { mRandomizeDefaultSymbolColor = randomized; }
110
114 double defaultSymbolOpacity() const { return mDefaultSymbolOpacity; }
115
119 void setDefaultSymbolOpacity( double opacity ) { mDefaultSymbolOpacity = opacity; }
120
124 void reset();
125
131 void removeProjectStyle();
132
138 void setProjectStyle( QgsStyle *style SIP_TRANSFER );
139
145 QgsStyle *projectStyle();
146
151 bool readXml( const QDomElement &element, const QgsReadWriteContext &context, Qgis::ProjectReadFlags flags = Qgis::ProjectReadFlags() );
152
157 QDomElement writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const;
158
169 QStringList styleDatabasePaths() const { return mStyleDatabases; }
170
176 QList< QgsStyle * > styles() const;
177
181 QgsStyle *styleAtPath( const QString &path );
182
195 void addStyleDatabasePath( const QString &path );
196
209 void setStyleDatabasePaths( const QStringList &paths );
210
221 QgsCombinedStyleModel *combinedStyleModel();
222
223 signals:
224
229
230#ifndef SIP_RUN
231
237 void styleDatabaseAboutToBeAdded( const QString &path );
238
244 void styleDatabaseAdded( const QString &path );
245
251 void styleDatabaseAboutToBeRemoved( const QString &path );
252
258 void styleDatabaseRemoved( const QString &path );
259
266
267#endif
268 private:
269
270 QgsProject *mProject = nullptr;
271
272 std::unique_ptr< QgsSymbol > mDefaultMarkerSymbol;
273 std::unique_ptr< QgsSymbol > mDefaultLineSymbol;
274 std::unique_ptr< QgsSymbol > mDefaultFillSymbol;
275 std::unique_ptr< QgsColorRamp > mDefaultColorRamp;
276 QgsTextFormat mDefaultTextFormat;
277
278 bool mRandomizeDefaultSymbolColor = true;
279 double mDefaultSymbolOpacity = 1.0;
280
281 QgsStyle *mProjectStyle = nullptr;
282 QStringList mStyleDatabases;
283 QList< QPointer< QgsStyle > > mStyles;
284
285 QgsCombinedStyleModel *mCombinedStyleModel = nullptr;
286
287 void loadStyleAtPath( const QString &path );
288 void clearStyles();
289
290};
291
300class CORE_EXPORT QgsProjectStyleDatabaseModel : public QAbstractListModel
301{
302 Q_OBJECT
303
304 public:
305
306 // *INDENT-OFF*
307
315 {
316 Style SIP_MONKEYPATCH_COMPAT_NAME( StyleRole ) = Qt::UserRole + 1,
317 Path SIP_MONKEYPATCH_COMPAT_NAME(PathRole)
318 };
319 Q_ENUM( CustomRole )
320 // *INDENT-ON*
321
322
325 explicit QgsProjectStyleDatabaseModel( QgsProjectStyleSettings *settings, QObject *parent SIP_TRANSFERTHIS = nullptr );
326
327 int rowCount( const QModelIndex &parent ) const override;
328 QVariant data( const QModelIndex &index, int role ) const override;
329
334 QgsStyle *styleFromIndex( const QModelIndex &index ) const;
335
340 QModelIndex indexFromStyle( QgsStyle *style ) const;
341
347 void setShowDefaultStyle( bool show );
348
354 bool showDefaultStyle() const { return mShowDefault; }
355
356 private slots:
357 void styleDatabaseAboutToBeAdded( const QString &path );
358 void styleDatabaseAboutToBeRemoved( const QString &path );
359 void styleDatabaseAdded( const QString &path );
360 void styleDatabaseRemoved( const QString &path );
361
362 void setProjectStyle( QgsStyle *style );
363 void projectStyleAboutToBeDestroyed();
364 void projectStyleDestroyed();
365 void projectStyleChanged();
366
367 private:
368 QgsProjectStyleSettings *mSettings = nullptr;
369 bool mShowDefault = false;
370 QPointer< QgsStyle > mProjectStyle;
371};
372
381class CORE_EXPORT QgsProjectStyleDatabaseProxyModel : public QSortFilterProxyModel
382{
383 Q_OBJECT
384
385 public:
386
388 enum class Filter : int SIP_ENUM_BASETYPE( IntFlag )
389 {
390 FilterHideReadOnly = 1 << 0,
391 };
392 Q_ENUM( Filter )
394 Q_DECLARE_FLAGS( Filters, Filter )
395 Q_FLAG( Filters )
396
401
402 bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const override;
403
410
416 void setFilters( QgsProjectStyleDatabaseProxyModel::Filters filters );
417
418 private:
419
421
422};
424
425
426#endif // QGSPROJECTSTYLESETTINGS_H
QFlags< ProjectReadFlag > ProjectReadFlags
Project load flags.
Definition: qgis.h:3562
SymbolType
Symbol types.
Definition: qgis.h:401
Abstract base class for color ramps.
Definition: qgscolorramp.h:29
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.
QFlags< Filter > Filters
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:107
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:94
Container for all settings relating to text rendering.
Definition: qgstextformat.h:41
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#define SIP_ENUM_BASETYPE(type)
Definition: qgis_sip.h:278
#define SIP_MONKEYPATCH_SCOPEENUM_UNNEST(OUTSIDE_CLASS, FORMERNAME)
Definition: qgis_sip.h:271
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_MONKEYPATCH_COMPAT_NAME(FORMERNAME)
Definition: qgis_sip.h:273
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsTextRendererUtils::CurvedTextFlags)