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 );
172 mStyleList =
new QListView(
this );
176 mStyleList->setModel( mModel );
178 mStyleList->setObjectName(
"mStyleList" );
179 mStyleList->setViewMode( QListView::ListMode );
180 mStyleList->setResizeMode( QListView::Adjust );
182 QToolBar *toolbar =
new QToolBar(
this );
183 QAction *addAction = toolbar->addAction( tr(
"Add" ) );
185 connect( addAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::addStyle );
186 QAction *removeAction = toolbar->addAction( tr(
"Remove Current" ) );
188 connect( removeAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::removeStyle );
189 QAction *loadFromFileAction = toolbar->addAction( tr(
"Load Style" ) );
191 connect( loadFromFileAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::loadStyle );
192 QAction *saveAction = toolbar->addAction( tr(
"Save Style" ) );
194 connect( saveAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::saveStyle );
195 QAction *saveAsDefaultAction = toolbar->addAction( tr(
"Save as Default" ) );
196 connect( saveAsDefaultAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::saveAsDefault );
197 QAction *loadDefaultAction = toolbar->addAction( tr(
"Restore Default" ) );
198 connect( loadDefaultAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::loadDefault );
200 setLayout(
new QVBoxLayout() );
201 layout()->setContentsMargins( 0, 0, 0, 0 );
202 layout()->addWidget( toolbar );
203 layout()->addWidget( mStyleList );
208 connect( mStyleList->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsMapLayerStyleManagerWidget::selectionChanged );
210 const QString active =
mLayer->styleManager()->currentStyle();
211 currentStyleChanged( active );
228 mStyleList->setModel(
nullptr );
229 mModel->deleteLater();
237 mStyleList->setModel( mModel );
238 currentStyleChanged(
mLayer->styleManager()->currentStyle() );
239 connect( mStyleList->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsMapLayerStyleManagerWidget::selectionChanged );
243void QgsMapLayerStyleManagerWidget::selectionChanged(
const QItemSelection &selected,
const QItemSelection & )
245 if ( !
mLayer || selected.empty() )
248 const QModelIndex index = selected.indexes().first();
249 if ( !index.isValid() )
252 const QString name = index.data().toString();
253 mLayer->styleManager()->setCurrentStyle( name );
256void QgsMapLayerStyleManagerWidget::currentStyleChanged(
const QString &name )
258 const QModelIndex listIndex = mModel->indexForName( name );
259 if ( listIndex.isValid() )
261 mStyleList->selectionModel()->select( listIndex, QItemSelectionModel::SelectionFlag::ClearAndSelect );
265void QgsMapLayerStyleManagerWidget::addStyle()
268 const QString text = QInputDialog::getText(
nullptr, tr(
"New Style" ), tr(
"Style name:" ), QLineEdit::Normal, u
"new style"_s, &ok );
269 if ( !ok || text.isEmpty() )
272 const bool res =
mLayer->styleManager()->addStyleFromLayer( text );
275 mLayer->styleManager()->setCurrentStyle( text );
283void QgsMapLayerStyleManagerWidget::removeStyle()
285 const QString current =
mLayer->styleManager()->currentStyle();
286 const bool res =
mLayer->styleManager()->removeStyle( current );
291void QgsMapLayerStyleManagerWidget::saveAsDefault()
330void QgsMapLayerStyleManagerWidget::loadDefault()
369void QgsMapLayerStyleManagerWidget::saveStyle()
408void 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.
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.
#define QgsDebugError(str)