18#include <QInputDialog>
39 mModel =
new QStandardItemModel(
this );
40 mStyleList =
new QListView(
this );
41 mStyleList->setModel( mModel );
42 mStyleList->setViewMode( QListView::ListMode );
43 mStyleList->setResizeMode( QListView::Adjust );
45 QToolBar *toolbar =
new QToolBar(
this );
46 QAction *addAction = toolbar->addAction( tr(
"Add" ) );
48 connect( addAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::addStyle );
49 QAction *removeAction = toolbar->addAction( tr(
"Remove Current" ) );
51 connect( removeAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::removeStyle );
52 QAction *loadFromFileAction = toolbar->addAction( tr(
"Load Style" ) );
54 connect( loadFromFileAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::loadStyle );
55 QAction *saveAction = toolbar->addAction( tr(
"Save Style" ) );
57 connect( saveAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::saveStyle );
58 QAction *saveAsDefaultAction = toolbar->addAction( tr(
"Save as Default" ) );
59 connect( saveAsDefaultAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::saveAsDefault );
60 QAction *loadDefaultAction = toolbar->addAction( tr(
"Restore Default" ) );
61 connect( loadDefaultAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::loadDefault );
66 connect( mStyleList, &QAbstractItemView::clicked,
this, &QgsMapLayerStyleManagerWidget::styleClicked );
68 setLayout(
new QVBoxLayout() );
69 layout()->setContentsMargins( 0, 0, 0, 0 );
70 layout()->addWidget( toolbar );
71 layout()->addWidget( mStyleList );
81 for (
const QString &styleName : styles )
83 QStandardItem *item =
new QStandardItem( styleName );
84 item->setData( styleName );
85 mModel->appendRow( item );
89 currentStyleChanged( active );
91 connect( mModel, &QStandardItemModel::itemChanged,
this, &QgsMapLayerStyleManagerWidget::renameStyle );
94void QgsMapLayerStyleManagerWidget::styleClicked(
const QModelIndex &index )
99 const QString name = index.data().toString();
103void QgsMapLayerStyleManagerWidget::currentStyleChanged(
const QString &name )
105 const QList<QStandardItem *> items = mModel->findItems( name );
106 if ( items.isEmpty() )
109 QStandardItem *item = items.at( 0 );
111 mStyleList->setCurrentIndex( item->index() );
114void QgsMapLayerStyleManagerWidget::styleAdded(
const QString &name )
117 QStandardItem *item =
new QStandardItem( name );
118 item->setData( name );
119 mModel->appendRow( item );
122void QgsMapLayerStyleManagerWidget::styleRemoved(
const QString &name )
124 const QList<QStandardItem *> items = mModel->findItems( name );
125 if ( items.isEmpty() )
128 QStandardItem *item = items.at( 0 );
129 mModel->removeRow( item->row() );
132void QgsMapLayerStyleManagerWidget::styleRenamed(
const QString &oldname,
const QString &newname )
134 const QList<QStandardItem *> items = mModel->findItems( oldname );
135 if ( items.isEmpty() )
138 QStandardItem *item = items.at( 0 );
139 item->setText( newname );
140 item->setData( newname );
143void QgsMapLayerStyleManagerWidget::addStyle()
146 const QString text = QInputDialog::getText(
nullptr, tr(
"New Style" ),
147 tr(
"Style name:" ), QLineEdit::Normal,
148 QStringLiteral(
"new style" ), &ok );
149 if ( !ok || text.isEmpty() )
163void QgsMapLayerStyleManagerWidget::removeStyle()
168 QgsDebugError( QStringLiteral(
"Failed to remove current style" ) );
171void QgsMapLayerStyleManagerWidget::renameStyle( QStandardItem *item )
173 const QString oldName = item->data().toString();
174 const QString newName = item->text();
175 item->setData( newName );
176 whileBlocking(
this )->mLayer->styleManager()->renameStyle( oldName, newName );
179void QgsMapLayerStyleManagerWidget::saveAsDefault()
217void QgsMapLayerStyleManagerWidget::loadDefault()
255void QgsMapLayerStyleManagerWidget::saveStyle()
293void 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)