QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
18#include "qgsapplication.h"
19#include "qgssingleitemmodel.h"
20#include "qgsstyle.h"
21#include "qgsstylemodel.h"
22
23#include "moc_qgscombinedstylemodel.cpp"
24
26 : QConcatenateTablesProxyModel( parent )
27{
28
29}
30
31QVariant QgsCombinedStyleModel::headerData( int section, Qt::Orientation orientation, int role ) const
32{
33 return QgsStyleModel::headerDataStatic( section, orientation, role );
34}
35
37{
38 connect( style, &QgsStyle::destroyed, this, [this, style]()
39 {
40 if ( QgsSingleItemModel *model = mTitleModels.value( style ) )
41 {
42 removeSourceModel( model );
43 mTitleModels.remove( style );
44 delete model;
45 }
46
47 if ( QgsStyleModel *model = mOwnedStyleModels.value( style ) )
48 {
49 removeSourceModel( model );
50 mOwnedStyleModels.remove( style );
51 delete model;
52 }
53 mStyles.removeAll( style );
54 } );
55
56 mStyles.append( style );
57
58 QgsSingleItemModel *titleModel = new QgsSingleItemModel( this,
59 {
60 {
61 { Qt::DisplayRole, style->name() },
62 { Qt::ToolTipRole, style->name() },
63 { static_cast< int >( QgsStyleModel::CustomRole::IsTitle ), true },
64 { static_cast< int >( QgsStyleModel::CustomRole::StyleFileName ), style->fileName() },
65 { static_cast< int >( QgsStyleModel::CustomRole::StyleName ), style->name() },
66 }, {
67 { static_cast< int >( QgsStyleModel::CustomRole::IsTitle ), true },
68 { static_cast< int >( QgsStyleModel::CustomRole::StyleFileName ), style->fileName() },
69 { static_cast< int >( QgsStyleModel::CustomRole::StyleName ), style->name() }
70 }
71 } );
72 addSourceModel( titleModel );
73 mTitleModels.insert( style, titleModel );
74
75 QgsStyleModel *styleModel = new QgsStyleModel( style, this );
76
77 for ( QSize size : std::as_const( mAdditionalSizes ) )
78 {
79 styleModel->addDesiredIconSize( size );
80 }
81
82 for ( auto it = mTargetScreenProperties.constBegin(); it != mTargetScreenProperties.constEnd(); ++it )
83 {
84 styleModel->addTargetScreenProperties( *it );
85 }
86
87 addSourceModel( styleModel );
88 mOwnedStyleModels.insert( style, styleModel );
89}
90
92{
93 if ( QgsSingleItemModel *model = mTitleModels.value( style ) )
94 {
95 removeSourceModel( model );
96 mTitleModels.remove( style );
97 delete model;
98 }
99
100 if ( QgsStyleModel *model = mOwnedStyleModels.value( style ) )
101 {
102 removeSourceModel( model );
103 mOwnedStyleModels.remove( style );
104 delete model;
105 }
106 mStyles.removeAll( style );
107}
108
110{
111 QgsStyle *defaultStyle = QgsStyle::defaultStyle( false );
113 if ( !defaultStyle || !styleModel )
114 return;
115
116 mStyles.append( defaultStyle );
117
118 QgsSingleItemModel *titleModel = new QgsSingleItemModel( this,
119 {
120 {
121 { Qt::DisplayRole, defaultStyle->name() },
122 { Qt::ToolTipRole, defaultStyle->name() },
123 { static_cast< int >( QgsStyleModel::CustomRole::IsTitle ), true },
124 { static_cast< int >( QgsStyleModel::CustomRole::StyleFileName ), defaultStyle->fileName() },
125 { static_cast< int >( QgsStyleModel::CustomRole::StyleName ), defaultStyle->name() },
126 }, {
127 { static_cast< int >( QgsStyleModel::CustomRole::IsTitle ), true },
128 { static_cast< int >( QgsStyleModel::CustomRole::StyleFileName ), defaultStyle->fileName() },
129 { static_cast< int >( QgsStyleModel::CustomRole::StyleName ), defaultStyle->name() }
130 }
131 } );
132
133 addSourceModel( titleModel );
134 mTitleModels.insert( defaultStyle, titleModel );
135
136 for ( QSize size : std::as_const( mAdditionalSizes ) )
137 {
138 styleModel->addDesiredIconSize( size );
139 }
140
141 for ( auto it = mTargetScreenProperties.constBegin(); it != mTargetScreenProperties.constEnd(); ++it )
142 {
143 styleModel->addTargetScreenProperties( *it );
144 }
145
146 addSourceModel( styleModel );
147}
148
149QList< QgsStyle * > QgsCombinedStyleModel::styles() const
150{
151 return mStyles;
152}
153
155{
156 if ( !mAdditionalSizes.contains( size ) )
157 mAdditionalSizes.append( size );
158
159 for ( auto it = mOwnedStyleModels.constBegin(); it != mOwnedStyleModels.constEnd(); ++it )
160 {
161 it.value()->addDesiredIconSize( size );
162 }
163
164 if ( mStyles.contains( QgsStyle::defaultStyle() ) )
165 {
167 }
168}
169
171{
172 if ( !mTargetScreenProperties.contains( properties ) )
173 mTargetScreenProperties.insert( properties );
174
175 for ( auto it = mOwnedStyleModels.constBegin(); it != mOwnedStyleModels.constEnd(); ++it )
176 {
177 it.value()->addTargetScreenProperties( properties );
178 }
179
180 if ( mStyles.contains( QgsStyle::defaultStyle() ) )
181 {
183 }
184}
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 QAbstractItemModel 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.
@ StyleFileName
File name of associated QgsStyle (QgsStyle::fileName()).
@ StyleName
Name of associated QgsStyle (QgsStyle::name()).
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.
A database of saved style entities, including symbols, color ramps, text formats and others.
Definition qgsstyle.h:88
QString fileName() const
Returns the current file name of the style database.
Definition qgsstyle.h:926
static QgsStyle * defaultStyle(bool initialize=true)
Returns the default application-wide style.
Definition qgsstyle.cpp:147
QString name() const
Returns the name of the style.
Definition qgsstyle.cpp:108