19#include <QInputDialog> 
   21#include <QActionGroup> 
   35QAction *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 );
 
   43QAction *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 ) );
 
   52QAction *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 ) );
 
   60QList<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;
 
 
  124void 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() )
 
  152void 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();
 
  170void QgsMapLayerStyleGuiUtils::removeStyle()
 
  172  QAction *a = qobject_cast<QAction *>( sender() );
 
  175  QgsMapLayer *layer = qobject_cast<QgsMapLayer *>( a->data().value<QObject *>() );
 
  182    QgsDebugError( QStringLiteral( 
"Failed to remove current style" ) );
 
  187void 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,
 
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.
 
#define QgsDebugError(str)