32#include <QDesktopServices>
37#include <qstackedwidget.h>
39#include "moc_qgslayerpropertiesdialog.cpp"
41using namespace Qt::StringLiterals;
51 mMetadataWidget = widget;
57 if ( !mLayer || !mMetadataWidget )
61 const QString lastUsedDir = settings.
value( u
"style/lastStyleDir"_s, QDir::homePath() ).toString();
63 const QString fileName = QFileDialog::getOpenFileName(
this, tr(
"Load Layer Metadata" ), lastUsedDir, tr(
"QGIS Layer Metadata File" ) +
" (*.qmd)" );
64 if ( fileName.isNull() )
69 bool defaultLoadedFlag =
false;
70 const QString message = mLayer->loadNamedMetadata( fileName, defaultLoadedFlag );
73 if ( defaultLoadedFlag )
75 mMetadataWidget->setMetadata( &mLayer->metadata() );
80 QMessageBox::warning(
this, tr(
"Load Metadata" ), message );
83 settings.
setValue( u
"style/lastStyleDir"_s, QFileInfo( fileName ).path() );
90 if ( !mLayer || !mMetadataWidget )
94 const QString lastUsedDir = settings.
value( u
"style/lastStyleDir"_s, QDir::homePath() ).toString();
96 QString outputFileName = QFileDialog::getSaveFileName(
this, tr(
"Save Layer Metadata as QMD" ), lastUsedDir, tr(
"QMD File" ) +
" (*.qmd)" );
100 if ( outputFileName.isEmpty() )
105 mMetadataWidget->acceptMetadata();
113 bool defaultLoadedFlag =
false;
114 const QString message = mLayer->saveNamedMetadata( outputFileName, defaultLoadedFlag );
115 if ( defaultLoadedFlag )
116 settings.
setValue( u
"style/lastStyleDir"_s, QFileInfo( outputFileName ).absolutePath() );
118 QMessageBox::information(
this, tr(
"Save Metadata" ), message );
125 if ( !mLayer || !mMetadataWidget )
128 mMetadataWidget->acceptMetadata();
130 bool defaultSavedFlag =
false;
131 const QString infoWindowTitle = QObject::tr(
"Save Default Metadata" );
132 const QString errorMsg = mLayer->saveDefaultMetadata( defaultSavedFlag );
133 if ( !defaultSavedFlag )
135 QMessageBox::warning(
this, infoWindowTitle, errorMsg );
140 QMessageBox::information(
this, infoWindowTitle, tr(
"Metadata saved." ) );
146 if ( !mLayer || !mMetadataWidget )
149 bool defaultLoadedFlag =
false;
150 const QString message = mLayer->loadNamedMetadata( mLayer->metadataUri(), defaultLoadedFlag );
152 if ( defaultLoadedFlag )
154 mMetadataWidget->setMetadata( &mLayer->metadata() );
158 QMessageBox::information(
this, tr(
"Default Metadata" ), message );
169 const QString lastUsedDir = settings.
value( u
"style/lastStyleDir"_s, QDir::homePath() ).toString();
171 QString fileName = QFileDialog::getOpenFileName(
this, tr(
"Load layer properties from style file" ), lastUsedDir, tr(
"QGIS Layer Style File" ) +
" (*.qml)" );
172 if ( fileName.isEmpty() )
176 if ( !fileName.endsWith(
".qml"_L1, Qt::CaseInsensitive ) )
177 fileName +=
".qml"_L1;
181 bool defaultLoadedFlag =
false;
182 const QString message = mLayer->loadNamedStyle( fileName, defaultLoadedFlag );
183 if ( defaultLoadedFlag )
185 settings.
setValue( u
"style/lastStyleDir"_s, QFileInfo( fileName ).absolutePath() );
190 QMessageBox::information(
this, tr(
"Load Style" ), message );
201 const QString lastUsedDir = settings.
value( u
"style/lastStyleDir"_s, QDir::homePath() ).toString();
203 QString outputFileName = QFileDialog::getSaveFileName(
this, tr(
"Save layer properties as style file" ), lastUsedDir, tr(
"QGIS Layer Style File" ) +
" (*.qml)" );
207 if ( outputFileName.isEmpty() )
216 bool defaultLoadedFlag =
false;
217 const QString message = mLayer->saveNamedStyle( outputFileName, defaultLoadedFlag );
219 if ( defaultLoadedFlag )
221 settings.
setValue( u
"style/lastStyleDir"_s, QFileInfo( outputFileName ).absolutePath() );
225 QMessageBox::information(
this, tr(
"Save Style" ), message );
238 bool defaultSavedFlag =
false;
244 const QString message = mLayer->saveDefaultStyle( defaultSavedFlag );
246 if ( !defaultSavedFlag )
249 QMessageBox::information(
this, tr(
"Default Style" ), message );
275 if ( beforePage.isEmpty() )
286 bool defaultLoadedFlag =
false;
293 QMessageBox askToUser;
294 askToUser.setText( tr(
"Load default style from: " ) );
295 askToUser.setIcon( QMessageBox::Question );
296 askToUser.addButton( tr(
"Cancel" ), QMessageBox::RejectRole );
297 askToUser.addButton( tr(
"Local Database" ), QMessageBox::NoRole );
298 askToUser.addButton( tr(
"Datasource Database" ), QMessageBox::YesRole );
300 switch ( askToUser.exec() )
305 msg = mLayer->loadNamedStyle( mLayer->styleURI(), defaultLoadedFlag,
false );
306 if ( !defaultLoadedFlag )
309 QMessageBox::information(
this, tr(
"Default Style" ), msg );
311 if ( msg.compare( tr(
"Loaded from Provider" ) ) )
313 QMessageBox::information(
this, tr(
"Default Style" ), tr(
"No default style was found for this layer." ) );
327 QString myMessage = mLayer->loadNamedStyle( mLayer->styleURI(), defaultLoadedFlag,
true );
330 if ( defaultLoadedFlag )
339 QMessageBox::information(
this, tr(
"Default Style" ), myMessage );
351 QMessageBox askToUser;
352 askToUser.setText( tr(
"Save default style to: " ) );
353 askToUser.setIcon( QMessageBox::Question );
354 askToUser.addButton( tr(
"Cancel" ), QMessageBox::RejectRole );
355 askToUser.addButton( tr(
"Local Database" ), QMessageBox::NoRole );
356 askToUser.addButton( tr(
"Datasource Database" ), QMessageBox::YesRole );
358 switch ( askToUser.exec() )
365 QString errorMessage;
369 question(
nullptr, QObject::tr(
"Save style in database" ), QObject::tr(
"A matching style already exists in the database for this layer. Do you want to overwrite it?" ), QMessageBox::Yes | QMessageBox::No )
375 else if ( !errorMessage.isEmpty() )
377 QMessageBox::warning(
nullptr, QObject::tr(
"Save style in database" ), errorMessage );
381 mLayer->saveStyleToDatabaseV2( QString(), QString(),
true, QString(), errorMsg );
382 if ( errorMsg.isNull() )
398 if ( !mLayer->dataProvider() )
406 bool defaultLoadedFlag =
false;
407 QString errorMessage;
417 errorMessage = mLayer->saveNamedStyle( filePath, defaultLoadedFlag, dlg.
styleCategories() );
421 errorMessage = mLayer->saveSldStyleV2( defaultLoadedFlag, sldContext );
425 if ( defaultLoadedFlag )
432 QMessageBox::information(
this, tr(
"Save Style" ), errorMessage );
439 QString infoWindowTitle = QObject::tr(
"Save style to DB (%1)" ).arg( mLayer->providerType() );
446 question(
nullptr, QObject::tr(
"Save style in database" ), QObject::tr(
"A matching style already exists in the database for this layer. Do you want to overwrite it?" ), QMessageBox::Yes | QMessageBox::No )
452 else if ( !errorMessage.isEmpty() )
454 QMessageBox::warning(
this, infoWindowTitle, errorMessage );
460 if ( !errorMessage.isNull() )
462 QMessageBox::warning(
this, infoWindowTitle, errorMessage );
466 QMessageBox::information(
this, infoWindowTitle, tr(
"Style saved" ) );
472 QString infoWindowTitle = tr(
"Save default style to local database" );
473 errorMessage = mLayer->saveDefaultStyle( defaultLoadedFlag, dlg.
styleCategories() );
474 if ( !defaultLoadedFlag )
476 QMessageBox::warning(
this, infoWindowTitle, errorMessage );
480 QMessageBox::information(
this, infoWindowTitle, tr(
"Style saved" ) );
491 QStringList ids, names, descriptions;
494 int sectionLimit = mLayer->listStylesInDatabase( ids, names, descriptions, errorMsg );
500 mOldStyle = mLayer->styleManager()->style( mLayer->styleManager()->currentStyle() );
503 bool defaultLoadedFlag =
false;
512 errorMsg = mLayer->loadSldStyle( filePath, defaultLoadedFlag );
516 errorMsg = mLayer->loadNamedStyle( filePath, defaultLoadedFlag,
true, categories );
519 if ( defaultLoadedFlag )
527 QMessageBox::warning(
this, tr(
"Load Style" ), errorMsg );
535 QString qmlStyle = mLayer->getStyleFromDatabase( selectedStyleId, errorMsg );
536 if ( !errorMsg.isNull() )
538 QMessageBox::warning(
this, tr(
"Load Styles from Database" ), errorMsg );
542 QDomDocument myDocument( u
"qgis"_s );
543 myDocument.setContent( qmlStyle );
545 if ( mLayer->importNamedStyle( myDocument, errorMsg, categories ) )
552 QMessageBox::warning(
this, tr(
"Load Styles from Database" ), tr(
"The retrieved style is not a valid named style. Error message: %1" ).arg( errorMsg ) );
558 errorMsg = mLayer->loadNamedStyle( mLayer->styleURI(), defaultLoadedFlag,
true, categories );
560 if ( defaultLoadedFlag )
567 QMessageBox::warning(
this, tr(
"Load Default Style" ), errorMsg );
581 mOldStyle = mLayer->styleManager()->style( mLayer->styleManager()->currentStyle() );
584QString QgsLayerPropertiesDialog::generateDialogTitle()
const
586 QString title = tr(
"Layer Properties - %1" ).arg( mLayer->name() );
588 if ( !mLayer->styleManager()->isDefault( mLayer->styleManager()->currentStyle() ) )
589 title += u
" (%1)"_s.arg( mLayer->styleManager()->currentStyle() );
596 if (
mOldStyle.xmlData() != mLayer->styleManager()->style( mLayer->styleManager()->currentStyle() ).xmlData() )
600 QDomDocument doc( u
"qgis"_s );
601 int errorLine, errorColumn;
602 doc.setContent(
mOldStyle.xmlData(),
false, &message, &errorLine, &errorColumn );
603 mLayer->importNamedStyle( doc, message );
614 const bool isMetadataPanel = ( index ==
mOptStackedWidget->indexOf( mMetadataPage ) );
615 mBtnStyle->setVisible( !isMetadataPanel );
622 QFileInfo file( url.toLocalFile() );
623 if ( file.exists() && !file.isDir() )
626 QDesktopServices::openUrl( url );
@ NoVendorExtension
No vendor extensions.
Abstract base class for spatial data provider implementations.
virtual Qgis::ProviderStyleStorageCapabilities styleStorageCapabilities() const
Returns the style storage capabilities.
static QString ensureFileNameHasExtension(const QString &fileName, const QStringList &extensions)
Ensures that a fileName ends with an extension from the provided list of extensions.
static QgsNative * nativePlatformInterface()
Returns the global native interface, which offers abstraction to the host OS's underlying public inte...
QPushButton * mBtnStyle
Style button.
void saveMetadataToFile()
Allows the user to save the layer's metadata as a file.
virtual void rollback()
Rolls back changes made to the layer.
void optionsStackedWidget_CurrentChanged(int index) override
StyleType
Style storage type.
void saveStyleAsDefault()
Saves the current layer style as the default for the layer.
QList< QgsMapLayerConfigWidget * > mConfigWidgets
Layer config widgets.
void setMetadataWidget(QgsMetadataWidget *widget, QWidget *page)
Sets the metadata widget and page associated with the dialog.
void loadDefaultStyle()
Reloads the default style for the layer.
void saveStyleAs()
Saves a style when appriate button is pressed.
virtual void apply()=0
Applies the dialog settings to the layer.
virtual void syncToLayer()=0
Resets the dialog to the current layer state.
void loadStyle()
Triggers a dialog to load a saved style.
void saveStyleToFile()
Allows the user to save the layer's style to a file.
QgsMapCanvas * mCanvas
Associated map canvas.
void loadDefaultMetadata()
Reloads the default layer metadata for the layer.
void refocusDialog()
Ensures the dialog is focused and activated.
QgsLayerPropertiesDialog(QgsMapLayer *layer, QgsMapCanvas *canvas, const QString &settingsKey, QWidget *parent=nullptr, Qt::WindowFlags fl=Qt::WindowFlags(), QgsSettings *settings=nullptr)
Constructor for QgsLayerPropertiesDialog.
void storeCurrentStyleForUndo()
Stores the current layer style so that undo operations can be performed.
QgsMapLayerStyle mOldStyle
Previous layer style.
void loadMetadataFromFile()
Allows the user to load layer metadata from a file.
void saveDefaultStyle()
Saves the default style when appropriate button is pressed.
virtual void addPropertiesPageFactory(const QgsMapLayerConfigWidgetFactory *factory)
Adds properties page from a factory.
void loadStyleFromFile()
Allows the user to load layer style from a file.
QPushButton * mBtnMetadata
Metadata button.
void initialize()
Initialize the dialog.
void saveMetadataAsDefault()
Saves the current layer metadata as the default for the layer.
void openUrl(const QUrl &url)
Handles opening a url from the dialog.
Map canvas is a class for displaying all GIS data types on a canvas.
A reusable dialog which allows users to select stored layer styles and categories to load for a map l...
QgsMapLayer::StyleCategories styleCategories() const
Returns the list of selected style categories the user has opted to load.
void initializeLists(const QStringList &ids, const QStringList &names, const QStringList &descriptions, int sectionLimit)
Initialize list of database stored styles.
QString selectedStyleId()
Returns the ID of the selected database stored style.
QString filePath() const
Returns the full path to the selected layer style source file.
QgsLayerPropertiesDialog::StyleType currentStyleType() const
Returns the selected style type.
A dialog which allows saving map layer styles.
QString outputFilePath() const
Returns the selected file output path.
Qgis::SldExportOptions sldExportOptions() const
Returns the SLD export options.
SaveToDbSettings saveToDbSettings() const
Returns the database settings for saving the style in the DB.
QgsMapLayer::StyleCategories styleCategories() const
Returns the available style categories.
QgsLayerPropertiesDialog::StyleType currentStyleType() const
Returns the selected style storage type.
Base class for all map layer types.
QFlags< StyleCategory > StyleCategories
static QString extensionPropertyType(PropertyType type)
Returns the extension of a Property.
virtual void optionsStackedWidget_CurrentChanged(int index)
Select relevant tab on current page change.
QgsOptionsDialogBase(const QString &settingsKey, QWidget *parent=nullptr, Qt::WindowFlags fl=Qt::WindowFlags(), QgsSettings *settings=nullptr)
Constructor.
void addPage(const QString &title, const QString &tooltip, const QIcon &icon, QWidget *widget, const QStringList &path=QStringList(), const QString &key=QString())
Adds a new page to the dialog pages.
void restoreOptionsBaseUi(const QString &title=QString())
Restore the base ui.
QStackedWidget * mOptStackedWidget
void insertPage(const QString &title, const QString &tooltip, const QIcon &icon, QWidget *widget, const QString &before, const QStringList &path=QStringList(), const QString &key=QString())
Inserts a new page into the dialog pages.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
Stores settings for use within QGIS.
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.
Holds SLD export options and other information related to SLD export of a QGIS layer style.
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH