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;
124void QgsMapLayerStyleGuiUtils::addStyle()
126 QAction *a = qobject_cast<QAction *>( sender() );
129 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
134 const QString text = QInputDialog::getText(
nullptr, tr(
"New Style" ), tr(
"Style name:" ), QLineEdit::Normal, QStringLiteral(
"new style" ), &ok );
135 if ( !ok || text.isEmpty() )
150void QgsMapLayerStyleGuiUtils::useStyle()
152 QAction *a = qobject_cast<QAction *>( sender() );
155 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
158 const QString name = a->text();
168void QgsMapLayerStyleGuiUtils::removeStyle()
170 QAction *a = qobject_cast<QAction *>( sender() );
173 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
180 QgsDebugError( QStringLiteral(
"Failed to remove current style" ) );
185void QgsMapLayerStyleGuiUtils::renameStyle()
187 QAction *a = qobject_cast<QAction *>( sender() );
190 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
197 const QString text = QInputDialog::getText(
nullptr, tr(
"Rename Style" ), tr(
"Style name:" ), QLineEdit::Normal, name, &ok );
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)