QGIS API Documentation 3.41.0-Master (cea29feecf2)
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" ), tr( "Style name:" ), QLineEdit::Normal, QStringLiteral( "new style" ), &ok );
148 if ( !ok || text.isEmpty() )
149 return;
150
151 const bool res = mLayer->styleManager()->addStyleFromLayer( text );
152 if ( res ) // make it active!
153 {
155 }
156 else
157 {
158 QgsDebugError( "Failed to add style: " + text );
159 }
160}
161
162void QgsMapLayerStyleManagerWidget::removeStyle()
163{
164 const QString current = mLayer->styleManager()->currentStyle();
165 const bool res = mLayer->styleManager()->removeStyle( current );
166 if ( !res )
167 QgsDebugError( QStringLiteral( "Failed to remove current style" ) );
168}
169
170void QgsMapLayerStyleManagerWidget::renameStyle( QStandardItem *item )
171{
172 const QString oldName = item->data().toString();
173 const QString newName = item->text();
174 item->setData( newName );
175 whileBlocking( this )->mLayer->styleManager()->renameStyle( oldName, newName );
176}
177
178void QgsMapLayerStyleManagerWidget::saveAsDefault()
179{
180 if ( !mLayer )
181 return;
182
183 switch ( mLayer->type() )
184 {
187 break;
188
191 break;
192
195 break;
196
199 break;
200
201 // Not available for these
207 break;
208 }
209}
210
211void QgsMapLayerStyleManagerWidget::loadDefault()
212{
213 if ( !mLayer )
214 return;
215
216 switch ( mLayer->type() )
217 {
220 break;
221
224 break;
225
228 break;
229
232 break;
233
234 // Not available for these
240 break;
241 }
242}
243
244void QgsMapLayerStyleManagerWidget::saveStyle()
245{
246 if ( !mLayer )
247 return;
248
249 switch ( mLayer->type() )
250 {
253 break;
254
257 break;
258
261 break;
262
265 break;
266
267 // Not available for these
273 break;
274 }
275}
276
277void QgsMapLayerStyleManagerWidget::loadStyle()
278{
279 if ( !mLayer )
280 return;
281
282 switch ( mLayer->type() )
283 {
286 break;
287
290 break;
291
294 break;
295
298 break;
299
300 // Not available for these
306 break;
307 }
308}
@ 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:5928
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
#define QgsDebugError(str)
Definition qgslogger.h:38