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 )
97 void QgsMapLayerStyleGuiUtils::addStyle()
99 QAction *a = qobject_cast<QAction *>( sender() );
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() )
125 void QgsMapLayerStyleGuiUtils::useStyle()
127 QAction *a = qobject_cast<QAction *>( sender() );
133 QString name = a->text();
138 QgsDebugMsg(
"Failed to set current style: " + name );
143 void QgsMapLayerStyleGuiUtils::removeStyle()
145 QAction *a = qobject_cast<QAction *>( sender() );
155 QgsDebugMsg( QStringLiteral(
"Failed to remove current style" ) );
160 void QgsMapLayerStyleGuiUtils::renameStyle()
162 QAction *a = qobject_cast<QAction *>( sender() );
172 QString text = QInputDialog::getText(
nullptr, tr(
"Rename Style" ),
173 tr(
"Style name:" ), QLineEdit::Normal,
Base class for all map layer types.
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's style manager.
QgsMapLayerStyleManager * styleManager() const
Gets access to the layer'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.