19 #include <QInputDialog>
21 #include <QActionGroup>
35 QAction *QgsMapLayerStyleGuiUtils::actionAddStyle(
QgsMapLayer *layer, QObject *parent )
37 QAction *a =
new QAction( tr(
"Add…" ), parent );
38 a->setData( QVariant::fromValue<QObject *>( layer ) );
39 connect( a, &QAction::triggered,
this, &QgsMapLayerStyleGuiUtils::addStyle );
43 QAction *QgsMapLayerStyleGuiUtils::actionRemoveStyle(
QgsMapLayer *layer, QObject *parent )
45 QAction *a =
new QAction( tr(
"Remove Current" ), parent );
46 connect( a, &QAction::triggered,
this, &QgsMapLayerStyleGuiUtils::removeStyle );
47 a->setData( QVariant::fromValue<QObject *>( layer ) );
52 QAction *QgsMapLayerStyleGuiUtils::actionRenameStyle(
QgsMapLayer *layer, QObject *parent )
54 QAction *a =
new QAction( tr(
"Rename Current…" ), parent );
55 connect( a, &QAction::triggered,
this, &QgsMapLayerStyleGuiUtils::renameStyle );
56 a->setData( QVariant::fromValue<QObject *>( layer ) );
60 QList<QAction *> QgsMapLayerStyleGuiUtils::actionsUseStyle(
QgsMapLayer *layer, QObject *parent )
63 const bool onlyOneStyle = mgr->
styles().count() == 1;
65 QList<QAction *> actions;
66 QActionGroup *styleGroup =
new QActionGroup( parent );
67 const auto constStyles = mgr->
styles();
68 for (
const QString &name : constStyles )
71 QAction *actionUse =
new QAction( name, styleGroup );
72 connect( actionUse, &QAction::triggered,
this, &QgsMapLayerStyleGuiUtils::useStyle );
73 actionUse->setCheckable(
true );
74 actionUse->setChecked( active );
75 actionUse->setEnabled( !onlyOneStyle );
77 actionUse->setData( QVariant::fromValue<QObject *>( layer ) );
88 m->addAction( actionAddStyle( layer, m ) );
90 m->addAction( actionRemoveStyle( layer, m ) );
91 m->addAction( actionRenameStyle( layer, m ) );
93 const auto actions {actionsUseStyle( layer, m )};
94 for ( QAction *a : actions )
104 bool gotFirstSeparator =
false;
105 QList<QAction *> actions = m->actions();
106 for (
int i = 0; i < actions.count(); ++i )
108 if ( actions[i]->isSeparator() )
110 if ( gotFirstSeparator )
113 while ( actions.count() != i )
114 delete actions.takeAt( i );
118 gotFirstSeparator =
true;
124 void 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" ),
135 tr(
"Style name:" ), QLineEdit::Normal,
136 QStringLiteral(
"new style" ), &ok );
137 if ( !ok || text.isEmpty() )
152 void QgsMapLayerStyleGuiUtils::useStyle()
154 QAction *a = qobject_cast<QAction *>( sender() );
157 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
160 const QString name = a->text();
165 QgsDebugMsg(
"Failed to set current style: " + name );
170 void QgsMapLayerStyleGuiUtils::removeStyle()
172 QAction *a = qobject_cast<QAction *>( sender() );
175 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
182 QgsDebugMsg( QStringLiteral(
"Failed to remove current style" ) );
187 void QgsMapLayerStyleGuiUtils::renameStyle()
189 QAction *a = qobject_cast<QAction *>( sender() );
192 QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
199 const QString text = QInputDialog::getText(
nullptr, tr(
"Rename Style" ),
200 tr(
"Style name:" ), QLineEdit::Normal,