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