36#include <QInputDialog>
42#include "moc_qgsmaplayerstylemanagerwidget.cpp"
44using namespace Qt::StringLiterals;
50 : QAbstractListModel( parent )
59 mStyleNames = mManager->styles();
65 if ( parent.isValid() )
68 return static_cast< int >( mStyleNames.size() );
73 if ( index.row() < 0 || index.row() >=
rowCount( QModelIndex() ) )
81 return mStyleNames.at( index.row() );
91 if ( !mManager || !index.isValid() || role != Qt::EditRole )
95 if ( index.row() >= mStyleNames.size() )
100 if ( value.toString().isEmpty() )
104 const bool changed = mStyleNames.at( index.row() ) != value.toString();
109 if ( mStyleNames.contains( value.toString() ) )
112 mManager->renameStyle( mStyleNames[index.row()], value.toString() );
113 mStyleNames[index.row()] = value.toString();
119 Qt::ItemFlags
flags = QAbstractListModel::flags( index );
120 if ( index.isValid() )
122 return flags | Qt::ItemIsEditable;
132 const int row =
static_cast< int >( mStyleNames.indexOf( name ) );
135 return index( row, 0, QModelIndex() );
137 return QModelIndex();
140void QgsMapLayerStyleModel::styleAdded(
const QString &name )
142 beginInsertRows( QModelIndex(),
static_cast< int >( mStyleNames.size() ),
static_cast< int >( mStyleNames.size() ) );
147void QgsMapLayerStyleModel::styleRemoved(
const QString &name )
149 const int row =
static_cast< int >( mStyleNames.indexOf( name ) );
152 beginRemoveRows( QModelIndex(), row, row );
153 mStyleNames.remove( row );
158void QgsMapLayerStyleModel::styleRenamed(
const QString &oldname,
const QString &newname )
161 mStyleNames[styleIndex.row()] = newname;
162 emit dataChanged( styleIndex, styleIndex );
173 mStyleList =
new QListView(
this );
174 mStyleList->setModel( mModel );
175 mStyleList->setViewMode( QListView::ListMode );
176 mStyleList->setResizeMode( QListView::Adjust );
178 QToolBar *toolbar =
new QToolBar(
this );
179 QAction *addAction = toolbar->addAction( tr(
"Add" ) );
181 connect( addAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::addStyle );
182 QAction *removeAction = toolbar->addAction( tr(
"Remove Current" ) );
184 connect( removeAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::removeStyle );
185 QAction *loadFromFileAction = toolbar->addAction( tr(
"Load Style" ) );
187 connect( loadFromFileAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::loadStyle );
188 QAction *saveAction = toolbar->addAction( tr(
"Save Style" ) );
190 connect( saveAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::saveStyle );
191 QAction *saveAsDefaultAction = toolbar->addAction( tr(
"Save as Default" ) );
192 connect( saveAsDefaultAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::saveAsDefault );
193 QAction *loadDefaultAction = toolbar->addAction( tr(
"Restore Default" ) );
194 connect( loadDefaultAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::loadDefault );
196 connect( mStyleList->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsMapLayerStyleManagerWidget::selectionChanged );
198 setLayout(
new QVBoxLayout() );
199 layout()->setContentsMargins( 0, 0, 0, 0 );
200 layout()->addWidget( toolbar );
201 layout()->addWidget( mStyleList );
205 const QString active =
mLayer->styleManager()->currentStyle();
206 currentStyleChanged( active );
209void QgsMapLayerStyleManagerWidget::selectionChanged(
const QItemSelection &selected,
const QItemSelection & )
211 if ( !
mLayer || selected.empty() )
214 const QModelIndex index = selected.indexes().first();
215 if ( !index.isValid() )
218 const QString name = index.data().toString();
222void QgsMapLayerStyleManagerWidget::currentStyleChanged(
const QString &name )
224 const QModelIndex listIndex = mModel->indexForName( name );
225 if ( listIndex.isValid() )
227 mStyleList->selectionModel()->select( listIndex, QItemSelectionModel::SelectionFlag::ClearAndSelect );
231void QgsMapLayerStyleManagerWidget::addStyle()
234 const QString text = QInputDialog::getText(
nullptr, tr(
"New Style" ), tr(
"Style name:" ), QLineEdit::Normal, u
"new style"_s, &ok );
235 if ( !ok || text.isEmpty() )
238 const bool res =
mLayer->styleManager()->addStyleFromLayer( text );
241 mLayer->styleManager()->setCurrentStyle( text );
249void QgsMapLayerStyleManagerWidget::removeStyle()
251 const QString current =
mLayer->styleManager()->currentStyle();
252 const bool res =
mLayer->styleManager()->removeStyle( current );
257void QgsMapLayerStyleManagerWidget::saveAsDefault()
296void QgsMapLayerStyleManagerWidget::loadDefault()
335void QgsMapLayerStyleManagerWidget::saveStyle()
374void 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.
Management of styles for use with one map layer.
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.
A model for display of styles from a QgsMapLayerStyleManager.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QModelIndex indexForName(const QString &name) const
Returns the model index corresponding to the specified style name.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
QgsMapLayerStyleModel(QgsMapLayerStyleManager *manager, QObject *parent=nullptr)
Constructor for QgsMapLayerStyleModel, showing styles from the specified manager.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Base class for all map layer types.
QgsMapLayerStyleManager * styleManager() const
Gets access to the layer's style manager.
#define QgsDebugError(str)