QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
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 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(
56 this,
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 addSourceModel( titleModel );
69 mTitleModels.insert( style, titleModel );
70
71 QgsStyleModel *styleModel = new QgsStyleModel( style, this );
72
73 for ( QSize size : std::as_const( mAdditionalSizes ) )
74 {
75 styleModel->addDesiredIconSize( size );
76 }
77
78 for ( auto it = mTargetScreenProperties.constBegin(); it != mTargetScreenProperties.constEnd(); ++it )
79 {
80 styleModel->addTargetScreenProperties( *it );
81 }
82
83 addSourceModel( styleModel );
84 mOwnedStyleModels.insert( style, styleModel );
85}
86
88{
89 if ( QgsSingleItemModel *model = mTitleModels.value( style ) )
90 {
91 removeSourceModel( model );
92 mTitleModels.remove( style );
93 delete model;
94 }
95
96 if ( QgsStyleModel *model = mOwnedStyleModels.value( style ) )
97 {
98 removeSourceModel( model );
99 mOwnedStyleModels.remove( style );
100 delete model;
101 }
102 mStyles.removeAll( style );
103}
104
106{
107 QgsStyle *defaultStyle = QgsStyle::defaultStyle( false );
109 if ( !defaultStyle || !styleModel )
110 return;
111
112 mStyles.append( defaultStyle );
113
114 QgsSingleItemModel *titleModel = new QgsSingleItemModel(
115 this,
116 { {
117 { Qt::DisplayRole, defaultStyle->name() },
118 { Qt::ToolTipRole, defaultStyle->name() },
119 { static_cast< int >( QgsStyleModel::CustomRole::IsTitle ), true },
120 { static_cast< int >( QgsStyleModel::CustomRole::StyleFileName ), defaultStyle->fileName() },
121 { static_cast< int >( QgsStyleModel::CustomRole::StyleName ), defaultStyle->name() },
122 },
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
128 addSourceModel( titleModel );
129 mTitleModels.insert( defaultStyle, titleModel );
130
131 for ( QSize size : std::as_const( mAdditionalSizes ) )
132 {
133 styleModel->addDesiredIconSize( size );
134 }
135
136 for ( auto it = mTargetScreenProperties.constBegin(); it != mTargetScreenProperties.constEnd(); ++it )
137 {
138 styleModel->addTargetScreenProperties( *it );
139 }
140
141 addSourceModel( styleModel );
142}
143
144QList< QgsStyle * > QgsCombinedStyleModel::styles() const
145{
146 return mStyles;
147}
148
150{
151 if ( !mAdditionalSizes.contains( size ) )
152 mAdditionalSizes.append( size );
153
154 for ( auto it = mOwnedStyleModels.constBegin(); it != mOwnedStyleModels.constEnd(); ++it )
155 {
156 it.value()->addDesiredIconSize( size );
157 }
158
159 if ( mStyles.contains( QgsStyle::defaultStyle() ) )
160 {
162 }
163}
164
166{
167 if ( !mTargetScreenProperties.contains( properties ) )
168 mTargetScreenProperties.insert( properties );
169
170 for ( auto it = mOwnedStyleModels.constBegin(); it != mOwnedStyleModels.constEnd(); ++it )
171 {
172 it.value()->addTargetScreenProperties( properties );
173 }
174
175 if ( mStyles.contains( QgsStyle::defaultStyle() ) )
176 {
178 }
179}
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:89
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:148
QString name() const
Returns the name of the style.
Definition qgsstyle.cpp:110