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