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,