32#include <QDesktopServices>
38#include <qstackedwidget.h>
40#include "moc_qgslayerpropertiesdialog.cpp"
42using namespace Qt::StringLiterals;
52 mMetadataWidget = widget;
58 if ( !mLayer || !mMetadataWidget )
62 const QString lastUsedDir = settings.
value( u
"style/lastStyleDir"_s, QDir::homePath() ).toString();
64 const QString fileName = QFileDialog::getOpenFileName(
this, tr(
"Load Layer Metadata" ), lastUsedDir, tr(
"QGIS Layer Metadata File" ) +
" (*.qmd)" );
65 if ( fileName.isNull() )
70 bool defaultLoadedFlag =
false;
71 const QString message = mLayer->loadNamedMetadata( fileName, defaultLoadedFlag );
74 if ( defaultLoadedFlag )
76 mMetadataWidget->setMetadata( &mLayer->metadata() );
81 QMessageBox::warning(
this, tr(
"Load Metadata" ), message );
84 settings.
setValue( u
"style/lastStyleDir"_s, QFileInfo( fileName ).path() );
91 if ( !mLayer || !mMetadataWidget )
95 const QString lastUsedDir = settings.
value( u
"style/lastStyleDir"_s, QDir::homePath() ).toString();
97 QString outputFileName = QFileDialog::getSaveFileName(
this, tr(
"Save Layer Metadata as QMD" ), lastUsedDir, tr(
"QMD File" ) +
" (*.qmd)" );
101 if ( outputFileName.isEmpty() )
106 mMetadataWidget->acceptMetadata();
114 bool defaultLoadedFlag =
false;
115 const QString message = mLayer->saveNamedMetadata( outputFileName, defaultLoadedFlag );
116 if ( defaultLoadedFlag )
117 settings.
setValue( u
"style/lastStyleDir"_s, QFileInfo( outputFileName ).absolutePath() );
119 QMessageBox::information(
this, tr(
"Save Metadata" ), message );
126 if ( !mLayer || !mMetadataWidget )
129 mMetadataWidget->acceptMetadata();
131 bool defaultSavedFlag =
false;
132 const QString infoWindowTitle = QObject::tr(
"Save Default Metadata" );
133 const QString errorMsg = mLayer->saveDefaultMetadata( defaultSavedFlag );
134 if ( !defaultSavedFlag )
136 QMessageBox::warning(
this, infoWindowTitle, errorMsg );
141 QMessageBox::information(
this, infoWindowTitle, tr(
"Metadata saved." ) );
147 if ( !mLayer || !mMetadataWidget )
150 bool defaultLoadedFlag =
false;
151 const QString message = mLayer->loadNamedMetadata( mLayer->metadataUri(), defaultLoadedFlag );
153 if ( defaultLoadedFlag )
155 mMetadataWidget->setMetadata( &mLayer->metadata() );
159 QMessageBox::information(
this, tr(
"Default Metadata" ), message );
170 const QString lastUsedDir = settings.
value( u
"style/lastStyleDir"_s, QDir::homePath() ).toString();
172 QString fileName = QFileDialog::getOpenFileName(
this, tr(
"Load layer properties from style file" ), lastUsedDir, tr(
"QGIS Layer Style File" ) +
" (*.qml)" );
173 if ( fileName.isEmpty() )
177 if ( !fileName.endsWith(
".qml"_L1, Qt::CaseInsensitive ) )
178 fileName +=
".qml"_L1;
182 bool defaultLoadedFlag =
false;
183 const QString message = mLayer->loadNamedStyle( fileName, defaultLoadedFlag );
184 if ( defaultLoadedFlag )
186 settings.
setValue( u
"style/lastStyleDir"_s, QFileInfo( fileName ).absolutePath() );
191 QMessageBox::information(
this, tr(
"Load Style" ), message );
202 const QString lastUsedDir = settings.
value( u
"style/lastStyleDir"_s, QDir::homePath() ).toString();
204 QString outputFileName = QFileDialog::getSaveFileName(
this, tr(
"Save layer properties as style file" ), lastUsedDir, tr(
"QGIS Layer Style File" ) +
" (*.qml)" );
208 if ( outputFileName.isEmpty() )
217 bool defaultLoadedFlag =
false;
218 const QString message = mLayer->saveNamedStyle( outputFileName, defaultLoadedFlag );
220 if ( defaultLoadedFlag )
222 settings.
setValue( u
"style/lastStyleDir"_s, QFileInfo( outputFileName ).absolutePath() );
226 QMessageBox::information(
this, tr(
"Save Style" ), message );
239 bool defaultSavedFlag =
false;
245 const QString message = mLayer->saveDefaultStyle( defaultSavedFlag );
247 if ( !defaultSavedFlag )
250 QMessageBox::information(
this, tr(
"Default Style" ), message );
275 QgsDebugError( u
"Failed to create widget from factory '%1'"_s.arg( factory->
title() ) );
282 if ( beforePage.isEmpty() )
293 bool defaultLoadedFlag =
false;
300 QMessageBox askToUser;
301 askToUser.setText( tr(
"Load default style from: " ) );
302 askToUser.setIcon( QMessageBox::Question );
303 askToUser.addButton( tr(
"Cancel" ), QMessageBox::RejectRole );
304 askToUser.addButton( tr(
"Local Database" ), QMessageBox::NoRole );
305 askToUser.addButton( tr(
"Datasource Database" ), QMessageBox::YesRole );
307 switch ( askToUser.exec() )
312 msg = mLayer->loadNamedStyle( mLayer->styleURI(), defaultLoadedFlag,
false );
313 if ( !defaultLoadedFlag )
316 QMessageBox::information(
this, tr(
"Default Style" ), msg );
318 if ( msg.compare( tr(
"Loaded from Provider" ) ) )
320 QMessageBox::information(
this, tr(
"Default Style" ), tr(
"No default style was found for this layer." ) );
334 QString myMessage = mLayer->loadNamedStyle( mLayer->styleURI(), defaultLoadedFlag,
true );
337 if ( defaultLoadedFlag )
346 QMessageBox::information(
this, tr(
"Default Style" ), myMessage );
358 QMessageBox askToUser;
359 askToUser.setText( tr(
"Save default style to: " ) );
360 askToUser.setIcon( QMessageBox::Question );
361 QPushButton *cancelButton = askToUser.addButton( tr(
"Cancel" ), QMessageBox::RejectRole );
362 askToUser.addButton( tr(
"Local Database" ), QMessageBox::NoRole );
363 QPushButton *datasourceButton = askToUser.addButton( tr(
"Datasource Database" ), QMessageBox::YesRole );
367 if ( askToUser.clickedButton() == cancelButton )
371 else if ( askToUser.clickedButton() == datasourceButton )
374 QString errorMessage;
378 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 )
384 else if ( !errorMessage.isEmpty() )
386 QMessageBox::warning(
nullptr, QObject::tr(
"Save style in database" ), errorMessage );
390 mLayer->saveStyleToDatabaseV2( QString(), QString(),
true, QString(), errorMsg );
391 if ( errorMsg.isNull() )
403 if ( !mLayer->dataProvider() )
411 bool defaultLoadedFlag =
false;
412 QString errorMessage;
422 errorMessage = mLayer->saveNamedStyle( filePath, defaultLoadedFlag, dlg.
styleCategories() );
426 errorMessage = mLayer->saveSldStyleV2( defaultLoadedFlag, sldContext );
430 if ( defaultLoadedFlag )
437 QMessageBox::information(
this, tr(
"Save Style" ), errorMessage );
444 QString infoWindowTitle = QObject::tr(
"Save style to DB (%1)" ).arg( mLayer->providerType() );
451 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 )
457 else if ( !errorMessage.isEmpty() )
459 QMessageBox::warning(
this, infoWindowTitle, errorMessage );
465 if ( !errorMessage.isNull() )
467 QMessageBox::warning(
this, infoWindowTitle, errorMessage );
471 QMessageBox::information(
this, infoWindowTitle, tr(
"Style saved" ) );
477 QString infoWindowTitle = tr(
"Save default style to local database" );
478 errorMessage = mLayer->saveDefaultStyle( defaultLoadedFlag, dlg.
styleCategories() );
479 if ( !defaultLoadedFlag )
481 QMessageBox::warning(
this, infoWindowTitle, errorMessage );
485 QMessageBox::information(
this, infoWindowTitle, tr(
"Style saved" ) );
496 QStringList ids, names, descriptions;
499 int sectionLimit = mLayer->listStylesInDatabase( ids, names, descriptions, errorMsg );
505 mOldStyle = mLayer->styleManager()->style( mLayer->styleManager()->currentStyle() );
508 bool defaultLoadedFlag =
false;
517 errorMsg = mLayer->loadSldStyle( filePath, defaultLoadedFlag );
521 errorMsg = mLayer->loadNamedStyle( filePath, defaultLoadedFlag,
true, categories );
524 if ( defaultLoadedFlag )
532 QMessageBox::warning(
this, tr(
"Load Style" ), errorMsg );
540 QString qmlStyle = mLayer->getStyleFromDatabase( selectedStyleId, errorMsg );
541 if ( !errorMsg.isNull() )
543 QMessageBox::warning(
this, tr(
"Load Styles from Database" ), errorMsg );
547 QDomDocument myDocument( u
"qgis"_s );
548 myDocument.setContent( qmlStyle );
550 if ( mLayer->importNamedStyle( myDocument, errorMsg, categories ) )
557 QMessageBox::warning(
this, tr(
"Load Styles from Database" ), tr(
"The retrieved style is not a valid named style. Error message: %1" ).arg( errorMsg ) );
563 errorMsg = mLayer->loadNamedStyle( mLayer->styleURI(), defaultLoadedFlag,
true, categories );
565 if ( defaultLoadedFlag )
572 QMessageBox::warning(
this, tr(
"Load Default Style" ), errorMsg );
586 mOldStyle = mLayer->styleManager()->style( mLayer->styleManager()->currentStyle() );
589QString QgsLayerPropertiesDialog::generateDialogTitle()
const
591 QString title = tr(
"Layer Properties - %1" ).arg( mLayer->name() );
593 if ( !mLayer->styleManager()->isDefault( mLayer->styleManager()->currentStyle() ) )
594 title += u
" (%1)"_s.arg( mLayer->styleManager()->currentStyle() );
601 if (
mOldStyle.xmlData() != mLayer->styleManager()->style( mLayer->styleManager()->currentStyle() ).xmlData() )
605 QDomDocument doc( u
"qgis"_s );
606 int errorLine, errorColumn;
607 doc.setContent(
mOldStyle.xmlData(),
false, &message, &errorLine, &errorColumn );
608 mLayer->importNamedStyle( doc, message );
619 const bool isMetadataPanel = ( index ==
mOptStackedWidget->indexOf( mMetadataPage ) );
620 mBtnStyle->setVisible( !isMetadataPanel );
627 QFileInfo file( url.toLocalFile() );
628 if ( file.exists() && !file.isDir() )
631 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
#define QgsDebugError(str)