32#include <QInputDialog>
38#include "moc_qgsmaplayerstylemanagerwidget.cpp"
40using namespace Qt::StringLiterals;
45 mModel =
new QStandardItemModel(
this );
46 mStyleList =
new QListView(
this );
47 mStyleList->setModel( mModel );
48 mStyleList->setViewMode( QListView::ListMode );
49 mStyleList->setResizeMode( QListView::Adjust );
51 QToolBar *toolbar =
new QToolBar(
this );
52 QAction *addAction = toolbar->addAction( tr(
"Add" ) );
54 connect( addAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::addStyle );
55 QAction *removeAction = toolbar->addAction( tr(
"Remove Current" ) );
57 connect( removeAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::removeStyle );
58 QAction *loadFromFileAction = toolbar->addAction( tr(
"Load Style" ) );
60 connect( loadFromFileAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::loadStyle );
61 QAction *saveAction = toolbar->addAction( tr(
"Save Style" ) );
63 connect( saveAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::saveStyle );
64 QAction *saveAsDefaultAction = toolbar->addAction( tr(
"Save as Default" ) );
65 connect( saveAsDefaultAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::saveAsDefault );
66 QAction *loadDefaultAction = toolbar->addAction( tr(
"Restore Default" ) );
67 connect( loadDefaultAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::loadDefault );
69 connect( mStyleList, &QAbstractItemView::clicked,
this, &QgsMapLayerStyleManagerWidget::styleClicked );
71 setLayout(
new QVBoxLayout() );
72 layout()->setContentsMargins( 0, 0, 0, 0 );
73 layout()->addWidget( toolbar );
74 layout()->addWidget( mStyleList );
83 const QStringList styles =
mLayer->styleManager()->styles();
84 for (
const QString &styleName : styles )
86 QStandardItem *item =
new QStandardItem( styleName );
87 item->setData( styleName );
88 mModel->appendRow( item );
91 const QString active =
mLayer->styleManager()->currentStyle();
92 currentStyleChanged( active );
94 connect( mModel, &QStandardItemModel::itemChanged,
this, &QgsMapLayerStyleManagerWidget::renameStyle );
97void QgsMapLayerStyleManagerWidget::styleClicked(
const QModelIndex &index )
102 const QString name = index.data().toString();
106void QgsMapLayerStyleManagerWidget::currentStyleChanged(
const QString &name )
108 const QList<QStandardItem *> items = mModel->findItems( name );
109 if ( items.isEmpty() )
112 QStandardItem *item = items.at( 0 );
114 mStyleList->setCurrentIndex( item->index() );
117void QgsMapLayerStyleManagerWidget::styleAdded(
const QString &name )
120 QStandardItem *item =
new QStandardItem( name );
121 item->setData( name );
122 mModel->appendRow( item );
125void QgsMapLayerStyleManagerWidget::styleRemoved(
const QString &name )
127 const QList<QStandardItem *> items = mModel->findItems( name );
128 if ( items.isEmpty() )
131 QStandardItem *item = items.at( 0 );
132 mModel->removeRow( item->row() );
135void QgsMapLayerStyleManagerWidget::styleRenamed(
const QString &oldname,
const QString &newname )
137 const QList<QStandardItem *> items = mModel->findItems( oldname );
138 if ( items.isEmpty() )
141 QStandardItem *item = items.at( 0 );
142 item->setText( newname );
143 item->setData( newname );
146void QgsMapLayerStyleManagerWidget::addStyle()
149 const QString text = QInputDialog::getText(
nullptr, tr(
"New Style" ), tr(
"Style name:" ), QLineEdit::Normal, u
"new style"_s, &ok );
150 if ( !ok || text.isEmpty() )
153 const bool res =
mLayer->styleManager()->addStyleFromLayer( text );
156 mLayer->styleManager()->setCurrentStyle( text );
164void QgsMapLayerStyleManagerWidget::removeStyle()
166 const QString current =
mLayer->styleManager()->currentStyle();
167 const bool res =
mLayer->styleManager()->removeStyle( current );
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()
213void QgsMapLayerStyleManagerWidget::loadDefault()
246void QgsMapLayerStyleManagerWidget::saveStyle()
279void 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.
Map canvas is a class for displaying all GIS data types on a canvas.
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.
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.
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)