19 #include <QInputDialog>
34 QAction *QgsMapLayerStyleGuiUtils::actionAddStyle(
QgsMapLayer *layer, QObject *parent )
36 QAction *a =
new QAction( tr(
"Add…" ), parent );
37 a->setData( QVariant::fromValue<QObject *>( layer ) );
38 connect( a, &QAction::triggered,
this, &QgsMapLayerStyleGuiUtils::addStyle );
42 QAction *QgsMapLayerStyleGuiUtils::actionRemoveStyle(
QgsMapLayer *layer, QObject *parent )
44 QAction *a =
new QAction( tr(
"Remove Current" ), parent );
45 connect( a, &QAction::triggered,
this, &QgsMapLayerStyleGuiUtils::removeStyle );
46 a->setData( QVariant::fromValue<QObject *>( layer ) );
51 QAction *QgsMapLayerStyleGuiUtils::actionRenameStyle(
QgsMapLayer *layer, QObject *parent )
53 QAction *a =
new QAction( tr(
"Rename Current…" ), parent );
54 connect( a, &QAction::triggered,
this, &QgsMapLayerStyleGuiUtils::renameStyle );
55 a->setData( QVariant::fromValue<QObject *>( layer ) );
59 QList<QAction *> QgsMapLayerStyleGuiUtils::actionsUseStyle(
QgsMapLayer *layer, QObject *parent )
62 bool onlyOneStyle = mgr->
styles().count() == 1;
64 QList<QAction *> actions;
65 QActionGroup *styleGroup =
new QActionGroup( parent );
66 const auto constStyles = mgr->
styles();
67 for (
const QString &name : constStyles )
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 );
76 actionUse->setData( QVariant::fromValue<QObject *>( layer ) );
87 m->addAction( actionAddStyle( layer, m ) );
89 m->addAction( actionRemoveStyle( layer, m ) );
90 m->addAction( actionRenameStyle( layer, m ) );
92 const auto actions {actionsUseStyle( layer, m )};
93 for ( QAction *a : actions )
103 bool gotFirstSeparator =
false;
104 QList<QAction *> actions = m->actions();
105 for (
int i = 0; i < actions.count(); ++i )
107 if ( actions[i]->isSeparator() )
109 if ( gotFirstSeparator )
112 while ( actions.count() != i )
113 delete actions.takeAt( i );
117 gotFirstSeparator =
true;
123 void QgsMapLayerStyleGuiUtils::addStyle()
125 QAction *a = qobject_cast<QAction *>( sender() );
128 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
133 QString text = QInputDialog::getText(
nullptr, tr(
"New Style" ),
134 tr(
"Style name:" ), QLineEdit::Normal,
135 QStringLiteral(
"new style" ), &ok );
136 if ( !ok || text.isEmpty() )
151 void QgsMapLayerStyleGuiUtils::useStyle()
153 QAction *a = qobject_cast<QAction *>( sender() );
156 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
159 QString name = a->text();
164 QgsDebugMsg(
"Failed to set current style: " + name );
169 void QgsMapLayerStyleGuiUtils::removeStyle()
171 QAction *a = qobject_cast<QAction *>( sender() );
174 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
181 QgsDebugMsg( QStringLiteral(
"Failed to remove current style" ) );
186 void QgsMapLayerStyleGuiUtils::renameStyle()
188 QAction *a = qobject_cast<QAction *>( sender() );
191 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
198 QString text = QInputDialog::getText(
nullptr, tr(
"Rename Style" ),
199 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.