QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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 ***************************************************************************/
16
17#include "qgsapplication.h"
18#include "qgslogger.h"
19#include "qgsmapcanvas.h"
20#include "qgsmaplayer.h"
25#include "qgsvectorlayer.h"
27#include "qgsvectortilelayer.h"
29
30#include <QAction>
31#include <QFileDialog>
32#include <QInputDialog>
33#include <QMessageBox>
34#include <QToolBar>
35#include <QVBoxLayout>
36
37#include "moc_qgsmaplayerstylemanagerwidget.cpp"
38
40 : QgsMapLayerConfigWidget( layer, canvas, parent )
41{
42 mModel = new QStandardItemModel( this );
43 mStyleList = new QListView( this );
44 mStyleList->setModel( mModel );
45 mStyleList->setViewMode( QListView::ListMode );
46 mStyleList->setResizeMode( QListView::Adjust );
47
48 QToolBar *toolbar = new QToolBar( this );
49 QAction *addAction = toolbar->addAction( tr( "Add" ) );
50 addAction->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "symbologyAdd.svg" ) ) );
51 connect( addAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::addStyle );
52 QAction *removeAction = toolbar->addAction( tr( "Remove Current" ) );
53 removeAction->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "symbologyRemove.svg" ) ) );
54 connect( removeAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::removeStyle );
55 QAction *loadFromFileAction = toolbar->addAction( tr( "Load Style" ) );
56 loadFromFileAction->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionFileOpen.svg" ) ) );
57 connect( loadFromFileAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::loadStyle );
58 QAction *saveAction = toolbar->addAction( tr( "Save Style" ) );
59 saveAction->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionFileSave.svg" ) ) );
60 connect( saveAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::saveStyle );
61 QAction *saveAsDefaultAction = toolbar->addAction( tr( "Save as Default" ) );
62 connect( saveAsDefaultAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::saveAsDefault );
63 QAction *loadDefaultAction = toolbar->addAction( tr( "Restore Default" ) );
64 connect( loadDefaultAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::loadDefault );
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" ), tr( "Style name:" ), QLineEdit::Normal, QStringLiteral( "new style" ), &ok );
147 if ( !ok || text.isEmpty() )
148 return;
149
150 const bool res = mLayer->styleManager()->addStyleFromLayer( text );
151 if ( res ) // make it active!
152 {
153 mLayer->styleManager()->setCurrentStyle( text );
154 }
155 else
156 {
157 QgsDebugError( "Failed to add style: " + text );
158 }
159}
160
161void QgsMapLayerStyleManagerWidget::removeStyle()
162{
163 const QString current = mLayer->styleManager()->currentStyle();
164 const bool res = mLayer->styleManager()->removeStyle( current );
165 if ( !res )
166 QgsDebugError( QStringLiteral( "Failed to remove current style" ) );
167}
168
169void QgsMapLayerStyleManagerWidget::renameStyle( QStandardItem *item )
170{
171 const QString oldName = item->data().toString();
172 const QString newName = item->text();
173 item->setData( newName );
174 whileBlocking( this )->mLayer->styleManager()->renameStyle( oldName, newName );
175}
176
177void QgsMapLayerStyleManagerWidget::saveAsDefault()
178{
179 if ( !mLayer )
180 return;
181
182 switch ( mLayer->type() )
183 {
185 QgsVectorLayerProperties( mMapCanvas, mMapLayerConfigWidgetContext.messageBar(), qobject_cast<QgsVectorLayer *>( mLayer ) ).saveDefaultStyle();
186 break;
187
189 QgsRasterLayerProperties( mLayer, mMapCanvas ).saveStyleAsDefault();
190 break;
191
193 QgsMeshLayerProperties( mLayer, mMapCanvas ).saveStyleAsDefault();
194 break;
195
197 QgsVectorTileLayerProperties( qobject_cast<QgsVectorTileLayer *>( mLayer ), mMapCanvas, mMapLayerConfigWidgetContext.messageBar() ).saveStyleAsDefault();
198 break;
199
200 // Not available for these
206 break;
207 }
208}
209
210void QgsMapLayerStyleManagerWidget::loadDefault()
211{
212 if ( !mLayer )
213 return;
214
215 switch ( mLayer->type() )
216 {
218 QgsVectorLayerProperties( mMapCanvas, mMapLayerConfigWidgetContext.messageBar(), qobject_cast<QgsVectorLayer *>( mLayer ) ).loadDefaultStyle();
219 break;
220
222 QgsRasterLayerProperties( mLayer, mMapCanvas ).loadDefaultStyle();
223 break;
224
226 QgsMeshLayerProperties( mLayer, mMapCanvas ).loadDefaultStyle();
227 break;
228
230 QgsVectorTileLayerProperties( qobject_cast<QgsVectorTileLayer *>( mLayer ), mMapCanvas, mMapLayerConfigWidgetContext.messageBar() ).loadDefaultStyle();
231 break;
232
233 // Not available for these
239 break;
240 }
241}
242
243void QgsMapLayerStyleManagerWidget::saveStyle()
244{
245 if ( !mLayer )
246 return;
247
248 switch ( mLayer->type() )
249 {
251 QgsVectorLayerProperties( mMapCanvas, mMapLayerConfigWidgetContext.messageBar(), qobject_cast<QgsVectorLayer *>( mLayer ) ).saveStyleAs();
252 break;
253
255 QgsRasterLayerProperties( mLayer, mMapCanvas ).saveStyleAs();
256 break;
257
259 QgsMeshLayerProperties( mLayer, mMapCanvas ).saveStyleToFile();
260 break;
261
263 QgsVectorTileLayerProperties( qobject_cast<QgsVectorTileLayer *>( mLayer ), mMapCanvas, mMapLayerConfigWidgetContext.messageBar() ).saveStyleToFile();
264 break;
265
266 // Not available for these
272 break;
273 }
274}
275
276void QgsMapLayerStyleManagerWidget::loadStyle()
277{
278 if ( !mLayer )
279 return;
280
281 switch ( mLayer->type() )
282 {
284 QgsVectorLayerProperties( mMapCanvas, mMapLayerConfigWidgetContext.messageBar(), qobject_cast<QgsVectorLayer *>( mLayer ) ).loadStyle();
285 break;
286
288 QgsRasterLayerProperties( mLayer, mMapCanvas ).loadStyleFromFile();
289 break;
290
292 QgsMeshLayerProperties( mLayer, mMapCanvas ).loadStyleFromFile();
293 break;
294
296 QgsVectorTileLayerProperties( qobject_cast<QgsVectorTileLayer *>( mLayer ), mMapCanvas, mMapLayerConfigWidgetContext.messageBar() ).loadStyle();
297 break;
298
299 // Not available for these
305 break;
306 }
307}
@ Group
Composite group layer. Added in QGIS 3.24.
Definition qgis.h:198
@ Plugin
Plugin based layer.
Definition qgis.h:193
@ TiledScene
Tiled scene layer. Added in QGIS 3.34.
Definition qgis.h:199
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
Definition qgis.h:196
@ Vector
Vector layer.
Definition qgis.h:191
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
Definition qgis.h:195
@ Mesh
Mesh layer. Added in QGIS 3.2.
Definition qgis.h:194
@ Raster
Raster layer.
Definition qgis.h:192
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
Definition qgis.h:197
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.
QgsMapLayerConfigWidget(QgsMapLayer *layer, QgsMapCanvas *canvas, QWidget *parent=nullptr)
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.
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.
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:80
QgsMapLayerStyleManager * styleManager() const
Gets access to the layer's style manager.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:6511
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:61
#define QgsDebugError(str)
Definition qgslogger.h:57