QGIS API Documentation 3.99.0-Master (09f76ad7019)
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 <QString>
35#include <QToolBar>
36#include <QVBoxLayout>
37
38#include "moc_qgsmaplayerstylemanagerwidget.cpp"
39
40using namespace Qt::StringLiterals;
41
43 : QgsMapLayerConfigWidget( layer, canvas, parent )
44{
45 mModel = new QStandardItemModel( this );
46 mStyleList = new QListView( this );
47 mStyleList->setModel( mModel );
48 mStyleList->setViewMode( QListView::ListMode );
49 mStyleList->setResizeMode( QListView::Adjust );
50
51 QToolBar *toolbar = new QToolBar( this );
52 QAction *addAction = toolbar->addAction( tr( "Add" ) );
53 addAction->setIcon( QgsApplication::getThemeIcon( u"symbologyAdd.svg"_s ) );
54 connect( addAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::addStyle );
55 QAction *removeAction = toolbar->addAction( tr( "Remove Current" ) );
56 removeAction->setIcon( QgsApplication::getThemeIcon( u"symbologyRemove.svg"_s ) );
57 connect( removeAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::removeStyle );
58 QAction *loadFromFileAction = toolbar->addAction( tr( "Load Style" ) );
59 loadFromFileAction->setIcon( QgsApplication::getThemeIcon( u"/mActionFileOpen.svg"_s ) );
60 connect( loadFromFileAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::loadStyle );
61 QAction *saveAction = toolbar->addAction( tr( "Save Style" ) );
62 saveAction->setIcon( QgsApplication::getThemeIcon( u"mActionFileSave.svg"_s ) );
63 connect( saveAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::saveStyle );
64 QAction *saveAsDefaultAction = toolbar->addAction( tr( "Save as Default" ) );
65 connect( saveAsDefaultAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::saveAsDefault );
66 QAction *loadDefaultAction = toolbar->addAction( tr( "Restore Default" ) );
67 connect( loadDefaultAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::loadDefault );
68
69 connect( mStyleList, &QAbstractItemView::clicked, this, &QgsMapLayerStyleManagerWidget::styleClicked );
70
71 setLayout( new QVBoxLayout() );
72 layout()->setContentsMargins( 0, 0, 0, 0 );
73 layout()->addWidget( toolbar );
74 layout()->addWidget( mStyleList );
75
76 connect( mLayer->styleManager(), &QgsMapLayerStyleManager::currentStyleChanged, this, &QgsMapLayerStyleManagerWidget::currentStyleChanged );
77 connect( mLayer->styleManager(), &QgsMapLayerStyleManager::styleAdded, this, &QgsMapLayerStyleManagerWidget::styleAdded );
78 connect( mLayer->styleManager(), &QgsMapLayerStyleManager::styleRemoved, this, &QgsMapLayerStyleManagerWidget::styleRemoved );
79 connect( mLayer->styleManager(), &QgsMapLayerStyleManager::styleRenamed, this, &QgsMapLayerStyleManagerWidget::styleRenamed );
80
81 mModel->clear();
82
83 const QStringList styles = mLayer->styleManager()->styles();
84 for ( const QString &styleName : styles )
85 {
86 QStandardItem *item = new QStandardItem( styleName );
87 item->setData( styleName );
88 mModel->appendRow( item );
89 }
90
91 const QString active = mLayer->styleManager()->currentStyle();
92 currentStyleChanged( active );
93
94 connect( mModel, &QStandardItemModel::itemChanged, this, &QgsMapLayerStyleManagerWidget::renameStyle );
95}
96
97void QgsMapLayerStyleManagerWidget::styleClicked( const QModelIndex &index )
98{
99 if ( !mLayer || !index.isValid() )
100 return;
101
102 const QString name = index.data().toString();
104}
105
106void QgsMapLayerStyleManagerWidget::currentStyleChanged( const QString &name )
107{
108 const QList<QStandardItem *> items = mModel->findItems( name );
109 if ( items.isEmpty() )
110 return;
111
112 QStandardItem *item = items.at( 0 );
113
114 mStyleList->setCurrentIndex( item->index() );
115}
116
117void QgsMapLayerStyleManagerWidget::styleAdded( const QString &name )
118{
119 QgsDebugMsgLevel( u"Style added"_s, 2 );
120 QStandardItem *item = new QStandardItem( name );
121 item->setData( name );
122 mModel->appendRow( item );
123}
124
125void QgsMapLayerStyleManagerWidget::styleRemoved( const QString &name )
126{
127 const QList<QStandardItem *> items = mModel->findItems( name );
128 if ( items.isEmpty() )
129 return;
130
131 QStandardItem *item = items.at( 0 );
132 mModel->removeRow( item->row() );
133}
134
135void QgsMapLayerStyleManagerWidget::styleRenamed( const QString &oldname, const QString &newname )
136{
137 const QList<QStandardItem *> items = mModel->findItems( oldname );
138 if ( items.isEmpty() )
139 return;
140
141 QStandardItem *item = items.at( 0 );
142 item->setText( newname );
143 item->setData( newname );
144}
145
146void QgsMapLayerStyleManagerWidget::addStyle()
147{
148 bool ok;
149 const QString text = QInputDialog::getText( nullptr, tr( "New Style" ), tr( "Style name:" ), QLineEdit::Normal, u"new style"_s, &ok );
150 if ( !ok || text.isEmpty() )
151 return;
152
153 const bool res = mLayer->styleManager()->addStyleFromLayer( text );
154 if ( res ) // make it active!
155 {
156 mLayer->styleManager()->setCurrentStyle( text );
157 }
158 else
159 {
160 QgsDebugError( "Failed to add style: " + text );
161 }
162}
163
164void QgsMapLayerStyleManagerWidget::removeStyle()
165{
166 const QString current = mLayer->styleManager()->currentStyle();
167 const bool res = mLayer->styleManager()->removeStyle( current );
168 if ( !res )
169 QgsDebugError( u"Failed to remove current style"_s );
170}
171
172void QgsMapLayerStyleManagerWidget::renameStyle( QStandardItem *item )
173{
174 const QString oldName = item->data().toString();
175 const QString newName = item->text();
176 item->setData( newName );
177 whileBlocking( this )->mLayer->styleManager()->renameStyle( oldName, newName );
178}
179
180void QgsMapLayerStyleManagerWidget::saveAsDefault()
181{
182 if ( !mLayer )
183 return;
184
185 switch ( mLayer->type() )
186 {
188 QgsVectorLayerProperties( mMapCanvas, mMapLayerConfigWidgetContext.messageBar(), qobject_cast<QgsVectorLayer *>( mLayer ) ).saveDefaultStyle();
189 break;
190
192 QgsRasterLayerProperties( mLayer, mMapCanvas ).saveStyleAsDefault();
193 break;
194
196 QgsMeshLayerProperties( mLayer, mMapCanvas ).saveStyleAsDefault();
197 break;
198
200 QgsVectorTileLayerProperties( qobject_cast<QgsVectorTileLayer *>( mLayer ), mMapCanvas, mMapLayerConfigWidgetContext.messageBar() ).saveStyleAsDefault();
201 break;
202
203 // Not available for these
209 break;
210 }
211}
212
213void QgsMapLayerStyleManagerWidget::loadDefault()
214{
215 if ( !mLayer )
216 return;
217
218 switch ( mLayer->type() )
219 {
221 QgsVectorLayerProperties( mMapCanvas, mMapLayerConfigWidgetContext.messageBar(), qobject_cast<QgsVectorLayer *>( mLayer ) ).loadDefaultStyle();
222 break;
223
225 QgsRasterLayerProperties( mLayer, mMapCanvas ).loadDefaultStyle();
226 break;
227
229 QgsMeshLayerProperties( mLayer, mMapCanvas ).loadDefaultStyle();
230 break;
231
233 QgsVectorTileLayerProperties( qobject_cast<QgsVectorTileLayer *>( mLayer ), mMapCanvas, mMapLayerConfigWidgetContext.messageBar() ).loadDefaultStyle();
234 break;
235
236 // Not available for these
242 break;
243 }
244}
245
246void QgsMapLayerStyleManagerWidget::saveStyle()
247{
248 if ( !mLayer )
249 return;
250
251 switch ( mLayer->type() )
252 {
254 QgsVectorLayerProperties( mMapCanvas, mMapLayerConfigWidgetContext.messageBar(), qobject_cast<QgsVectorLayer *>( mLayer ) ).saveStyleAs();
255 break;
256
258 QgsRasterLayerProperties( mLayer, mMapCanvas ).saveStyleAs();
259 break;
260
262 QgsMeshLayerProperties( mLayer, mMapCanvas ).saveStyleToFile();
263 break;
264
266 QgsVectorTileLayerProperties( qobject_cast<QgsVectorTileLayer *>( mLayer ), mMapCanvas, mMapLayerConfigWidgetContext.messageBar() ).saveStyleToFile();
267 break;
268
269 // Not available for these
275 break;
276 }
277}
278
279void QgsMapLayerStyleManagerWidget::loadStyle()
280{
281 if ( !mLayer )
282 return;
283
284 switch ( mLayer->type() )
285 {
287 QgsVectorLayerProperties( mMapCanvas, mMapLayerConfigWidgetContext.messageBar(), qobject_cast<QgsVectorLayer *>( mLayer ) ).loadStyle();
288 break;
289
291 QgsRasterLayerProperties( mLayer, mMapCanvas ).loadStyleFromFile();
292 break;
293
295 QgsMeshLayerProperties( mLayer, mMapCanvas ).loadStyleFromFile();
296 break;
297
299 QgsVectorTileLayerProperties( qobject_cast<QgsVectorTileLayer *>( mLayer ), mMapCanvas, mMapLayerConfigWidgetContext.messageBar() ).loadStyle();
300 break;
301
302 // Not available for these
308 break;
309 }
310}
@ Group
Composite group layer. Added in QGIS 3.24.
Definition qgis.h:201
@ Plugin
Plugin based layer.
Definition qgis.h:196
@ TiledScene
Tiled scene layer. Added in QGIS 3.34.
Definition qgis.h:202
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
Definition qgis.h:199
@ Vector
Vector layer.
Definition qgis.h:194
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
Definition qgis.h:198
@ Mesh
Mesh layer. Added in QGIS 3.2.
Definition qgis.h:197
@ Raster
Raster layer.
Definition qgis.h:195
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
Definition qgis.h:200
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:83
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:6839
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63
#define QgsDebugError(str)
Definition qgslogger.h:59