24#include <QActionGroup>
25#include <QInputDialog>
29#include "moc_qgsmaplayerstyleguiutils.cpp"
31using namespace Qt::StringLiterals;
39QAction *QgsMapLayerStyleGuiUtils::actionAddStyle(
QgsMapLayer *layer, QObject *parent )
41 QAction *a =
new QAction( tr(
"Add…" ), parent );
42 a->setData( QVariant::fromValue<QObject *>( layer ) );
43 connect( a, &QAction::triggered,
this, &QgsMapLayerStyleGuiUtils::addStyle );
47QAction *QgsMapLayerStyleGuiUtils::actionRemoveStyle(
QgsMapLayer *layer, QObject *parent )
49 QAction *a =
new QAction( tr(
"Remove Current" ), parent );
50 connect( a, &QAction::triggered,
this, &QgsMapLayerStyleGuiUtils::removeStyle );
51 a->setData( QVariant::fromValue<QObject *>( layer ) );
56QAction *QgsMapLayerStyleGuiUtils::actionRenameStyle(
QgsMapLayer *layer, QObject *parent )
58 QAction *a =
new QAction( tr(
"Rename Current…" ), parent );
59 connect( a, &QAction::triggered,
this, &QgsMapLayerStyleGuiUtils::renameStyle );
60 a->setData( QVariant::fromValue<QObject *>( layer ) );
64QList<QAction *> QgsMapLayerStyleGuiUtils::actionsUseStyle(
QgsMapLayer *layer, QObject *parent )
67 const bool onlyOneStyle = mgr->
styles().count() == 1;
69 QList<QAction *> actions;
70 QActionGroup *styleGroup =
new QActionGroup( parent );
71 const auto constStyles = mgr->
styles();
72 for (
const QString &name : constStyles )
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 );
81 actionUse->setData( QVariant::fromValue<QObject *>( layer ) );
92 m->addAction( actionAddStyle( layer, m ) );
94 m->addAction( actionRemoveStyle( layer, m ) );
95 m->addAction( actionRenameStyle( layer, m ) );
97 const auto actions { actionsUseStyle( layer, m ) };
98 for ( QAction *a : actions )
108 bool gotFirstSeparator =
false;
109 QList<QAction *> actions = m->actions();
110 for (
int i = 0; i < actions.count(); ++i )
112 if ( actions[i]->isSeparator() )
114 if ( gotFirstSeparator )
117 while ( actions.count() != i )
118 delete actions.takeAt( i );
122 gotFirstSeparator =
true;
127void QgsMapLayerStyleGuiUtils::addStyle()
129 QAction *a = qobject_cast<QAction *>( sender() );
132 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
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() )
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 *>() );
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" ), 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
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)