16 #include <QMessageBox>
22 #include "qgsvectorlayerproperties.h"
35 setWindowTitle( tr(
"Database Styles Manager" ) );
37 mDeleteButton = mButtonBox->button( QDialogButtonBox::StandardButton::Close );
38 mDeleteButton->setText( tr(
"Delete Style" ) );
40 mLoadButton = mButtonBox->button( QDialogButtonBox::StandardButton::Open );
41 mLoadButton->setText( tr(
"Load Style" ) );
42 mCancelButton = mButtonBox->button( QDialogButtonBox::StandardButton::Cancel );
47 if ( providerName == QLatin1String(
"ogr" ) )
51 if ( providerName == QLatin1String(
"GPKG" ) )
52 providerName = QStringLiteral(
"GeoPackage" );
55 QString myLastUsedDir = settings.
value( QStringLiteral(
"style/lastStyleDir" ), QDir::homePath() ).toString();
58 connect( mStyleTypeComboBox, qgis::overload<int>::of( &QComboBox::currentIndexChanged ),
this, [ = ](
int )
62 mFileLabel->setVisible( !vl || type != QgsVectorLayerProperties::StyleType::DB );
63 mFileWidget->setVisible( !vl || type != QgsVectorLayerProperties::StyleType::DB );
66 mFromDbWidget->setVisible( type == QgsVectorLayerProperties::StyleType::DB );
71 mFromDbWidget->setVisible(
false );
72 mDeleteButton->setVisible(
false );
75 mStyleCategoriesListView->setEnabled( !vl ||
currentStyleType() != QgsVectorLayerProperties::StyleType::SLD );
76 updateLoadButtonState();
78 mStyleTypeComboBox->addItem( tr(
"From File" ), QgsVectorLayerProperties::QML );
80 if (
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( mLayer ) )
82 if ( vl->dataProvider()->isSaveAndLoadStyleToDatabaseSupported() )
84 mStyleTypeComboBox->addItem( tr(
"From Database (%1)" ).arg( providerName ), QgsVectorLayerProperties::StyleType::DB );
85 if ( settings.
value( QStringLiteral(
"style/lastLoadStyleTypeSelection" ) ) == QgsVectorLayerProperties::StyleType::DB )
87 mStyleTypeComboBox->setCurrentIndex( mStyleTypeComboBox->findData( QgsVectorLayerProperties::StyleType::DB ) );
96 mStyleCategoriesListView->setModel( mModel );
99 switch ( mLayer->
type() )
102 mFileWidget->setFilter( tr(
"QGIS Layer Style File, SLD File" ) + QStringLiteral(
" (*.qml *.sld)" ) );
106 mFileWidget->setFilter( tr(
"All Styles" ) + QStringLiteral(
" (*.qml *.json);;" )
107 + tr(
"QGIS Layer Style File" ) + QStringLiteral(
" (*.qml);;" )
108 + tr(
"MapBox GL Style JSON File" ) + QStringLiteral(
" (*.json)" ) );
120 mFileWidget->setDefaultRoot( myLastUsedDir );
124 mStyleCategoriesListView->setEnabled( !vl ||
currentStyleType() != QgsVectorLayerProperties::SLD );
126 QFileInfo tmplFileInfo( path );
127 settings.
setValue( QStringLiteral(
"style/lastStyleDir" ), tmplFileInfo.absolutePath() );
129 updateLoadButtonState();
133 mLoadButton->setDisabled(
true );
134 mDeleteButton->setDisabled(
true );
135 mRelatedTable->setEditTriggers( QTableWidget::NoEditTriggers );
136 mRelatedTable->horizontalHeader()->setStretchLastSection(
true );
137 mRelatedTable->setSelectionBehavior( QTableWidget::SelectRows );
138 mRelatedTable->verticalHeader()->setVisible(
false );
139 mOthersTable->setEditTriggers( QTableWidget::NoEditTriggers );
140 mOthersTable->horizontalHeader()->setStretchLastSection(
true );
141 mOthersTable->setSelectionBehavior( QTableWidget::SelectRows );
142 mOthersTable->verticalHeader()->setVisible(
false );
143 connect( mRelatedTable->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsMapLayerLoadStyleDialog::onRelatedTableSelectionChanged );
144 connect( mOthersTable->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsMapLayerLoadStyleDialog::onOthersTableSelectionChanged );
145 connect( mRelatedTable, &QTableWidget::doubleClicked,
this, &QDialog::accept );
146 connect( mOthersTable, &QTableWidget::doubleClicked,
this, &QDialog::accept );
147 connect( mCancelButton, &QPushButton::clicked,
this, &QDialog::reject );
148 connect( mButtonBox, &QDialogButtonBox::helpRequested,
this, &QgsMapLayerLoadStyleDialog::showHelp );
149 connect( mLoadButton, &QPushButton::clicked,
this, &QDialog::accept );
150 connect( mDeleteButton, &QPushButton::clicked,
this, &QgsMapLayerLoadStyleDialog::deleteStyleFromDB );
151 connect(
this, &QgsMapLayerLoadStyleDialog::rejected, [ = ]
156 setTabOrder( mRelatedTable, mOthersTable );
158 mStyleCategoriesListView->adjustSize();
168 QgsVectorLayerProperties::StyleType type = mStyleTypeComboBox->currentData().value<QgsVectorLayerProperties::StyleType>();
169 if ( type == QgsVectorLayerProperties::QML )
171 QFileInfo fi( mFileWidget->filePath() );
172 if ( fi.exists() && fi.suffix().compare( QStringLiteral(
"sld" ), Qt::CaseInsensitive ) == 0 )
173 type = QgsVectorLayerProperties::SLD;
180 return QFileInfo( mFileWidget->filePath() ).suffix();
185 return mFileWidget->filePath();
191 mSectionLimit = sectionLimit;
192 int relatedTableNOfCols = sectionLimit > 0 ? 2 : 1;
193 int othersTableNOfCols = ( sectionLimit >= 0 && ids.count() - sectionLimit > 0 ) ? 2 : 1;
194 QString twoColsHeader( QStringLiteral(
"Name;Description" ) );
195 QString oneColsHeader( QStringLiteral(
"No styles found in the database" ) );
196 QString relatedTableHeader = relatedTableNOfCols == 1 ? oneColsHeader : twoColsHeader;
197 QString othersTableHeader = othersTableNOfCols == 1 ? oneColsHeader : twoColsHeader;
199 mRelatedTable->setColumnCount( relatedTableNOfCols );
200 mOthersTable->setColumnCount( othersTableNOfCols );
201 mRelatedTable->setHorizontalHeaderLabels( relatedTableHeader.split(
';' ) );
202 mOthersTable->setHorizontalHeaderLabels( othersTableHeader.split(
';' ) );
203 mRelatedTable->setRowCount( sectionLimit );
204 mOthersTable->setRowCount( sectionLimit >= 0 ? ( ids.count() - sectionLimit ) : 0 );
205 mRelatedTable->setDisabled( relatedTableNOfCols == 1 );
206 mOthersTable->setDisabled( othersTableNOfCols == 1 );
208 if ( sectionLimit >= 0 )
210 for (
int i = 0; i < sectionLimit; i++ )
212 QTableWidgetItem *item =
new QTableWidgetItem( names.value( i, QString() ) );
213 item->setData( Qt::UserRole, ids[i] );
214 mRelatedTable->setItem( i, 0, item );
215 mRelatedTable->setItem( i, 1,
new QTableWidgetItem( descriptions.value( i, QString() ) ) );
217 for (
int i = sectionLimit; i < ids.count(); i++ )
219 int j = i - sectionLimit;
220 QTableWidgetItem *item =
new QTableWidgetItem( names.value( i, QString() ) );
221 item->setData( Qt::UserRole, ids[i] );
222 mOthersTable->setItem( j, 0, item );
223 mOthersTable->setItem( j, 1,
new QTableWidgetItem( descriptions.value( i, QString() ) ) );
230 return mSelectedStyleId;
233 void QgsMapLayerLoadStyleDialog::onRelatedTableSelectionChanged()
235 selectionChanged( mRelatedTable );
236 if ( mRelatedTable->selectionModel()->hasSelection() )
238 if ( mOthersTable->selectionModel()->hasSelection() )
240 disconnect( mOthersTable->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsMapLayerLoadStyleDialog::onOthersTableSelectionChanged );
241 QTableWidgetSelectionRange range( 0, 0, mOthersTable->rowCount() - 1, mOthersTable->columnCount() - 1 );
242 mOthersTable->setRangeSelected( range,
false );
243 connect( mOthersTable->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsMapLayerLoadStyleDialog::onOthersTableSelectionChanged );
248 void QgsMapLayerLoadStyleDialog::onOthersTableSelectionChanged()
250 selectionChanged( mOthersTable );
251 if ( mOthersTable->selectionModel()->hasSelection() )
253 if ( mRelatedTable->selectionModel()->hasSelection() )
255 disconnect( mRelatedTable->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsMapLayerLoadStyleDialog::onRelatedTableSelectionChanged );
256 QTableWidgetSelectionRange range( 0, 0, mRelatedTable->rowCount() - 1, mRelatedTable->columnCount() - 1 );
257 mRelatedTable->setRangeSelected( range,
false );
258 connect( mRelatedTable->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsMapLayerLoadStyleDialog::onRelatedTableSelectionChanged );
263 void QgsMapLayerLoadStyleDialog::selectionChanged( QTableWidget *styleTable )
265 QTableWidgetItem *item =
nullptr;
266 QList<QTableWidgetItem *> selected = styleTable->selectedItems();
268 if ( !selected.isEmpty() )
270 item = selected.at( 0 );
271 mSelectedStyleName = item->text();
272 mSelectedStyleId = item->data( Qt::UserRole ).toString();
273 mLoadButton->setEnabled(
true );
274 mDeleteButton->setEnabled(
true );
278 mSelectedStyleName.clear();
279 mSelectedStyleId.clear();
280 mLoadButton->setEnabled(
false );
281 mDeleteButton->setEnabled(
false );
284 updateLoadButtonState();
295 void QgsMapLayerLoadStyleDialog::deleteStyleFromDB()
302 QString opInfo = QObject::tr(
"Delete style %1 from %2" ).arg( mSelectedStyleName, mLayer->
providerType() );
304 if ( QMessageBox::question(
nullptr, QObject::tr(
"Delete Style" ),
305 QObject::tr(
"Are you sure you want to delete the style %1?" ).arg( mSelectedStyleName ),
306 QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
310 if ( !msgError.isNull() )
313 QMessageBox::warning(
this, opInfo, tr(
"%1: fail. %2" ).arg( opInfo, msgError ) );
320 mRelatedTable->setRowCount( 0 );
321 mOthersTable->setRowCount( 0 );
325 QStringList ids, names, descriptions;
328 if ( !errorMsg.isNull() )
330 QMessageBox::warning(
this, tr(
"Error occurred while retrieving styles from database" ), errorMsg );
339 void QgsMapLayerLoadStyleDialog::updateLoadButtonState()
344 mLoadButton->setEnabled( ( type == QgsVectorLayerProperties::DB
345 && ( mRelatedTable->selectionModel()->hasSelection() || mOthersTable->selectionModel()->hasSelection()
347 ( type != QgsVectorLayerProperties::DB && !mFileWidget->filePath().isEmpty() ) );
351 mLoadButton->setEnabled( !mFileWidget->filePath().isEmpty() );
355 void QgsMapLayerLoadStyleDialog::showHelp()
357 QgsHelp::openHelp( QStringLiteral(
"introduction/general_tools.html#save-and-share-layer-properties" ) );