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 );
83 QString stylename = name;
84 QStandardItem *item =
new QStandardItem( stylename );
85 mModel->appendRow( item );
89 currentStyleChanged( active );
92 void QgsMapLayerStyleManagerWidget::styleClicked(
const QModelIndex &index )
97 QString name = index.data().toString();
101 void QgsMapLayerStyleManagerWidget::currentStyleChanged(
const QString &name )
103 QList<QStandardItem *> items = mModel->findItems( name );
104 if ( items.isEmpty() )
107 QStandardItem *item = items.at( 0 );
109 mStyleList->setCurrentIndex( item->index() );
112 void QgsMapLayerStyleManagerWidget::styleAdded(
const QString &name )
115 QStandardItem *item =
new QStandardItem( name );
116 mModel->appendRow( item );
119 void QgsMapLayerStyleManagerWidget::styleRemoved(
const QString &name )
121 QList<QStandardItem *> items = mModel->findItems( name );
122 if ( items.isEmpty() )
125 QStandardItem *item = items.at( 0 );
126 mModel->removeRow( item->row() );
129 void QgsMapLayerStyleManagerWidget::styleRenamed(
const QString &oldname,
const QString &newname )
131 QList<QStandardItem *> items = mModel->findItems( oldname );
132 if ( items.isEmpty() )
135 QStandardItem *item = items.at( 0 );
136 item->setText( newname );
139 void QgsMapLayerStyleManagerWidget::addStyle()
142 QString text = QInputDialog::getText(
nullptr, tr(
"New style" ),
143 tr(
"Style name:" ), QLineEdit::Normal,
144 QStringLiteral(
"new style" ), &ok );
145 if ( !ok || text.isEmpty() )
159 void QgsMapLayerStyleManagerWidget::removeStyle()
162 QList<QStandardItem *> items = mModel->findItems( current );
163 if ( items.isEmpty() )
166 QStandardItem *item = items.at( 0 );
170 mModel->removeRow( item->row() );
179 void QgsMapLayerStyleManagerWidget::saveAsDefault()
185 if ( layer->dataProvider()->isSaveAndLoadStyleToDatabaseSupported() )
187 QMessageBox askToUser;
188 askToUser.setWindowTitle( tr(
"Save Style" ) );
189 askToUser.setText( tr(
"Save default style to: " ) );
190 askToUser.setIcon( QMessageBox::Question );
191 askToUser.addButton( tr(
"Cancel" ), QMessageBox::RejectRole );
192 askToUser.addButton( tr(
"Local database" ), QMessageBox::NoRole );
193 askToUser.addButton( tr(
"Datasource database" ), QMessageBox::YesRole );
195 switch ( askToUser.exec() )
200 layer->saveStyleToDatabase( QLatin1String(
"" ), QLatin1String(
"" ),
true, QLatin1String(
"" ), errorMsg );
201 if ( errorMsg.isNull() )
212 bool defaultSavedFlag =
false;
214 if ( !defaultSavedFlag )
216 QMessageBox::warning(
this, tr(
"Default Style" ), errorMsg );
221 void QgsMapLayerStyleManagerWidget::loadDefault()
224 bool defaultLoadedFlag =
false;
228 if ( layer->dataProvider()->isSaveAndLoadStyleToDatabaseSupported() )
230 QMessageBox askToUser;
231 askToUser.setWindowTitle( tr(
"Load Style" ) );
232 askToUser.setText( tr(
"Load default style from: " ) );
233 askToUser.setIcon( QMessageBox::Question );
234 askToUser.addButton( tr(
"Cancel" ), QMessageBox::RejectRole );
235 askToUser.addButton( tr(
"Local database" ), QMessageBox::NoRole );
236 askToUser.addButton( tr(
"Datasource database" ), QMessageBox::YesRole );
238 switch ( askToUser.exec() )
243 msg = layer->loadNamedStyle(
mLayer->
styleURI(), defaultLoadedFlag );
244 if ( !defaultLoadedFlag )
247 QMessageBox::information(
this, tr(
"Default Style" ), msg );
249 if ( msg.compare( tr(
"Loaded from Provider" ) ) )
251 QMessageBox::information(
this, tr(
"Default Style" ),
252 tr(
"No default style was found for this layer" ) );
264 myMessage = layer->loadNamedStyle(
mLayer->
styleURI(), defaultLoadedFlag, true );
268 myMessage = layer->loadNamedStyle(
mLayer->
styleURI(), defaultLoadedFlag );
275 if ( !defaultLoadedFlag )
278 QMessageBox::information(
this, tr(
"Default Style" ), myMessage );
287 void QgsMapLayerStyleManagerWidget::saveStyle()
292 void QgsMapLayerStyleManagerWidget::loadStyle()
295 QString myLastUsedDir = myQSettings.
value( QStringLiteral(
"style/lastStyleDir" ), QDir::homePath() ).toString();
297 QString myFileName = QFileDialog::getOpenFileName(
this, tr(
"Load layer properties from style file" ), myLastUsedDir,
298 tr(
"QGIS Layer Style File" ) +
" (*.qml);;" + tr(
"SLD File" ) +
" (*.sld)" );
299 if ( myFileName.isNull() )
305 bool defaultLoadedFlag =
false;
307 if ( myFileName.endsWith( QLatin1String(
".sld" ), Qt::CaseInsensitive ) )
317 if ( defaultLoadedFlag )
324 QMessageBox::warning(
this, tr(
"Load Style" ), myMessage );
327 QFileInfo myFI( myFileName );
328 QString myPath = myFI.path();
329 myQSettings.
setValue( QStringLiteral(
"style/lastStyleDir" ), myPath );
Base class for all map layer types.
QStringList styles() const
Return list of all defined style names.
void currentStyleChanged(const QString ¤tName)
Emitted when the current style has been changed.
virtual QString loadSldStyle(const QString &uri, bool &resultFlag)
Attempts to style the layer using the formatting from an SLD type file.
void styleRenamed(const QString &oldName, const QString &newName)
Emitted when a style has been renamed.
This class is a composition of two QSettings instances:
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
virtual QString loadNamedStyle(const QString &uri, bool &resultFlag)
Retrieve a named style for this layer if one exists (either as a .qml file on disk or as a record in ...
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
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...
bool isValid() const
Return the status of the layer.
Map canvas is a class for displaying all GIS data types on a canvas.
QgsMapLayerStyleManager * styleManager() const
Get access to the layer's style manager.
void styleAdded(const QString &name)
Emitted when a new style has been added.
bool removeStyle(const QString &name)
Remove a stored style.
void setValue(const QString &key, const QVariant &value, const QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
void styleRemoved(const QString &name)
Emitted when a style has been removed.
bool addStyleFromLayer(const QString &name)
Add style by cloning the current one.
QString currentStyle() const
Return name of the current style.
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 ...
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
Represents a vector layer which manages a vector based data sets.
bool setCurrentStyle(const QString &name)
Set a different style as the current style - will apply it to the layer.