QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgsmaplayerstylemanagerwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaplayerstylemanagerwidget.cpp
3 ---------------------
4 begin : June 2016
5 copyright : (C) 2016 by Nathan Woodrow
6 email : woodrow dot nathan 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#include <QAction>
16#include <QVBoxLayout>
17#include <QToolBar>
18#include <QInputDialog>
19#include <QMessageBox>
20#include <QFileDialog>
21
23#include "qgssettings.h"
24#include "qgslogger.h"
25#include "qgsmaplayer.h"
26#include "qgsmapcanvas.h"
31#include "qgsvectorlayer.h"
32#include "qgsvectortilelayer.h"
33#include "qgsrasterlayer.h"
34#include "qgsapplication.h"
35#include "qgsproviderregistry.h"
40
42 : QgsMapLayerConfigWidget( layer, canvas, parent )
43{
44 mModel = new QStandardItemModel( this );
45 mStyleList = new QListView( this );
46 mStyleList->setModel( mModel );
47 mStyleList->setViewMode( QListView::ListMode );
48 mStyleList->setResizeMode( QListView::Adjust );
49
50 QToolBar *toolbar = new QToolBar( this );
51 QAction *addAction = toolbar->addAction( tr( "Add" ) );
52 addAction->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "symbologyAdd.svg" ) ) );
53 connect( addAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::addStyle );
54 QAction *removeAction = toolbar->addAction( tr( "Remove Current" ) );
55 removeAction->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "symbologyRemove.svg" ) ) );
56 connect( removeAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::removeStyle );
57 QAction *loadFromFileAction = toolbar->addAction( tr( "Load Style" ) );
58 loadFromFileAction->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionFileOpen.svg" ) ) );
59 connect( loadFromFileAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::loadStyle );
60 QAction *saveAction = toolbar->addAction( tr( "Save Style" ) );
61 saveAction->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionFileSave.svg" ) ) );
62 connect( saveAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::saveStyle );
63 QAction *saveAsDefaultAction = toolbar->addAction( tr( "Save as Default" ) );
64 connect( saveAsDefaultAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::saveAsDefault );
65 QAction *loadDefaultAction = toolbar->addAction( tr( "Restore Default" ) );
66 connect( loadDefaultAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::loadDefault );
67
68 //broken connect - not sure what the purpose of this was?
69// connect( canvas, &QgsMapCanvas::mapCanvasRefreshed, this, SLOT( updateCurrent() ) );
70
71 connect( mStyleList, &QAbstractItemView::clicked, this, &QgsMapLayerStyleManagerWidget::styleClicked );
72
73 setLayout( new QVBoxLayout() );
74 layout()->setContentsMargins( 0, 0, 0, 0 );
75 layout()->addWidget( toolbar );
76 layout()->addWidget( mStyleList );
77
78 connect( mLayer->styleManager(), &QgsMapLayerStyleManager::currentStyleChanged, this, &QgsMapLayerStyleManagerWidget::currentStyleChanged );
79 connect( mLayer->styleManager(), &QgsMapLayerStyleManager::styleAdded, this, &QgsMapLayerStyleManagerWidget::styleAdded );
80 connect( mLayer->styleManager(), &QgsMapLayerStyleManager::styleRemoved, this, &QgsMapLayerStyleManagerWidget::styleRemoved );
81 connect( mLayer->styleManager(), &QgsMapLayerStyleManager::styleRenamed, this, &QgsMapLayerStyleManagerWidget::styleRenamed );
82
83 mModel->clear();
84
85 const QStringList styles = mLayer->styleManager()->styles();
86 for ( const QString &styleName : styles )
87 {
88 QStandardItem *item = new QStandardItem( styleName );
89 item->setData( styleName );
90 mModel->appendRow( item );
91 }
92
93 const QString active = mLayer->styleManager()->currentStyle();
94 currentStyleChanged( active );
95
96 connect( mModel, &QStandardItemModel::itemChanged, this, &QgsMapLayerStyleManagerWidget::renameStyle );
97}
98
99void QgsMapLayerStyleManagerWidget::styleClicked( const QModelIndex &index )
100{
101 if ( !mLayer || !index.isValid() )
102 return;
103
104 const QString name = index.data().toString();
106}
107
108void QgsMapLayerStyleManagerWidget::currentStyleChanged( const QString &name )
109{
110 const QList<QStandardItem *> items = mModel->findItems( name );
111 if ( items.isEmpty() )
112 return;
113
114 QStandardItem *item = items.at( 0 );
115
116 mStyleList->setCurrentIndex( item->index() );
117}
118
119void QgsMapLayerStyleManagerWidget::styleAdded( const QString &name )
120{
121 QgsDebugMsgLevel( QStringLiteral( "Style added" ), 2 );
122 QStandardItem *item = new QStandardItem( name );
123 item->setData( name );
124 mModel->appendRow( item );
125}
126
127void QgsMapLayerStyleManagerWidget::styleRemoved( const QString &name )
128{
129 const QList<QStandardItem *> items = mModel->findItems( name );
130 if ( items.isEmpty() )
131 return;
132
133 QStandardItem *item = items.at( 0 );
134 mModel->removeRow( item->row() );
135}
136
137void QgsMapLayerStyleManagerWidget::styleRenamed( const QString &oldname, const QString &newname )
138{
139 const QList<QStandardItem *> items = mModel->findItems( oldname );
140 if ( items.isEmpty() )
141 return;
142
143 QStandardItem *item = items.at( 0 );
144 item->setText( newname );
145 item->setData( newname );
146}
147
148void QgsMapLayerStyleManagerWidget::addStyle()
149{
150 bool ok;
151 const QString text = QInputDialog::getText( nullptr, tr( "New Style" ),
152 tr( "Style name:" ), QLineEdit::Normal,
153 QStringLiteral( "new style" ), &ok );
154 if ( !ok || text.isEmpty() )
155 return;
156
157 const bool res = mLayer->styleManager()->addStyleFromLayer( text );
158 if ( res ) // make it active!
159 {
161 }
162 else
163 {
164 QgsDebugError( "Failed to add style: " + text );
165 }
166}
167
168void QgsMapLayerStyleManagerWidget::removeStyle()
169{
170 const QString current = mLayer->styleManager()->currentStyle();
171 const bool res = mLayer->styleManager()->removeStyle( current );
172 if ( !res )
173 QgsDebugError( QStringLiteral( "Failed to remove current style" ) );
174}
175
176void QgsMapLayerStyleManagerWidget::renameStyle( QStandardItem *item )
177{
178 const QString oldName = item->data().toString();
179 const QString newName = item->text();
180 item->setData( newName );
181 whileBlocking( this )->mLayer->styleManager()->renameStyle( oldName, newName );
182}
183
184void QgsMapLayerStyleManagerWidget::saveAsDefault()
185{
186 if ( !mLayer )
187 return;
188
189 switch ( mLayer->type() )
190 {
191
192 case Qgis::LayerType::Vector:
195 qobject_cast<QgsVectorLayer *>( mLayer ) ).saveDefaultStyle();
196 break;
197
198 case Qgis::LayerType::Raster:
200 break;
201
202 case Qgis::LayerType::Mesh:
204 break;
205
206 case Qgis::LayerType::VectorTile:
207 QgsVectorTileLayerProperties( qobject_cast<QgsVectorTileLayer *>( mLayer ),
210 break;
211
212 // Not available for these
213 case Qgis::LayerType::PointCloud:
214 case Qgis::LayerType::Annotation:
215 case Qgis::LayerType::Plugin:
216 case Qgis::LayerType::Group:
217 default:
218 break;
219 }
220}
221
222void QgsMapLayerStyleManagerWidget::loadDefault()
223{
224 if ( !mLayer )
225 return;
226
227 switch ( mLayer->type() )
228 {
229
230 case Qgis::LayerType::Vector:
233 qobject_cast<QgsVectorLayer *>( mLayer ) ).loadDefaultStyle();
234 break;
235
236 case Qgis::LayerType::Raster:
238 break;
239
240 case Qgis::LayerType::Mesh:
242 break;
243
244 case Qgis::LayerType::VectorTile:
245 QgsVectorTileLayerProperties( qobject_cast<QgsVectorTileLayer *>( mLayer ),
248 break;
249
250 // Not available for these
251 case Qgis::LayerType::PointCloud:
252 case Qgis::LayerType::Annotation:
253 case Qgis::LayerType::Plugin:
254 case Qgis::LayerType::Group:
255 default:
256 break;
257 }
258}
259
260void QgsMapLayerStyleManagerWidget::saveStyle()
261{
262 if ( !mLayer )
263 return;
264
265 switch ( mLayer->type() )
266 {
267
268 case Qgis::LayerType::Vector:
271 qobject_cast<QgsVectorLayer *>( mLayer ) ).saveStyleAs();
272 break;
273
274 case Qgis::LayerType::Raster:
276 break;
277
278 case Qgis::LayerType::Mesh:
280 break;
281
282 case Qgis::LayerType::VectorTile:
283 QgsVectorTileLayerProperties( qobject_cast<QgsVectorTileLayer *>( mLayer ),
286 break;
287
288 // Not available for these
289 case Qgis::LayerType::PointCloud:
290 case Qgis::LayerType::Annotation:
291 case Qgis::LayerType::Plugin:
292 case Qgis::LayerType::Group:
293 default:
294 break;
295 }
296}
297
298void QgsMapLayerStyleManagerWidget::loadStyle()
299{
300 if ( !mLayer )
301 return;
302
303 switch ( mLayer->type() )
304 {
305
306 case Qgis::LayerType::Vector:
309 qobject_cast<QgsVectorLayer *>( mLayer ) ).loadStyle();
310 break;
311
312 case Qgis::LayerType::Raster:
314 break;
315
316 case Qgis::LayerType::Mesh:
318 break;
319
320 case Qgis::LayerType::VectorTile:
321 QgsVectorTileLayerProperties( qobject_cast<QgsVectorTileLayer *>( mLayer ),
324 break;
325
326 // Not available for these
327 case Qgis::LayerType::PointCloud:
328 case Qgis::LayerType::Annotation:
329 case Qgis::LayerType::Plugin:
330 case Qgis::LayerType::Group:
331 default:
332 break;
333 }
334}
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:90
QgsMessageBar * messageBar() const
Returns the message bar associated with the widget.
A panel widget that can be shown in the map style dock.
QgsMapLayerConfigWidgetContext mMapLayerConfigWidgetContext
QgsMapLayerStyleManagerWidget(QgsMapLayer *layer, QgsMapCanvas *canvas, QWidget *parent=nullptr)
Style manager widget to manage the layers styles.
QString currentStyle() const
Returns name of the current style.
bool removeStyle(const QString &name)
Remove a stored style.
QStringList styles() const
Returns list of all defined style names.
bool setCurrentStyle(const QString &name)
Set a different style as the current style - will apply it to the layer.
void styleAdded(const QString &name)
Emitted when a new style has been added.
void styleRenamed(const QString &oldName, const QString &newName)
Emitted when a style has been renamed.
bool addStyleFromLayer(const QString &name)
Add style by cloning the current one.
void currentStyleChanged(const QString &currentName)
Emitted when the current style has been changed.
void styleRemoved(const QString &name)
Emitted when a style has been removed.
Base class for all map layer types.
Definition: qgsmaplayer.h:73
Qgis::LayerType type
Definition: qgsmaplayer.h:80
bool isValid
Definition: qgsmaplayer.h:81
QgsMapLayerStyleManager * styleManager() const
Gets access to the layer's style manager.
Property sheet for a mesh map layer.
void loadStyle()
Loads a saved style when appropriate button is pressed.
void loadDefaultStyle()
Loads the default style when appropriate button is pressed.
void saveDefaultStyle()
Saves the default style when appropriate button is pressed.
void saveStyleAs()
Saves a style when appriate button is pressed.
Property sheet for a raster map layer.
void saveDefaultStyle()
Saves the default style when appropriate button is pressed.
void loadDefaultStyle()
Loads the default style when appropriate button is pressed.
void saveStyleAs()
Saves a style when appriate button is pressed.
void loadStyle()
Loads a saved style when appropriate button is pressed.
void loadDefaultStyle()
Loads the default style when appropriate button is pressed.
void saveStyleAs()
Saves a style when appriate button is pressed.
void loadStyle()
Loads a saved style when appropriate button is pressed.
void saveDefaultStyle()
Saves the default style when appropriate button is pressed.
Vectortile layer properties dialog.
void saveDefaultStyle()
Saves the default style when appropriate button is pressed.
void saveStyleAs()
Saves a style when appriate button is pressed.
void loadDefaultStyle()
Loads the default style when appropriate button is pressed.
void loadStyle()
Loads a saved style when appropriate button is pressed.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:3914
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
#define QgsDebugError(str)
Definition: qgslogger.h:38