16 #include <QVBoxLayout>
18 #include <QInputDialog>
19 #include <QMessageBox>
20 #include <QFileDialog>
38 mModel =
new QStandardItemModel(
this );
39 mStyleList =
new QListView(
this );
40 mStyleList->setModel( mModel );
41 mStyleList->setViewMode( QListView::ListMode );
42 mStyleList->setResizeMode( QListView::Adjust );
44 QToolBar *toolbar =
new QToolBar(
this );
45 QAction *addAction = toolbar->addAction( tr(
"Add" ) );
47 connect( addAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::addStyle );
48 QAction *removeAction = toolbar->addAction( tr(
"Remove Current" ) );
50 connect( removeAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::removeStyle );
51 QAction *loadFromFileAction = toolbar->addAction( tr(
"Load Style" ) );
53 connect( loadFromFileAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::loadStyle );
54 QAction *saveAsDefaultAction = toolbar->addAction( tr(
"Save as Default" ) );
55 connect( saveAsDefaultAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::saveAsDefault );
56 QAction *loadDefaultAction = toolbar->addAction( tr(
"Restore Default" ) );
57 connect( loadDefaultAction, &QAction::triggered,
this, &QgsMapLayerStyleManagerWidget::loadDefault );
67 connect( mStyleList, &QAbstractItemView::clicked,
this, &QgsMapLayerStyleManagerWidget::styleClicked );
69 setLayout(
new QVBoxLayout() );
70 layout()->setContentsMargins( 0, 0, 0, 0 );
71 layout()->addWidget( toolbar );
72 layout()->addWidget( mStyleList );
82 for (
const QString &styleName : styles )
84 QStandardItem *item =
new QStandardItem( styleName );
85 item->setData( styleName );
86 mModel->appendRow( item );
90 currentStyleChanged( active );
92 connect( mModel, &QStandardItemModel::itemChanged,
this, &QgsMapLayerStyleManagerWidget::renameStyle );
95 void QgsMapLayerStyleManagerWidget::styleClicked(
const QModelIndex &index )
100 const QString name = index.data().toString();
104 void QgsMapLayerStyleManagerWidget::currentStyleChanged(
const QString &name )
106 const QList<QStandardItem *> items = mModel->findItems( name );
107 if ( items.isEmpty() )
110 QStandardItem *item = items.at( 0 );
112 mStyleList->setCurrentIndex( item->index() );
115 void QgsMapLayerStyleManagerWidget::styleAdded(
const QString &name )
118 QStandardItem *item =
new QStandardItem( name );
119 item->setData( name );
120 mModel->appendRow( item );
123 void QgsMapLayerStyleManagerWidget::styleRemoved(
const QString &name )
125 const QList<QStandardItem *> items = mModel->findItems( name );
126 if ( items.isEmpty() )
129 QStandardItem *item = items.at( 0 );
130 mModel->removeRow( item->row() );
133 void QgsMapLayerStyleManagerWidget::styleRenamed(
const QString &oldname,
const QString &newname )
135 const QList<QStandardItem *> items = mModel->findItems( oldname );
136 if ( items.isEmpty() )
139 QStandardItem *item = items.at( 0 );
140 item->setText( newname );
141 item->setData( newname );
144 void QgsMapLayerStyleManagerWidget::addStyle()
147 const QString text = QInputDialog::getText(
nullptr, tr(
"New Style" ),
148 tr(
"Style name:" ), QLineEdit::Normal,
149 QStringLiteral(
"new style" ), &ok );
150 if ( !ok || text.isEmpty() )
164 void QgsMapLayerStyleManagerWidget::removeStyle()
167 const QList<QStandardItem *> items = mModel->findItems( current );
168 if ( items.isEmpty() )
171 QStandardItem *item = items.at( 0 );
175 mModel->removeRow( item->row() );
179 QgsDebugMsg( QStringLiteral(
"Failed to remove current style" ) );
184 void QgsMapLayerStyleManagerWidget::renameStyle( QStandardItem *item )
186 const QString oldName = item->data().toString();
187 const QString newName = item->text();
188 item->setData( newName );
189 whileBlocking(
this )->mLayer->styleManager()->renameStyle( oldName, newName );
192 void QgsMapLayerStyleManagerWidget::saveAsDefault()
198 if ( layer->dataProvider()->isSaveAndLoadStyleToDatabaseSupported() )
200 QMessageBox askToUser;
201 askToUser.setWindowTitle( tr(
"Save Style" ) );
202 askToUser.setText( tr(
"Save default style to: " ) );
203 askToUser.setIcon( QMessageBox::Question );
204 askToUser.addButton( tr(
"Cancel" ), QMessageBox::RejectRole );
205 askToUser.addButton( tr(
"Local Database" ), QMessageBox::NoRole );
206 askToUser.addButton( tr(
"Datasource Database" ), QMessageBox::YesRole );
208 switch ( askToUser.exec() )
213 layer->saveStyleToDatabase( QString(), QString(),
true, QString(), errorMsg );
214 if ( errorMsg.isNull() )
225 bool defaultSavedFlag =
false;
227 if ( !defaultSavedFlag )
229 QMessageBox::warning(
this, tr(
"Default Style" ), errorMsg );
234 void QgsMapLayerStyleManagerWidget::loadDefault()
237 bool defaultLoadedFlag =
false;
241 if ( layer->dataProvider()->isSaveAndLoadStyleToDatabaseSupported() )
243 QMessageBox askToUser;
244 askToUser.setWindowTitle( tr(
"Load Style" ) );
245 askToUser.setText( tr(
"Load default style from: " ) );
246 askToUser.setIcon( QMessageBox::Question );
247 askToUser.addButton( tr(
"Cancel" ), QMessageBox::RejectRole );
248 askToUser.addButton( tr(
"Local Database" ), QMessageBox::NoRole );
249 askToUser.addButton( tr(
"Datasource Database" ), QMessageBox::YesRole );
251 switch ( askToUser.exec() )
256 msg = layer->loadNamedStyle(
mLayer->
styleURI(), defaultLoadedFlag );
257 if ( !defaultLoadedFlag )
260 QMessageBox::information(
this, tr(
"Default Style" ), msg );
262 if ( msg.compare( tr(
"Loaded from Provider" ) ) )
264 QMessageBox::information(
this, tr(
"Default Style" ),
265 tr(
"No default style was found for this layer" ) );
277 myMessage = layer->loadNamedStyle(
mLayer->
styleURI(), defaultLoadedFlag,
true );
281 myMessage = layer->loadNamedStyle(
mLayer->
styleURI(), defaultLoadedFlag );
288 if ( !defaultLoadedFlag )
291 QMessageBox::information(
this, tr(
"Default Style" ), myMessage );
300 void QgsMapLayerStyleManagerWidget::saveStyle()
305 void QgsMapLayerStyleManagerWidget::loadStyle()
308 const QString myLastUsedDir = myQSettings.
value( QStringLiteral(
"style/lastStyleDir" ), QDir::homePath() ).toString();
310 const QString myFileName = QFileDialog::getOpenFileName(
this, tr(
"Load layer properties from style file" ), myLastUsedDir,
311 tr(
"QGIS Layer Style File" ) +
" (*.qml);;" + tr(
"SLD File" ) +
" (*.sld)" );
312 if ( myFileName.isNull() )
318 bool defaultLoadedFlag =
false;
320 if ( myFileName.endsWith( QLatin1String(
".sld" ), Qt::CaseInsensitive ) )
330 if ( defaultLoadedFlag )
337 QMessageBox::warning(
this, tr(
"Load Style" ), myMessage );
340 const QFileInfo myFI( myFileName );
341 const QString myPath = myFI.path();
342 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 saveDefaultStyle(bool &resultFlag)
Save the properties of this layer as the default style (either as a .qml file on disk or as a record ...
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.
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.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.