QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
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 mStyleCategoriesListView->setWordWrap( true );
79 mStyleCategoriesListView->setItemDelegate( new QgsCategoryDisplayLabelDelegate( this ) );
80
81 // select and deselect all categories
82 connect( mSelectAllButton, &QPushButton::clicked, this, &QgsMapLayerSaveStyleDialog::selectAll );
83 connect( mDeselectAllButton, &QPushButton::clicked, this, &QgsMapLayerSaveStyleDialog::deselectAll );
84 connect( mInvertSelectionButton, &QPushButton::clicked, this, &QgsMapLayerSaveStyleDialog::invertSelection );
85
86 mStyleCategoriesListView->adjustSize();
87
88 setupMultipleStyles();
89
90}
91
92void QgsMapLayerSaveStyleDialog::invertSelection()
93{
94 for ( int i = 0; i < mModel->rowCount( QModelIndex() ); i++ )
95 {
96 QModelIndex index = mModel->index( i, 0 );
97 Qt::CheckState currentState = Qt::CheckState( mModel->data( index, Qt::CheckStateRole ).toInt() );
98 Qt::CheckState newState = ( currentState == Qt::Checked ) ? Qt::Unchecked : Qt::Checked;
99 mModel->setData( index, newState, Qt::CheckStateRole );
100 }
101}
102
103void QgsMapLayerSaveStyleDialog::selectAll()
104{
105 for ( int i = 0; i < mModel->rowCount( QModelIndex() ); i++ )
106 {
107 QModelIndex index = mModel->index( i, 0 );
108 mModel->setData( index, Qt::Checked, Qt::CheckStateRole );
109 }
110}
111
112void QgsMapLayerSaveStyleDialog::deselectAll()
113{
114 for ( int i = 0; i < mModel->rowCount( QModelIndex() ); i++ )
115 {
116 QModelIndex index = mModel->index( i, 0 );
117 mModel->setData( index, Qt::Unchecked, Qt::CheckStateRole );
118 }
119}
120
121void QgsMapLayerSaveStyleDialog::populateStyleComboBox()
122{
123 mStyleTypeComboBox->clear();
124 mStyleTypeComboBox->addItem( tr( "As QGIS QML style file" ), QgsLayerPropertiesDialog::QML );
125 mStyleTypeComboBox->addItem( tr( "As SLD style file" ), QgsLayerPropertiesDialog::SLD );
126
128 mStyleTypeComboBox->addItem( tr( "In datasource database" ), QgsLayerPropertiesDialog::DatasourceDatabase );
129
130 if ( mSaveOnlyCurrentStyle )
131 mStyleTypeComboBox->addItem( tr( "As default in local user database" ), QgsLayerPropertiesDialog::UserDatabase );
132}
133
135{
136 QgsSettings().setFlagValue( QStringLiteral( "style/lastStyleCategories" ), styleCategories() );
137 QDialog::accept();
138}
139
140void QgsMapLayerSaveStyleDialog::updateSaveButtonState()
141{
143 bool enabled { false };
144 switch ( type )
145 {
147 if ( saveOnlyCurrentStyle( ) )
148 {
149 enabled = ! mDbStyleNameEdit->text().isEmpty();
150 }
151 else
152 {
153 enabled = true;
154 }
155 break;
158 enabled = ! mFileWidget->filePath().isEmpty();
159 break;
161 enabled = true;
162 break;
163 }
164 buttonBox->button( QDialogButtonBox::Ok )->setEnabled( enabled );
165}
166
168{
169 SaveToDbSettings settings;
170 settings.name = mDbStyleNameEdit->text();
171 settings.description = mDbStyleDescriptionEdit->toPlainText();
172 settings.isDefault = mDbStyleUseAsDefault->isChecked();
173 settings.uiFileContent = mUiFileContent;
174 return settings;
175}
176
178{
179 return mFileWidget->filePath();
180}
181
186
191
192void QgsMapLayerSaveStyleDialog::readUiFileContent( const QString &filePath )
193{
194 QgsSettings myQSettings; // where we keep last used filter in persistent state
195 mUiFileContent = QString();
196
197 if ( filePath.isNull() )
198 {
199 return;
200 }
201
202 const QFileInfo myFI( filePath );
203 QFile uiFile( myFI.filePath() );
204
205 const QString myPath = myFI.path();
206 myQSettings.setValue( QStringLiteral( "style/lastStyleDir" ), myPath );
207
208 if ( uiFile.open( QIODevice::ReadOnly ) )
209 {
210 const QString content( uiFile.readAll() );
211 QDomDocument doc;
212
213 if ( !doc.setContent( content ) || doc.documentElement().tagName().compare( QLatin1String( "ui" ) ) )
214 {
215 QMessageBox::warning( this, tr( "Attach UI File" ),
216 tr( "The selected file does not appear to be a valid Qt Designer UI file." ) );
217 return;
218 }
219 mUiFileContent = content;
220 }
221}
222
223void QgsMapLayerSaveStyleDialog::setupMultipleStyles()
224{
225 // Show/hide part of the UI according to multiple style support
226 if ( ! mSaveOnlyCurrentStyle )
227 {
228 const QgsMapLayerStyleManager *styleManager { mLayer->styleManager() };
229 const QStringList constStyles = styleManager->styles();
230 for ( const QString &name : constStyles )
231 {
232 QListWidgetItem *item = new QListWidgetItem( name, mStylesWidget );
233 item->setCheckState( Qt::CheckState::Checked );
234 // Highlight the current style
235 if ( name == styleManager->currentStyle() )
236 {
237 item->setToolTip( tr( "Current style" ) );
238 QFont font { item->font() };
239 font.setItalic( true );
240 item->setFont( font );
241 }
242 mStylesWidget->addItem( item );
243 }
244 mDbStyleNameEdit->setToolTip( tr( "Leave blank to use style names or set the base name (an incremental number will be automatically appended)" ) );
245 }
246 else
247 {
248 mDbStyleNameEdit->setToolTip( QString() );
249 }
250
251 mStylesWidget->setVisible( ! mSaveOnlyCurrentStyle );
252 mStylesWidgetLabel->setVisible( ! mSaveOnlyCurrentStyle );
253
254 mDbStyleDescriptionEdit->setVisible( mSaveOnlyCurrentStyle );
255 descriptionLabel->setVisible( mSaveOnlyCurrentStyle );
256 mDbStyleUseAsDefault->setVisible( mSaveOnlyCurrentStyle );
257
258 populateStyleComboBox();
259}
260
262{
263 return mSaveOnlyCurrentStyle;
264}
265
267{
268 if ( mSaveOnlyCurrentStyle != saveOnlyCurrentStyle )
269 {
270 mSaveOnlyCurrentStyle = saveOnlyCurrentStyle;
271 setupMultipleStyles();
272 }
273}
274
276{
277 return mStylesWidget;
278}
279
281{
283
284 if ( mStyleTypeComboBox->currentData( ) == QgsLayerPropertiesDialog::SLD && mSldExportPng->isChecked() )
285 {
286 options.setFlag( Qgis::SldExportOption::Png );
287 }
288 return options;
289}
290
291void QgsMapLayerSaveStyleDialog::showHelp()
292{
293 QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#save-and-share-layer-properties" ) );
294}
@ Png
Export complex styles to separate PNG files for better compatibility with OGC servers.
@ Polygon
Polygons.
@ Vector
Vector layer.
QFlags< SldExportOption > SldExportOptions
Definition qgis.h:666
A label delegate being able to display html encoded content.
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:208
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:39
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.
QVariant data(const QModelIndex &index, int role) const override
bool setData(const QModelIndex &index, const QVariant &value, int role) override
QgsMapLayer::StyleCategories categories() const
Returns the categories as defined in the model.
int rowCount(const QModelIndex &=QModelIndex()) const override
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:76
Qgis::LayerType type
Definition qgsmaplayer.h:86
QFlags< StyleCategory > StyleCategories
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:64
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.