QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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() },
62 { QgsStyleModel::StyleName, style->name() },
63 }, {
66 { QgsStyleModel::StyleName, style->name() }
67 }
68 } );
69 addSourceModel( titleModel );
70 mTitleModels.insert( style, titleModel );
71
72 QgsStyleModel *styleModel = new QgsStyleModel( style );
73
74 for ( QSize size : std::as_const( mAdditionalSizes ) )
75 {
76 styleModel->addDesiredIconSize( size );
77 }
78
79 addSourceModel( styleModel );
80 mOwnedStyleModels.insert( style, styleModel );
81}
82
84{
85 if ( QgsSingleItemModel *model = mTitleModels.value( style ) )
86 {
87 removeSourceModel( model );
88 mTitleModels.remove( style );
89 delete model;
90 }
91
92 if ( QgsStyleModel *model = mOwnedStyleModels.value( style ) )
93 {
94 removeSourceModel( model );
95 mOwnedStyleModels.remove( style );
96 delete model;
97 }
98 mStyles.removeAll( style );
99}
100
102{
103 QgsStyle *defaultStyle = QgsStyle::defaultStyle();
105 if ( !defaultStyle || !styleModel )
106 return;
107
108 mStyles.append( defaultStyle );
109
110 QgsSingleItemModel *titleModel = new QgsSingleItemModel( this,
111 {
112 {
113 { Qt::DisplayRole, defaultStyle->name() },
114 { Qt::ToolTipRole, defaultStyle->name() },
116 { QgsStyleModel::StyleFileName, defaultStyle->fileName() },
117 { QgsStyleModel::StyleName, defaultStyle->name() },
118 }, {
120 { QgsStyleModel::StyleFileName, defaultStyle->fileName() },
121 { QgsStyleModel::StyleName, defaultStyle->name() }
122 }
123 } );
124
125 addSourceModel( titleModel );
126 mTitleModels.insert( defaultStyle, titleModel );
127
128 for ( QSize size : std::as_const( mAdditionalSizes ) )
129 {
130 styleModel->addDesiredIconSize( size );
131 }
132
133 addSourceModel( styleModel );
134}
135
136QList< QgsStyle * > QgsCombinedStyleModel::styles() const
137{
138 return mStyles;
139}
140
142{
143 if ( !mAdditionalSizes.contains( size ) )
144 mAdditionalSizes.append( size );
145
146 for ( auto it = mOwnedStyleModels.constBegin(); it != mOwnedStyleModels.constEnd(); ++it )
147 {
148 it.value()->addDesiredIconSize( size );
149 }
150
151 if ( mStyles.contains( QgsStyle::defaultStyle() ) )
152 {
154 }
155}
static QgsStyleModel * defaultStyleModel()
Returns a shared QgsStyleModel containing the default style library (see QgsStyle::defaultStyle()).
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.
A QgsSingleItemModel subclass which contains a single read-only item.
A QAbstractItemModel subclass for showing symbol and color ramp entities contained within a QgsStyle ...
@ StyleName
Name of associated QgsStyle (QgsStyle::name()) (since QGIS 3.26)
@ StyleFileName
File name of associated QgsStyle (QgsStyle::fileName()) (since QGIS 3.26)
@ IsTitleRole
True if the index corresponds to a title item (since QGIS 3.26)
void addDesiredIconSize(QSize size)
Adds an additional icon size to generate for Qt::DecorationRole data.
static QgsStyle * defaultStyle()
Returns default application-wide style.
Definition: qgsstyle.cpp:145
QString fileName() const
Returns the current file name of the style database.
Definition: qgsstyle.h:888
QString name() const
Returns the name of the style.
Definition: qgsstyle.cpp:106