34 setWindowTitle( tr(
"Database Styles Manager" ) );
36 mDeleteButton = mButtonBox->button( QDialogButtonBox::StandardButton::Close );
37 mDeleteButton->setText( tr(
"Delete Style" ) );
39 mLoadButton = mButtonBox->button( QDialogButtonBox::StandardButton::Open );
40 mLoadButton->setText( tr(
"Load Style" ) );
41 mCancelButton = mButtonBox->button( QDialogButtonBox::StandardButton::Cancel );
45 const QString myLastUsedDir = settings.
value( QStringLiteral(
"style/lastStyleDir" ), QDir::homePath() ).toString();
48 connect( mStyleTypeComboBox, qOverload<int>( &QComboBox::currentIndexChanged ),
this, [ = ](
int )
57 updateLoadButtonState();
75 mStyleCategoriesListView->setModel( mModel );
76 mStyleCategoriesListView->setWordWrap(
true );
80 switch ( mLayer->
type() )
83 mFileWidget->setFilter( tr(
"QGIS Layer Style File, SLD File" ) + QStringLiteral(
" (*.qml *.sld)" ) );
87 mFileWidget->setFilter( tr(
"All Styles" ) + QStringLiteral(
" (*.qml *.json);;" )
88 + tr(
"QGIS Layer Style File" ) + QStringLiteral(
" (*.qml);;" )
89 + tr(
"MapBox GL Style JSON File" ) + QStringLiteral(
" (*.json)" ) );
104 mFileWidget->setDefaultRoot( myLastUsedDir );
109 const QFileInfo tmplFileInfo( path );
110 settings.
setValue( QStringLiteral(
"style/lastStyleDir" ), tmplFileInfo.absolutePath() );
112 updateLoadButtonState();
116 mLoadButton->setDisabled(
true );
117 mDeleteButton->setDisabled(
true );
118 mRelatedTable->setEditTriggers( QTableWidget::NoEditTriggers );
119 mRelatedTable->horizontalHeader()->setStretchLastSection(
true );
120 mRelatedTable->setSelectionBehavior( QTableWidget::SelectRows );
121 mRelatedTable->verticalHeader()->setVisible(
false );
122 mOthersTable->setEditTriggers( QTableWidget::NoEditTriggers );
123 mOthersTable->horizontalHeader()->setStretchLastSection(
true );
124 mOthersTable->setSelectionBehavior( QTableWidget::SelectRows );
125 mOthersTable->verticalHeader()->setVisible(
false );
126 connect( mRelatedTable->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsMapLayerLoadStyleDialog::onRelatedTableSelectionChanged );
127 connect( mOthersTable->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsMapLayerLoadStyleDialog::onOthersTableSelectionChanged );
128 connect( mRelatedTable, &QTableWidget::doubleClicked,
this, &QDialog::accept );
129 connect( mOthersTable, &QTableWidget::doubleClicked,
this, &QDialog::accept );
130 connect( mCancelButton, &QPushButton::clicked,
this, &QDialog::reject );
131 connect( mButtonBox, &QDialogButtonBox::helpRequested,
this, &QgsMapLayerLoadStyleDialog::showHelp );
132 connect( mLoadButton, &QPushButton::clicked,
this, &QDialog::accept );
133 connect( mDeleteButton, &QPushButton::clicked,
this, &QgsMapLayerLoadStyleDialog::deleteStyleFromDB );
134 connect(
this, &QgsMapLayerLoadStyleDialog::rejected, [ = ]
139 setTabOrder( mRelatedTable, mOthersTable );
141 mStyleCategoriesListView->adjustSize();
144 connect( mSelectAllButton, &QPushButton::clicked,
this, &QgsMapLayerLoadStyleDialog::selectAll );
145 connect( mDeselectAllButton, &QPushButton::clicked,
this, &QgsMapLayerLoadStyleDialog::deselectAll );
146 connect( mInvertSelectionButton, &QPushButton::clicked,
this, &QgsMapLayerLoadStyleDialog::invertSelection );
149void QgsMapLayerLoadStyleDialog::invertSelection()
151 for (
int i = 0; i < mModel->
rowCount( QModelIndex() ); i++ )
153 QModelIndex index = mModel->index( i, 0 );
154 Qt::CheckState currentState = Qt::CheckState( mModel->
data( index, Qt::CheckStateRole ).toInt() );
155 Qt::CheckState newState = ( currentState == Qt::Checked ) ? Qt::Unchecked : Qt::Checked;
156 mModel->
setData( index, newState, Qt::CheckStateRole );
160void QgsMapLayerLoadStyleDialog::selectAll()
162 for (
int i = 0; i < mModel->
rowCount( QModelIndex() ); i++ )
164 QModelIndex index = mModel->index( i, 0 );
165 mModel->
setData( index, Qt::Checked, Qt::CheckStateRole );
169void QgsMapLayerLoadStyleDialog::deselectAll()
171 for (
int i = 0; i < mModel->
rowCount( QModelIndex() ); i++ )
173 QModelIndex index = mModel->index( i, 0 );
174 mModel->
setData( index, Qt::Unchecked, Qt::CheckStateRole );
188 const QFileInfo fi( mFileWidget->filePath() );
189 if ( fi.exists() && fi.suffix().compare( QStringLiteral(
"sld" ), Qt::CaseInsensitive ) == 0 )
197 return QFileInfo( mFileWidget->filePath() ).suffix();
202 return mFileWidget->filePath();
208 mSectionLimit = sectionLimit;
209 const int relatedTableNOfCols = sectionLimit > 0 ? 2 : 1;
210 const int othersTableNOfCols = ( sectionLimit >= 0 && ids.count() - sectionLimit > 0 ) ? 2 : 1;
211 const QString twoColsHeader( QStringLiteral(
"Name;Description" ) );
212 const QString oneColsHeader( QStringLiteral(
"No styles found in the database" ) );
213 const QString relatedTableHeader = relatedTableNOfCols == 1 ? oneColsHeader : twoColsHeader;
214 const QString othersTableHeader = othersTableNOfCols == 1 ? oneColsHeader : twoColsHeader;
216 mRelatedTable->setColumnCount( relatedTableNOfCols );
217 mOthersTable->setColumnCount( othersTableNOfCols );
218 mRelatedTable->setHorizontalHeaderLabels( relatedTableHeader.split(
';' ) );
219 mOthersTable->setHorizontalHeaderLabels( othersTableHeader.split(
';' ) );
220 mRelatedTable->setRowCount( sectionLimit );
221 mOthersTable->setRowCount( sectionLimit >= 0 ? ( ids.count() - sectionLimit ) : 0 );
222 mRelatedTable->setDisabled( relatedTableNOfCols == 1 );
223 mOthersTable->setDisabled( othersTableNOfCols == 1 );
225 if ( sectionLimit >= 0 )
227 for (
int i = 0; i < sectionLimit; i++ )
229 QTableWidgetItem *item =
new QTableWidgetItem( names.value( i, QString() ) );
230 item->setData( Qt::UserRole, ids[i] );
231 mRelatedTable->setItem( i, 0, item );
232 mRelatedTable->setItem( i, 1,
new QTableWidgetItem( descriptions.value( i, QString() ) ) );
234 for (
int i = sectionLimit; i < ids.count(); i++ )
236 const int j = i - sectionLimit;
237 QTableWidgetItem *item =
new QTableWidgetItem( names.value( i, QString() ) );
238 item->setData( Qt::UserRole, ids[i] );
239 mOthersTable->setItem( j, 0, item );
240 mOthersTable->setItem( j, 1,
new QTableWidgetItem( descriptions.value( i, QString() ) ) );
247 return mSelectedStyleId;
250void QgsMapLayerLoadStyleDialog::onRelatedTableSelectionChanged()
252 selectionChanged( mRelatedTable );
253 if ( mRelatedTable->selectionModel()->hasSelection() )
255 if ( mOthersTable->selectionModel()->hasSelection() )
257 disconnect( mOthersTable->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsMapLayerLoadStyleDialog::onOthersTableSelectionChanged );
258 const QTableWidgetSelectionRange range( 0, 0, mOthersTable->rowCount() - 1, mOthersTable->columnCount() - 1 );
259 mOthersTable->setRangeSelected( range,
false );
260 connect( mOthersTable->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsMapLayerLoadStyleDialog::onOthersTableSelectionChanged );
265void QgsMapLayerLoadStyleDialog::onOthersTableSelectionChanged()
267 selectionChanged( mOthersTable );
268 if ( mOthersTable->selectionModel()->hasSelection() )
270 if ( mRelatedTable->selectionModel()->hasSelection() )
272 disconnect( mRelatedTable->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsMapLayerLoadStyleDialog::onRelatedTableSelectionChanged );
273 const QTableWidgetSelectionRange range( 0, 0, mRelatedTable->rowCount() - 1, mRelatedTable->columnCount() - 1 );
274 mRelatedTable->setRangeSelected( range,
false );
275 connect( mRelatedTable->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsMapLayerLoadStyleDialog::onRelatedTableSelectionChanged );
280void QgsMapLayerLoadStyleDialog::selectionChanged( QTableWidget *styleTable )
282 QTableWidgetItem *item =
nullptr;
283 const QList<QTableWidgetItem *> selected = styleTable->selectedItems();
285 if ( !selected.isEmpty() )
287 item = selected.at( 0 );
288 mSelectedStyleName = item->text();
289 mSelectedStyleId = item->data( Qt::UserRole ).toString();
290 mLoadButton->setEnabled(
true );
291 mDeleteButton->setEnabled(
true );
295 mSelectedStyleName.clear();
296 mSelectedStyleId.clear();
297 mLoadButton->setEnabled(
false );
298 mDeleteButton->setEnabled(
false );
301 updateLoadButtonState();
312void QgsMapLayerLoadStyleDialog::deleteStyleFromDB()
315 const QString opInfo = QObject::tr(
"Delete style %1 from %2" ).arg( mSelectedStyleName, mLayer->
providerType() );
317 if ( QMessageBox::question(
nullptr, QObject::tr(
"Delete Style" ),
318 QObject::tr(
"Are you sure you want to delete the style %1?" ).arg( mSelectedStyleName ),
319 QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
323 if ( !msgError.isNull() )
326 QMessageBox::warning(
this, opInfo, tr(
"%1: fail. %2" ).arg( opInfo, msgError ) );
333 mRelatedTable->setRowCount( 0 );
334 mOthersTable->setRowCount( 0 );
338 QStringList ids, names, descriptions;
341 if ( !errorMsg.isNull() )
343 QMessageBox::warning(
this, tr(
"Error occurred while retrieving styles from database" ), errorMsg );
352void QgsMapLayerLoadStyleDialog::updateLoadButtonState()
356 && ( mRelatedTable->selectionModel()->hasSelection() || mOthersTable->selectionModel()->hasSelection()
362void QgsMapLayerLoadStyleDialog::showHelp()
364 QgsHelp::openHelp( QStringLiteral(
"introduction/general_tools.html#save-and-share-layer-properties" ) );
@ Group
Composite group layer. Added in QGIS 3.24.
@ Plugin
Plugin based layer.
@ TiledScene
Tiled scene layer. Added in QGIS 3.34.
@ Annotation
Contains freeform, georeferenced annotations. Added in QGIS 3.16.
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
@ Mesh
Mesh layer. Added in QGIS 3.2.
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
A label delegate being able to display html encoded content.
virtual Qgis::ProviderStyleStorageCapabilities styleStorageCapabilities() const
Returns the style storage capabilities.
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.
QgsMapLayer::StyleCategories styleCategories() const
Returns the list of selected style categories the user has opted to load.
void initializeLists(const QStringList &ids, const QStringList &names, const QStringList &descriptions, int sectionLimit)
Initialize list of database stored styles.
QString selectedStyleId()
Returns the ID of the selected database stored style.
QgsMapLayerLoadStyleDialog(QgsMapLayer *layer, QWidget *parent=nullptr)
Constructor for QgsMapLayerLoadStyleDialog, associated with the specified map layer.
QString filePath() const
Returns the full path to the selected layer style source file.
QString fileExtension() const
Returns the file extension for the selected layer style source file.
QgsLayerPropertiesDialog::StyleType currentStyleType() const
Returns the selected style type.
Model for layer style categories.
void setCategories(QgsMapLayer::StyleCategories categories)
Reset the model data.
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
Base class for all map layer types.
virtual bool deleteStyleFromDatabase(const QString &styleId, QString &msgError)
Deletes a style from the database.
QString providerType() const
Returns the provider type (provider key) for this layer.
virtual int listStylesInDatabase(QStringList &ids, QStringList &names, QStringList &descriptions, QString &msgError)
Lists all the style in db split into related to the layer and not related to.
QFlags< StyleCategory > StyleCategories
virtual Q_INVOKABLE QgsDataProvider * dataProvider()
Returns the layer's data provider, it may be nullptr.
This class is a composition of two QSettings instances:
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.
#define QgsDebugError(str)