QGIS API Documentation  3.12.1-București (121cc00ff0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 <QAction>
19 #include <QInputDialog>
20 #include <QMenu>
21 
22 #include "qgslogger.h"
23 #include "qgsmapcanvas.h"
24 #include "qgsmaplayer.h"
26 
27 
29 {
30  static QgsMapLayerStyleGuiUtils sInstance;
31  return &sInstance;
32 }
33 
34 QAction *QgsMapLayerStyleGuiUtils::actionAddStyle( QgsMapLayer *layer, QObject *parent )
35 {
36  QAction *a = new QAction( tr( "Add…" ), parent );
37  a->setData( QVariant::fromValue<QObject *>( layer ) );
38  connect( a, &QAction::triggered, this, &QgsMapLayerStyleGuiUtils::addStyle );
39  return a;
40 }
41 
42 QAction *QgsMapLayerStyleGuiUtils::actionRemoveStyle( QgsMapLayer *layer, QObject *parent )
43 {
44  QAction *a = new QAction( tr( "Remove Current" ), parent );
45  connect( a, &QAction::triggered, this, &QgsMapLayerStyleGuiUtils::removeStyle );
46  a->setData( QVariant::fromValue<QObject *>( layer ) );
47  a->setEnabled( layer->styleManager()->styles().count() > 1 );
48  return a;
49 }
50 
51 QAction *QgsMapLayerStyleGuiUtils::actionRenameStyle( QgsMapLayer *layer, QObject *parent )
52 {
53  QAction *a = new QAction( tr( "Rename Current…" ), parent );
54  connect( a, &QAction::triggered, this, &QgsMapLayerStyleGuiUtils::renameStyle );
55  a->setData( QVariant::fromValue<QObject *>( layer ) );
56  return a;
57 }
58 
59 QList<QAction *> QgsMapLayerStyleGuiUtils::actionsUseStyle( QgsMapLayer *layer, QObject *parent )
60 {
61  QgsMapLayerStyleManager *mgr = layer->styleManager();
62  bool onlyOneStyle = mgr->styles().count() == 1;
63 
64  QList<QAction *> actions;
65  QActionGroup *styleGroup = new QActionGroup( parent );
66  const auto constStyles = mgr->styles();
67  for ( const QString &name : constStyles )
68  {
69  bool active = name == mgr->currentStyle();
70  QAction *actionUse = new QAction( name, styleGroup );
71  connect( actionUse, &QAction::triggered, this, &QgsMapLayerStyleGuiUtils::useStyle );
72  actionUse->setCheckable( true );
73  actionUse->setChecked( active );
74  actionUse->setEnabled( !onlyOneStyle );
75 
76  actionUse->setData( QVariant::fromValue<QObject *>( layer ) );
77  actions << actionUse;
78  }
79  return actions;
80 }
81 
83 {
84  if ( ! layer )
85  return;
86 
87  m->addAction( actionAddStyle( layer, m ) );
88  if ( layer->styleManager()->styles().count() > 1 )
89  m->addAction( actionRemoveStyle( layer, m ) );
90  m->addAction( actionRenameStyle( layer, m ) );
91  m->addSeparator();
92  const auto actions {actionsUseStyle( layer, m )};
93  for ( QAction *a : actions )
94  m->addAction( a );
95 }
96 
97 void QgsMapLayerStyleGuiUtils::addStyle()
98 {
99  QAction *a = qobject_cast<QAction *>( sender() );
100  if ( !a )
101  return;
102  QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
103  if ( !layer )
104  return;
105 
106  bool ok;
107  QString text = QInputDialog::getText( nullptr, tr( "New Style" ),
108  tr( "Style name:" ), QLineEdit::Normal,
109  QStringLiteral( "new style" ), &ok );
110  if ( !ok || text.isEmpty() )
111  return;
112 
113  bool res = layer->styleManager()->addStyleFromLayer( text );
114 
115  if ( res ) // make it active!
116  {
117  layer->styleManager()->setCurrentStyle( text );
118  }
119  else
120  {
121  QgsDebugMsg( "Failed to add style: " + text );
122  }
123 }
124 
125 void QgsMapLayerStyleGuiUtils::useStyle()
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  QString name = a->text();
134 
135  bool res = layer->styleManager()->setCurrentStyle( name );
136  if ( !res )
137  {
138  QgsDebugMsg( "Failed to set current style: " + name );
139  }
140 }
141 
142 
143 void QgsMapLayerStyleGuiUtils::removeStyle()
144 {
145  QAction *a = qobject_cast<QAction *>( sender() );
146  if ( !a )
147  return;
148  QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
149  if ( !layer )
150  return;
151 
152  bool res = layer->styleManager()->removeStyle( layer->styleManager()->currentStyle() );
153  if ( !res )
154  {
155  QgsDebugMsg( QStringLiteral( "Failed to remove current style" ) );
156  }
157 }
158 
159 
160 void QgsMapLayerStyleGuiUtils::renameStyle()
161 {
162  QAction *a = qobject_cast<QAction *>( sender() );
163  if ( !a )
164  return;
165  QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
166  if ( !layer )
167  return;
168 
169  QString name = layer->styleManager()->currentStyle();
170 
171  bool ok;
172  QString text = QInputDialog::getText( nullptr, tr( "Rename Style" ),
173  tr( "Style name:" ), QLineEdit::Normal,
174  name, &ok );
175  if ( !ok )
176  return;
177 
178  bool res = layer->styleManager()->renameStyle( name, text );
179  if ( !res )
180  {
181  QgsDebugMsg( "Failed to rename style: " + name );
182  }
183 }
Base class for all map layer types.
Definition: qgsmaplayer.h:79
static QgsMapLayerStyleGuiUtils * instance()
returns a singleton instance of this class
QStringList styles() const
Returns list of all defined style names.
Various GUI utility functions for dealing with map layer&#39;s style manager.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsMapLayerStyleManager * styleManager() const
Gets access to the layer&#39;s style manager.
bool removeStyle(const QString &name)
Remove a stored style.
bool addStyleFromLayer(const QString &name)
Add style by cloning the current one.
QString currentStyle() const
Returns name of the current style.
bool renameStyle(const QString &name, const QString &newName)
Rename a stored style to a different name.
void addStyleManagerActions(QMenu *m, QgsMapLayer *layer)
adds actions to the menu in accordance to the layer
Management of styles for use with one map layer.
bool setCurrentStyle(const QString &name)
Set a different style as the current style - will apply it to the layer.