QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
16 #include "qgscombinedstylemodel.h"
17 #include "qgsstyle.h"
18 #include "qgsstylemodel.h"
19 #include "qgssingleitemmodel.h"
20 #include "qgsapplication.h"
21 
22 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
23 
24 
26  : QConcatenateTablesProxyModel( parent )
27 {
28 
29 }
30 
31 QVariant 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() },
65  { QgsStyleModel::StyleName, style->name() },
66  }, {
69  { QgsStyleModel::StyleName, style->name() }
70  }
71  } );
72  addSourceModel( titleModel );
73  mTitleModels.insert( style, titleModel );
74 
75  QgsStyleModel *styleModel = new QgsStyleModel( style );
76 
77  for ( QSize size : std::as_const( mAdditionalSizes ) )
78  {
79  styleModel->addDesiredIconSize( size );
80  }
81 
82  addSourceModel( styleModel );
83  mOwnedStyleModels.insert( style, styleModel );
84 }
85 
87 {
88  if ( QgsSingleItemModel *model = mTitleModels.value( style ) )
89  {
90  removeSourceModel( model );
91  mTitleModels.remove( style );
92  delete model;
93  }
94 
95  if ( QgsStyleModel *model = mOwnedStyleModels.value( style ) )
96  {
97  removeSourceModel( model );
98  mOwnedStyleModels.remove( style );
99  delete model;
100  }
101  mStyles.removeAll( style );
102 }
103 
105 {
106  QgsStyle *defaultStyle = QgsStyle::defaultStyle();
108  if ( !defaultStyle || !styleModel )
109  return;
110 
111  mStyles.append( defaultStyle );
112 
113  QgsSingleItemModel *titleModel = new QgsSingleItemModel( this,
114  {
115  {
116  { Qt::DisplayRole, defaultStyle->name() },
117  { Qt::ToolTipRole, defaultStyle->name() },
118  { QgsStyleModel::IsTitleRole, true },
119  { QgsStyleModel::StyleFileName, defaultStyle->fileName() },
120  { QgsStyleModel::StyleName, defaultStyle->name() },
121  }, {
122  { QgsStyleModel::IsTitleRole, true },
123  { QgsStyleModel::StyleFileName, defaultStyle->fileName() },
124  { QgsStyleModel::StyleName, defaultStyle->name() }
125  }
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  addSourceModel( styleModel );
137 }
138 
139 QList< QgsStyle * > QgsCombinedStyleModel::styles() const
140 {
141  return mStyles;
142 }
143 
145 {
146  if ( !mAdditionalSizes.contains( size ) )
147  mAdditionalSizes.append( size );
148 
149  for ( auto it = mOwnedStyleModels.constBegin(); it != mOwnedStyleModels.constEnd(); ++it )
150  {
151  it.value()->addDesiredIconSize( size );
152  }
153 
154  if ( mStyles.contains( QgsStyle::defaultStyle() ) )
155  {
157  }
158 }
159 
160 #endif
QgsStyle::name
QString name() const
Returns the name of the style.
Definition: qgsstyle.cpp:106
QgsStyleModel::StyleName
@ StyleName
Name of associated QgsStyle (QgsStyle::name()) (since QGIS 3.26)
Definition: qgsstylemodel.h:130
QgsStyleModel
A QAbstractItemModel subclass for showing symbol and color ramp entities contained within a QgsStyle ...
Definition: qgsstylemodel.h:107
QgsCombinedStyleModel::removeStyle
void removeStyle(QgsStyle *style)
Removes a style from the model.
Definition: qgscombinedstylemodel.cpp:86
QgsStyle::fileName
QString fileName() const
Returns the current file name of the style database.
Definition: qgsstyle.h:888
QgsCombinedStyleModel::addDefaultStyle
void addDefaultStyle()
Adds the default style (QgsStyle::defaultStyle()) to the model.
Definition: qgscombinedstylemodel.cpp:104
QgsCombinedStyleModel::addDesiredIconSize
void addDesiredIconSize(QSize size)
Adds an additional icon size to generate for Qt::DecorationRole data.
Definition: qgscombinedstylemodel.cpp:144
QgsCombinedStyleModel::QgsCombinedStyleModel
QgsCombinedStyleModel(QObject *parent=nullptr)
Constructor for QgsCombinedStyleModel with the specified parent object.
Definition: qgscombinedstylemodel.cpp:25
QgsStyleModel::IsTitleRole
@ IsTitleRole
True if the index corresponds to a title item (since QGIS 3.26)
Definition: qgsstylemodel.h:132
QgsStyle::defaultStyle
static QgsStyle * defaultStyle()
Returns default application-wide style.
Definition: qgsstyle.cpp:145
qgsapplication.h
qgscombinedstylemodel.h
QgsCombinedStyleModel::addStyle
void addStyle(QgsStyle *style)
Adds a style to the model.
Definition: qgscombinedstylemodel.cpp:36
QgsSingleItemModel
A QgsSingleItemModel subclass which contains a single read-only item.
Definition: qgssingleitemmodel.h:31
QgsApplication::defaultStyleModel
static QgsStyleModel * defaultStyleModel()
Returns a shared QgsStyleModel containing the default style library (see QgsStyle::defaultStyle()).
Definition: qgsapplication.cpp:2445
qgsstylemodel.h
QgsCombinedStyleModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Definition: qgscombinedstylemodel.cpp:31
qgsstyle.h
QgsStyle
Definition: qgsstyle.h:159
QgsCombinedStyleModel::styles
QList< QgsStyle * > styles() const
Returns a list of all styles shown in the model.
Definition: qgscombinedstylemodel.cpp:139
QgsStyleModel::addDesiredIconSize
void addDesiredIconSize(QSize size)
Adds an additional icon size to generate for Qt::DecorationRole data.
Definition: qgsstylemodel.cpp:588
qgssingleitemmodel.h
QgsStyleModel::StyleFileName
@ StyleFileName
File name of associated QgsStyle (QgsStyle::fileName()) (since QGIS 3.26)
Definition: qgsstylemodel.h:131