QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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 "moc_qgsmaplayersavestyledialog.cpp"
22#include "qgssettings.h"
23#include "qgshelp.h"
24#include "qgsgui.h"
27#include "qgsvectorlayer.h"
28
30 : QDialog( parent )
31 , mLayer( layer )
32{
33 setupUi( this );
35
36 QgsSettings settings;
37
38 const QString myLastUsedDir = settings.value( QStringLiteral( "style/lastStyleDir" ), QDir::homePath() ).toString();
39
40 // save style type combobox
41 connect( mStyleTypeComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [ = ]( int )
42 {
46 mSaveToDbWidget->setVisible( type == QgsLayerPropertiesDialog::DatasourceDatabase );
47 mSaveToSldWidget->setVisible( type == QgsLayerPropertiesDialog::SLD && layer->type() == Qgis::LayerType::Vector && static_cast<QgsVectorLayer *>( layer )->geometryType() == Qgis::GeometryType::Polygon );
48 mStyleCategoriesListView->setEnabled( type != QgsLayerPropertiesDialog::SLD );
49 mFileWidget->setFilter( type == QgsLayerPropertiesDialog::QML ? tr( "QGIS Layer Style File (*.qml)" ) : tr( "SLD File (*.sld)" ) );
50 updateSaveButtonState();
51 } );
52
53 // Save to DB setup
54 connect( mDbStyleNameEdit, &QLineEdit::textChanged, this, &QgsMapLayerSaveStyleDialog::updateSaveButtonState );
55 mDbStyleDescriptionEdit->setTabChangesFocus( true );
56 setTabOrder( mDbStyleNameEdit, mDbStyleDescriptionEdit );
57 setTabOrder( mDbStyleDescriptionEdit, mDbStyleUseAsDefault );
58 mDbStyleUIFileWidget->setDefaultRoot( myLastUsedDir );
59 mDbStyleUIFileWidget->setFilter( tr( "Qt Designer UI file (*.ui)" ) );
60 connect( mDbStyleUIFileWidget, &QgsFileWidget::fileChanged, this, &QgsMapLayerSaveStyleDialog::readUiFileContent );
61 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsMapLayerSaveStyleDialog::showHelp );
62
63 // save to file setup
64 connect( mFileWidget, &QgsFileWidget::fileChanged, this, &QgsMapLayerSaveStyleDialog::updateSaveButtonState );
65 mFileWidget->setStorageMode( QgsFileWidget::SaveFile );
66 mFileWidget->setDefaultRoot( myLastUsedDir );
67 connect( mFileWidget, &QgsFileWidget::fileChanged, this, [ = ]( const QString & path )
68 {
69 QgsSettings settings;
70 const QFileInfo tmplFileInfo( path );
71 settings.setValue( QStringLiteral( "style/lastStyleDir" ), tmplFileInfo.absolutePath() );
72 } );
73
74 // fill style categories
75 mModel = new QgsMapLayerStyleCategoriesModel( mLayer->type(), this );
76 const QgsMapLayer::StyleCategories lastStyleCategories = settings.flagValue( QStringLiteral( "style/lastStyleCategories" ), QgsMapLayer::AllStyleCategories );
77 mModel->setCategories( lastStyleCategories );
78 mStyleCategoriesListView->setModel( mModel );
79 mStyleCategoriesListView->setWordWrap( true );
80 mStyleCategoriesListView->setItemDelegate( new QgsCategoryDisplayLabelDelegate( this ) );
81
82 // select and deselect all categories
83 connect( mSelectAllButton, &QPushButton::clicked, this, &QgsMapLayerSaveStyleDialog::selectAll );
84 connect( mDeselectAllButton, &QPushButton::clicked, this, &QgsMapLayerSaveStyleDialog::deselectAll );
85 connect( mInvertSelectionButton, &QPushButton::clicked, this, &QgsMapLayerSaveStyleDialog::invertSelection );
86
87 mStyleCategoriesListView->adjustSize();
88
89 setupMultipleStyles();
90
91}
92
93void QgsMapLayerSaveStyleDialog::invertSelection()
94{
95 for ( int i = 0; i < mModel->rowCount( QModelIndex() ); i++ )
96 {
97 QModelIndex index = mModel->index( i, 0 );
98 Qt::CheckState currentState = Qt::CheckState( mModel->data( index, Qt::CheckStateRole ).toInt() );
99 Qt::CheckState newState = ( currentState == Qt::Checked ) ? Qt::Unchecked : Qt::Checked;
100 mModel->setData( index, newState, Qt::CheckStateRole );
101 }
102}
103
104void QgsMapLayerSaveStyleDialog::selectAll()
105{
106 for ( int i = 0; i < mModel->rowCount( QModelIndex() ); i++ )
107 {
108 QModelIndex index = mModel->index( i, 0 );
109 mModel->setData( index, Qt::Checked, Qt::CheckStateRole );
110 }
111}
112
113void QgsMapLayerSaveStyleDialog::deselectAll()
114{
115 for ( int i = 0; i < mModel->rowCount( QModelIndex() ); i++ )
116 {
117 QModelIndex index = mModel->index( i, 0 );
118 mModel->setData( index, Qt::Unchecked, Qt::CheckStateRole );
119 }
120}
121
122void QgsMapLayerSaveStyleDialog::populateStyleComboBox()
123{
124 mStyleTypeComboBox->clear();
125 mStyleTypeComboBox->addItem( tr( "As QGIS QML style file" ), QgsLayerPropertiesDialog::QML );
126 mStyleTypeComboBox->addItem( tr( "As SLD style file" ), QgsLayerPropertiesDialog::SLD );
127
129 mStyleTypeComboBox->addItem( tr( "In datasource database" ), QgsLayerPropertiesDialog::DatasourceDatabase );
130
131 if ( mSaveOnlyCurrentStyle )
132 mStyleTypeComboBox->addItem( tr( "As default in local user database" ), QgsLayerPropertiesDialog::UserDatabase );
133}
134
136{
137 QgsSettings().setFlagValue( QStringLiteral( "style/lastStyleCategories" ), styleCategories() );
138 QDialog::accept();
139}
140
141void QgsMapLayerSaveStyleDialog::updateSaveButtonState()
142{
144 bool enabled { false };
145 switch ( type )
146 {
148 if ( saveOnlyCurrentStyle( ) )
149 {
150 enabled = ! mDbStyleNameEdit->text().isEmpty();
151 }
152 else
153 {
154 enabled = true;
155 }
156 break;
159 enabled = ! mFileWidget->filePath().isEmpty();
160 break;
162 enabled = true;
163 break;
164 }
165 buttonBox->button( QDialogButtonBox::Ok )->setEnabled( enabled );
166}
167
169{
170 SaveToDbSettings settings;
171 settings.name = mDbStyleNameEdit->text();
172 settings.description = mDbStyleDescriptionEdit->toPlainText();
173 settings.isDefault = mDbStyleUseAsDefault->isChecked();
174 settings.uiFileContent = mUiFileContent;
175 return settings;
176}
177
179{
180 return mFileWidget->filePath();
181}
182
187
192
193void QgsMapLayerSaveStyleDialog::readUiFileContent( const QString &filePath )
194{
195 QgsSettings myQSettings; // where we keep last used filter in persistent state
196 mUiFileContent = QString();
197
198 if ( filePath.isNull() )
199 {
200 return;
201 }
202
203 const QFileInfo myFI( filePath );
204 QFile uiFile( myFI.filePath() );
205
206 const QString myPath = myFI.path();
207 myQSettings.setValue( QStringLiteral( "style/lastStyleDir" ), myPath );
208
209 if ( uiFile.open( QIODevice::ReadOnly ) )
210 {
211 const QString content( uiFile.readAll() );
212 QDomDocument doc;
213
214 if ( !doc.setContent( content ) || doc.documentElement().tagName().compare( QLatin1String( "ui" ) ) )
215 {
216 QMessageBox::warning( this, tr( "Attach UI File" ),
217 tr( "The selected file does not appear to be a valid Qt Designer UI file." ) );
218 return;
219 }
220 mUiFileContent = content;
221 }
222}
223
224void QgsMapLayerSaveStyleDialog::setupMultipleStyles()
225{
226 // Show/hide part of the UI according to multiple style support
227 if ( ! mSaveOnlyCurrentStyle )
228 {
229 const QgsMapLayerStyleManager *styleManager { mLayer->styleManager() };
230 const QStringList constStyles = styleManager->styles();
231 for ( const QString &name : constStyles )
232 {
233 QListWidgetItem *item = new QListWidgetItem( name, mStylesWidget );
234 item->setCheckState( Qt::CheckState::Checked );
235 // Highlight the current style
236 if ( name == styleManager->currentStyle() )
237 {
238 item->setToolTip( tr( "Current style" ) );
239 QFont font { item->font() };
240 font.setItalic( true );
241 item->setFont( font );
242 }
243 mStylesWidget->addItem( item );
244 }
245 mDbStyleNameEdit->setToolTip( tr( "Leave blank to use style names or set the base name (an incremental number will be automatically appended)" ) );
246 }
247 else
248 {
249 mDbStyleNameEdit->setToolTip( QString() );
250 }
251
252 mStylesWidget->setVisible( ! mSaveOnlyCurrentStyle );
253 mStylesWidgetLabel->setVisible( ! mSaveOnlyCurrentStyle );
254
255 mDbStyleDescriptionEdit->setVisible( mSaveOnlyCurrentStyle );
256 descriptionLabel->setVisible( mSaveOnlyCurrentStyle );
257 mDbStyleUseAsDefault->setVisible( mSaveOnlyCurrentStyle );
258
259 populateStyleComboBox();
260}
261
263{
264 return mSaveOnlyCurrentStyle;
265}
266
268{
269 if ( mSaveOnlyCurrentStyle != saveOnlyCurrentStyle )
270 {
271 mSaveOnlyCurrentStyle = saveOnlyCurrentStyle;
272 setupMultipleStyles();
273 }
274}
275
277{
278 return mStylesWidget;
279}
280
282{
284
285 if ( mStyleTypeComboBox->currentData( ) == QgsLayerPropertiesDialog::SLD && mSldExportPng->isChecked() )
286 {
287 options.setFlag( Qgis::SldExportOption::Png );
288 }
289 return options;
290}
291
292void QgsMapLayerSaveStyleDialog::showHelp()
293{
294 QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#save-and-share-layer-properties" ) );
295}
@ 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:209
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.