QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
225  void styleDatabasesChanged();
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 
262  void projectStyleChanged();
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
QgsColorRamp
Abstract base class for color ramps.
Definition: qgscolorramp.h:29
QgsCombinedStyleModel
A model which contains entities from multiple QgsStyle databases.
Definition: qgscombinedstylemodel.h:42
QgsProjectStyleSettings::styleDatabasePaths
QStringList styleDatabasePaths() const
Returns a list of all style databases (file paths) associated with the project.
Definition: qgsprojectstylesettings.h:161
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsProjectStyleDatabaseModel
List model representing the style databases associated with a QgsProject.
Definition: qgsprojectstylesettings.h:297
QgsProjectStyleSettings::setDefaultSymbolOpacity
void setDefaultSymbolOpacity(double opacity)
Sets the default symbol opacity.
Definition: qgsprojectstylesettings.h:118
QgsProjectStyleSettings
Contains settings and properties relating to how a QgsProject should handle styling.
Definition: qgsprojectstylesettings.h:43
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:92
QgsProjectStyleDatabaseModel::PathRole
@ PathRole
Style path.
Definition: qgsprojectstylesettings.h:307
QgsProjectStyleDatabaseProxyModel::Filter
Filter
Available filter flags for filtering the model.
Definition: qgsprojectstylesettings.h:376
QgsProjectStyleDatabaseModel::showDefaultStyle
bool showDefaultStyle() const
Returns true if the model includes the default style.
Definition: qgsprojectstylesettings.h:342
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:103
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsTextFormat
Container for all settings relating to text rendering.
Definition: qgstextformat.h:40
QgsProjectStyleSettings::randomizeDefaultSymbolColor
bool randomizeDefaultSymbolColor() const
Returns whether the default symbol fill color is randomized.
Definition: qgsprojectstylesettings.h:103
QgsProjectStyleSettings::setRandomizeDefaultSymbolColor
void setRandomizeDefaultSymbolColor(bool randomized)
Sets whether the default symbol fill color is randomized.
Definition: qgsprojectstylesettings.h:108
QgsProjectStyleDatabaseModel::Role
Role
Custom model roles.
Definition: qgsprojectstylesettings.h:304
Qgis::SymbolType
SymbolType
Symbol types.
Definition: qgis.h:205
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
Q_DECLARE_OPERATORS_FOR_FLAGS
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.
SIP_IF_FEATURE
#define SIP_IF_FEATURE(feature)
Definition: qgis_sip.h:176
QgsStyle
Definition: qgsstyle.h:159
QgsProjectStyleDatabaseProxyModel
A proxy model for filtering QgsProjectStyleDatabaseModel.
Definition: qgsprojectstylesettings.h:369
qgstextformat.h
QgsProjectStyleSettings::defaultSymbolOpacity
double defaultSymbolOpacity() const
Returns the default symbol opacity.
Definition: qgsprojectstylesettings.h:113
qgswkbtypes.h
SIP_END
#define SIP_END
Definition: qgis_sip.h:203
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53