QGIS API Documentation  3.15.0-Master (a49cb7c9f3)
qgsstyle.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsstyle.h
3  ---------------------
4  begin : November 2009
5  copyright : (C) 2009 by Martin Dobias
6  email : wonder dot sk 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 
16 #ifndef QGSSTYLE_H
17 #define QGSSTYLE_H
18 
19 #include "qgis_core.h"
20 #include "qgis.h"
21 #include <QMap>
22 #include <QMultiMap>
23 #include <QString>
24 
25 #include <sqlite3.h>
26 
27 #include "qgssqliteutils.h"
28 #include "qgssymbollayerutils.h" // QgsStringMap
29 #include "qgstextformat.h"
30 #include "qgspallabeling.h"
32 
33 class QgsSymbol;
34 class QgsSymbolLayer;
35 class QgsColorRamp;
37 
38 class QDomDocument;
39 class QDomElement;
40 
41 typedef QMap<QString, QgsColorRamp * > QgsVectorColorRampMap;
42 typedef QMap<int, QString> QgsSymbolGroupMap;
43 
48 typedef QMap<QString, QgsTextFormat > QgsTextFormatMap;
49 
54 typedef QMap<QString, QgsPalLayerSettings > QgsLabelSettingsMap;
55 
56 /*
57  * Constants used to describe copy-paste MIME types
58  */
59 #define QGSCLIPBOARD_STYLE_MIME "application/qgis.style"
60 
79 typedef QMultiMap<QString, QString> QgsSmartConditionMap;
80 
81 // enumerators representing sqlite DB columns
82 
87 {
92 };
93 
98 {
101 };
102 
107 {
110 };
111 
116 {
121 };
122 
127 {
132 };
133 
138 {
143 };
144 
149 {
153 };
154 
159 class CORE_EXPORT QgsStyle : public QObject
160 {
161  Q_OBJECT
162 
163  public:
164 
168  QgsStyle();
169  ~QgsStyle() override;
170 
179  {
187  };
188 
200  bool addEntity( const QString &name, const QgsStyleEntityInterface *entity, bool update = false );
201 
211  bool addSymbol( const QString &name, QgsSymbol *symbol SIP_TRANSFER, bool update = false );
212 
221  bool addColorRamp( const QString &name, QgsColorRamp *colorRamp SIP_TRANSFER, bool update = false );
222 
233  bool addTextFormat( const QString &name, const QgsTextFormat &format, bool update = false );
234 
245  bool addLabelSettings( const QString &name, const QgsPalLayerSettings &settings, bool update = false );
246 
257  bool addLegendPatchShape( const QString &name, const QgsLegendPatchShape &shape, bool update = false );
258 
265  int addTag( const QString &tagName );
266 
276  int addSmartgroup( const QString &name, const QString &op, const QgsSmartConditionMap &conditions ) SIP_SKIP;
277 
290  int addSmartgroup( const QString &name, const QString &op, const QStringList &matchTag, const QStringList &noMatchTag,
291  const QStringList &matchName, const QStringList &noMatchName );
292 
299  QStringList tags() const;
300 
302  void clear();
303 
308  QgsColorRamp *colorRamp( const QString &name ) const SIP_FACTORY;
309 
311  int colorRampCount();
312 
314  QStringList colorRampNames() const;
315 
317  const QgsColorRamp *colorRampRef( const QString &name ) const;
318 
323  int colorrampId( const QString &name );
324 
330  QgsTextFormat textFormat( const QString &name ) const;
331 
336  int textFormatCount() const;
337 
342  QStringList textFormatNames() const;
343 
350  int textFormatId( const QString &name );
351 
357  QgsPalLayerSettings labelSettings( const QString &name ) const;
358 
364  QgsLegendPatchShape legendPatchShape( const QString &name ) const;
365 
370  int legendPatchShapesCount() const;
371 
379  QgsSymbol::SymbolType legendPatchShapeSymbolType( const QString &name ) const;
380 
388  QgsWkbTypes::GeometryType labelSettingsLayerType( const QString &name ) const;
389 
394  int labelSettingsCount() const;
395 
400  QStringList labelSettingsNames() const;
401 
408  int labelSettingsId( const QString &name );
409 
411  static QgsStyle *defaultStyle();
412 
414  static void cleanDefaultStyle() SIP_SKIP;
415 
425  bool tagSymbol( StyleEntity type, const QString &symbol, const QStringList &tags );
426 
436  bool detagSymbol( StyleEntity type, const QString &symbol, const QStringList &tags );
437 
446  bool detagSymbol( StyleEntity type, const QString &symbol );
447 
449  bool removeSymbol( const QString &name );
450 
458  bool renameEntity( StyleEntity type, const QString &oldName, const QString &newName );
459 
465  bool renameSymbol( const QString &oldName, const QString &newName );
466 
468  QgsSymbol *symbol( const QString &name ) SIP_FACTORY;
469 
471  const QgsSymbol *symbolRef( const QString &name ) const;
472 
474  int symbolCount();
475 
477  QStringList symbolNames() const;
478 
483  int symbolId( const QString &name );
484 
489  int entityId( StyleEntity type, const QString &name );
490 
492  int tagId( const QString &tag );
494  int smartgroupId( const QString &smartgroup );
495 
500  QStringList allNames( StyleEntity type ) const;
501 
508  QStringList symbolsOfFavorite( StyleEntity type ) const;
509 
517  QStringList symbolsWithTag( StyleEntity type, int tagid ) const;
518 
526  bool addFavorite( StyleEntity type, const QString &name );
527 
535  bool removeFavorite( StyleEntity type, const QString &name );
536 
544  bool rename( StyleEntity type, int id, const QString &newName );
545 
554  bool remove( StyleEntity type, int id );
555 
562  bool removeEntityByName( StyleEntity type, const QString &name );
563 
573  bool saveSymbol( const QString &name, QgsSymbol *symbol, bool favorite, const QStringList &tags );
574 
584  bool saveColorRamp( const QString &name, QgsColorRamp *ramp, bool favorite, const QStringList &tags );
585 
587  bool removeColorRamp( const QString &name );
588 
590  bool renameColorRamp( const QString &oldName, const QString &newName );
591 
601  bool saveTextFormat( const QString &name, const QgsTextFormat &format, bool favorite, const QStringList &tags );
602 
607  bool removeTextFormat( const QString &name );
608 
614  bool renameTextFormat( const QString &oldName, const QString &newName );
615 
625  bool saveLabelSettings( const QString &name, const QgsPalLayerSettings &settings, bool favorite, const QStringList &tags );
626 
631  bool removeLabelSettings( const QString &name );
632 
638  bool renameLabelSettings( const QString &oldName, const QString &newName );
639 
651  bool saveLegendPatchShape( const QString &name, const QgsLegendPatchShape &shape, bool favorite, const QStringList &tags );
652 
658  bool renameLegendPatchShape( const QString &oldName, const QString &newName );
659 
664  QStringList legendPatchShapeNames() const;
665 
673  const QgsSymbol *previewSymbolForPatchShape( const QgsLegendPatchShape &shape ) const;
674 
681  QgsLegendPatchShape defaultPatch( QgsSymbol::SymbolType type, QSizeF size ) const;
682 
689  QList< QList< QPolygonF > > defaultPatchAsQPolygonF( QgsSymbol::SymbolType type, QSizeF size ) const;
690 
699  bool createDatabase( const QString &filename );
700 
709  bool createMemoryDatabase();
710 
720  void createTables();
721 
729  bool load( const QString &filename );
730 
732  bool save( QString filename = QString() );
733 
735  QString errorString() { return mErrorString; }
736 
738  QString fileName() { return mFileName; }
739 
747  QStringList findSymbols( StyleEntity type, const QString &qword );
748 
756  QStringList tagsOfSymbol( StyleEntity type, const QString &symbol );
757 
764  bool isFavorite( StyleEntity type, const QString &name );
765 
774  bool symbolHasTag( StyleEntity type, const QString &symbol, const QString &tag );
775 
777  QString tag( int id ) const;
778 
780  QgsSymbolGroupMap smartgroupsListMap();
781 
783  QStringList smartgroupNames() const;
784 
786  QgsSmartConditionMap smartgroup( int id );
787 
791  QString smartgroupOperator( int id );
792 
794  QStringList symbolsOfSmartgroup( StyleEntity type, int id );
795 
797  bool exportXml( const QString &filename );
798 
800  bool importXml( const QString &filename );
801 
810  static bool isXmlStyleFile( const QString &path );
811 
812  signals:
813 
822  void symbolSaved( const QString &name, QgsSymbol *symbol );
823 
832  void symbolChanged( const QString &name );
833 
835  void groupsModified();
836 
842  void entityTagsChanged( QgsStyle::StyleEntity entity, const QString &name, const QStringList &newTags );
843 
849  void favoritedChanged( QgsStyle::StyleEntity entity, const QString &name, bool isFavorite );
850 
856  void entityAdded( QgsStyle::StyleEntity entity, const QString &name );
857 
864  void entityRemoved( QgsStyle::StyleEntity entity, const QString &name );
865 
870  void entityRenamed( QgsStyle::StyleEntity entity, const QString &oldName, const QString &newName );
871 
878  void entityChanged( QgsStyle::StyleEntity entity, const QString &name );
879 
887  void symbolRemoved( const QString &name );
888 
894  void symbolRenamed( const QString &oldName, const QString &newName );
895 
901  void rampRenamed( const QString &oldName, const QString &newName );
902 
910  void rampAdded( const QString &name );
911 
919  void rampRemoved( const QString &name );
920 
929  void rampChanged( const QString &name );
930 
931 
937  void textFormatRenamed( const QString &oldName, const QString &newName );
938 
946  void textFormatAdded( const QString &name );
947 
955  void textFormatRemoved( const QString &name );
956 
965  void textFormatChanged( const QString &name );
966 
972  void labelSettingsRenamed( const QString &oldName, const QString &newName );
973 
981  void labelSettingsAdded( const QString &name );
982 
990  void labelSettingsRemoved( const QString &name );
991 
1000  void labelSettingsChanged( const QString &name );
1001 
1002  private:
1003 
1004  QgsSymbolMap mSymbols;
1005  QgsVectorColorRampMap mColorRamps;
1006  QgsTextFormatMap mTextFormats;
1007  QgsLabelSettingsMap mLabelSettings;
1008  QMap<QString, QgsLegendPatchShape > mLegendPatchShapes;
1009 
1010  QHash< QgsStyle::StyleEntity, QHash< QString, QStringList > > mCachedTags;
1011  QHash< QgsStyle::StyleEntity, QHash< QString, bool > > mCachedFavorites;
1012 
1013  QString mErrorString;
1014  QString mFileName;
1015 
1016  sqlite3_database_unique_ptr mCurrentDB;
1017 
1018  std::unique_ptr< QgsSymbol > mPatchMarkerSymbol;
1019  std::unique_ptr< QgsSymbol > mPatchLineSymbol;
1020  std::unique_ptr< QgsSymbol > mPatchFillSymbol;
1021 
1022  mutable QHash< QgsSymbol::SymbolType, QHash< QSizeF, QgsLegendPatchShape > > mDefaultPatchCache;
1023  mutable QHash< QgsSymbol::SymbolType, QHash< QSizeF, QList< QList< QPolygonF > > > > mDefaultPatchQPolygonFCache;
1024 
1025  static QgsStyle *sDefaultStyle;
1026 
1028  bool openDatabase( const QString &filename );
1029 
1031  bool importXml( const QString &filename, int sinceVersion );
1032 
1039  bool runEmptyQuery( const QString &query );
1040 
1042  int getId( const QString &table, const QString &name );
1043 
1045  QString getName( const QString &table, int id ) const;
1046 
1055  bool updateSymbol( StyleEntity type, const QString &name );
1056 
1057  void clearCachedTags( StyleEntity type, const QString &name );
1058 
1059 
1060  void upgradeIfRequired();
1061 
1065  static QString entityTableName( StyleEntity type );
1066 
1070  static QString tagmapTableName( StyleEntity type );
1071 
1075  static QString tagmapEntityIdFieldName( StyleEntity type );
1076 
1077  Q_DISABLE_COPY( QgsStyle )
1078 };
1079 
1086 class CORE_EXPORT QgsStyleEntityInterface
1087 {
1088 
1089 #ifdef SIP_RUN
1091  switch ( sipCpp->type() )
1092  {
1094  sipType = sipType_QgsStyleSymbolEntity;
1095  break;
1096 
1098  sipType = sipType_QgsStyleColorRampEntity;
1099  break;
1100 
1102  sipType = sipType_QgsStyleTextFormatEntity;
1103  break;
1104 
1106  sipType = sipType_QgsStyleLabelSettingsEntity;
1107  break;
1108 
1110  case QgsStyle::TagEntity:
1111  sipType = 0;
1112  break;
1113  }
1114  SIP_END
1115 #endif
1116 
1117  public:
1118 
1119  virtual ~QgsStyleEntityInterface() = default;
1120 
1124  virtual QgsStyle::StyleEntity type() const = 0;
1125 
1126 };
1127 
1135 {
1136  public:
1137 
1144  : mSymbol( symbol )
1145  {}
1146 
1147  QgsStyle::StyleEntity type() const override;
1148 
1152  const QgsSymbol *symbol() const { return mSymbol; }
1153 
1154  private:
1155 
1156  const QgsSymbol *mSymbol = nullptr;
1157 
1158 };
1159 
1167 {
1168  public:
1169 
1176  : mRamp( ramp )
1177  {}
1178 
1179  QgsStyle::StyleEntity type() const override;
1180 
1184  const QgsColorRamp *ramp() const { return mRamp; }
1185 
1186  private:
1187 
1188  const QgsColorRamp *mRamp = nullptr;
1189 };
1190 
1198 {
1199  public:
1200 
1205  : mFormat( format )
1206  {}
1207 
1208  QgsStyle::StyleEntity type() const override;
1209 
1213  QgsTextFormat format() const { return mFormat; }
1214 
1215  private:
1216 
1217  QgsTextFormat mFormat;
1218 
1219 };
1220 
1228 {
1229  public:
1230 
1235  : mSettings( settings )
1236  {}
1237 
1238  QgsStyle::StyleEntity type() const override;
1239 
1240 
1244  const QgsPalLayerSettings &settings() const { return mSettings; }
1245 
1246  private:
1247 
1248  QgsPalLayerSettings mSettings;
1249 };
1250 
1258 {
1259  public:
1260 
1265  : mShape( shape )
1266  {}
1267 
1268  QgsStyle::StyleEntity type() const override;
1269 
1270 
1274  const QgsLegendPatchShape &shape() const { return mShape; }
1275 
1276  private:
1277 
1278  QgsLegendPatchShape mShape;
1279 };
1280 
1281 #endif
TagmapTable
Columns available in the tag to symbol table.
Definition: qgsstyle.h:106
Text format ID.
Definition: qgsstyle.h:128
Tag ID.
Definition: qgsstyle.h:108
Text format is favorite flag.
Definition: qgsstyle.h:131
A symbol entity for QgsStyle databases.
Definition: qgsstyle.h:1134
const QgsLegendPatchShape & shape() const
Returns the entity&#39;s legend patch shape.
Definition: qgsstyle.h:1274
QgsStyleColorRampEntity(const QgsColorRamp *ramp)
Constructor for QgsStyleColorRampEntity, with the specified color ramp.
Definition: qgsstyle.h:1175
const QgsSymbol * symbol() const
Returns the entity&#39;s symbol.
Definition: qgsstyle.h:1152
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:63
An interface for entities which can be placed in a QgsStyle database.
Definition: qgsstyle.h:1086
QgsStyleSymbolEntity(const QgsSymbol *symbol)
Constructor for QgsStyleSymbolEntity, with the specified symbol.
Definition: qgsstyle.h:1143
QMap< QString, QgsPalLayerSettings > QgsLabelSettingsMap
Map of name to label settings.
Definition: qgsstyle.h:54
Abstract base class for color ramps.
Definition: qgscolorramp.h:31
QMap< int, QString > QgsSymbolGroupMap
Definition: qgsstyle.h:42
A legend patch shape entity for QgsStyle databases.
Definition: qgsstyle.h:1257
Tag name.
Definition: qgsstyle.h:100
Label settings ID.
Definition: qgsstyle.h:139
Smart group definition (as XML)
Definition: qgsstyle.h:152
const QgsColorRamp * ramp() const
Returns the entity&#39;s color ramp.
Definition: qgsstyle.h:1184
Symbol definition (as XML)
Definition: qgsstyle.h:90
SmartgroupTable
Columns available in the smart group table.
Definition: qgsstyle.h:148
Represents a patch shape for use in map legends.
StyleEntity
Enum for Entities involved in a style.
Definition: qgsstyle.h:178
TextFormatTable
Columns available in the text format table.
Definition: qgsstyle.h:126
SymbolType
Type of the symbol.
Definition: qgssymbol.h:85
const QgsPalLayerSettings & settings() const
Returns the entity&#39;s label settings.
Definition: qgsstyle.h:1244
Smart group name.
Definition: qgsstyle.h:151
QgsTextFormat format() const
Returns the entity&#39;s text format.
Definition: qgsstyle.h:1213
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsStyleLabelSettingsEntity(const QgsPalLayerSettings &settings)
Constructor for QgsStyleLabelSettingsEntity, with the specified label settings.
Definition: qgsstyle.h:1234
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_END
Definition: qgis_sip.h:189
TagTable
Columns available in the Tags table.
Definition: qgsstyle.h:97
Legend patch shape (since QGIS 3.14)
Definition: qgsstyle.h:186
#define SIP_FACTORY
Definition: qgis_sip.h:76
A text format entity for QgsStyle databases.
Definition: qgsstyle.h:1197
Label settings name.
Definition: qgsstyle.h:140
QgsStyleLegendPatchShapeEntity(const QgsLegendPatchShape &shape)
Constructor for QgsStyleLegendPatchShapeEntity, with the specified legend patch shape.
Definition: qgsstyle.h:1264
Symbol ID.
Definition: qgsstyle.h:88
QMap< QString, QgsTextFormat > QgsTextFormatMap
Map of name to text format.
Definition: qgsstyle.h:48
Symbol ID.
Definition: qgsstyle.h:109
Unique pointer for sqlite3 databases, which automatically closes the database when the pointer goes o...
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:139
Text format name.
Definition: qgsstyle.h:129
virtual QgsStyle::StyleEntity type() const =0
Returns the type of style entity.
Text format definition (as XML)
Definition: qgsstyle.h:130
ColorrampTable
Columns available in the color ramp table.
Definition: qgsstyle.h:115
QMap< QString, QgsColorRamp *> QgsVectorColorRampMap
Definition: qgsstyle.h:39
QString fileName()
Returns current file name of the style.
Definition: qgsstyle.h:738
A label settings entity for QgsStyle databases.
Definition: qgsstyle.h:1227
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:172
QMap< QString, QgsSymbol *> QgsSymbolMap
Definition: qgsrenderer.h:46
Tag ID.
Definition: qgsstyle.h:99
SymbolTable
Columns available in the Symbols table.
Definition: qgsstyle.h:86
Label settings is favorite flag.
Definition: qgsstyle.h:142
LabelSettingsTable
Columns available in the label settings table.
Definition: qgsstyle.h:137
Smart group ID.
Definition: qgsstyle.h:150
A color ramp entity for QgsStyle databases.
Definition: qgsstyle.h:1166
Symbol is favorite flag.
Definition: qgsstyle.h:91
Symbol Name.
Definition: qgsstyle.h:89
Color ramp name.
Definition: qgsstyle.h:118
Container for all settings relating to text rendering.
Definition: qgstextformat.h:38
Label settings definition (as XML)
Definition: qgsstyle.h:141
Color ramp is favorite flag.
Definition: qgsstyle.h:120
QgsStyleTextFormatEntity(const QgsTextFormat &format)
Constructor for QgsStyleTextFormatEntity, with the specified text format.
Definition: qgsstyle.h:1204
Color ramp definition (as XML)
Definition: qgsstyle.h:119
QMultiMap< QString, QString > QgsSmartConditionMap
A multimap to hold the smart group conditions as constraint and parameter pairs.
Definition: qgsstyle.h:79
Color ramp ID.
Definition: qgsstyle.h:117