17#include "moc_qgsmaplayerstyleguiutils.cpp"
20#include <QInputDialog>
22#include <QActionGroup>
36QAction *QgsMapLayerStyleGuiUtils::actionAddStyle(
QgsMapLayer *layer, QObject *parent )
38 QAction *a =
new QAction( tr(
"Add…" ), parent );
39 a->setData( QVariant::fromValue<QObject *>( layer ) );
40 connect( a, &QAction::triggered,
this, &QgsMapLayerStyleGuiUtils::addStyle );
44QAction *QgsMapLayerStyleGuiUtils::actionRemoveStyle(
QgsMapLayer *layer, QObject *parent )
46 QAction *a =
new QAction( tr(
"Remove Current" ), parent );
47 connect( a, &QAction::triggered,
this, &QgsMapLayerStyleGuiUtils::removeStyle );
48 a->setData( QVariant::fromValue<QObject *>( layer ) );
53QAction *QgsMapLayerStyleGuiUtils::actionRenameStyle(
QgsMapLayer *layer, QObject *parent )
55 QAction *a =
new QAction( tr(
"Rename Current…" ), parent );
56 connect( a, &QAction::triggered,
this, &QgsMapLayerStyleGuiUtils::renameStyle );
57 a->setData( QVariant::fromValue<QObject *>( layer ) );
61QList<QAction *> QgsMapLayerStyleGuiUtils::actionsUseStyle(
QgsMapLayer *layer, QObject *parent )
64 const bool onlyOneStyle = mgr->
styles().count() == 1;
66 QList<QAction *> actions;
67 QActionGroup *styleGroup =
new QActionGroup( parent );
68 const auto constStyles = mgr->
styles();
69 for (
const QString &name : constStyles )
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 );
78 actionUse->setData( QVariant::fromValue<QObject *>( layer ) );
89 m->addAction( actionAddStyle( layer, m ) );
91 m->addAction( actionRemoveStyle( layer, m ) );
92 m->addAction( actionRenameStyle( layer, m ) );
94 const auto actions {actionsUseStyle( layer, m )};
95 for ( QAction *a : actions )
105 bool gotFirstSeparator =
false;
106 QList<QAction *> actions = m->actions();
107 for (
int i = 0; i < actions.count(); ++i )
109 if ( actions[i]->isSeparator() )
111 if ( gotFirstSeparator )
114 while ( actions.count() != i )
115 delete actions.takeAt( i );
119 gotFirstSeparator =
true;
125void QgsMapLayerStyleGuiUtils::addStyle()
127 QAction *a = qobject_cast<QAction *>( sender() );
130 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
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() )
153void QgsMapLayerStyleGuiUtils::useStyle()
155 QAction *a = qobject_cast<QAction *>( sender() );
158 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
161 const QString name = a->text();
171void QgsMapLayerStyleGuiUtils::removeStyle()
173 QAction *a = qobject_cast<QAction *>( sender() );
176 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
183 QgsDebugError( QStringLiteral(
"Failed to remove current style" ) );
188void QgsMapLayerStyleGuiUtils::renameStyle()
190 QAction *a = qobject_cast<QAction *>( sender() );
193 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
200 const QString text = QInputDialog::getText(
nullptr, tr(
"Rename Style" ),
201 tr(
"Style name:" ), QLineEdit::Normal,
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.
QgsMapLayerStyleManager * styleManager() const
Gets access to the layer's style manager.
#define QgsDebugError(str)