Skip to content

Commit

Permalink
Start generalising QgsVectorLayerLoadStyleDialog to also handle other
Browse files Browse the repository at this point in the history
map layer types
  • Loading branch information
nyalldawson committed Sep 7, 2020
1 parent d75ec1a commit ba23708
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/gui/CMakeLists.txt
Expand Up @@ -25,7 +25,6 @@ SET(QGIS_GUI_SRCS
vector/qgsjoindialog.cpp
vector/qgssourcefieldsproperties.cpp
vector/qgsvectorlayerlegendwidget.cpp
vector/qgsvectorlayerloadstyledialog.cpp
vector/qgsvectorlayerproperties.cpp
vector/qgsvectorlayersavestyledialog.cpp
vector/qgswmsdimensiondialog.cpp
Expand Down Expand Up @@ -460,6 +459,7 @@ SET(QGIS_GUI_SRCS
qgsmaplayeractionregistry.cpp
qgsmaplayercombobox.cpp
qgsmaplayerconfigwidgetfactory.cpp
qgsmaplayerloadstyledialog.cpp
qgsmaplayerstylecategoriesmodel.cpp
qgsmaplayerstyleguiutils.cpp
qgsmaplayerstylemanagerwidget.cpp
Expand Down Expand Up @@ -698,6 +698,7 @@ SET(QGIS_GUI_HDRS
qgsmaplayercombobox.h
qgsmaplayerconfigwidget.h
qgsmaplayerconfigwidgetfactory.h
qgsmaplayerloadstyledialog.h
qgsmaplayerstylecategoriesmodel.h
qgsmaplayerstyleguiutils.h
qgsmaplayerstylemanagerwidget.h
Expand Down Expand Up @@ -1086,7 +1087,6 @@ SET(QGIS_GUI_HDRS
vector/qgsjoindialog.h
vector/qgssourcefieldsproperties.h
vector/qgsvectorlayerlegendwidget.h
vector/qgsvectorlayerloadstyledialog.h
vector/qgsvectorlayerproperties.h
vector/qgsvectorlayersavestyledialog.h
vector/qgswmsdimensiondialog.h
Expand Down
Expand Up @@ -26,7 +26,7 @@
#include "qgsgui.h"


QgsVectorLayerLoadStyleDialog::QgsVectorLayerLoadStyleDialog( QgsVectorLayer *layer, QWidget *parent )
QgsMapLayerLoadStyleDialog::QgsMapLayerLoadStyleDialog( QgsMapLayer *layer, QWidget *parent )
: QDialog( parent )
, mLayer( layer )
{
Expand All @@ -46,7 +46,8 @@ QgsVectorLayerLoadStyleDialog::QgsVectorLayerLoadStyleDialog( QgsVectorLayer *la
QString providerName = mLayer->providerType();
if ( providerName == QLatin1String( "ogr" ) )
{
providerName = mLayer->dataProvider()->storageType();
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( mLayer );
providerName = vl->dataProvider()->storageType();
if ( providerName == QLatin1String( "GPKG" ) )
providerName = QStringLiteral( "GeoPackage" );
}
Expand All @@ -58,18 +59,31 @@ QgsVectorLayerLoadStyleDialog::QgsVectorLayerLoadStyleDialog( QgsVectorLayer *la
{
QgsVectorLayerProperties::StyleType type = currentStyleType();
mFromFileWidget->setVisible( type != QgsVectorLayerProperties::StyleType::DB );
mFromDbWidget->setVisible( type == QgsVectorLayerProperties::StyleType::DB );
mDeleteButton->setVisible( type == QgsVectorLayerProperties::StyleType::DB && mLayer->dataProvider()->isDeleteStyleFromDatabaseSupported() );
if ( QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( mLayer ) )
{
mFromDbWidget->setVisible( type == QgsVectorLayerProperties::StyleType::DB );
mDeleteButton->setVisible( type == QgsVectorLayerProperties::StyleType::DB && vl->dataProvider()->isDeleteStyleFromDatabaseSupported() );
}
else
{
mFromDbWidget->setVisible( false );
mDeleteButton->setVisible( false );
}

mStyleCategoriesListView->setEnabled( currentStyleType() != QgsVectorLayerProperties::StyleType::SLD );
updateLoadButtonState();
} );
mStyleTypeComboBox->addItem( tr( "From File" ), QgsVectorLayerProperties::QML ); // QML is used as entry, but works for SLD too, see currentStyleType()
if ( mLayer->dataProvider()->isSaveAndLoadStyleToDatabaseSupported() )

if ( QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( mLayer ) )
{
mStyleTypeComboBox->addItem( tr( "From Database (%1)" ).arg( providerName ), QgsVectorLayerProperties::StyleType::DB );
if ( settings.value( QStringLiteral( "style/lastLoadStyleTypeSelection" ) ) == QgsVectorLayerProperties::StyleType::DB )
if ( vl->dataProvider()->isSaveAndLoadStyleToDatabaseSupported() )
{
mStyleTypeComboBox->setCurrentIndex( mStyleTypeComboBox->findData( QgsVectorLayerProperties::StyleType::DB ) );
mStyleTypeComboBox->addItem( tr( "From Database (%1)" ).arg( providerName ), QgsVectorLayerProperties::StyleType::DB );
if ( settings.value( QStringLiteral( "style/lastLoadStyleTypeSelection" ) ) == QgsVectorLayerProperties::StyleType::DB )
{
mStyleTypeComboBox->setCurrentIndex( mStyleTypeComboBox->findData( QgsVectorLayerProperties::StyleType::DB ) );
}
}
}

Expand Down Expand Up @@ -104,15 +118,15 @@ QgsVectorLayerLoadStyleDialog::QgsVectorLayerLoadStyleDialog( QgsVectorLayer *la
mOthersTable->horizontalHeader()->setStretchLastSection( true );
mOthersTable->setSelectionBehavior( QTableWidget::SelectRows );
mOthersTable->verticalHeader()->setVisible( false );
connect( mRelatedTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsVectorLayerLoadStyleDialog::onRelatedTableSelectionChanged );
connect( mOthersTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsVectorLayerLoadStyleDialog::onOthersTableSelectionChanged );
connect( mRelatedTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsMapLayerLoadStyleDialog::onRelatedTableSelectionChanged );
connect( mOthersTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsMapLayerLoadStyleDialog::onOthersTableSelectionChanged );
connect( mRelatedTable, &QTableWidget::doubleClicked, this, &QDialog::accept );
connect( mOthersTable, &QTableWidget::doubleClicked, this, &QDialog::accept );
connect( mCancelButton, &QPushButton::clicked, this, &QDialog::reject );
connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsVectorLayerLoadStyleDialog::showHelp );
connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsMapLayerLoadStyleDialog::showHelp );
connect( mLoadButton, &QPushButton::clicked, this, &QDialog::accept );
connect( mDeleteButton, &QPushButton::clicked, this, &QgsVectorLayerLoadStyleDialog::deleteStyleFromDB );
connect( this, &QgsVectorLayerLoadStyleDialog::rejected, [ = ]
connect( mDeleteButton, &QPushButton::clicked, this, &QgsMapLayerLoadStyleDialog::deleteStyleFromDB );
connect( this, &QgsMapLayerLoadStyleDialog::rejected, [ = ]
{
QgsSettings().setValue( QStringLiteral( "style/lastLoadStyleTypeSelection" ), currentStyleType() );
} );
Expand All @@ -122,12 +136,12 @@ QgsVectorLayerLoadStyleDialog::QgsVectorLayerLoadStyleDialog( QgsVectorLayer *la
mStyleCategoriesListView->adjustSize();
}

QgsMapLayer::StyleCategories QgsVectorLayerLoadStyleDialog::styleCategories() const
QgsMapLayer::StyleCategories QgsMapLayerLoadStyleDialog::styleCategories() const
{
return mModel->categories();
}

QgsVectorLayerProperties::StyleType QgsVectorLayerLoadStyleDialog::currentStyleType() const
QgsVectorLayerProperties::StyleType QgsMapLayerLoadStyleDialog::currentStyleType() const
{
QgsVectorLayerProperties::StyleType type = mStyleTypeComboBox->currentData().value<QgsVectorLayerProperties::StyleType>();
if ( type == QgsVectorLayerProperties::QML )
Expand All @@ -139,12 +153,17 @@ QgsVectorLayerProperties::StyleType QgsVectorLayerLoadStyleDialog::currentStyleT
return type;
}

QString QgsVectorLayerLoadStyleDialog::filePath() const
QString QgsMapLayerLoadStyleDialog::fileExtension() const
{
return QFileInfo( mFileWidget->filePath() ).suffix();
}

QString QgsMapLayerLoadStyleDialog::filePath() const
{
return mFileWidget->filePath();
}

void QgsVectorLayerLoadStyleDialog::initializeLists( const QStringList &ids, const QStringList &names, const QStringList &descriptions, int sectionLimit )
void QgsMapLayerLoadStyleDialog::initializeLists( const QStringList &ids, const QStringList &names, const QStringList &descriptions, int sectionLimit )
{
// -1 means no ids
mSectionLimit = sectionLimit;
Expand Down Expand Up @@ -184,42 +203,42 @@ void QgsVectorLayerLoadStyleDialog::initializeLists( const QStringList &ids, con
}
}

QString QgsVectorLayerLoadStyleDialog::selectedStyleId()
QString QgsMapLayerLoadStyleDialog::selectedStyleId()
{
return mSelectedStyleId;
}

void QgsVectorLayerLoadStyleDialog::onRelatedTableSelectionChanged()
void QgsMapLayerLoadStyleDialog::onRelatedTableSelectionChanged()
{
selectionChanged( mRelatedTable );
if ( mRelatedTable->selectionModel()->hasSelection() )
{
if ( mOthersTable->selectionModel()->hasSelection() )
{
disconnect( mOthersTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsVectorLayerLoadStyleDialog::onOthersTableSelectionChanged );
disconnect( mOthersTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsMapLayerLoadStyleDialog::onOthersTableSelectionChanged );
QTableWidgetSelectionRange range( 0, 0, mOthersTable->rowCount() - 1, mOthersTable->columnCount() - 1 );
mOthersTable->setRangeSelected( range, false );
connect( mOthersTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsVectorLayerLoadStyleDialog::onOthersTableSelectionChanged );
connect( mOthersTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsMapLayerLoadStyleDialog::onOthersTableSelectionChanged );
}
}
}

void QgsVectorLayerLoadStyleDialog::onOthersTableSelectionChanged()
void QgsMapLayerLoadStyleDialog::onOthersTableSelectionChanged()
{
selectionChanged( mOthersTable );
if ( mOthersTable->selectionModel()->hasSelection() )
{
if ( mRelatedTable->selectionModel()->hasSelection() )
{
disconnect( mRelatedTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsVectorLayerLoadStyleDialog::onRelatedTableSelectionChanged );
disconnect( mRelatedTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsMapLayerLoadStyleDialog::onRelatedTableSelectionChanged );
QTableWidgetSelectionRange range( 0, 0, mRelatedTable->rowCount() - 1, mRelatedTable->columnCount() - 1 );
mRelatedTable->setRangeSelected( range, false );
connect( mRelatedTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsVectorLayerLoadStyleDialog::onRelatedTableSelectionChanged );
connect( mRelatedTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsMapLayerLoadStyleDialog::onRelatedTableSelectionChanged );
}
}
}

void QgsVectorLayerLoadStyleDialog::selectionChanged( QTableWidget *styleTable )
void QgsMapLayerLoadStyleDialog::selectionChanged( QTableWidget *styleTable )
{
QTableWidgetItem *item = nullptr;
QList<QTableWidgetItem *> selected = styleTable->selectedItems();
Expand All @@ -243,16 +262,20 @@ void QgsVectorLayerLoadStyleDialog::selectionChanged( QTableWidget *styleTable )
updateLoadButtonState();
}

void QgsVectorLayerLoadStyleDialog::accept()
void QgsMapLayerLoadStyleDialog::accept()
{
QgsSettings settings;
settings.setFlagValue( QStringLiteral( "style/lastStyleCategories" ), styleCategories() );
settings.setValue( QStringLiteral( "style/lastLoadStyleTypeSelection" ), currentStyleType() );
QDialog::accept();
}

void QgsVectorLayerLoadStyleDialog::deleteStyleFromDB()
void QgsMapLayerLoadStyleDialog::deleteStyleFromDB()
{
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer *>( mLayer );
if ( !vl )
return;

QString msgError;
QString opInfo = QObject::tr( "Delete style %1 from %2" ).arg( mSelectedStyleName, mLayer->providerType() );

Expand All @@ -261,7 +284,7 @@ void QgsVectorLayerLoadStyleDialog::deleteStyleFromDB()
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
return;

mLayer->deleteStyleFromDatabase( mSelectedStyleId, msgError );
vl->deleteStyleFromDatabase( mSelectedStyleId, msgError );
if ( !msgError.isNull() )
{
QgsDebugMsg( opInfo + " failed." );
Expand All @@ -279,7 +302,7 @@ void QgsVectorLayerLoadStyleDialog::deleteStyleFromDB()
QString errorMsg;
QStringList ids, names, descriptions;
//get the list of styles in the db
int sectionLimit = mLayer->listStylesInDatabase( ids, names, descriptions, errorMsg );
int sectionLimit = vl->listStylesInDatabase( ids, names, descriptions, errorMsg );
if ( !errorMsg.isNull() )
{
QMessageBox::warning( this, tr( "Error occurred while retrieving styles from database" ), errorMsg );
Expand All @@ -291,7 +314,7 @@ void QgsVectorLayerLoadStyleDialog::deleteStyleFromDB()
}
}

void QgsVectorLayerLoadStyleDialog::updateLoadButtonState()
void QgsMapLayerLoadStyleDialog::updateLoadButtonState()
{
QgsVectorLayerProperties::StyleType type = currentStyleType();
mLoadButton->setEnabled( ( type == QgsVectorLayerProperties::DB
Expand All @@ -300,7 +323,7 @@ void QgsVectorLayerLoadStyleDialog::updateLoadButtonState()
( type != QgsVectorLayerProperties::DB && !mFileWidget->filePath().isEmpty() ) );
}

void QgsVectorLayerLoadStyleDialog::showHelp()
void QgsMapLayerLoadStyleDialog::showHelp()
{
QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#save-and-share-layer-properties" ) );
}
Expand Up @@ -27,16 +27,18 @@

class QgsMapLayerStyleCategoriesModel;

class GUI_EXPORT QgsVectorLayerLoadStyleDialog : public QDialog, private Ui::QgsVectorLayerLoadStyleDialog
class GUI_EXPORT QgsMapLayerLoadStyleDialog : public QDialog, private Ui::QgsVectorLayerLoadStyleDialog
{
Q_OBJECT
public:
explicit QgsVectorLayerLoadStyleDialog( QgsVectorLayer *layer, QWidget *parent = nullptr );
explicit QgsMapLayerLoadStyleDialog( QgsMapLayer *layer, QWidget *parent = nullptr );

QgsMapLayer::StyleCategories styleCategories() const;

QgsVectorLayerProperties::StyleType currentStyleType() const;

QString fileExtension() const;

QString filePath() const;

void initializeLists( const QStringList &ids, const QStringList &names, const QStringList &descriptions, int sectionLimit );
Expand All @@ -54,7 +56,7 @@ class GUI_EXPORT QgsVectorLayerLoadStyleDialog : public QDialog, private Ui::Qgs
void showHelp();

private:
QgsVectorLayer *mLayer = nullptr;
QgsMapLayer *mLayer = nullptr;
QgsMapLayerStyleCategoriesModel *mModel;
QString mSelectedStyleId;
QString mSelectedStyleName;
Expand Down
2 changes: 1 addition & 1 deletion src/gui/vector/qgsvectorlayerproperties.cpp
Expand Up @@ -1290,7 +1290,7 @@ void QgsVectorLayerProperties::loadStyle()

//get the list of styles in the db
int sectionLimit = mLayer->listStylesInDatabase( ids, names, descriptions, errorMsg );
QgsVectorLayerLoadStyleDialog dlg( mLayer );
QgsMapLayerLoadStyleDialog dlg( mLayer );
dlg.initializeLists( ids, names, descriptions, sectionLimit );

if ( dlg.exec() )
Expand Down

0 comments on commit ba23708

Please sign in to comment.