QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgsvectorlayersavestyledialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvectorlayersavestyledialog.h
3 --------------------------------------
4 Date : September 2018
5 Copyright : (C) 2018 by Denis Rouzaud
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include <QListWidgetItem>
17#include <QMessageBox>
18
20#include "qgsvectorlayer.h"
21#include "qgssettings.h"
22#include "qgshelp.h"
23#include "qgsgui.h"
26
28 : QDialog( parent )
29 , mLayer( layer )
30{
31 setupUi( this );
33
34 QgsSettings settings;
35
36 const QString myLastUsedDir = settings.value( QStringLiteral( "style/lastStyleDir" ), QDir::homePath() ).toString();
37
38 // save style type combobox
39 connect( mStyleTypeComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [ = ]( int )
40 {
42 mFileLabel->setVisible( type != QgsVectorLayerProperties::DB && type != QgsVectorLayerProperties::Local );
43 mFileWidget->setVisible( type != QgsVectorLayerProperties::DB && type != QgsVectorLayerProperties::Local );
44 mSaveToDbWidget->setVisible( type == QgsVectorLayerProperties::DB );
45 mSaveToSldWidget->setVisible( type == QgsVectorLayerProperties::SLD && layer->geometryType() == Qgis::GeometryType::Polygon );
46 mStyleCategoriesListView->setEnabled( type != QgsVectorLayerProperties::SLD );
47 mFileWidget->setFilter( type == QgsVectorLayerProperties::QML ? tr( "QGIS Layer Style File (*.qml)" ) : tr( "SLD File (*.sld)" ) );
48 updateSaveButtonState();
49 } );
50
51 // Save to DB setup
52 connect( mDbStyleNameEdit, &QLineEdit::textChanged, this, &QgsVectorLayerSaveStyleDialog::updateSaveButtonState );
53 mDbStyleDescriptionEdit->setTabChangesFocus( true );
54 setTabOrder( mDbStyleNameEdit, mDbStyleDescriptionEdit );
55 setTabOrder( mDbStyleDescriptionEdit, mDbStyleUseAsDefault );
56 mDbStyleUIFileWidget->setDefaultRoot( myLastUsedDir );
57 mDbStyleUIFileWidget->setFilter( tr( "Qt Designer UI file (*.ui)" ) );
58 connect( mDbStyleUIFileWidget, &QgsFileWidget::fileChanged, this, &QgsVectorLayerSaveStyleDialog::readUiFileContent );
59 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsVectorLayerSaveStyleDialog::showHelp );
60
61 // save to file setup
62 connect( mFileWidget, &QgsFileWidget::fileChanged, this, &QgsVectorLayerSaveStyleDialog::updateSaveButtonState );
63 mFileWidget->setStorageMode( QgsFileWidget::SaveFile );
64 mFileWidget->setDefaultRoot( myLastUsedDir );
65 connect( mFileWidget, &QgsFileWidget::fileChanged, this, [ = ]( const QString & path )
66 {
67 QgsSettings settings;
68 const QFileInfo tmplFileInfo( path );
69 settings.setValue( QStringLiteral( "style/lastStyleDir" ), tmplFileInfo.absolutePath() );
70 } );
71
72 // fill style categories
73 mModel = new QgsMapLayerStyleCategoriesModel( mLayer->type(), this );
74 const QgsMapLayer::StyleCategories lastStyleCategories = settings.flagValue( QStringLiteral( "style/lastStyleCategories" ), QgsMapLayer::AllStyleCategories );
75 mModel->setCategories( lastStyleCategories );
76 mStyleCategoriesListView->setModel( mModel );
77
78 mStyleCategoriesListView->adjustSize();
79
80 setupMultipleStyles();
81
82}
83
84void QgsVectorLayerSaveStyleDialog::populateStyleComboBox()
85{
86 QString providerName = mLayer->providerType();
87 if ( providerName == QLatin1String( "ogr" ) )
88 {
89 providerName = mLayer->dataProvider()->storageType();
90 if ( providerName == QLatin1String( "GPKG" ) )
91 providerName = QStringLiteral( "GeoPackage" );
92 }
93
94 mStyleTypeComboBox->clear();
95 mStyleTypeComboBox->addItem( tr( "As QGIS QML Style File" ), QgsVectorLayerProperties::QML );
96 mStyleTypeComboBox->addItem( tr( "As SLD Style File" ), QgsVectorLayerProperties::SLD );
97
99 mStyleTypeComboBox->addItem( tr( "In Database (%1)" ).arg( providerName ), QgsVectorLayerProperties::DB );
100
101 if ( mSaveOnlyCurrentStyle )
102 mStyleTypeComboBox->addItem( tr( "As Default In Local Database" ), QgsVectorLayerProperties::Local );
103}
104
106{
107 QgsSettings().setFlagValue( QStringLiteral( "style/lastStyleCategories" ), styleCategories() );
108 QDialog::accept();
109}
110
111void QgsVectorLayerSaveStyleDialog::updateSaveButtonState()
112{
114 bool enabled { false };
115 switch ( type )
116 {
118 if ( saveOnlyCurrentStyle( ) )
119 {
120 enabled = ! mDbStyleNameEdit->text().isEmpty();
121 }
122 else
123 {
124 enabled = true;
125 }
126 break;
129 enabled = ! mFileWidget->filePath().isEmpty();
130 break;
132 enabled = true;
133 break;
134 }
135 buttonBox->button( QDialogButtonBox::Ok )->setEnabled( enabled );
136}
137
139{
140 SaveToDbSettings settings;
141 settings.name = mDbStyleNameEdit->text();
142 settings.description = mDbStyleDescriptionEdit->toPlainText();
143 settings.isDefault = mDbStyleUseAsDefault->isChecked();
144 settings.uiFileContent = mUiFileContent;
145 return settings;
146}
147
149{
150 return mFileWidget->filePath();
151}
152
153QgsMapLayer::StyleCategories QgsVectorLayerSaveStyleDialog::styleCategories() const
154{
155 return mModel->categories();
156}
157
159{
160 return mStyleTypeComboBox->currentData().value<QgsVectorLayerProperties::StyleType>();
161}
162
163void QgsVectorLayerSaveStyleDialog::readUiFileContent( const QString &filePath )
164{
165 QgsSettings myQSettings; // where we keep last used filter in persistent state
166 mUiFileContent = QString();
167
168 if ( filePath.isNull() )
169 {
170 return;
171 }
172
173 const QFileInfo myFI( filePath );
174 QFile uiFile( myFI.filePath() );
175
176 const QString myPath = myFI.path();
177 myQSettings.setValue( QStringLiteral( "style/lastStyleDir" ), myPath );
178
179 if ( uiFile.open( QIODevice::ReadOnly ) )
180 {
181 const QString content( uiFile.readAll() );
182 QDomDocument doc;
183
184 if ( !doc.setContent( content ) || doc.documentElement().tagName().compare( QLatin1String( "ui" ) ) )
185 {
186 QMessageBox::warning( this, tr( "Attach UI File" ),
187 tr( "The selected file does not appear to be a valid Qt Designer UI file." ) );
188 return;
189 }
190 mUiFileContent = content;
191 }
192}
193
194void QgsVectorLayerSaveStyleDialog::setupMultipleStyles()
195{
196 // Show/hide part of the UI according to multiple style support
197 if ( ! mSaveOnlyCurrentStyle )
198 {
199 const QgsMapLayerStyleManager *styleManager { mLayer->styleManager() };
200 const QStringList constStyles = styleManager->styles();
201 for ( const QString &name : constStyles )
202 {
203 QListWidgetItem *item = new QListWidgetItem( name, mStylesWidget );
204 item->setCheckState( Qt::CheckState::Checked );
205 // Highlight the current style
206 if ( name == styleManager->currentStyle() )
207 {
208 item->setToolTip( tr( "Current style" ) );
209 QFont font { item->font() };
210 font.setItalic( true );
211 item->setFont( font );
212 }
213 mStylesWidget->addItem( item );
214 }
215 mDbStyleNameEdit->setToolTip( tr( "Leave blank to use style names or set the base name (an incremental number will be automatically appended)" ) );
216 }
217 else
218 {
219 mDbStyleNameEdit->setToolTip( QString() );
220 }
221
222 mStylesWidget->setVisible( ! mSaveOnlyCurrentStyle );
223 mStylesWidgetLabel->setVisible( ! mSaveOnlyCurrentStyle );
224
225 mDbStyleDescriptionEdit->setVisible( mSaveOnlyCurrentStyle );
226 descriptionLabel->setVisible( mSaveOnlyCurrentStyle );
227 mDbStyleUseAsDefault->setVisible( mSaveOnlyCurrentStyle );
228
229 populateStyleComboBox();
230}
231
233{
234 return mSaveOnlyCurrentStyle;
235}
236
238{
239 if ( mSaveOnlyCurrentStyle != saveOnlyCurrentStyle )
240 {
241 mSaveOnlyCurrentStyle = saveOnlyCurrentStyle;
242 setupMultipleStyles();
243 }
244}
245
247{
248 return mStylesWidget;
249}
250
252{
253 Qgis::SldExportOptions options;
254
255 if ( mStyleTypeComboBox->currentData( ) == QgsVectorLayerProperties::SLD )
256 {
257 options.setFlag( Qgis::SldExportOption::Png );
258 }
259 return options;
260}
261
262void QgsVectorLayerSaveStyleDialog::showHelp()
263{
264 QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#save-and-share-layer-properties" ) );
265}
@ Png
Export complex styles to separate PNG files for better compatibility with OGC servers.
@ SaveFile
Select a single new or pre-existing file.
Definition: qgsfilewidget.h:71
void fileChanged(const QString &path)
Emitted whenever the current file or directory path is changed.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition: qgsgui.cpp:193
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:38
Model for layer style categories.
QgsMapLayer::StyleCategories categories() const
Returns the categories as defined in the model.
Management of styles for use with one map layer.
QStringList styles() const
Returns list of all defined style names.
QString providerType() const
Returns the provider type (provider key) for this layer.
Qgis::LayerType type
Definition: qgsmaplayer.h:80
QgsMapLayerStyleManager * styleManager() const
Gets access to the layer's style manager.
@ AllStyleCategories
Definition: qgsmaplayer.h:180
This class is a composition of two QSettings instances:
Definition: qgssettings.h:63
T flagValue(const QString &key, const T &defaultValue, const Section section=NoSection)
Returns the setting value for a setting based on a flag.
Definition: qgssettings.h:340
void setFlagValue(const QString &key, const T &value, const Section section=NoSection)
Set the value of a setting based on a flag.
Definition: qgssettings.h:404
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.
virtual bool isSaveAndLoadStyleToDatabaseSupported() const
It returns false by default.
virtual QString storageType() const
Returns the permanent storage type for this layer as a friendly name.
Qgis::SldExportOptions sldExportOptions() const
Returns the SLD export options.
void setSaveOnlyCurrentStyle(bool saveCurrentStyle)
QgsVectorLayerSaveStyleDialog(QgsVectorLayer *layer, QWidget *parent=nullptr)
QgsMapLayer::StyleCategories styleCategories() const
QgsVectorLayerProperties::StyleType currentStyleType() const
Represents a vector layer which manages a vector based data sets.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.