QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgscombinedstylemodel.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscombinedstylemodel.cpp
3 ---------------
4 begin : May 2022
5 copyright : (C) 2022 by Nyall Dawson
6 email : nyall dot dawson 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
17#include "qgsstyle.h"
18#include "qgsstylemodel.h"
19#include "qgssingleitemmodel.h"
20#include "qgsapplication.h"
21
23 : QConcatenateTablesProxyModel( parent )
24{
25
26}
27
28QVariant QgsCombinedStyleModel::headerData( int section, Qt::Orientation orientation, int role ) const
29{
30 return QgsStyleModel::headerDataStatic( section, orientation, role );
31}
32
34{
35 connect( style, &QgsStyle::destroyed, this, [this, style]()
36 {
37 if ( QgsSingleItemModel *model = mTitleModels.value( style ) )
38 {
39 removeSourceModel( model );
40 mTitleModels.remove( style );
41 delete model;
42 }
43
44 if ( QgsStyleModel *model = mOwnedStyleModels.value( style ) )
45 {
46 removeSourceModel( model );
47 mOwnedStyleModels.remove( style );
48 delete model;
49 }
50 mStyles.removeAll( style );
51 } );
52
53 mStyles.append( style );
54
55 QgsSingleItemModel *titleModel = new QgsSingleItemModel( this,
56 {
57 {
58 { Qt::DisplayRole, style->name() },
59 { Qt::ToolTipRole, style->name() },
60 { static_cast< int >( QgsStyleModel::CustomRole::IsTitle ), true },
61 { static_cast< int >( QgsStyleModel::CustomRole::StyleFileName ), style->fileName() },
62 { static_cast< int >( QgsStyleModel::CustomRole::StyleName ), style->name() },
63 }, {
64 { static_cast< int >( QgsStyleModel::CustomRole::IsTitle ), true },
65 { static_cast< int >( QgsStyleModel::CustomRole::StyleFileName ), style->fileName() },
66 { static_cast< int >( QgsStyleModel::CustomRole::StyleName ), style->name() }
67 }
68 } );
69 addSourceModel( titleModel );
70 mTitleModels.insert( style, titleModel );
71
72 QgsStyleModel *styleModel = new QgsStyleModel( style, this );
73
74 for ( QSize size : std::as_const( mAdditionalSizes ) )
75 {
76 styleModel->addDesiredIconSize( size );
77 }
78
79 for ( auto it = mTargetScreenProperties.constBegin(); it != mTargetScreenProperties.constEnd(); ++it )
80 {
81 styleModel->addTargetScreenProperties( *it );
82 }
83
84 addSourceModel( styleModel );
85 mOwnedStyleModels.insert( style, styleModel );
86}
87
89{
90 if ( QgsSingleItemModel *model = mTitleModels.value( style ) )
91 {
92 removeSourceModel( model );
93 mTitleModels.remove( style );
94 delete model;
95 }
96
97 if ( QgsStyleModel *model = mOwnedStyleModels.value( style ) )
98 {
99 removeSourceModel( model );
100 mOwnedStyleModels.remove( style );
101 delete model;
102 }
103 mStyles.removeAll( style );
104}
105
107{
108 QgsStyle *defaultStyle = QgsStyle::defaultStyle( false );
110 if ( !defaultStyle || !styleModel )
111 return;
112
113 mStyles.append( defaultStyle );
114
115 QgsSingleItemModel *titleModel = new QgsSingleItemModel( this,
116 {
117 {
118 { Qt::DisplayRole, defaultStyle->name() },
119 { Qt::ToolTipRole, defaultStyle->name() },
120 { static_cast< int >( QgsStyleModel::CustomRole::IsTitle ), true },
121 { static_cast< int >( QgsStyleModel::CustomRole::StyleFileName ), defaultStyle->fileName() },
122 { static_cast< int >( QgsStyleModel::CustomRole::StyleName ), defaultStyle->name() },
123 }, {
124 { static_cast< int >( QgsStyleModel::CustomRole::IsTitle ), true },
125 { static_cast< int >( QgsStyleModel::CustomRole::StyleFileName ), defaultStyle->fileName() },
126 { static_cast< int >( QgsStyleModel::CustomRole::StyleName ), defaultStyle->name() }
127 }
128 } );
129
130 addSourceModel( titleModel );
131 mTitleModels.insert( defaultStyle, titleModel );
132
133 for ( QSize size : std::as_const( mAdditionalSizes ) )
134 {
135 styleModel->addDesiredIconSize( size );
136 }
137
138 for ( auto it = mTargetScreenProperties.constBegin(); it != mTargetScreenProperties.constEnd(); ++it )
139 {
140 styleModel->addTargetScreenProperties( *it );
141 }
142
143 addSourceModel( styleModel );
144}
145
146QList< QgsStyle * > QgsCombinedStyleModel::styles() const
147{
148 return mStyles;
149}
150
152{
153 if ( !mAdditionalSizes.contains( size ) )
154 mAdditionalSizes.append( size );
155
156 for ( auto it = mOwnedStyleModels.constBegin(); it != mOwnedStyleModels.constEnd(); ++it )
157 {
158 it.value()->addDesiredIconSize( size );
159 }
160
161 if ( mStyles.contains( QgsStyle::defaultStyle() ) )
162 {
164 }
165}
166
168{
169 if ( !mTargetScreenProperties.contains( properties ) )
170 mTargetScreenProperties.insert( properties );
171
172 for ( auto it = mOwnedStyleModels.constBegin(); it != mOwnedStyleModels.constEnd(); ++it )
173 {
174 it.value()->addTargetScreenProperties( properties );
175 }
176
177 if ( mStyles.contains( QgsStyle::defaultStyle() ) )
178 {
180 }
181}
static QgsStyleModel * defaultStyleModel()
Returns a shared QgsStyleModel containing the default style library (see QgsStyle::defaultStyle()).
void addTargetScreenProperties(const QgsScreenProperties &properties)
Adds additional target screen properties to use when generating icons for Qt::DecorationRole data.
QList< QgsStyle * > styles() const
Returns a list of all styles shown in the model.
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
QgsCombinedStyleModel(QObject *parent=nullptr)
Constructor for QgsCombinedStyleModel with the specified parent object.
void removeStyle(QgsStyle *style)
Removes a style from the model.
void addStyle(QgsStyle *style)
Adds a style to the model.
void addDefaultStyle()
Adds the default style (QgsStyle::defaultStyle()) to the model.
void addDesiredIconSize(QSize size)
Adds an additional icon size to generate for Qt::DecorationRole data.
Stores properties relating to a screen.
A QgsSingleItemModel subclass which contains a single read-only item.
A QAbstractItemModel subclass for showing symbol and color ramp entities contained within a QgsStyle ...
@ IsTitle
True if the index corresponds to a title item (since QGIS 3.26)
@ StyleFileName
File name of associated QgsStyle (QgsStyle::fileName()) (since QGIS 3.26)
@ StyleName
Name of associated QgsStyle (QgsStyle::name()) (since QGIS 3.26)
void addTargetScreenProperties(const QgsScreenProperties &properties)
Adds additional target screen properties to use when generating icons for Qt::DecorationRole data.
void addDesiredIconSize(QSize size)
Adds an additional icon size to generate for Qt::DecorationRole data.
QString fileName() const
Returns the current file name of the style database.
Definition: qgsstyle.h:901
static QgsStyle * defaultStyle(bool initialize=true)
Returns the default application-wide style.
Definition: qgsstyle.cpp:145
QString name() const
Returns the name of the style.
Definition: qgsstyle.cpp:106