QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgsmaplayerstyleguiutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaplayerstyleguiutils.cpp
3 --------------------------------------
4 Date : January 2015
5 Copyright : (C) 2015 by Martin Dobias
6 Email : wonder dot sk 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
17#include "moc_qgsmaplayerstyleguiutils.cpp"
18
19#include <QAction>
20#include <QInputDialog>
21#include <QMenu>
22#include <QActionGroup>
23
24#include "qgslogger.h"
25#include "qgsmapcanvas.h"
26#include "qgsmaplayer.h"
28
29
31{
32 static QgsMapLayerStyleGuiUtils sInstance;
33 return &sInstance;
34}
35
36QAction *QgsMapLayerStyleGuiUtils::actionAddStyle( QgsMapLayer *layer, QObject *parent )
37{
38 QAction *a = new QAction( tr( "Add…" ), parent );
39 a->setData( QVariant::fromValue<QObject *>( layer ) );
40 connect( a, &QAction::triggered, this, &QgsMapLayerStyleGuiUtils::addStyle );
41 return a;
42}
43
44QAction *QgsMapLayerStyleGuiUtils::actionRemoveStyle( QgsMapLayer *layer, QObject *parent )
45{
46 QAction *a = new QAction( tr( "Remove Current" ), parent );
47 connect( a, &QAction::triggered, this, &QgsMapLayerStyleGuiUtils::removeStyle );
48 a->setData( QVariant::fromValue<QObject *>( layer ) );
49 a->setEnabled( layer->styleManager()->styles().count() > 1 );
50 return a;
51}
52
53QAction *QgsMapLayerStyleGuiUtils::actionRenameStyle( QgsMapLayer *layer, QObject *parent )
54{
55 QAction *a = new QAction( tr( "Rename Current…" ), parent );
56 connect( a, &QAction::triggered, this, &QgsMapLayerStyleGuiUtils::renameStyle );
57 a->setData( QVariant::fromValue<QObject *>( layer ) );
58 return a;
59}
60
61QList<QAction *> QgsMapLayerStyleGuiUtils::actionsUseStyle( QgsMapLayer *layer, QObject *parent )
62{
64 const bool onlyOneStyle = mgr->styles().count() == 1;
65
66 QList<QAction *> actions;
67 QActionGroup *styleGroup = new QActionGroup( parent );
68 const auto constStyles = mgr->styles();
69 for ( const QString &name : constStyles )
70 {
71 const bool active = name == mgr->currentStyle();
72 QAction *actionUse = new QAction( name, styleGroup );
73 connect( actionUse, &QAction::triggered, this, &QgsMapLayerStyleGuiUtils::useStyle );
74 actionUse->setCheckable( true );
75 actionUse->setChecked( active );
76 actionUse->setEnabled( !onlyOneStyle );
77
78 actionUse->setData( QVariant::fromValue<QObject *>( layer ) );
79 actions << actionUse;
80 }
81 return actions;
82}
83
85{
86 if ( ! layer )
87 return;
88
89 m->addAction( actionAddStyle( layer, m ) );
90 if ( layer->styleManager()->styles().count() > 1 )
91 m->addAction( actionRemoveStyle( layer, m ) );
92 m->addAction( actionRenameStyle( layer, m ) );
93 m->addSeparator();
94 const auto actions {actionsUseStyle( layer, m )};
95 for ( QAction *a : actions )
96 m->addAction( a );
97}
98
100{
101 if ( !m )
102 return;
103
104 // Get rid of previously added style manager actions (they are dynamic)
105 bool gotFirstSeparator = false;
106 QList<QAction *> actions = m->actions();
107 for ( int i = 0; i < actions.count(); ++i )
108 {
109 if ( actions[i]->isSeparator() )
110 {
111 if ( gotFirstSeparator )
112 {
113 // remove all actions after second separator (including it)
114 while ( actions.count() != i )
115 delete actions.takeAt( i );
116 break;
117 }
118 else
119 gotFirstSeparator = true;
120 }
121 }
122
123}
124
125void QgsMapLayerStyleGuiUtils::addStyle()
126{
127 QAction *a = qobject_cast<QAction *>( sender() );
128 if ( !a )
129 return;
130 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
131 if ( !layer )
132 return;
133
134 bool ok;
135 const QString text = QInputDialog::getText( nullptr, tr( "New Style" ),
136 tr( "Style name:" ), QLineEdit::Normal,
137 QStringLiteral( "new style" ), &ok );
138 if ( !ok || text.isEmpty() )
139 return;
140
141 const bool res = layer->styleManager()->addStyleFromLayer( text );
142
143 if ( res ) // make it active!
144 {
145 layer->styleManager()->setCurrentStyle( text );
146 }
147 else
148 {
149 QgsDebugError( "Failed to add style: " + text );
150 }
151}
152
153void QgsMapLayerStyleGuiUtils::useStyle()
154{
155 QAction *a = qobject_cast<QAction *>( sender() );
156 if ( !a )
157 return;
158 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
159 if ( !layer )
160 return;
161 const QString name = a->text();
162
163 const bool res = layer->styleManager()->setCurrentStyle( name );
164 if ( !res )
165 {
166 QgsDebugError( "Failed to set current style: " + name );
167 }
168}
169
170
171void QgsMapLayerStyleGuiUtils::removeStyle()
172{
173 QAction *a = qobject_cast<QAction *>( sender() );
174 if ( !a )
175 return;
176 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
177 if ( !layer )
178 return;
179
180 const bool res = layer->styleManager()->removeStyle( layer->styleManager()->currentStyle() );
181 if ( !res )
182 {
183 QgsDebugError( QStringLiteral( "Failed to remove current style" ) );
184 }
185}
186
187
188void QgsMapLayerStyleGuiUtils::renameStyle()
189{
190 QAction *a = qobject_cast<QAction *>( sender() );
191 if ( !a )
192 return;
193 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
194 if ( !layer )
195 return;
196
197 const QString name = layer->styleManager()->currentStyle();
198
199 bool ok;
200 const QString text = QInputDialog::getText( nullptr, tr( "Rename Style" ),
201 tr( "Style name:" ), QLineEdit::Normal,
202 name, &ok );
203 if ( !ok )
204 return;
205
206 const bool res = layer->styleManager()->renameStyle( name, text );
207 if ( !res )
208 {
209 QgsDebugError( "Failed to rename style: " + name );
210 }
211}
Various GUI utility functions for dealing with map layer's style manager.
void removesExtraMenuSeparators(QMenu *m)
removes extra separators from the menu
void addStyleManagerActions(QMenu *m, QgsMapLayer *layer)
adds actions to the menu in accordance to the layer
static QgsMapLayerStyleGuiUtils * instance()
returns a singleton instance of this class
Management of styles for use with one map layer.
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.
bool addStyleFromLayer(const QString &name)
Add style by cloning the current one.
bool renameStyle(const QString &name, const QString &newName)
Rename a stored style to a different name.
Base class for all map layer types.
Definition qgsmaplayer.h:76
QgsMapLayerStyleManager * styleManager() const
Gets access to the layer's style manager.
#define QgsDebugError(str)
Definition qgslogger.h:38