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