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 *saveAsDefaultAction = toolbar->addAction( tr(
"Save as Default" ) );
56 connect( saveAsDefaultAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::saveAsDefault );
57 QAction *loadDefaultAction = toolbar->addAction( tr(
"Restore Default" ) );
58 connect( loadDefaultAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::loadDefault );
68 connect( mStyleList, &QAbstractItemView::clicked,
this, &QgsMapLayerStyleManagerWidget::styleClicked );
70 setLayout(
new QVBoxLayout() );
71 layout()->setContentsMargins( 0, 0, 0, 0 );
72 layout()->addWidget( toolbar );
73 layout()->addWidget( mStyleList );
83 for (
const QString &styleName : styles )
85 QStandardItem *item =
new QStandardItem( styleName );
86 item->setData( styleName );
87 mModel->appendRow( item );
91 currentStyleChanged( active );
93 connect( mModel, &QStandardItemModel::itemChanged,
this, &QgsMapLayerStyleManagerWidget::renameStyle );
96void QgsMapLayerStyleManagerWidget::styleClicked(
const QModelIndex &index )
101 const QString name = index.data().toString();
105void QgsMapLayerStyleManagerWidget::currentStyleChanged(
const QString &name )
107 const QList<QStandardItem *> items = mModel->findItems( name );
108 if ( items.isEmpty() )
111 QStandardItem *item = items.at( 0 );
113 mStyleList->setCurrentIndex( item->index() );
116void QgsMapLayerStyleManagerWidget::styleAdded(
const QString &name )
119 QStandardItem *item =
new QStandardItem( name );
120 item->setData( name );
121 mModel->appendRow( item );
124void QgsMapLayerStyleManagerWidget::styleRemoved(
const QString &name )
126 const QList<QStandardItem *> items = mModel->findItems( name );
127 if ( items.isEmpty() )
130 QStandardItem *item = items.at( 0 );
131 mModel->removeRow( item->row() );
134void QgsMapLayerStyleManagerWidget::styleRenamed(
const QString &oldname,
const QString &newname )
136 const QList<QStandardItem *> items = mModel->findItems( oldname );
137 if ( items.isEmpty() )
140 QStandardItem *item = items.at( 0 );
141 item->setText( newname );
142 item->setData( newname );
145void QgsMapLayerStyleManagerWidget::addStyle()
148 const QString text = QInputDialog::getText(
nullptr, tr(
"New Style" ),
149 tr(
"Style name:" ), QLineEdit::Normal,
150 QStringLiteral(
"new style" ), &ok );
151 if ( !ok || text.isEmpty() )
165void QgsMapLayerStyleManagerWidget::removeStyle()
168 const QList<QStandardItem *> items = mModel->findItems( current );
169 if ( items.isEmpty() )
172 QStandardItem *item = items.at( 0 );
176 mModel->removeRow( item->row() );
180 QgsDebugMsg( QStringLiteral(
"Failed to remove current style" ) );
185void QgsMapLayerStyleManagerWidget::renameStyle( QStandardItem *item )
187 const QString oldName = item->data().toString();
188 const QString newName = item->text();
189 item->setData( newName );
190 whileBlocking(
this )->mLayer->styleManager()->renameStyle( oldName, newName );
193void QgsMapLayerStyleManagerWidget::saveAsDefault()
199 if ( layer->dataProvider()->isSaveAndLoadStyleToDatabaseSupported() )
201 QMessageBox askToUser;
202 askToUser.setWindowTitle( tr(
"Save Style" ) );
203 askToUser.setText( tr(
"Save default style to: " ) );
204 askToUser.setIcon( QMessageBox::Question );
205 askToUser.addButton( tr(
"Cancel" ), QMessageBox::RejectRole );
206 askToUser.addButton( tr(
"Local Database" ), QMessageBox::NoRole );
207 askToUser.addButton( tr(
"Datasource Database" ), QMessageBox::YesRole );
209 switch ( askToUser.exec() )
215 QString errorMessage;
218 if ( QMessageBox::question(
nullptr, tr(
"Save style in database" ),
219 tr(
"A matching style already exists in the database for this layer. Do you want to overwrite it?" ),
220 QMessageBox::Yes | QMessageBox::No ) == QMessageBox::No )
225 else if ( !errorMessage.isEmpty() )
227 QMessageBox::warning(
nullptr, tr(
"Save style in database" ),
232 layer->saveStyleToDatabase( QString(), QString(),
true, QString(), errorMsg );
233 if ( errorMsg.isNull() )
245 bool defaultSavedFlag =
false;
251 if ( !defaultSavedFlag )
253 QMessageBox::warning(
this, tr(
"Default Style" ), errorMsg );
258void QgsMapLayerStyleManagerWidget::loadDefault()
261 bool defaultLoadedFlag =
false;
265 if ( layer->dataProvider()->isSaveAndLoadStyleToDatabaseSupported() )
267 QMessageBox askToUser;
268 askToUser.setWindowTitle( tr(
"Load Style" ) );
269 askToUser.setText( tr(
"Load default style from: " ) );
270 askToUser.setIcon( QMessageBox::Question );
271 askToUser.addButton( tr(
"Cancel" ), QMessageBox::RejectRole );
272 askToUser.addButton( tr(
"Local Database" ), QMessageBox::NoRole );
273 askToUser.addButton( tr(
"Datasource Database" ), QMessageBox::YesRole );
275 switch ( askToUser.exec() )
280 msg = layer->loadNamedStyle(
mLayer->
styleURI(), defaultLoadedFlag );
281 if ( !defaultLoadedFlag )
284 QMessageBox::information(
this, tr(
"Default Style" ), msg );
286 if ( msg.compare( tr(
"Loaded from Provider" ) ) )
288 QMessageBox::information(
this, tr(
"Default Style" ),
289 tr(
"No default style was found for this layer" ) );
301 myMessage = layer->loadNamedStyle(
mLayer->
styleURI(), defaultLoadedFlag,
true );
305 myMessage = layer->loadNamedStyle(
mLayer->
styleURI(), defaultLoadedFlag );
312 if ( !defaultLoadedFlag )
315 QMessageBox::information(
this, tr(
"Default Style" ), myMessage );
324void QgsMapLayerStyleManagerWidget::saveStyle()
329void QgsMapLayerStyleManagerWidget::loadStyle()
332 const QString myLastUsedDir = myQSettings.
value( QStringLiteral(
"style/lastStyleDir" ), QDir::homePath() ).toString();
334 const QString myFileName = QFileDialog::getOpenFileName(
this, tr(
"Load layer properties from style file" ), myLastUsedDir,
335 tr(
"QGIS Layer Style File" ) +
" (*.qml);;" + tr(
"SLD File" ) +
" (*.sld)" );
336 if ( myFileName.isNull() )
342 bool defaultLoadedFlag =
false;
344 if ( myFileName.endsWith( QLatin1String(
".sld" ), Qt::CaseInsensitive ) )
354 if ( defaultLoadedFlag )
361 QMessageBox::warning(
this, tr(
"Load Style" ), myMessage );
364 const QFileInfo myFI( myFileName );
365 const QString myPath = myFI.path();
366 myQSettings.
setValue( QStringLiteral(
"style/lastStyleDir" ), myPath );
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.
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.
virtual QString loadSldStyle(const QString &uri, bool &resultFlag)
Attempts to style the layer using the formatting from an SLD type file.
virtual QString styleURI() const
Retrieve the style URI for this layer (either as a .qml file on disk or as a record in the users styl...
virtual QString loadNamedStyle(const QString &uri, bool &resultFlag, QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories)
Retrieve a named style for this layer if one exists (either as a .qml file on disk or as a record in ...
QgsMapLayerStyleManager * styleManager() const
Gets access to the layer's style manager.
virtual QString saveDefaultStyle(bool &resultFlag, StyleCategories categories)
Save the properties of this layer as the default style (either as a .qml file on disk or as a record ...
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
Represents a raster layer.
This class is a composition of two QSettings instances:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Represents a vector layer which manages a vector based data sets.
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.