QGIS API Documentation 3.41.0-Master (3440c17df1d)
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 "moc_qgsmaplayerstylemanagerwidget.cpp"
24#include "qgslogger.h"
25#include "qgsmaplayer.h"
26#include "qgsmapcanvas.h"
29#include "qgsvectorlayer.h"
30#include "qgsvectortilelayer.h"
31#include "qgsapplication.h"
36
38 : QgsMapLayerConfigWidget( layer, canvas, parent )
39{
40 mModel = new QStandardItemModel( this );
41 mStyleList = new QListView( this );
42 mStyleList->setModel( mModel );
43 mStyleList->setViewMode( QListView::ListMode );
44 mStyleList->setResizeMode( QListView::Adjust );
45
46 QToolBar *toolbar = new QToolBar( this );
47 QAction *addAction = toolbar->addAction( tr( "Add" ) );
48 addAction->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "symbologyAdd.svg" ) ) );
49 connect( addAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::addStyle );
50 QAction *removeAction = toolbar->addAction( tr( "Remove Current" ) );
51 removeAction->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "symbologyRemove.svg" ) ) );
52 connect( removeAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::removeStyle );
53 QAction *loadFromFileAction = toolbar->addAction( tr( "Load Style" ) );
54 loadFromFileAction->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionFileOpen.svg" ) ) );
55 connect( loadFromFileAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::loadStyle );
56 QAction *saveAction = toolbar->addAction( tr( "Save Style" ) );
57 saveAction->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionFileSave.svg" ) ) );
58 connect( saveAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::saveStyle );
59 QAction *saveAsDefaultAction = toolbar->addAction( tr( "Save as Default" ) );
60 connect( saveAsDefaultAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::saveAsDefault );
61 QAction *loadDefaultAction = toolbar->addAction( tr( "Restore Default" ) );
62 connect( loadDefaultAction, &QAction::triggered, this, &QgsMapLayerStyleManagerWidget::loadDefault );
63
64 //broken connect - not sure what the purpose of this was?
65// connect( canvas, &QgsMapCanvas::mapCanvasRefreshed, this, SLOT( updateCurrent() ) );
66
67 connect( mStyleList, &QAbstractItemView::clicked, this, &QgsMapLayerStyleManagerWidget::styleClicked );
68
69 setLayout( new QVBoxLayout() );
70 layout()->setContentsMargins( 0, 0, 0, 0 );
71 layout()->addWidget( toolbar );
72 layout()->addWidget( mStyleList );
73
74 connect( mLayer->styleManager(), &QgsMapLayerStyleManager::currentStyleChanged, this, &QgsMapLayerStyleManagerWidget::currentStyleChanged );
75 connect( mLayer->styleManager(), &QgsMapLayerStyleManager::styleAdded, this, &QgsMapLayerStyleManagerWidget::styleAdded );
76 connect( mLayer->styleManager(), &QgsMapLayerStyleManager::styleRemoved, this, &QgsMapLayerStyleManagerWidget::styleRemoved );
77 connect( mLayer->styleManager(), &QgsMapLayerStyleManager::styleRenamed, this, &QgsMapLayerStyleManagerWidget::styleRenamed );
78
79 mModel->clear();
80
81 const QStringList styles = mLayer->styleManager()->styles();
82 for ( const QString &styleName : styles )
83 {
84 QStandardItem *item = new QStandardItem( styleName );
85 item->setData( styleName );
86 mModel->appendRow( item );
87 }
88
89 const QString active = mLayer->styleManager()->currentStyle();
90 currentStyleChanged( active );
91
92 connect( mModel, &QStandardItemModel::itemChanged, this, &QgsMapLayerStyleManagerWidget::renameStyle );
93}
94
95void QgsMapLayerStyleManagerWidget::styleClicked( const QModelIndex &index )
96{
97 if ( !mLayer || !index.isValid() )
98 return;
99
100 const QString name = index.data().toString();
102}
103
104void QgsMapLayerStyleManagerWidget::currentStyleChanged( const QString &name )
105{
106 const QList<QStandardItem *> items = mModel->findItems( name );
107 if ( items.isEmpty() )
108 return;
109
110 QStandardItem *item = items.at( 0 );
111
112 mStyleList->setCurrentIndex( item->index() );
113}
114
115void QgsMapLayerStyleManagerWidget::styleAdded( const QString &name )
116{
117 QgsDebugMsgLevel( QStringLiteral( "Style added" ), 2 );
118 QStandardItem *item = new QStandardItem( name );
119 item->setData( name );
120 mModel->appendRow( item );
121}
122
123void QgsMapLayerStyleManagerWidget::styleRemoved( const QString &name )
124{
125 const QList<QStandardItem *> items = mModel->findItems( name );
126 if ( items.isEmpty() )
127 return;
128
129 QStandardItem *item = items.at( 0 );
130 mModel->removeRow( item->row() );
131}
132
133void QgsMapLayerStyleManagerWidget::styleRenamed( const QString &oldname, const QString &newname )
134{
135 const QList<QStandardItem *> items = mModel->findItems( oldname );
136 if ( items.isEmpty() )
137 return;
138
139 QStandardItem *item = items.at( 0 );
140 item->setText( newname );
141 item->setData( newname );
142}
143
144void QgsMapLayerStyleManagerWidget::addStyle()
145{
146 bool ok;
147 const QString text = QInputDialog::getText( nullptr, tr( "New Style" ),
148 tr( "Style name:" ), QLineEdit::Normal,
149 QStringLiteral( "new style" ), &ok );
150 if ( !ok || text.isEmpty() )
151 return;
152
153 const bool res = mLayer->styleManager()->addStyleFromLayer( text );
154 if ( res ) // make it active!
155 {
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( QStringLiteral( "Failed to remove current style" ) );
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 {
187
191 qobject_cast<QgsVectorLayer *>( mLayer ) ).saveDefaultStyle();
192 break;
193
196 break;
197
200 break;
201
203 QgsVectorTileLayerProperties( qobject_cast<QgsVectorTileLayer *>( mLayer ),
206 break;
207
208 // Not available for these
214 break;
215 }
216}
217
218void QgsMapLayerStyleManagerWidget::loadDefault()
219{
220 if ( !mLayer )
221 return;
222
223 switch ( mLayer->type() )
224 {
225
229 qobject_cast<QgsVectorLayer *>( mLayer ) ).loadDefaultStyle();
230 break;
231
234 break;
235
238 break;
239
241 QgsVectorTileLayerProperties( qobject_cast<QgsVectorTileLayer *>( mLayer ),
244 break;
245
246 // Not available for these
252 break;
253 }
254}
255
256void QgsMapLayerStyleManagerWidget::saveStyle()
257{
258 if ( !mLayer )
259 return;
260
261 switch ( mLayer->type() )
262 {
263
267 qobject_cast<QgsVectorLayer *>( mLayer ) ).saveStyleAs();
268 break;
269
272 break;
273
276 break;
277
279 QgsVectorTileLayerProperties( qobject_cast<QgsVectorTileLayer *>( mLayer ),
282 break;
283
284 // Not available for these
290 break;
291 }
292}
293
294void QgsMapLayerStyleManagerWidget::loadStyle()
295{
296 if ( !mLayer )
297 return;
298
299 switch ( mLayer->type() )
300 {
301
305 qobject_cast<QgsVectorLayer *>( mLayer ) ).loadStyle();
306 break;
307
310 break;
311
314 break;
315
317 QgsVectorTileLayerProperties( qobject_cast<QgsVectorTileLayer *>( mLayer ),
320 break;
321
322 // Not available for these
328 break;
329 }
330}
@ 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:76
Qgis::LayerType type
Definition qgsmaplayer.h:86
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:5862
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
#define QgsDebugError(str)
Definition qgslogger.h:38