QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
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 "qgslogger.h"
24#include "qgsmaplayer.h"
25#include "qgsmapcanvas.h"
28#include "qgsvectorlayer.h"
29#include "qgsvectortilelayer.h"
30#include "qgsapplication.h"
35
37 : QgsMapLayerConfigWidget( layer, canvas, parent )
38{
39 mModel = new QStandardItemModel( this );
40 mStyleList = new QListView( this );
41 mStyleList->setModel( mModel );
42 mStyleList->setViewMode( QListView::ListMode );
43 mStyleList->setResizeMode( QListView::Adjust );
44
45 QToolBar *toolbar = new QToolBar( this );
46 QAction *addAction = toolbar->addAction( tr( "Add" ) );
47 addAction->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "symbologyAdd.svg" ) ) );
48 connect( addAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::addStyle );
49 QAction *removeAction = toolbar->addAction( tr( "Remove Current" ) );
50 removeAction->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "symbologyRemove.svg" ) ) );
51 connect( removeAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::removeStyle );
52 QAction *loadFromFileAction = toolbar->addAction( tr( "Load Style" ) );
53 loadFromFileAction->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionFileOpen.svg" ) ) );
54 connect( loadFromFileAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::loadStyle );
55 QAction *saveAction = toolbar->addAction( tr( "Save Style" ) );
56 saveAction->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionFileSave.svg" ) ) );
57 connect( saveAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::saveStyle );
58 QAction *saveAsDefaultAction = toolbar->addAction( tr( "Save as Default" ) );
59 connect( saveAsDefaultAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::saveAsDefault );
60 QAction *loadDefaultAction = toolbar->addAction( tr( "Restore Default" ) );
61 connect( loadDefaultAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::loadDefault );
62
63 //broken connect - not sure what the purpose of this was?
64// connect( canvas, &QgsMapCanvas::mapCanvasRefreshed, this, SLOT( updateCurrent() ) );
65
66 connect( mStyleList, &QAbstractItemView::clicked, this, &QgsMapLayerStyleManagerWidget::styleClicked );
67
68 setLayout( new QVBoxLayout() );
69 layout()->setContentsMargins( 0, 0, 0, 0 );
70 layout()->addWidget( toolbar );
71 layout()->addWidget( mStyleList );
72
73 connect( mLayer->styleManager(), &QgsMapLayerStyleManager::currentStyleChanged, this, &QgsMapLayerStyleManagerWidget::currentStyleChanged );
74 connect( mLayer->styleManager(), &QgsMapLayerStyleManager::styleAdded, this, &QgsMapLayerStyleManagerWidget::styleAdded );
75 connect( mLayer->styleManager(), &QgsMapLayerStyleManager::styleRemoved, this, &QgsMapLayerStyleManagerWidget::styleRemoved );
76 connect( mLayer->styleManager(), &QgsMapLayerStyleManager::styleRenamed, this, &QgsMapLayerStyleManagerWidget::styleRenamed );
77
78 mModel->clear();
79
80 const QStringList styles = mLayer->styleManager()->styles();
81 for ( const QString &styleName : styles )
82 {
83 QStandardItem *item = new QStandardItem( styleName );
84 item->setData( styleName );
85 mModel->appendRow( item );
86 }
87
88 const QString active = mLayer->styleManager()->currentStyle();
89 currentStyleChanged( active );
90
91 connect( mModel, &QStandardItemModel::itemChanged, this, &QgsMapLayerStyleManagerWidget::renameStyle );
92}
93
94void QgsMapLayerStyleManagerWidget::styleClicked( const QModelIndex &index )
95{
96 if ( !mLayer || !index.isValid() )
97 return;
98
99 const QString name = index.data().toString();
101}
102
103void QgsMapLayerStyleManagerWidget::currentStyleChanged( const QString &name )
104{
105 const QList<QStandardItem *> items = mModel->findItems( name );
106 if ( items.isEmpty() )
107 return;
108
109 QStandardItem *item = items.at( 0 );
110
111 mStyleList->setCurrentIndex( item->index() );
112}
113
114void QgsMapLayerStyleManagerWidget::styleAdded( const QString &name )
115{
116 QgsDebugMsgLevel( QStringLiteral( "Style added" ), 2 );
117 QStandardItem *item = new QStandardItem( name );
118 item->setData( name );
119 mModel->appendRow( item );
120}
121
122void QgsMapLayerStyleManagerWidget::styleRemoved( const QString &name )
123{
124 const QList<QStandardItem *> items = mModel->findItems( name );
125 if ( items.isEmpty() )
126 return;
127
128 QStandardItem *item = items.at( 0 );
129 mModel->removeRow( item->row() );
130}
131
132void QgsMapLayerStyleManagerWidget::styleRenamed( const QString &oldname, const QString &newname )
133{
134 const QList<QStandardItem *> items = mModel->findItems( oldname );
135 if ( items.isEmpty() )
136 return;
137
138 QStandardItem *item = items.at( 0 );
139 item->setText( newname );
140 item->setData( newname );
141}
142
143void QgsMapLayerStyleManagerWidget::addStyle()
144{
145 bool ok;
146 const QString text = QInputDialog::getText( nullptr, tr( "New Style" ),
147 tr( "Style name:" ), QLineEdit::Normal,
148 QStringLiteral( "new style" ), &ok );
149 if ( !ok || text.isEmpty() )
150 return;
151
152 const bool res = mLayer->styleManager()->addStyleFromLayer( text );
153 if ( res ) // make it active!
154 {
156 }
157 else
158 {
159 QgsDebugError( "Failed to add style: " + text );
160 }
161}
162
163void QgsMapLayerStyleManagerWidget::removeStyle()
164{
165 const QString current = mLayer->styleManager()->currentStyle();
166 const bool res = mLayer->styleManager()->removeStyle( current );
167 if ( !res )
168 QgsDebugError( QStringLiteral( "Failed to remove current style" ) );
169}
170
171void QgsMapLayerStyleManagerWidget::renameStyle( QStandardItem *item )
172{
173 const QString oldName = item->data().toString();
174 const QString newName = item->text();
175 item->setData( newName );
176 whileBlocking( this )->mLayer->styleManager()->renameStyle( oldName, newName );
177}
178
179void QgsMapLayerStyleManagerWidget::saveAsDefault()
180{
181 if ( !mLayer )
182 return;
183
184 switch ( mLayer->type() )
185 {
186
190 qobject_cast<QgsVectorLayer *>( mLayer ) ).saveDefaultStyle();
191 break;
192
195 break;
196
199 break;
200
202 QgsVectorTileLayerProperties( qobject_cast<QgsVectorTileLayer *>( mLayer ),
205 break;
206
207 // Not available for these
213 break;
214 }
215}
216
217void QgsMapLayerStyleManagerWidget::loadDefault()
218{
219 if ( !mLayer )
220 return;
221
222 switch ( mLayer->type() )
223 {
224
228 qobject_cast<QgsVectorLayer *>( mLayer ) ).loadDefaultStyle();
229 break;
230
233 break;
234
237 break;
238
240 QgsVectorTileLayerProperties( qobject_cast<QgsVectorTileLayer *>( mLayer ),
243 break;
244
245 // Not available for these
251 break;
252 }
253}
254
255void QgsMapLayerStyleManagerWidget::saveStyle()
256{
257 if ( !mLayer )
258 return;
259
260 switch ( mLayer->type() )
261 {
262
266 qobject_cast<QgsVectorLayer *>( mLayer ) ).saveStyleAs();
267 break;
268
271 break;
272
275 break;
276
278 QgsVectorTileLayerProperties( qobject_cast<QgsVectorTileLayer *>( mLayer ),
281 break;
282
283 // Not available for these
289 break;
290 }
291}
292
293void QgsMapLayerStyleManagerWidget::loadStyle()
294{
295 if ( !mLayer )
296 return;
297
298 switch ( mLayer->type() )
299 {
300
304 qobject_cast<QgsVectorLayer *>( mLayer ) ).loadStyle();
305 break;
306
309 break;
310
313 break;
314
316 QgsVectorTileLayerProperties( qobject_cast<QgsVectorTileLayer *>( mLayer ),
319 break;
320
321 // Not available for these
327 break;
328 }
329}
@ Group
Composite group layer. Added in QGIS 3.24.
@ Plugin
Plugin based layer.
@ TiledScene
Tiled scene layer. Added in QGIS 3.34.
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
@ Vector
Vector layer.
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
@ Mesh
Mesh layer. Added in QGIS 3.2.
@ Raster
Raster layer.
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
void saveStyleAsDefault()
Saves the current layer style as the default for the layer.
void loadDefaultStyle()
Reloads the default style for the layer.
void saveStyleAs()
Saves a style when appriate button is pressed.
void loadStyle()
Triggers a dialog to load a saved style.
void saveStyleToFile()
Allows the user to save the layer's style to a file.
void saveDefaultStyle()
Saves the default style when appropriate button is pressed.
void loadStyleFromFile()
Allows the user to load layer style from a file.
Map canvas is a class for displaying all GIS data types on a canvas.
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:74
Qgis::LayerType type
Definition qgsmaplayer.h:81
QgsMapLayerStyleManager * styleManager() const
Gets access to the layer's style manager.
Property sheet for a mesh map layer.
Property sheet for a raster map layer.
Vectortile layer properties dialog.
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:4258
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
#define QgsDebugError(str)
Definition qgslogger.h:38