QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
qgsmaplayersavestyledialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaplayersavestyledialog.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#include <QPushButton>
19
21#include "qgssettings.h"
22#include "qgshelp.h"
23#include "qgsgui.h"
26#include "qgsvectorlayer.h"
27
29 : QDialog( parent )
30 , mLayer( layer )
31{
32 setupUi( this );
34
35 QgsSettings settings;
36
37 const QString myLastUsedDir = settings.value( QStringLiteral( "style/lastStyleDir" ), QDir::homePath() ).toString();
38
39 // save style type combobox
40 connect( mStyleTypeComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [ = ]( int )
41 {
45 mSaveToDbWidget->setVisible( type == QgsLayerPropertiesDialog::DatasourceDatabase );
46 mSaveToSldWidget->setVisible( type == QgsLayerPropertiesDialog::SLD && layer->type() == Qgis::LayerType::Vector && static_cast<QgsVectorLayer *>( layer )->geometryType() == Qgis::GeometryType::Polygon );
47 mStyleCategoriesListView->setEnabled( type != QgsLayerPropertiesDialog::SLD );
48 mFileWidget->setFilter( type == QgsLayerPropertiesDialog::QML ? tr( "QGIS Layer Style File (*.qml)" ) : tr( "SLD File (*.sld)" ) );
49 updateSaveButtonState();
50 } );
51
52 // Save to DB setup
53 connect( mDbStyleNameEdit, &QLineEdit::textChanged, this, &QgsMapLayerSaveStyleDialog::updateSaveButtonState );
54 mDbStyleDescriptionEdit->setTabChangesFocus( true );
55 setTabOrder( mDbStyleNameEdit, mDbStyleDescriptionEdit );
56 setTabOrder( mDbStyleDescriptionEdit, mDbStyleUseAsDefault );
57 mDbStyleUIFileWidget->setDefaultRoot( myLastUsedDir );
58 mDbStyleUIFileWidget->setFilter( tr( "Qt Designer UI file (*.ui)" ) );
59 connect( mDbStyleUIFileWidget, &QgsFileWidget::fileChanged, this, &QgsMapLayerSaveStyleDialog::readUiFileContent );
60 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsMapLayerSaveStyleDialog::showHelp );
61
62 // save to file setup
63 connect( mFileWidget, &QgsFileWidget::fileChanged, this, &QgsMapLayerSaveStyleDialog::updateSaveButtonState );
64 mFileWidget->setStorageMode( QgsFileWidget::SaveFile );
65 mFileWidget->setDefaultRoot( myLastUsedDir );
66 connect( mFileWidget, &QgsFileWidget::fileChanged, this, [ = ]( const QString & path )
67 {
68 QgsSettings settings;
69 const QFileInfo tmplFileInfo( path );
70 settings.setValue( QStringLiteral( "style/lastStyleDir" ), tmplFileInfo.absolutePath() );
71 } );
72
73 // fill style categories
74 mModel = new QgsMapLayerStyleCategoriesModel( mLayer->type(), this );
75 const QgsMapLayer::StyleCategories lastStyleCategories = settings.flagValue( QStringLiteral( "style/lastStyleCategories" ), QgsMapLayer::AllStyleCategories );
76 mModel->setCategories( lastStyleCategories );
77 mStyleCategoriesListView->setModel( mModel );
78
79 mStyleCategoriesListView->adjustSize();
80
81 setupMultipleStyles();
82
83}
84
85void QgsMapLayerSaveStyleDialog::populateStyleComboBox()
86{
87 mStyleTypeComboBox->clear();
88 mStyleTypeComboBox->addItem( tr( "As QGIS QML style file" ), QgsLayerPropertiesDialog::QML );
89 mStyleTypeComboBox->addItem( tr( "As SLD style file" ), QgsLayerPropertiesDialog::SLD );
90
92 mStyleTypeComboBox->addItem( tr( "In datasource database" ), QgsLayerPropertiesDialog::DatasourceDatabase );
93
94 if ( mSaveOnlyCurrentStyle )
95 mStyleTypeComboBox->addItem( tr( "As default in local user database" ), QgsLayerPropertiesDialog::UserDatabase );
96}
97
99{
100 QgsSettings().setFlagValue( QStringLiteral( "style/lastStyleCategories" ), styleCategories() );
101 QDialog::accept();
102}
103
104void QgsMapLayerSaveStyleDialog::updateSaveButtonState()
105{
107 bool enabled { false };
108 switch ( type )
109 {
111 if ( saveOnlyCurrentStyle( ) )
112 {
113 enabled = ! mDbStyleNameEdit->text().isEmpty();
114 }
115 else
116 {
117 enabled = true;
118 }
119 break;
122 enabled = ! mFileWidget->filePath().isEmpty();
123 break;
125 enabled = true;
126 break;
127 }
128 buttonBox->button( QDialogButtonBox::Ok )->setEnabled( enabled );
129}
130
132{
133 SaveToDbSettings settings;
134 settings.name = mDbStyleNameEdit->text();
135 settings.description = mDbStyleDescriptionEdit->toPlainText();
136 settings.isDefault = mDbStyleUseAsDefault->isChecked();
137 settings.uiFileContent = mUiFileContent;
138 return settings;
139}
140
142{
143 return mFileWidget->filePath();
144}
145
146QgsMapLayer::StyleCategories QgsMapLayerSaveStyleDialog::styleCategories() const
147{
148 return mModel->categories();
149}
150
155
156void QgsMapLayerSaveStyleDialog::readUiFileContent( const QString &filePath )
157{
158 QgsSettings myQSettings; // where we keep last used filter in persistent state
159 mUiFileContent = QString();
160
161 if ( filePath.isNull() )
162 {
163 return;
164 }
165
166 const QFileInfo myFI( filePath );
167 QFile uiFile( myFI.filePath() );
168
169 const QString myPath = myFI.path();
170 myQSettings.setValue( QStringLiteral( "style/lastStyleDir" ), myPath );
171
172 if ( uiFile.open( QIODevice::ReadOnly ) )
173 {
174 const QString content( uiFile.readAll() );
175 QDomDocument doc;
176
177 if ( !doc.setContent( content ) || doc.documentElement().tagName().compare( QLatin1String( "ui" ) ) )
178 {
179 QMessageBox::warning( this, tr( "Attach UI File" ),
180 tr( "The selected file does not appear to be a valid Qt Designer UI file." ) );
181 return;
182 }
183 mUiFileContent = content;
184 }
185}
186
187void QgsMapLayerSaveStyleDialog::setupMultipleStyles()
188{
189 // Show/hide part of the UI according to multiple style support
190 if ( ! mSaveOnlyCurrentStyle )
191 {
192 const QgsMapLayerStyleManager *styleManager { mLayer->styleManager() };
193 const QStringList constStyles = styleManager->styles();
194 for ( const QString &name : constStyles )
195 {
196 QListWidgetItem *item = new QListWidgetItem( name, mStylesWidget );
197 item->setCheckState( Qt::CheckState::Checked );
198 // Highlight the current style
199 if ( name == styleManager->currentStyle() )
200 {
201 item->setToolTip( tr( "Current style" ) );
202 QFont font { item->font() };
203 font.setItalic( true );
204 item->setFont( font );
205 }
206 mStylesWidget->addItem( item );
207 }
208 mDbStyleNameEdit->setToolTip( tr( "Leave blank to use style names or set the base name (an incremental number will be automatically appended)" ) );
209 }
210 else
211 {
212 mDbStyleNameEdit->setToolTip( QString() );
213 }
214
215 mStylesWidget->setVisible( ! mSaveOnlyCurrentStyle );
216 mStylesWidgetLabel->setVisible( ! mSaveOnlyCurrentStyle );
217
218 mDbStyleDescriptionEdit->setVisible( mSaveOnlyCurrentStyle );
219 descriptionLabel->setVisible( mSaveOnlyCurrentStyle );
220 mDbStyleUseAsDefault->setVisible( mSaveOnlyCurrentStyle );
221
222 populateStyleComboBox();
223}
224
226{
227 return mSaveOnlyCurrentStyle;
228}
229
231{
232 if ( mSaveOnlyCurrentStyle != saveOnlyCurrentStyle )
233 {
234 mSaveOnlyCurrentStyle = saveOnlyCurrentStyle;
235 setupMultipleStyles();
236 }
237}
238
240{
241 return mStylesWidget;
242}
243
245{
246 Qgis::SldExportOptions options;
247
248 if ( mStyleTypeComboBox->currentData( ) == QgsLayerPropertiesDialog::SLD )
249 {
250 options.setFlag( Qgis::SldExportOption::Png );
251 }
252 return options;
253}
254
255void QgsMapLayerSaveStyleDialog::showHelp()
256{
257 QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#save-and-share-layer-properties" ) );
258}
@ Png
Export complex styles to separate PNG files for better compatibility with OGC servers.
@ Polygon
Polygons.
@ Vector
Vector layer.
virtual Qgis::ProviderStyleStorageCapabilities styleStorageCapabilities() const
Returns the style storage capabilities.
@ SaveFile
Select a single new or pre-existing file.
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:194
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:38
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.
void setSaveOnlyCurrentStyle(bool saveCurrentStyle)
Sets whether the user only allowed to save the current style.
bool saveOnlyCurrentStyle() const
Returns whether the user only allowed to save the current style.
QgsMapLayer::StyleCategories styleCategories() const
Returns the available style categories.
QgsLayerPropertiesDialog::StyleType currentStyleType() const
Returns the selected style storage type.
QgsMapLayerSaveStyleDialog(QgsMapLayer *layer, QWidget *parent=nullptr)
Constructor.
const QListWidget * stylesWidget()
Returns the styles list widget.
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.
Base class for all map layer types.
Definition qgsmaplayer.h:74
Qgis::LayerType type
Definition qgsmaplayer.h:81
QgsMapLayerStyleManager * styleManager() const
Gets access to the layer's style manager.
virtual Q_INVOKABLE QgsDataProvider * dataProvider()
Returns the layer's data provider, it may be nullptr.
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.
void setFlagValue(const QString &key, const T &value, const Section section=NoSection)
Set the value of a setting based on a flag.
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.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.