18#include <QInputDialog> 
   23#include "moc_qgsmaplayerstylemanagerwidget.cpp" 
   40  mModel = 
new QStandardItemModel( 
this );
 
   41  mStyleList = 
new QListView( 
this );
 
   42  mStyleList->setModel( mModel );
 
   43  mStyleList->setViewMode( QListView::ListMode );
 
   44  mStyleList->setResizeMode( QListView::Adjust );
 
   46  QToolBar *toolbar = 
new QToolBar( 
this );
 
   47  QAction *addAction = toolbar->addAction( tr( 
"Add" ) );
 
   49  connect( addAction, &QAction::triggered, 
this, &QgsMapLayerStyleManagerWidget::addStyle );
 
   50  QAction *removeAction = toolbar->addAction( tr( 
"Remove Current" ) );
 
   52  connect( removeAction, &QAction::triggered, 
this, &QgsMapLayerStyleManagerWidget::removeStyle );
 
   53  QAction *loadFromFileAction = toolbar->addAction( tr( 
"Load Style" ) );
 
   55  connect( loadFromFileAction, &QAction::triggered, 
this, &QgsMapLayerStyleManagerWidget::loadStyle );
 
   56  QAction *saveAction = toolbar->addAction( tr( 
"Save Style" ) );
 
   58  connect( saveAction, &QAction::triggered, 
this, &QgsMapLayerStyleManagerWidget::saveStyle );
 
   59  QAction *saveAsDefaultAction = toolbar->addAction( tr( 
"Save as Default" ) );
 
   60  connect( saveAsDefaultAction, &QAction::triggered, 
this, &QgsMapLayerStyleManagerWidget::saveAsDefault );
 
   61  QAction *loadDefaultAction = toolbar->addAction( tr( 
"Restore Default" ) );
 
   62  connect( loadDefaultAction, &QAction::triggered, 
this, &QgsMapLayerStyleManagerWidget::loadDefault );
 
   67  connect( mStyleList, &QAbstractItemView::clicked, 
this, &QgsMapLayerStyleManagerWidget::styleClicked );
 
   69  setLayout( 
new QVBoxLayout() );
 
   70  layout()->setContentsMargins( 0, 0, 0, 0 );
 
   71  layout()->addWidget( toolbar );
 
   72  layout()->addWidget( mStyleList );
 
   82  for ( 
const QString &styleName : styles )
 
   84    QStandardItem *item = 
new QStandardItem( styleName );
 
   85    item->setData( styleName );
 
   86    mModel->appendRow( item );
 
   90  currentStyleChanged( active );
 
   92  connect( mModel, &QStandardItemModel::itemChanged, 
this, &QgsMapLayerStyleManagerWidget::renameStyle );
 
 
   95void QgsMapLayerStyleManagerWidget::styleClicked( 
const QModelIndex &index )
 
  100  const QString name = index.data().toString();
 
  104void QgsMapLayerStyleManagerWidget::currentStyleChanged( 
const QString &name )
 
  106  const QList<QStandardItem *> items = mModel->findItems( name );
 
  107  if ( items.isEmpty() )
 
  110  QStandardItem *item = items.at( 0 );
 
  112  mStyleList->setCurrentIndex( item->index() );
 
  115void QgsMapLayerStyleManagerWidget::styleAdded( 
const QString &name )
 
  118  QStandardItem *item = 
new QStandardItem( name );
 
  119  item->setData( name );
 
  120  mModel->appendRow( item );
 
  123void QgsMapLayerStyleManagerWidget::styleRemoved( 
const QString &name )
 
  125  const QList<QStandardItem *> items = mModel->findItems( name );
 
  126  if ( items.isEmpty() )
 
  129  QStandardItem *item = items.at( 0 );
 
  130  mModel->removeRow( item->row() );
 
  133void QgsMapLayerStyleManagerWidget::styleRenamed( 
const QString &oldname, 
const QString &newname )
 
  135  const QList<QStandardItem *> items = mModel->findItems( oldname );
 
  136  if ( items.isEmpty() )
 
  139  QStandardItem *item = items.at( 0 );
 
  140  item->setText( newname );
 
  141  item->setData( newname );
 
  144void QgsMapLayerStyleManagerWidget::addStyle()
 
  147  const QString text = QInputDialog::getText( 
nullptr, tr( 
"New Style" ), tr( 
"Style name:" ), QLineEdit::Normal, QStringLiteral( 
"new style" ), &ok );
 
  148  if ( !ok || text.isEmpty() )
 
  162void QgsMapLayerStyleManagerWidget::removeStyle()
 
  167    QgsDebugError( QStringLiteral( 
"Failed to remove current style" ) );
 
  170void QgsMapLayerStyleManagerWidget::renameStyle( QStandardItem *item )
 
  172  const QString oldName = item->data().toString();
 
  173  const QString newName = item->text();
 
  174  item->setData( newName );
 
  175  whileBlocking( 
this )->mLayer->styleManager()->renameStyle( oldName, newName );
 
  178void QgsMapLayerStyleManagerWidget::saveAsDefault()
 
  211void QgsMapLayerStyleManagerWidget::loadDefault()
 
  244void QgsMapLayerStyleManagerWidget::saveStyle()
 
  277void QgsMapLayerStyleManagerWidget::loadStyle()
 
@ Group
Composite group layer. Added in QGIS 3.24.
 
@ Plugin
Plugin based layer.
 
@ TiledScene
Tiled scene layer. Added in QGIS 3.34.
 
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
 
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
 
@ Mesh
Mesh layer. Added in QGIS 3.2.
 
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
 
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
 
void saveStyleAsDefault()
Saves the current layer style as the default for the layer.
 
void loadDefaultStyle()
Reloads the default style for the layer.
 
void saveStyleAs()
Saves a style when appriate button is pressed.
 
void loadStyle()
Triggers a dialog to load a saved style.
 
void saveStyleToFile()
Allows the user to save the layer's style to a file.
 
void saveDefaultStyle()
Saves the default style when appropriate button is pressed.
 
void loadStyleFromFile()
Allows the user to load layer style from a file.
 
Map canvas is a class for displaying all GIS data types on a canvas.
 
QgsMessageBar * messageBar() const
Returns the message bar associated with the widget.
 
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.
 
void styleAdded(const QString &name)
Emitted when a new style has been added.
 
void styleRenamed(const QString &oldName, const QString &newName)
Emitted when a style has been renamed.
 
bool addStyleFromLayer(const QString &name)
Add style by cloning the current one.
 
void currentStyleChanged(const QString ¤tName)
Emitted when the current style has been changed.
 
void styleRemoved(const QString &name)
Emitted when a style has been removed.
 
Base class for all map layer types.
 
QgsMapLayerStyleManager * styleManager() const
Gets access to the layer's style manager.
 
A dialog for configuring mesh layer properties.
 
A dialog for raster layer properties.
 
Layer properties dialog for vector layers.
 
Layer properties dialog for vector tile layers.
 
void loadStyle()
Loads a saved style when appropriate button is pressed.
 
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
 
#define QgsDebugMsgLevel(str, level)
 
#define QgsDebugError(str)