QGIS API Documentation  3.17.0-Master (a035f434f4)
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;
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  {
188  };
189 
201  bool addEntity( const QString &name, const QgsStyleEntityInterface *entity, bool update = false );
202 
212  bool addSymbol( const QString &name, QgsSymbol *symbol SIP_TRANSFER, bool update = false );
213 
222  bool addColorRamp( const QString &name, QgsColorRamp *colorRamp SIP_TRANSFER, bool update = false );
223 
234  bool addTextFormat( const QString &name, const QgsTextFormat &format, bool update = false );
235 
246  bool addLabelSettings( const QString &name, const QgsPalLayerSettings &settings, bool update = false );
247 
258  bool addLegendPatchShape( const QString &name, const QgsLegendPatchShape &shape, bool update = false );
259 
270  bool addSymbol3D( const QString &name, QgsAbstract3DSymbol *symbol SIP_TRANSFER, bool update = false );
271 
278  int addTag( const QString &tagName );
279 
289  int addSmartgroup( const QString &name, const QString &op, const QgsSmartConditionMap &conditions ) SIP_SKIP;
290 
303  int addSmartgroup( const QString &name, const QString &op, const QStringList &matchTag, const QStringList &noMatchTag,
304  const QStringList &matchName, const QStringList &noMatchName );
305 
312  QStringList tags() const;
313 
315  void clear();
316 
321  QgsColorRamp *colorRamp( const QString &name ) const SIP_FACTORY;
322 
324  int colorRampCount();
325 
327  QStringList colorRampNames() const;
328 
330  const QgsColorRamp *colorRampRef( const QString &name ) const;
331 
336  int colorrampId( const QString &name );
337 
343  QgsTextFormat textFormat( const QString &name ) const;
344 
349  int textFormatCount() const;
350 
355  QStringList textFormatNames() const;
356 
363  int textFormatId( const QString &name );
364 
370  QgsPalLayerSettings labelSettings( const QString &name ) const;
371 
377  QgsLegendPatchShape legendPatchShape( const QString &name ) const;
378 
383  int legendPatchShapesCount() const;
384 
392  QgsSymbol::SymbolType legendPatchShapeSymbolType( const QString &name ) const;
393 
399  QgsAbstract3DSymbol *symbol3D( const QString &name ) const SIP_FACTORY;
400 
405  int symbol3DCount() const;
406 
413  QList< QgsWkbTypes::GeometryType > symbol3DCompatibleGeometryTypes( const QString &name ) const;
414 
422  QgsWkbTypes::GeometryType labelSettingsLayerType( const QString &name ) const;
423 
428  int labelSettingsCount() const;
429 
434  QStringList labelSettingsNames() const;
435 
442  int labelSettingsId( const QString &name );
443 
445  static QgsStyle *defaultStyle();
446 
448  static void cleanDefaultStyle() SIP_SKIP;
449 
459  bool tagSymbol( StyleEntity type, const QString &symbol, const QStringList &tags );
460 
470  bool detagSymbol( StyleEntity type, const QString &symbol, const QStringList &tags );
471 
480  bool detagSymbol( StyleEntity type, const QString &symbol );
481 
483  bool removeSymbol( const QString &name );
484 
492  bool renameEntity( StyleEntity type, const QString &oldName, const QString &newName );
493 
499  bool renameSymbol( const QString &oldName, const QString &newName );
500 
502  QgsSymbol *symbol( const QString &name ) SIP_FACTORY;
503 
505  const QgsSymbol *symbolRef( const QString &name ) const;
506 
508  int symbolCount();
509 
511  QStringList symbolNames() const;
512 
517  int symbolId( const QString &name );
518 
523  int entityId( StyleEntity type, const QString &name );
524 
526  int tagId( const QString &tag );
528  int smartgroupId( const QString &smartgroup );
529 
534  QStringList allNames( StyleEntity type ) const;
535 
542  QStringList symbolsOfFavorite( StyleEntity type ) const;
543 
551  QStringList symbolsWithTag( StyleEntity type, int tagid ) const;
552 
560  bool addFavorite( StyleEntity type, const QString &name );
561 
569  bool removeFavorite( StyleEntity type, const QString &name );
570 
578  bool rename( StyleEntity type, int id, const QString &newName );
579 
588  bool remove( StyleEntity type, int id );
589 
596  bool removeEntityByName( StyleEntity type, const QString &name );
597 
607  bool saveSymbol( const QString &name, QgsSymbol *symbol, bool favorite, const QStringList &tags );
608 
618  bool saveColorRamp( const QString &name, QgsColorRamp *ramp, bool favorite, const QStringList &tags );
619 
621  bool removeColorRamp( const QString &name );
622 
624  bool renameColorRamp( const QString &oldName, const QString &newName );
625 
635  bool saveTextFormat( const QString &name, const QgsTextFormat &format, bool favorite, const QStringList &tags );
636 
641  bool removeTextFormat( const QString &name );
642 
648  bool renameTextFormat( const QString &oldName, const QString &newName );
649 
659  bool saveLabelSettings( const QString &name, const QgsPalLayerSettings &settings, bool favorite, const QStringList &tags );
660 
665  bool removeLabelSettings( const QString &name );
666 
672  bool renameLabelSettings( const QString &oldName, const QString &newName );
673 
685  bool saveLegendPatchShape( const QString &name, const QgsLegendPatchShape &shape, bool favorite, const QStringList &tags );
686 
692  bool renameLegendPatchShape( const QString &oldName, const QString &newName );
693 
698  QStringList legendPatchShapeNames() const;
699 
707  const QgsSymbol *previewSymbolForPatchShape( const QgsLegendPatchShape &shape ) const;
708 
715  QgsLegendPatchShape defaultPatch( QgsSymbol::SymbolType type, QSizeF size ) const;
716 
723  QList< QList< QPolygonF > > defaultPatchAsQPolygonF( QgsSymbol::SymbolType type, QSizeF size ) const;
724 
736  bool saveSymbol3D( const QString &name, QgsAbstract3DSymbol *symbol SIP_TRANSFER, bool favorite, const QStringList &tags );
737 
743  bool renameSymbol3D( const QString &oldName, const QString &newName );
744 
749  QStringList symbol3DNames() const;
750 
759  bool createDatabase( const QString &filename );
760 
769  bool createMemoryDatabase();
770 
780  void createTables();
781 
789  bool load( const QString &filename );
790 
792  bool save( QString filename = QString() );
793 
795  QString errorString() { return mErrorString; }
796 
798  QString fileName() { return mFileName; }
799 
807  QStringList findSymbols( StyleEntity type, const QString &qword );
808 
816  QStringList tagsOfSymbol( StyleEntity type, const QString &symbol );
817 
824  bool isFavorite( StyleEntity type, const QString &name );
825 
834  bool symbolHasTag( StyleEntity type, const QString &symbol, const QString &tag );
835 
837  QString tag( int id ) const;
838 
840  QgsSymbolGroupMap smartgroupsListMap();
841 
843  QStringList smartgroupNames() const;
844 
846  QgsSmartConditionMap smartgroup( int id );
847 
851  QString smartgroupOperator( int id );
852 
854  QStringList symbolsOfSmartgroup( StyleEntity type, int id );
855 
857  bool exportXml( const QString &filename );
858 
860  bool importXml( const QString &filename );
861 
870  static bool isXmlStyleFile( const QString &path );
871 
872  signals:
873 
882  void symbolSaved( const QString &name, QgsSymbol *symbol );
883 
892  void symbolChanged( const QString &name );
893 
895  void groupsModified();
896 
902  void entityTagsChanged( QgsStyle::StyleEntity entity, const QString &name, const QStringList &newTags );
903 
909  void favoritedChanged( QgsStyle::StyleEntity entity, const QString &name, bool isFavorite );
910 
916  void entityAdded( QgsStyle::StyleEntity entity, const QString &name );
917 
924  void entityRemoved( QgsStyle::StyleEntity entity, const QString &name );
925 
930  void entityRenamed( QgsStyle::StyleEntity entity, const QString &oldName, const QString &newName );
931 
938  void entityChanged( QgsStyle::StyleEntity entity, const QString &name );
939 
947  void symbolRemoved( const QString &name );
948 
954  void symbolRenamed( const QString &oldName, const QString &newName );
955 
961  void rampRenamed( const QString &oldName, const QString &newName );
962 
970  void rampAdded( const QString &name );
971 
979  void rampRemoved( const QString &name );
980 
989  void rampChanged( const QString &name );
990 
991 
997  void textFormatRenamed( const QString &oldName, const QString &newName );
998 
1006  void textFormatAdded( const QString &name );
1007 
1015  void textFormatRemoved( const QString &name );
1016 
1025  void textFormatChanged( const QString &name );
1026 
1032  void labelSettingsRenamed( const QString &oldName, const QString &newName );
1033 
1041  void labelSettingsAdded( const QString &name );
1042 
1050  void labelSettingsRemoved( const QString &name );
1051 
1060  void labelSettingsChanged( const QString &name );
1061 
1062  private:
1063 
1064  QgsSymbolMap mSymbols;
1065  QgsVectorColorRampMap mColorRamps;
1066  QgsTextFormatMap mTextFormats;
1067  QgsLabelSettingsMap mLabelSettings;
1068  QMap<QString, QgsLegendPatchShape > mLegendPatchShapes;
1069  QMap<QString, QgsAbstract3DSymbol * > m3dSymbols;
1070 
1071  QHash< QgsStyle::StyleEntity, QHash< QString, QStringList > > mCachedTags;
1072  QHash< QgsStyle::StyleEntity, QHash< QString, bool > > mCachedFavorites;
1073 
1074  QString mErrorString;
1075  QString mFileName;
1076 
1077  sqlite3_database_unique_ptr mCurrentDB;
1078 
1079  std::unique_ptr< QgsSymbol > mPatchMarkerSymbol;
1080  std::unique_ptr< QgsSymbol > mPatchLineSymbol;
1081  std::unique_ptr< QgsSymbol > mPatchFillSymbol;
1082 
1083  mutable QHash< QgsSymbol::SymbolType, QHash< QSizeF, QgsLegendPatchShape > > mDefaultPatchCache;
1084  mutable QHash< QgsSymbol::SymbolType, QHash< QSizeF, QList< QList< QPolygonF > > > > mDefaultPatchQPolygonFCache;
1085 
1086  QMap< QString, QDomElement > mDeferred3DsymbolElements;
1087  void handleDeferred3DSymbolCreation();
1088 
1089  static QgsStyle *sDefaultStyle;
1090 
1092  bool openDatabase( const QString &filename );
1093 
1095  bool importXml( const QString &filename, int sinceVersion );
1096 
1103  bool runEmptyQuery( const QString &query );
1104 
1106  int getId( const QString &table, const QString &name );
1107 
1109  QString getName( const QString &table, int id ) const;
1110 
1119  bool updateSymbol( StyleEntity type, const QString &name );
1120 
1121  void clearCachedTags( StyleEntity type, const QString &name );
1122 
1123 
1124  void upgradeIfRequired();
1125 
1129  static QString entityTableName( StyleEntity type );
1130 
1134  static QString tagmapTableName( StyleEntity type );
1135 
1139  static QString tagmapEntityIdFieldName( StyleEntity type );
1140 
1141  friend class Qgs3D;
1142 
1143  Q_DISABLE_COPY( QgsStyle )
1144 };
1145 
1152 class CORE_EXPORT QgsStyleEntityInterface
1153 {
1154 
1155 #ifdef SIP_RUN
1157  switch ( sipCpp->type() )
1158  {
1160  sipType = sipType_QgsStyleSymbolEntity;
1161  break;
1162 
1164  sipType = sipType_QgsStyleColorRampEntity;
1165  break;
1166 
1168  sipType = sipType_QgsStyleTextFormatEntity;
1169  break;
1170 
1172  sipType = sipType_QgsStyleLabelSettingsEntity;
1173  break;
1174 
1176  case QgsStyle::TagEntity:
1177  sipType = 0;
1178  break;
1179  }
1180  SIP_END
1181 #endif
1182 
1183  public:
1184 
1185  virtual ~QgsStyleEntityInterface() = default;
1186 
1190  virtual QgsStyle::StyleEntity type() const = 0;
1191 
1192 };
1193 
1201 {
1202  public:
1203 
1210  : mSymbol( symbol )
1211  {}
1212 
1213  QgsStyle::StyleEntity type() const override;
1214 
1218  QgsSymbol *symbol() const { return mSymbol; }
1219 
1220  private:
1221 
1222  QgsSymbol *mSymbol = nullptr;
1223 
1224 };
1225 
1233 {
1234  public:
1235 
1242  : mRamp( ramp )
1243  {}
1244 
1245  QgsStyle::StyleEntity type() const override;
1246 
1250  QgsColorRamp *ramp() const { return mRamp; }
1251 
1252  private:
1253 
1254  QgsColorRamp *mRamp = nullptr;
1255 };
1256 
1264 {
1265  public:
1266 
1271  : mFormat( format )
1272  {}
1273 
1274  QgsStyle::StyleEntity type() const override;
1275 
1279  QgsTextFormat format() const { return mFormat; }
1280 
1281  private:
1282 
1283  QgsTextFormat mFormat;
1284 
1285 };
1286 
1294 {
1295  public:
1296 
1301  : mSettings( settings )
1302  {}
1303 
1304  QgsStyle::StyleEntity type() const override;
1305 
1306 
1310  const QgsPalLayerSettings &settings() const { return mSettings; }
1311 
1312  private:
1313 
1314  QgsPalLayerSettings mSettings;
1315 };
1316 
1324 {
1325  public:
1326 
1331  : mShape( shape )
1332  {}
1333 
1334  QgsStyle::StyleEntity type() const override;
1335 
1336 
1340  const QgsLegendPatchShape &shape() const { return mShape; }
1341 
1342  private:
1343 
1344  QgsLegendPatchShape mShape;
1345 };
1346 
1354 {
1355  public:
1356 
1363  : mSymbol( symbol )
1364  {}
1365 
1366  QgsStyle::StyleEntity type() const override;
1367 
1371  const QgsAbstract3DSymbol *symbol() const { return mSymbol; }
1372 
1373  private:
1374 
1375  const QgsAbstract3DSymbol *mSymbol = nullptr;
1376 };
1377 
1378 #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:1200
const QgsLegendPatchShape & shape() const
Returns the entity&#39;s legend patch shape.
Definition: qgsstyle.h:1340
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:64
An interface for entities which can be placed in a QgsStyle database.
Definition: qgsstyle.h:1152
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:1323
Tag name.
Definition: qgsstyle.h:100
const QgsAbstract3DSymbol * symbol() const
Returns the entity&#39;s symbol.
Definition: qgsstyle.h:1371
Label settings ID.
Definition: qgsstyle.h:139
Smart group definition (as XML)
Definition: qgsstyle.h:152
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.
3 Abstract base class for 3D symbols that are used by VectorLayer3DRenderer objects.
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:86
const QgsPalLayerSettings & settings() const
Returns the entity&#39;s label settings.
Definition: qgsstyle.h:1310
Smart group name.
Definition: qgsstyle.h:151
QgsTextFormat format() const
Returns the entity&#39;s text format.
Definition: qgsstyle.h:1279
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsStyleLabelSettingsEntity(const QgsPalLayerSettings &settings)
Constructor for QgsStyleLabelSettingsEntity, with the specified label settings.
Definition: qgsstyle.h:1300
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_END
Definition: qgis_sip.h:194
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:1263
Label settings name.
Definition: qgsstyle.h:140
A 3d symbol entity for QgsStyle databases.
Definition: qgsstyle.h:1353
QgsStyleLegendPatchShapeEntity(const QgsLegendPatchShape &shape)
Constructor for QgsStyleLegendPatchShapeEntity, with the specified legend patch shape.
Definition: qgsstyle.h:1330
Symbol ID.
Definition: qgsstyle.h:88
QMap< QString, QgsTextFormat > QgsTextFormatMap
Map of name to text format.
Definition: qgsstyle.h:48
QgsSymbol * symbol() const
Returns the entity&#39;s symbol.
Definition: qgsstyle.h:1218
Qgs3D is a singleton class containing various registries and other global members related to 3D class...
Definition: qgs3d.h:32
Symbol ID.
Definition: qgsstyle.h:109
QgsColorRamp * ramp() const
Returns the entity&#39;s color ramp.
Definition: qgsstyle.h:1250
QgsStyleColorRampEntity(QgsColorRamp *ramp)
Constructor for QgsStyleColorRampEntity, with the specified color ramp.
Definition: qgsstyle.h:1241
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:140
Text format name.
Definition: qgsstyle.h:129
virtual QgsStyle::StyleEntity type() const =0
Returns the type of style entity.
QgsStyleSymbol3DEntity(const QgsAbstract3DSymbol *symbol)
Constructor for QgsStyleSymbol3DEntity, with the specified symbol.
Definition: qgsstyle.h:1362
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:798
A label settings entity for QgsStyle databases.
Definition: qgsstyle.h:1293
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
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
3D symbol entity (since QGIS 3.14)
Definition: qgsstyle.h:187
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:1232
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:39
QgsStyleSymbolEntity(QgsSymbol *symbol)
Constructor for QgsStyleSymbolEntity, with the specified symbol.
Definition: qgsstyle.h:1209
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:1270
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