36#include <QInputDialog>
42#include "moc_qgsmaplayerstylemanagerwidget.cpp"
44using namespace Qt::StringLiterals;
49 mModel =
new QStandardItemModel(
this );
50 mStyleList =
new QListView(
this );
51 mStyleList->setModel( mModel );
52 mStyleList->setViewMode( QListView::ListMode );
53 mStyleList->setResizeMode( QListView::Adjust );
55 QToolBar *toolbar =
new QToolBar(
this );
56 QAction *addAction = toolbar->addAction( tr(
"Add" ) );
58 connect( addAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::addStyle );
59 QAction *removeAction = toolbar->addAction( tr(
"Remove Current" ) );
61 connect( removeAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::removeStyle );
62 QAction *loadFromFileAction = toolbar->addAction( tr(
"Load Style" ) );
64 connect( loadFromFileAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::loadStyle );
65 QAction *saveAction = toolbar->addAction( tr(
"Save Style" ) );
67 connect( saveAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::saveStyle );
68 QAction *saveAsDefaultAction = toolbar->addAction( tr(
"Save as Default" ) );
69 connect( saveAsDefaultAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::saveAsDefault );
70 QAction *loadDefaultAction = toolbar->addAction( tr(
"Restore Default" ) );
71 connect( loadDefaultAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::loadDefault );
73 connect( mStyleList, &QAbstractItemView::clicked,
this, &QgsMapLayerStyleManagerWidget::styleClicked );
75 setLayout(
new QVBoxLayout() );
76 layout()->setContentsMargins( 0, 0, 0, 0 );
77 layout()->addWidget( toolbar );
78 layout()->addWidget( mStyleList );
87 const QStringList styles =
mLayer->styleManager()->styles();
88 for (
const QString &styleName : styles )
90 QStandardItem *item =
new QStandardItem( styleName );
91 item->setData( styleName );
92 mModel->appendRow( item );
95 const QString active =
mLayer->styleManager()->currentStyle();
96 currentStyleChanged( active );
98 connect( mModel, &QStandardItemModel::itemChanged,
this, &QgsMapLayerStyleManagerWidget::renameStyle );
101void QgsMapLayerStyleManagerWidget::styleClicked(
const QModelIndex &index )
106 const QString name = index.data().toString();
110void QgsMapLayerStyleManagerWidget::currentStyleChanged(
const QString &name )
112 const QList<QStandardItem *> items = mModel->findItems( name );
113 if ( items.isEmpty() )
116 QStandardItem *item = items.at( 0 );
118 mStyleList->setCurrentIndex( item->index() );
121void QgsMapLayerStyleManagerWidget::styleAdded(
const QString &name )
124 QStandardItem *item =
new QStandardItem( name );
125 item->setData( name );
126 mModel->appendRow( item );
129void QgsMapLayerStyleManagerWidget::styleRemoved(
const QString &name )
131 const QList<QStandardItem *> items = mModel->findItems( name );
132 if ( items.isEmpty() )
135 QStandardItem *item = items.at( 0 );
136 mModel->removeRow( item->row() );
139void QgsMapLayerStyleManagerWidget::styleRenamed(
const QString &oldname,
const QString &newname )
141 const QList<QStandardItem *> items = mModel->findItems( oldname );
142 if ( items.isEmpty() )
145 QStandardItem *item = items.at( 0 );
146 item->setText( newname );
147 item->setData( newname );
150void QgsMapLayerStyleManagerWidget::addStyle()
153 const QString text = QInputDialog::getText(
nullptr, tr(
"New Style" ), tr(
"Style name:" ), QLineEdit::Normal, u
"new style"_s, &ok );
154 if ( !ok || text.isEmpty() )
157 const bool res =
mLayer->styleManager()->addStyleFromLayer( text );
160 mLayer->styleManager()->setCurrentStyle( text );
168void QgsMapLayerStyleManagerWidget::removeStyle()
170 const QString current =
mLayer->styleManager()->currentStyle();
171 const bool res =
mLayer->styleManager()->removeStyle( current );
176void QgsMapLayerStyleManagerWidget::renameStyle( QStandardItem *item )
178 const QString oldName = item->data().toString();
179 const QString newName = item->text();
180 item->setData( newName );
181 whileBlocking(
this )->mLayer->styleManager()->renameStyle( oldName, newName );
184void QgsMapLayerStyleManagerWidget::saveAsDefault()
223void QgsMapLayerStyleManagerWidget::loadDefault()
262void QgsMapLayerStyleManagerWidget::saveStyle()
301void 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)