QGIS API Documentation  3.27.0-Master (0e23467727)
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 
27 class QDomElement;
29 class QDomDocument;
30 class QgsProject;
31 class QgsSymbol;
32 class QgsColorRamp;
33 class QgsStyle;
35 
43 class 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 
203 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
204  SIP_IF_FEATURE( CONCATENATED_TABLES_MODEL )
205 
206 
216  QgsCombinedStyleModel *combinedStyleModel();
217  SIP_END
218 #endif
219 
220  signals:
221 
226 
227 #ifndef SIP_RUN
228 
234  void styleDatabaseAboutToBeAdded( const QString &path );
235 
241  void styleDatabaseAdded( const QString &path );
242 
248  void styleDatabaseAboutToBeRemoved( const QString &path );
249 
255  void styleDatabaseRemoved( const QString &path );
256 
263 
264 #endif
265  private:
266 
267  QgsProject *mProject = nullptr;
268 
269  std::unique_ptr< QgsSymbol > mDefaultMarkerSymbol;
270  std::unique_ptr< QgsSymbol > mDefaultLineSymbol;
271  std::unique_ptr< QgsSymbol > mDefaultFillSymbol;
272  std::unique_ptr< QgsColorRamp > mDefaultColorRamp;
273  QgsTextFormat mDefaultTextFormat;
274 
275  bool mRandomizeDefaultSymbolColor = true;
276  double mDefaultSymbolOpacity = 1.0;
277 
278  QgsStyle *mProjectStyle = nullptr;
279  QStringList mStyleDatabases;
280  QList< QPointer< QgsStyle > > mStyles;
281 
282  QgsCombinedStyleModel *mCombinedStyleModel = nullptr;
283 
284  void loadStyleAtPath( const QString &path );
285  void clearStyles();
286 
287 };
288 
297 class CORE_EXPORT QgsProjectStyleDatabaseModel : public QAbstractListModel
298 {
299  Q_OBJECT
300 
301  public:
302 
304  enum Role
305  {
306  StyleRole = Qt::UserRole + 1,
308  };
309 
313  explicit QgsProjectStyleDatabaseModel( QgsProjectStyleSettings *settings, QObject *parent SIP_TRANSFERTHIS = nullptr );
314 
315  int rowCount( const QModelIndex &parent ) const override;
316  QVariant data( const QModelIndex &index, int role ) const override;
317 
322  QgsStyle *styleFromIndex( const QModelIndex &index ) const;
323 
328  QModelIndex indexFromStyle( QgsStyle *style ) const;
329 
335  void setShowDefaultStyle( bool show );
336 
342  bool showDefaultStyle() const { return mShowDefault; }
343 
344  private slots:
345  void styleDatabaseAboutToBeAdded( const QString &path );
346  void styleDatabaseAboutToBeRemoved( const QString &path );
347  void styleDatabaseAdded( const QString &path );
348  void styleDatabaseRemoved( const QString &path );
349 
350  void setProjectStyle( QgsStyle *style );
351  void projectStyleAboutToBeDestroyed();
352  void projectStyleDestroyed();
353  void projectStyleChanged();
354 
355  private:
356  QgsProjectStyleSettings *mSettings = nullptr;
357  bool mShowDefault = false;
358  QPointer< QgsStyle > mProjectStyle;
359 };
360 
369 class CORE_EXPORT QgsProjectStyleDatabaseProxyModel : public QSortFilterProxyModel
370 {
371  Q_OBJECT
372 
373  public:
374 
376  enum class Filter
377  {
378  FilterHideReadOnly = 1 << 0,
379  };
380  Q_ENUM( Filter )
382  Q_DECLARE_FLAGS( Filters, Filter )
383  Q_FLAG( Filters )
384 
385 
389 
390  bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const override;
391 
397  QgsProjectStyleDatabaseProxyModel::Filters filters() const;
398 
404  void setFilters( QgsProjectStyleDatabaseProxyModel::Filters filters );
405 
406  private:
407 
408  QgsProjectStyleDatabaseProxyModel::Filters mFilters;
409 
410 };
411 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProjectStyleDatabaseProxyModel::Filters )
412 
413 
414 #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_IF_FEATURE(feature)
Definition: qgis_sip.h:176
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_END
Definition: qgis_sip.h:203
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.