25#include <QListWidgetItem>
30#include "moc_qgsmaplayersavestyledialog.cpp"
32using namespace Qt::StringLiterals;
43 const QString myLastUsedDir = settings.
value( u
"style/lastStyleDir"_s, QDir::homePath() ).toString();
46 connect( mStyleTypeComboBox, qOverload<int>( &QComboBox::currentIndexChanged ),
this, [
this, layer](
int ) {
54 updateSaveButtonState();
58 connect( mDbStyleNameEdit, &QLineEdit::textChanged,
this, &QgsMapLayerSaveStyleDialog::updateSaveButtonState );
59 mDbStyleDescriptionEdit->setTabChangesFocus(
true );
60 setTabOrder( mDbStyleNameEdit, mDbStyleDescriptionEdit );
61 setTabOrder( mDbStyleDescriptionEdit, mDbStyleUseAsDefault );
62 mDbStyleUIFileWidget->setDefaultRoot( myLastUsedDir );
63 mDbStyleUIFileWidget->setFilter( tr(
"Qt Designer UI file (*.ui)" ) );
65 connect( buttonBox, &QDialogButtonBox::helpRequested,
this, &QgsMapLayerSaveStyleDialog::showHelp );
70 mFileWidget->setDefaultRoot( myLastUsedDir );
73 const QFileInfo tmplFileInfo( path );
74 settings.
setValue( u
"style/lastStyleDir"_s, tmplFileInfo.absolutePath() );
80 mModel->setCategories( lastStyleCategories );
81 mStyleCategoriesListView->setModel( mModel );
82 mStyleCategoriesListView->setWordWrap(
true );
86 connect( mSelectAllButton, &QPushButton::clicked,
this, &QgsMapLayerSaveStyleDialog::selectAll );
87 connect( mDeselectAllButton, &QPushButton::clicked,
this, &QgsMapLayerSaveStyleDialog::deselectAll );
88 connect( mInvertSelectionButton, &QPushButton::clicked,
this, &QgsMapLayerSaveStyleDialog::invertSelection );
90 mStyleCategoriesListView->adjustSize();
92 setupMultipleStyles();
95void QgsMapLayerSaveStyleDialog::invertSelection()
97 for (
int i = 0; i < mModel->
rowCount( QModelIndex() ); i++ )
99 QModelIndex index = mModel->index( i, 0 );
100 Qt::CheckState currentState = Qt::CheckState( mModel->
data( index, Qt::CheckStateRole ).toInt() );
101 Qt::CheckState newState = ( currentState == Qt::Checked ) ? Qt::Unchecked : Qt::Checked;
102 mModel->
setData( index, newState, Qt::CheckStateRole );
106void QgsMapLayerSaveStyleDialog::selectAll()
108 for (
int i = 0; i < mModel->rowCount( QModelIndex() ); i++ )
110 QModelIndex index = mModel->index( i, 0 );
111 mModel->setData( index, Qt::Checked, Qt::CheckStateRole );
115void QgsMapLayerSaveStyleDialog::deselectAll()
117 for (
int i = 0; i < mModel->rowCount( QModelIndex() ); i++ )
119 QModelIndex index = mModel->index( i, 0 );
120 mModel->setData( index, Qt::Unchecked, Qt::CheckStateRole );
124void QgsMapLayerSaveStyleDialog::populateStyleComboBox()
126 mStyleTypeComboBox->clear();
133 if ( mSaveOnlyCurrentStyle )
143void QgsMapLayerSaveStyleDialog::updateSaveButtonState()
146 bool enabled {
false };
152 enabled = !mDbStyleNameEdit->text().isEmpty();
161 enabled = !mFileWidget->filePath().isEmpty();
167 buttonBox->button( QDialogButtonBox::Ok )->setEnabled( enabled );
173 settings.
name = mDbStyleNameEdit->text();
174 settings.
description = mDbStyleDescriptionEdit->toPlainText();
175 settings.
isDefault = mDbStyleUseAsDefault->isChecked();
182 return mFileWidget->filePath();
187 return mModel->categories();
195void QgsMapLayerSaveStyleDialog::readUiFileContent(
const QString &filePath )
198 mUiFileContent = QString();
200 if ( filePath.isNull() )
205 const QFileInfo myFI( filePath );
206 QFile uiFile( myFI.filePath() );
208 const QString myPath = myFI.path();
209 myQSettings.
setValue( u
"style/lastStyleDir"_s, myPath );
211 if ( uiFile.open( QIODevice::ReadOnly ) )
213 const QString content( uiFile.readAll() );
216 if ( !doc.setContent( content ) || doc.documentElement().tagName().compare(
"ui"_L1 ) )
218 QMessageBox::warning(
this, tr(
"Attach UI File" ), tr(
"The selected file does not appear to be a valid Qt Designer UI file." ) );
221 mUiFileContent = content;
225void QgsMapLayerSaveStyleDialog::setupMultipleStyles()
228 if ( !mSaveOnlyCurrentStyle )
230 const QgsMapLayerStyleManager *styleManager { mLayer->styleManager() };
231 const QStringList constStyles = styleManager->
styles();
232 for (
const QString &name : constStyles )
234 QListWidgetItem *item =
new QListWidgetItem( name, mStylesWidget );
235 item->setCheckState( Qt::CheckState::Checked );
239 item->setToolTip( tr(
"Current style" ) );
240 QFont font { item->font() };
241 font.setItalic(
true );
242 item->setFont( font );
244 mStylesWidget->addItem( item );
246 mDbStyleNameEdit->setToolTip( tr(
"Leave blank to use style names or set the base name (an incremental number will be automatically appended)" ) );
250 mDbStyleNameEdit->setToolTip( QString() );
253 mStylesWidget->setVisible( !mSaveOnlyCurrentStyle );
254 mStylesWidgetLabel->setVisible( !mSaveOnlyCurrentStyle );
256 mDbStyleDescriptionEdit->setVisible( mSaveOnlyCurrentStyle );
257 descriptionLabel->setVisible( mSaveOnlyCurrentStyle );
258 mDbStyleUseAsDefault->setVisible( mSaveOnlyCurrentStyle );
260 populateStyleComboBox();
265 return mSaveOnlyCurrentStyle;
273 setupMultipleStyles();
279 return mStylesWidget;
293void QgsMapLayerSaveStyleDialog::showHelp()
295 QgsHelp::openHelp( u
"introduction/general_tools.html#save-and-share-layer-properties"_s );
@ Png
Export complex styles to separate PNG files for better compatibility with OGC servers.
QFlags< SldExportOption > SldExportOptions
A label delegate able to display HTML encoded content.
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...
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
StyleType
Style storage type.
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.
Model for layer style categories.
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.
QFlags< StyleCategory > StyleCategories
Stores settings for use within QGIS.
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.