QGIS API Documentation 3.41.0-Master (cea29feecf2)
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
124void QgsMapLayerStyleGuiUtils::addStyle()
125{
126 QAction *a = qobject_cast<QAction *>( sender() );
127 if ( !a )
128 return;
129 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
130 if ( !layer )
131 return;
132
133 bool ok;
134 const QString text = QInputDialog::getText( nullptr, tr( "New Style" ), tr( "Style name:" ), QLineEdit::Normal, QStringLiteral( "new style" ), &ok );
135 if ( !ok || text.isEmpty() )
136 return;
137
138 const bool res = layer->styleManager()->addStyleFromLayer( text );
139
140 if ( res ) // make it active!
141 {
142 layer->styleManager()->setCurrentStyle( text );
143 }
144 else
145 {
146 QgsDebugError( "Failed to add style: " + text );
147 }
148}
149
150void QgsMapLayerStyleGuiUtils::useStyle()
151{
152 QAction *a = qobject_cast<QAction *>( sender() );
153 if ( !a )
154 return;
155 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
156 if ( !layer )
157 return;
158 const QString name = a->text();
159
160 const bool res = layer->styleManager()->setCurrentStyle( name );
161 if ( !res )
162 {
163 QgsDebugError( "Failed to set current style: " + name );
164 }
165}
166
167
168void QgsMapLayerStyleGuiUtils::removeStyle()
169{
170 QAction *a = qobject_cast<QAction *>( sender() );
171 if ( !a )
172 return;
173 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
174 if ( !layer )
175 return;
176
177 const bool res = layer->styleManager()->removeStyle( layer->styleManager()->currentStyle() );
178 if ( !res )
179 {
180 QgsDebugError( QStringLiteral( "Failed to remove current style" ) );
181 }
182}
183
184
185void QgsMapLayerStyleGuiUtils::renameStyle()
186{
187 QAction *a = qobject_cast<QAction *>( sender() );
188 if ( !a )
189 return;
190 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
191 if ( !layer )
192 return;
193
194 const QString name = layer->styleManager()->currentStyle();
195
196 bool ok;
197 const QString text = QInputDialog::getText( nullptr, tr( "Rename Style" ), tr( "Style name:" ), QLineEdit::Normal, name, &ok );
198 if ( !ok )
199 return;
200
201 const bool res = layer->styleManager()->renameStyle( name, text );
202 if ( !res )
203 {
204 QgsDebugError( "Failed to rename style: " + name );
205 }
206}
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