20#include "moc_qgsmaplayerloadstyledialog.cpp"
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 );
46 const QString myLastUsedDir = settings.
value( QStringLiteral(
"style/lastStyleDir" ), QDir::homePath() ).toString();
49 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);;" ) + tr(
"QGIS Layer Style File" ) + QStringLiteral(
" (*.qml);;" ) + tr(
"MapBox GL Style JSON File" ) + QStringLiteral(
" (*.json)" ) );
101 mFileWidget->setDefaultRoot( myLastUsedDir );
105 const QFileInfo tmplFileInfo( path );
106 settings.
setValue( QStringLiteral(
"style/lastStyleDir" ), tmplFileInfo.absolutePath() );
108 updateLoadButtonState();
112 mLoadButton->setDisabled(
true );
113 mDeleteButton->setDisabled(
true );
114 mRelatedTable->setEditTriggers( QTableWidget::NoEditTriggers );
115 mRelatedTable->horizontalHeader()->setStretchLastSection(
true );
116 mRelatedTable->setSelectionBehavior( QTableWidget::SelectRows );
117 mRelatedTable->verticalHeader()->setVisible(
false );
118 mOthersTable->setEditTriggers( QTableWidget::NoEditTriggers );
119 mOthersTable->horizontalHeader()->setStretchLastSection(
true );
120 mOthersTable->setSelectionBehavior( QTableWidget::SelectRows );
121 mOthersTable->verticalHeader()->setVisible(
false );
122 connect( mRelatedTable->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsMapLayerLoadStyleDialog::onRelatedTableSelectionChanged );
123 connect( mOthersTable->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsMapLayerLoadStyleDialog::onOthersTableSelectionChanged );
124 connect( mRelatedTable, &QTableWidget::doubleClicked,
this, &QDialog::accept );
125 connect( mOthersTable, &QTableWidget::doubleClicked,
this, &QDialog::accept );
126 connect( mCancelButton, &QPushButton::clicked,
this, &QDialog::reject );
127 connect( mButtonBox, &QDialogButtonBox::helpRequested,
this, &QgsMapLayerLoadStyleDialog::showHelp );
128 connect( mLoadButton, &QPushButton::clicked,
this, &QDialog::accept );
129 connect( mDeleteButton, &QPushButton::clicked,
this, &QgsMapLayerLoadStyleDialog::deleteStyleFromDB );
130 connect(
this, &QgsMapLayerLoadStyleDialog::rejected, [=] {
134 setTabOrder( mRelatedTable, mOthersTable );
136 mStyleCategoriesListView->adjustSize();
139 connect( mSelectAllButton, &QPushButton::clicked,
this, &QgsMapLayerLoadStyleDialog::selectAll );
140 connect( mDeselectAllButton, &QPushButton::clicked,
this, &QgsMapLayerLoadStyleDialog::deselectAll );
141 connect( mInvertSelectionButton, &QPushButton::clicked,
this, &QgsMapLayerLoadStyleDialog::invertSelection );
144void QgsMapLayerLoadStyleDialog::invertSelection()
146 for (
int i = 0; i < mModel->
rowCount( QModelIndex() ); i++ )
148 QModelIndex index = mModel->index( i, 0 );
149 Qt::CheckState currentState = Qt::CheckState( mModel->
data( index, Qt::CheckStateRole ).toInt() );
150 Qt::CheckState newState = ( currentState == Qt::Checked ) ? Qt::Unchecked : Qt::Checked;
151 mModel->
setData( index, newState, Qt::CheckStateRole );
155void QgsMapLayerLoadStyleDialog::selectAll()
157 for (
int i = 0; i < mModel->
rowCount( QModelIndex() ); i++ )
159 QModelIndex index = mModel->index( i, 0 );
160 mModel->
setData( index, Qt::Checked, Qt::CheckStateRole );
164void QgsMapLayerLoadStyleDialog::deselectAll()
166 for (
int i = 0; i < mModel->
rowCount( QModelIndex() ); i++ )
168 QModelIndex index = mModel->index( i, 0 );
169 mModel->
setData( index, Qt::Unchecked, Qt::CheckStateRole );
183 const QFileInfo fi( mFileWidget->filePath() );
184 if ( fi.exists() && fi.suffix().compare( QStringLiteral(
"sld" ), Qt::CaseInsensitive ) == 0 )
192 return QFileInfo( mFileWidget->filePath() ).suffix();
197 return mFileWidget->filePath();
203 mSectionLimit = sectionLimit;
204 const int relatedTableNOfCols = sectionLimit > 0 ? 2 : 1;
205 const int othersTableNOfCols = ( sectionLimit >= 0 && ids.count() - sectionLimit > 0 ) ? 2 : 1;
206 const QString twoColsHeader( QStringLiteral(
"Name;Description" ) );
207 const QString oneColsHeader( QStringLiteral(
"No styles found in the database" ) );
208 const QString relatedTableHeader = relatedTableNOfCols == 1 ? oneColsHeader : twoColsHeader;
209 const QString othersTableHeader = othersTableNOfCols == 1 ? oneColsHeader : twoColsHeader;
211 mRelatedTable->setColumnCount( relatedTableNOfCols );
212 mOthersTable->setColumnCount( othersTableNOfCols );
213 mRelatedTable->setHorizontalHeaderLabels( relatedTableHeader.split(
';' ) );
214 mOthersTable->setHorizontalHeaderLabels( othersTableHeader.split(
';' ) );
215 mRelatedTable->setRowCount( sectionLimit );
216 mOthersTable->setRowCount( sectionLimit >= 0 ? ( ids.count() - sectionLimit ) : 0 );
217 mRelatedTable->setDisabled( relatedTableNOfCols == 1 );
218 mOthersTable->setDisabled( othersTableNOfCols == 1 );
220 if ( sectionLimit >= 0 )
222 for (
int i = 0; i < sectionLimit; i++ )
224 QTableWidgetItem *item =
new QTableWidgetItem( names.value( i, QString() ) );
225 item->setData( Qt::UserRole, ids[i] );
226 mRelatedTable->setItem( i, 0, item );
227 mRelatedTable->setItem( i, 1,
new QTableWidgetItem( descriptions.value( i, QString() ) ) );
229 for (
int i = sectionLimit; i < ids.count(); i++ )
231 const int j = i - sectionLimit;
232 QTableWidgetItem *item =
new QTableWidgetItem( names.value( i, QString() ) );
233 item->setData( Qt::UserRole, ids[i] );
234 mOthersTable->setItem( j, 0, item );
235 mOthersTable->setItem( j, 1,
new QTableWidgetItem( descriptions.value( i, QString() ) ) );
242 return mSelectedStyleId;
245void QgsMapLayerLoadStyleDialog::onRelatedTableSelectionChanged()
247 selectionChanged( mRelatedTable );
248 if ( mRelatedTable->selectionModel()->hasSelection() )
250 if ( mOthersTable->selectionModel()->hasSelection() )
252 disconnect( mOthersTable->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsMapLayerLoadStyleDialog::onOthersTableSelectionChanged );
253 const QTableWidgetSelectionRange range( 0, 0, mOthersTable->rowCount() - 1, mOthersTable->columnCount() - 1 );
254 mOthersTable->setRangeSelected( range,
false );
255 connect( mOthersTable->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsMapLayerLoadStyleDialog::onOthersTableSelectionChanged );
260void QgsMapLayerLoadStyleDialog::onOthersTableSelectionChanged()
262 selectionChanged( mOthersTable );
263 if ( mOthersTable->selectionModel()->hasSelection() )
265 if ( mRelatedTable->selectionModel()->hasSelection() )
267 disconnect( mRelatedTable->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsMapLayerLoadStyleDialog::onRelatedTableSelectionChanged );
268 const QTableWidgetSelectionRange range( 0, 0, mRelatedTable->rowCount() - 1, mRelatedTable->columnCount() - 1 );
269 mRelatedTable->setRangeSelected( range,
false );
270 connect( mRelatedTable->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &QgsMapLayerLoadStyleDialog::onRelatedTableSelectionChanged );
275void QgsMapLayerLoadStyleDialog::selectionChanged( QTableWidget *styleTable )
277 QTableWidgetItem *item =
nullptr;
278 const QList<QTableWidgetItem *> selected = styleTable->selectedItems();
280 if ( !selected.isEmpty() )
282 item = selected.at( 0 );
283 mSelectedStyleName = item->text();
284 mSelectedStyleId = item->data( Qt::UserRole ).toString();
285 mLoadButton->setEnabled(
true );
286 mDeleteButton->setEnabled(
true );
290 mSelectedStyleName.clear();
291 mSelectedStyleId.clear();
292 mLoadButton->setEnabled(
false );
293 mDeleteButton->setEnabled(
false );
296 updateLoadButtonState();
307void QgsMapLayerLoadStyleDialog::deleteStyleFromDB()
310 const QString opInfo = QObject::tr(
"Delete style %1 from %2" ).arg( mSelectedStyleName, mLayer->
providerType() );
312 if ( QMessageBox::question(
nullptr, QObject::tr(
"Delete Style" ), QObject::tr(
"Are you sure you want to delete the style %1?" ).arg( mSelectedStyleName ), QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
316 if ( !msgError.isNull() )
319 QMessageBox::warning(
this, opInfo, tr(
"%1: fail. %2" ).arg( opInfo, msgError ) );
326 mRelatedTable->setRowCount( 0 );
327 mOthersTable->setRowCount( 0 );
331 QStringList ids, names, descriptions;
334 if ( !errorMsg.isNull() )
336 QMessageBox::warning(
this, tr(
"Error occurred while retrieving styles from database" ), errorMsg );
345void QgsMapLayerLoadStyleDialog::updateLoadButtonState()
351void QgsMapLayerLoadStyleDialog::showHelp()
353 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)