QGIS API Documentation 3.99.0-Master (d270888f95f)
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
18#include "qgslogger.h"
19#include "qgsmapcanvas.h"
20#include "qgsmaplayer.h"
22
23#include <QAction>
24#include <QActionGroup>
25#include <QInputDialog>
26#include <QMenu>
27#include <QString>
28
29#include "moc_qgsmaplayerstyleguiutils.cpp"
30
31using namespace Qt::StringLiterals;
32
34{
35 static QgsMapLayerStyleGuiUtils sInstance;
36 return &sInstance;
37}
38
39QAction *QgsMapLayerStyleGuiUtils::actionAddStyle( QgsMapLayer *layer, QObject *parent )
40{
41 QAction *a = new QAction( tr( "Add…" ), parent );
42 a->setData( QVariant::fromValue<QObject *>( layer ) );
43 connect( a, &QAction::triggered, this, &QgsMapLayerStyleGuiUtils::addStyle );
44 return a;
45}
46
47QAction *QgsMapLayerStyleGuiUtils::actionRemoveStyle( QgsMapLayer *layer, QObject *parent )
48{
49 QAction *a = new QAction( tr( "Remove Current" ), parent );
50 connect( a, &QAction::triggered, this, &QgsMapLayerStyleGuiUtils::removeStyle );
51 a->setData( QVariant::fromValue<QObject *>( layer ) );
52 a->setEnabled( layer->styleManager()->styles().count() > 1 );
53 return a;
54}
55
56QAction *QgsMapLayerStyleGuiUtils::actionRenameStyle( QgsMapLayer *layer, QObject *parent )
57{
58 QAction *a = new QAction( tr( "Rename Current…" ), parent );
59 connect( a, &QAction::triggered, this, &QgsMapLayerStyleGuiUtils::renameStyle );
60 a->setData( QVariant::fromValue<QObject *>( layer ) );
61 return a;
62}
63
64QList<QAction *> QgsMapLayerStyleGuiUtils::actionsUseStyle( QgsMapLayer *layer, QObject *parent )
65{
66 QgsMapLayerStyleManager *mgr = layer->styleManager();
67 const bool onlyOneStyle = mgr->styles().count() == 1;
68
69 QList<QAction *> actions;
70 QActionGroup *styleGroup = new QActionGroup( parent );
71 const auto constStyles = mgr->styles();
72 for ( const QString &name : constStyles )
73 {
74 const bool active = name == mgr->currentStyle();
75 QAction *actionUse = new QAction( name, styleGroup );
76 connect( actionUse, &QAction::triggered, this, &QgsMapLayerStyleGuiUtils::useStyle );
77 actionUse->setCheckable( true );
78 actionUse->setChecked( active );
79 actionUse->setEnabled( !onlyOneStyle );
80
81 actionUse->setData( QVariant::fromValue<QObject *>( layer ) );
82 actions << actionUse;
83 }
84 return actions;
85}
86
88{
89 if ( !layer )
90 return;
91
92 m->addAction( actionAddStyle( layer, m ) );
93 if ( layer->styleManager()->styles().count() > 1 )
94 m->addAction( actionRemoveStyle( layer, m ) );
95 m->addAction( actionRenameStyle( layer, m ) );
96 m->addSeparator();
97 const auto actions { actionsUseStyle( layer, m ) };
98 for ( QAction *a : actions )
99 m->addAction( a );
100}
101
103{
104 if ( !m )
105 return;
106
107 // Get rid of previously added style manager actions (they are dynamic)
108 bool gotFirstSeparator = false;
109 QList<QAction *> actions = m->actions();
110 for ( int i = 0; i < actions.count(); ++i )
111 {
112 if ( actions[i]->isSeparator() )
113 {
114 if ( gotFirstSeparator )
115 {
116 // remove all actions after second separator (including it)
117 while ( actions.count() != i )
118 delete actions.takeAt( i );
119 break;
120 }
121 else
122 gotFirstSeparator = true;
123 }
124 }
125}
126
127void QgsMapLayerStyleGuiUtils::addStyle()
128{
129 QAction *a = qobject_cast<QAction *>( sender() );
130 if ( !a )
131 return;
132 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
133 if ( !layer )
134 return;
135
136 bool ok;
137 const QString text = QInputDialog::getText( nullptr, tr( "New Style" ), tr( "Style name:" ), QLineEdit::Normal, u"new style"_s, &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( u"Failed to remove current style"_s );
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" ), tr( "Style name:" ), QLineEdit::Normal, name, &ok );
201 if ( !ok )
202 return;
203
204 const bool res = layer->styleManager()->renameStyle( name, text );
205 if ( !res )
206 {
207 QgsDebugError( "Failed to rename style: " + name );
208 }
209}
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
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:83
QgsMapLayerStyleManager * styleManager() const
Gets access to the layer's style manager.
#define QgsDebugError(str)
Definition qgslogger.h:59