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" ),
148 tr(
"Style name:" ), QLineEdit::Normal,
149 QStringLiteral(
"new style" ), &ok );
150 if ( !ok || text.isEmpty() )
164void QgsMapLayerStyleManagerWidget::removeStyle()
169 QgsDebugError( QStringLiteral(
"Failed to remove current style" ) );
172void QgsMapLayerStyleManagerWidget::renameStyle( QStandardItem *item )
174 const QString oldName = item->data().toString();
175 const QString newName = item->text();
176 item->setData( newName );
177 whileBlocking(
this )->mLayer->styleManager()->renameStyle( oldName, newName );
180void QgsMapLayerStyleManagerWidget::saveAsDefault()
218void QgsMapLayerStyleManagerWidget::loadDefault()
256void QgsMapLayerStyleManagerWidget::saveStyle()
294void 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.
Property sheet for a mesh map layer.
Property sheet for a raster map layer.
Vectortile layer properties dialog.
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)