QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 "qgstextrenderer.h"
30 #include "qgspallabeling.h"
31 
32 class QgsSymbol;
33 class QgsSymbolLayer;
34 class QgsColorRamp;
36 
37 class QDomDocument;
38 class QDomElement;
39 
40 typedef QMap<QString, QgsColorRamp * > QgsVectorColorRampMap;
41 typedef QMap<int, QString> QgsSymbolGroupMap;
42 
47 typedef QMap<QString, QgsTextFormat > QgsTextFormatMap;
48 
53 typedef QMap<QString, QgsPalLayerSettings > QgsLabelSettingsMap;
54 
55 /*
56  * Constants used to describe copy-paste MIME types
57  */
58 #define QGSCLIPBOARD_STYLE_MIME "application/qgis.style"
59 
78 typedef QMultiMap<QString, QString> QgsSmartConditionMap;
79 
80 // enumerators representing sqlite DB columns
81 
86 {
91 };
92 
97 {
100 };
101 
106 {
109 };
110 
115 {
120 };
121 
126 {
131 };
132 
137 {
142 };
143 
148 {
152 };
153 
158 class CORE_EXPORT QgsStyle : public QObject
159 {
160  Q_OBJECT
161 
162  public:
163 
167  QgsStyle() = default;
168  ~QgsStyle() override;
169 
178  {
185  };
186 
198  bool addEntity( const QString &name, const QgsStyleEntityInterface *entity, bool update = false );
199 
209  bool addSymbol( const QString &name, QgsSymbol *symbol SIP_TRANSFER, bool update = false );
210 
219  bool addColorRamp( const QString &name, QgsColorRamp *colorRamp SIP_TRANSFER, bool update = false );
220 
231  bool addTextFormat( const QString &name, const QgsTextFormat &format, bool update = false );
232 
243  bool addLabelSettings( const QString &name, const QgsPalLayerSettings &settings, bool update = false );
244 
251  int addTag( const QString &tagName );
252 
262  int addSmartgroup( const QString &name, const QString &op, const QgsSmartConditionMap &conditions ) SIP_SKIP;
263 
276  int addSmartgroup( const QString &name, const QString &op, const QStringList &matchTag, const QStringList &noMatchTag,
277  const QStringList &matchName, const QStringList &noMatchName );
278 
285  QStringList tags() const;
286 
288  void clear();
289 
294  QgsColorRamp *colorRamp( const QString &name ) const SIP_FACTORY;
295 
297  int colorRampCount();
298 
300  QStringList colorRampNames() const;
301 
303  const QgsColorRamp *colorRampRef( const QString &name ) const;
304 
309  int colorrampId( const QString &name );
310 
316  QgsTextFormat textFormat( const QString &name ) const;
317 
322  int textFormatCount() const;
323 
328  QStringList textFormatNames() const;
329 
336  int textFormatId( const QString &name );
337 
343  QgsPalLayerSettings labelSettings( const QString &name ) const;
344 
352  QgsWkbTypes::GeometryType labelSettingsLayerType( const QString &name ) const;
353 
358  int labelSettingsCount() const;
359 
364  QStringList labelSettingsNames() const;
365 
372  int labelSettingsId( const QString &name );
373 
375  static QgsStyle *defaultStyle();
376 
378  static void cleanDefaultStyle() SIP_SKIP;
379 
389  bool tagSymbol( StyleEntity type, const QString &symbol, const QStringList &tags );
390 
400  bool detagSymbol( StyleEntity type, const QString &symbol, const QStringList &tags );
401 
410  bool detagSymbol( StyleEntity type, const QString &symbol );
411 
413  bool removeSymbol( const QString &name );
414 
420  bool renameSymbol( const QString &oldName, const QString &newName );
421 
423  QgsSymbol *symbol( const QString &name ) SIP_FACTORY;
424 
426  const QgsSymbol *symbolRef( const QString &name ) const;
427 
429  int symbolCount();
430 
432  QStringList symbolNames() const;
433 
438  int symbolId( const QString &name );
440  int tagId( const QString &tag );
442  int smartgroupId( const QString &smartgroup );
443 
448  QStringList allNames( StyleEntity type ) const;
449 
456  QStringList symbolsOfFavorite( StyleEntity type ) const;
457 
465  QStringList symbolsWithTag( StyleEntity type, int tagid ) const;
466 
474  bool addFavorite( StyleEntity type, const QString &name );
475 
483  bool removeFavorite( StyleEntity type, const QString &name );
484 
492  bool rename( StyleEntity type, int id, const QString &newName );
493 
500  bool remove( StyleEntity type, int id );
501 
511  bool saveSymbol( const QString &name, QgsSymbol *symbol, bool favorite, const QStringList &tags );
512 
522  bool saveColorRamp( const QString &name, QgsColorRamp *ramp, bool favorite, const QStringList &tags );
523 
525  bool removeColorRamp( const QString &name );
526 
528  bool renameColorRamp( const QString &oldName, const QString &newName );
529 
539  bool saveTextFormat( const QString &name, const QgsTextFormat &format, bool favorite, const QStringList &tags );
540 
545  bool removeTextFormat( const QString &name );
546 
552  bool renameTextFormat( const QString &oldName, const QString &newName );
553 
563  bool saveLabelSettings( const QString &name, const QgsPalLayerSettings &settings, bool favorite, const QStringList &tags );
564 
569  bool removeLabelSettings( const QString &name );
570 
576  bool renameLabelSettings( const QString &oldName, const QString &newName );
577 
586  bool createDatabase( const QString &filename );
587 
596  bool createMemoryDatabase();
597 
607  void createTables();
608 
616  bool load( const QString &filename );
617 
619  bool save( QString filename = QString() );
620 
622  QString errorString() { return mErrorString; }
623 
625  QString fileName() { return mFileName; }
626 
634  QStringList findSymbols( StyleEntity type, const QString &qword );
635 
643  QStringList tagsOfSymbol( StyleEntity type, const QString &symbol );
644 
651  bool isFavorite( StyleEntity type, const QString &name );
652 
661  bool symbolHasTag( StyleEntity type, const QString &symbol, const QString &tag );
662 
664  QString tag( int id ) const;
665 
667  QgsSymbolGroupMap smartgroupsListMap();
668 
670  QStringList smartgroupNames() const;
671 
673  QgsSmartConditionMap smartgroup( int id );
674 
679  QString smartgroupOperator( int id );
680 
682  QStringList symbolsOfSmartgroup( StyleEntity type, int id );
683 
685  bool exportXml( const QString &filename );
686 
688  bool importXml( const QString &filename );
689 
698  static bool isXmlStyleFile( const QString &path );
699 
700  signals:
701 
710  void symbolSaved( const QString &name, QgsSymbol *symbol );
711 
720  void symbolChanged( const QString &name );
721 
723  void groupsModified();
724 
730  void entityTagsChanged( QgsStyle::StyleEntity entity, const QString &name, const QStringList &newTags );
731 
737  void favoritedChanged( QgsStyle::StyleEntity entity, const QString &name, bool isFavorite );
738 
746  void symbolRemoved( const QString &name );
747 
753  void symbolRenamed( const QString &oldName, const QString &newName );
754 
760  void rampRenamed( const QString &oldName, const QString &newName );
761 
769  void rampAdded( const QString &name );
770 
778  void rampRemoved( const QString &name );
779 
788  void rampChanged( const QString &name );
789 
790 
796  void textFormatRenamed( const QString &oldName, const QString &newName );
797 
805  void textFormatAdded( const QString &name );
806 
814  void textFormatRemoved( const QString &name );
815 
824  void textFormatChanged( const QString &name );
825 
831  void labelSettingsRenamed( const QString &oldName, const QString &newName );
832 
840  void labelSettingsAdded( const QString &name );
841 
849  void labelSettingsRemoved( const QString &name );
850 
859  void labelSettingsChanged( const QString &name );
860 
861  private:
862 
863  QgsSymbolMap mSymbols;
864  QgsVectorColorRampMap mColorRamps;
865  QgsTextFormatMap mTextFormats;
866  QgsLabelSettingsMap mLabelSettings;
867 
868  QHash< QString, QStringList > mCachedSymbolTags;
869  QHash< QString, QStringList > mCachedColorRampTags;
870  QHash< QString, QStringList > mCachedTextFormatTags;
871  QHash< QString, QStringList > mCachedLabelSettingsTags;
872 
873  QHash< QString, bool > mCachedSymbolFavorites;
874  QHash< QString, bool > mCachedColorRampFavorites;
875  QHash< QString, bool > mCachedTextFormatFavorites;
876  QHash< QString, bool > mCachedLabelSettingsFavorites;
877 
878  QString mErrorString;
879  QString mFileName;
880 
881  sqlite3_database_unique_ptr mCurrentDB;
882 
883  static QgsStyle *sDefaultStyle;
884 
886  bool openDatabase( const QString &filename );
887 
894  bool runEmptyQuery( const QString &query );
895 
897  int getId( const QString &table, const QString &name );
898 
900  QString getName( const QString &table, int id ) const;
901 
910  bool updateSymbol( StyleEntity type, const QString &name );
911 
912  void clearCachedTags( StyleEntity type, const QString &name );
913 
914  Q_DISABLE_COPY( QgsStyle )
915 };
916 
923 class CORE_EXPORT QgsStyleEntityInterface
924 {
925 
926 #ifdef SIP_RUN
928  switch ( sipCpp->type() )
929  {
931  sipType = sipType_QgsStyleSymbolEntity;
932  break;
933 
935  sipType = sipType_QgsStyleColorRampEntity;
936  break;
937 
939  sipType = sipType_QgsStyleTextFormatEntity;
940  break;
941 
943  sipType = sipType_QgsStyleLabelSettingsEntity;
944  break;
945 
947  case QgsStyle::TagEntity:
948  sipType = 0;
949  break;
950  }
951  SIP_END
952 #endif
953 
954  public:
955 
956  virtual ~QgsStyleEntityInterface() = default;
957 
961  virtual QgsStyle::StyleEntity type() const = 0;
962 
963 };
964 
972 {
973  public:
974 
981  : mSymbol( symbol )
982  {}
983 
984  QgsStyle::StyleEntity type() const override;
985 
989  const QgsSymbol *symbol() const { return mSymbol; }
990 
991  private:
992 
993  const QgsSymbol *mSymbol = nullptr;
994 
995 };
996 
1004 {
1005  public:
1006 
1013  : mRamp( ramp )
1014  {}
1015 
1016  QgsStyle::StyleEntity type() const override;
1017 
1021  const QgsColorRamp *ramp() const { return mRamp; }
1022 
1023  private:
1024 
1025  const QgsColorRamp *mRamp = nullptr;
1026 };
1027 
1035 {
1036  public:
1037 
1042  : mFormat( format )
1043  {}
1044 
1045  QgsStyle::StyleEntity type() const override;
1046 
1050  QgsTextFormat format() const { return mFormat; }
1051 
1052  private:
1053 
1054  QgsTextFormat mFormat;
1055 
1056 };
1057 
1065 {
1066  public:
1067 
1072  : mSettings( settings )
1073  {}
1074 
1075  QgsStyle::StyleEntity type() const override;
1076 
1077 
1081  const QgsPalLayerSettings &settings() const { return mSettings; }
1082 
1083  private:
1084 
1085  QgsPalLayerSettings mSettings;
1086 };
1087 
1088 #endif
TagmapTable
Columns available in the tag to symbol table.
Definition: qgsstyle.h:105
Text format ID.
Definition: qgsstyle.h:127
Tag ID.
Definition: qgsstyle.h:107
Text format is favorite flag.
Definition: qgsstyle.h:130
A symbol entity for QgsStyle databases.
Definition: qgsstyle.h:971
QgsStyleColorRampEntity(const QgsColorRamp *ramp)
Constructor for QgsStyleColorRampEntity, with the specified color ramp.
Definition: qgsstyle.h:1012
const QgsSymbol * symbol() const
Returns the entity&#39;s symbol.
Definition: qgsstyle.h:989
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:61
An interface for entities which can be placed in a QgsStyle database.
Definition: qgsstyle.h:923
QgsStyleSymbolEntity(const QgsSymbol *symbol)
Constructor for QgsStyleSymbolEntity, with the specified symbol.
Definition: qgsstyle.h:980
QMap< QString, QgsPalLayerSettings > QgsLabelSettingsMap
Map of name to label settings.
Definition: qgsstyle.h:53
Abstract base class for color ramps.
Definition: qgscolorramp.h:31
QMap< int, QString > QgsSymbolGroupMap
Definition: qgsstyle.h:41
Tag name.
Definition: qgsstyle.h:99
Label settings ID.
Definition: qgsstyle.h:138
Smart group definition (as XML)
Definition: qgsstyle.h:151
const QgsColorRamp * ramp() const
Returns the entity&#39;s color ramp.
Definition: qgsstyle.h:1021
Symbol definition (as XML)
Definition: qgsstyle.h:89
SmartgroupTable
Columns available in the smart group table.
Definition: qgsstyle.h:147
StyleEntity
Enum for Entities involved in a style.
Definition: qgsstyle.h:177
TextFormatTable
Columns available in the text format table.
Definition: qgsstyle.h:125
const QgsPalLayerSettings & settings() const
Returns the entity&#39;s label settings.
Definition: qgsstyle.h:1081
Smart group name.
Definition: qgsstyle.h:150
QgsTextFormat format() const
Returns the entity&#39;s text format.
Definition: qgsstyle.h:1050
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsStyleLabelSettingsEntity(const QgsPalLayerSettings &settings)
Constructor for QgsStyleLabelSettingsEntity, with the specified label settings.
Definition: qgsstyle.h:1071
#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:96
#define SIP_FACTORY
Definition: qgis_sip.h:76
A text format entity for QgsStyle databases.
Definition: qgsstyle.h:1034
Label settings name.
Definition: qgsstyle.h:139
Symbol ID.
Definition: qgsstyle.h:87
QMap< QString, QgsTextFormat > QgsTextFormatMap
Map of name to text format.
Definition: qgsstyle.h:47
Symbol ID.
Definition: qgsstyle.h:108
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:128
virtual QgsStyle::StyleEntity type() const =0
Returns the type of style entity.
Text format definition (as XML)
Definition: qgsstyle.h:129
ColorrampTable
Columns available in the color ramp table.
Definition: qgsstyle.h:114
QMap< QString, QgsColorRamp *> QgsVectorColorRampMap
Definition: qgsstyle.h:38
QString fileName()
Returns current file name of the style.
Definition: qgsstyle.h:625
A label settings entity for QgsStyle databases.
Definition: qgsstyle.h:1064
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:172
QMap< QString, QgsSymbol *> QgsSymbolMap
Definition: qgsrenderer.h:45
Tag ID.
Definition: qgsstyle.h:98
SymbolTable
Columns available in the Symbols table.
Definition: qgsstyle.h:85
Label settings is favorite flag.
Definition: qgsstyle.h:141
LabelSettingsTable
Columns available in the label settings table.
Definition: qgsstyle.h:136
Smart group ID.
Definition: qgsstyle.h:149
A color ramp entity for QgsStyle databases.
Definition: qgsstyle.h:1003
Symbol is favorite flag.
Definition: qgsstyle.h:90
Symbol Name.
Definition: qgsstyle.h:88
Color ramp name.
Definition: qgsstyle.h:117
Container for all settings relating to text rendering.
Label settings definition (as XML)
Definition: qgsstyle.h:140
Color ramp is favorite flag.
Definition: qgsstyle.h:119
QgsStyleTextFormatEntity(const QgsTextFormat &format)
Constructor for QgsStyleTextFormatEntity, with the specified text format.
Definition: qgsstyle.h:1041
Color ramp definition (as XML)
Definition: qgsstyle.h:118
QMultiMap< QString, QString > QgsSmartConditionMap
A multimap to hold the smart group conditions as constraint and parameter pairs.
Definition: qgsstyle.h:78
Color ramp ID.
Definition: qgsstyle.h:116