16 #include <QListWidgetItem>
17 #include <QMessageBox>
35 const QString myLastUsedDir = settings.
value( QStringLiteral(
"style/lastStyleDir" ), QDir::homePath() ).toString();
38 connect( mStyleTypeComboBox, qOverload<int>( &QComboBox::currentIndexChanged ),
this, [ = ](
int )
46 updateSaveButtonState();
50 connect( mDbStyleNameEdit, &QLineEdit::textChanged,
this, &QgsVectorLayerSaveStyleDialog::updateSaveButtonState );
51 mDbStyleDescriptionEdit->setTabChangesFocus(
true );
52 setTabOrder( mDbStyleNameEdit, mDbStyleDescriptionEdit );
53 setTabOrder( mDbStyleDescriptionEdit, mDbStyleUseAsDefault );
54 mDbStyleUIFileWidget->setDefaultRoot( myLastUsedDir );
55 mDbStyleUIFileWidget->setFilter( tr(
"Qt Designer UI file (*.ui)" ) );
57 connect( buttonBox, &QDialogButtonBox::helpRequested,
this, &QgsVectorLayerSaveStyleDialog::showHelp );
62 mFileWidget->setDefaultRoot( myLastUsedDir );
66 const QFileInfo tmplFileInfo( path );
67 settings.
setValue( QStringLiteral(
"style/lastStyleDir" ), tmplFileInfo.absolutePath() );
73 mModel->setCategories( lastStyleCategories );
74 mStyleCategoriesListView->setModel( mModel );
76 mStyleCategoriesListView->adjustSize();
78 setupMultipleStyles();
82 void QgsVectorLayerSaveStyleDialog::populateStyleComboBox()
84 QString providerName = mLayer->providerType();
85 if ( providerName == QLatin1String(
"ogr" ) )
87 providerName = mLayer->dataProvider()->storageType();
88 if ( providerName == QLatin1String(
"GPKG" ) )
89 providerName = QStringLiteral(
"GeoPackage" );
92 mStyleTypeComboBox->clear();
96 if ( mLayer->dataProvider()->isSaveAndLoadStyleToDatabaseSupported() )
99 if ( mSaveOnlyCurrentStyle )
109 void QgsVectorLayerSaveStyleDialog::updateSaveButtonState()
112 bool enabled {
false };
116 if ( saveOnlyCurrentStyle( ) )
118 enabled = ! mDbStyleNameEdit->text().isEmpty();
127 enabled = ! mFileWidget->filePath().isEmpty();
133 buttonBox->button( QDialogButtonBox::Ok )->setEnabled( enabled );
139 settings.
name = mDbStyleNameEdit->text();
140 settings.
description = mDbStyleDescriptionEdit->toPlainText();
141 settings.
isDefault = mDbStyleUseAsDefault->isChecked();
148 return mFileWidget->filePath();
153 return mModel->categories();
161 void QgsVectorLayerSaveStyleDialog::readUiFileContent(
const QString &filePath )
164 mUiFileContent = QString();
166 if ( filePath.isNull() )
171 const QFileInfo myFI( filePath );
172 QFile uiFile( myFI.filePath() );
174 const QString myPath = myFI.path();
175 myQSettings.
setValue( QStringLiteral(
"style/lastStyleDir" ), myPath );
177 if ( uiFile.open( QIODevice::ReadOnly ) )
179 const QString content( uiFile.readAll() );
182 if ( !doc.setContent( content ) || doc.documentElement().tagName().compare( QLatin1String(
"ui" ) ) )
184 QMessageBox::warning(
this, tr(
"Attach UI File" ),
185 tr(
"The selected file does not appear to be a valid Qt Designer UI file." ) );
188 mUiFileContent = content;
192 void QgsVectorLayerSaveStyleDialog::setupMultipleStyles()
195 if ( ! mSaveOnlyCurrentStyle )
198 const QStringList constStyles = styleManager->
styles();
199 for (
const QString &name : constStyles )
201 QListWidgetItem *item =
new QListWidgetItem( name, mStylesWidget );
202 item->setCheckState( Qt::CheckState::Checked );
204 if ( name == styleManager->currentStyle() )
206 item->setToolTip( tr(
"Current style" ) );
207 QFont font { item->font() };
208 font.setItalic(
true );
209 item->setFont( font );
211 mStylesWidget->addItem( item );
213 mDbStyleNameEdit->setToolTip( tr(
"Leave blank to use style names or set the base name (an incremental number will be automatically appended)" ) );
217 mDbStyleNameEdit->setToolTip( QString() );
220 mStylesWidget->setVisible( ! mSaveOnlyCurrentStyle );
221 mStylesWidgetLabel->setVisible( ! mSaveOnlyCurrentStyle );
223 mDbStyleDescriptionEdit->setVisible( mSaveOnlyCurrentStyle );
224 descriptionLabel->setVisible( mSaveOnlyCurrentStyle );
225 mDbStyleUseAsDefault->setVisible( mSaveOnlyCurrentStyle );
227 populateStyleComboBox();
232 return mSaveOnlyCurrentStyle;
237 if ( mSaveOnlyCurrentStyle != saveOnlyCurrentStyle )
239 mSaveOnlyCurrentStyle = saveOnlyCurrentStyle;
240 setupMultipleStyles();
246 return mStylesWidget;
250 void QgsVectorLayerSaveStyleDialog::showHelp()
252 QgsHelp::openHelp( QStringLiteral(
"introduction/general_tools.html#save-and-share-layer-properties" ) );