Skip to content

Commit

Permalink
single dialog to load style (QML, SLD, DB)
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids authored and nyalldawson committed Sep 14, 2018
1 parent 0cfa77d commit 97376b8
Show file tree
Hide file tree
Showing 16 changed files with 557 additions and 389 deletions.
2 changes: 1 addition & 1 deletion python/core/auto_generated/qgsmaplayer.sip.in
Expand Up @@ -793,7 +793,7 @@ record in the users style table in their personal qgis.db)
.. seealso:: :py:func:`also`
%End

virtual QString loadNamedStyle( const QString &uri, bool &resultFlag /Out/ );
virtual QString loadNamedStyle( const QString &uri, bool &resultFlag /Out/, StyleCategories categories = QgsMapLayer::AllStyleCategories );
%Docstring
Retrieve a named style for this layer if one
exists (either as a .qml file on disk or as a
Expand Down
8 changes: 5 additions & 3 deletions python/core/auto_generated/qgsvectorlayer.sip.in
Expand Up @@ -820,17 +820,19 @@ Delete a style from the database
.. versionadded:: 3.0
%End

virtual QString loadNamedStyle( const QString &theURI, bool &resultFlag /Out/, bool loadFromLocalDb );
virtual QString loadNamedStyle( const QString &theURI, bool &resultFlag /Out/, bool loadFromLocalDb,
StyleCategories categories = QgsMapLayer::AllStyleCategories );
%Docstring
Load a named style from file/local db/datasource db

:param theURI: the URI of the style or the URI of the layer
:param resultFlag: will be set to true if a named style is correctly loaded
:param loadFromLocalDb: if true forces to load from local db instead of datasource one
:param categories: the style categories to be loaded.
%End

virtual QString loadNamedStyle( const QString &theURI, bool &resultFlag /Out/ );

virtual QString loadNamedStyle( const QString &theURI, bool &resultFlag SIP_OUT,
QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories );
%Docstring
Calls loadNamedStyle( theURI, resultFlag, false );
Retained for backward compatibility
Expand Down
4 changes: 2 additions & 2 deletions src/app/CMakeLists.txt
Expand Up @@ -59,7 +59,6 @@ SET(QGIS_APP_SRCS
qgslayertreeviewfilterindicator.cpp
qgslayertreeviewmemoryindicator.cpp
qgslayertreeviewnonremovableindicator.cpp
qgsloadstylefromdbdialog.cpp
qgsmapcanvasdockwidget.cpp
qgsmaplayerstyleguiutils.cpp
qgsmapsavedialog.cpp
Expand All @@ -71,6 +70,7 @@ SET(QGIS_APP_SRCS
qgsstatusbarcoordinateswidget.cpp
qgsstatusbarmagnifierwidget.cpp
qgsstatusbarscalewidget.cpp
qgsvectorlayerloadstyledialog.cpp
qgsversioninfo.cpp
qgswelcomepageitemsmodel.cpp
qgswelcomepage.cpp
Expand Down Expand Up @@ -283,7 +283,6 @@ SET (QGIS_APP_MOC_HDRS
qgslayertreeviewmemoryindicator.h
qgslayertreeviewfilterindicator.h
qgslayertreeviewnonremovableindicator.h
qgsloadstylefromdbdialog.h
qgsmapcanvasdockwidget.h
qgsmaplayerstyleguiutils.h
qgsmapsavedialog.h
Expand All @@ -294,6 +293,7 @@ SET (QGIS_APP_MOC_HDRS
qgsstatusbarcoordinateswidget.h
qgsstatusbarmagnifierwidget.h
qgsstatusbarscalewidget.h
qgsvectorlayerloadstyledialog.h
qgsversioninfo.h
qgswelcomepageitemsmodel.h
qgswelcomepage.h
Expand Down
Expand Up @@ -13,56 +13,135 @@
* *
***************************************************************************/

#include "qgsloadstylefromdbdialog.h"
#include <QMessageBox>
#include <QVector>

#include "qgsvectorlayerloadstyledialog.h"
#include "qgslogger.h"
#include "qgisapp.h"
#include "qgssettings.h"
#include "qgsvectorlayerproperties.h"


#include <QMessageBox>
#include <QVector>

QgsLoadStyleFromDBDialog::QgsLoadStyleFromDBDialog( QWidget *parent )
QgsVectorLayerLoadStyleDialog::QgsVectorLayerLoadStyleDialog( QgsVectorLayer *layer, QWidget *parent )
: QDialog( parent )
, mLayer( layer )
{
setupUi( this );
setWindowTitle( QStringLiteral( "Database styles manager" ) );

QgsSettings settings;

QString providerName = mLayer->providerType();
if ( providerName == QLatin1String( "ogr" ) )
{
providerName = mLayer->dataProvider()->storageType();
if ( providerName == QLatin1String( "GPKG" ) )
providerName = QStringLiteral( "GeoPackage" );
}

QString myLastUsedDir = settings.value( QStringLiteral( "style/lastStyleDir" ), QDir::homePath() ).toString();

// load style type combobox
connect( mStyleTypeComboBox, qgis::overload<int>::of( &QComboBox::currentIndexChanged ), this, [ = ]( int )
{
QgsVectorLayerProperties::StyleType type = currentStyleType();
mFromFileWidget->setVisible( type != QgsVectorLayerProperties::DB );
mFromDbWidget->setVisible( type == QgsVectorLayerProperties::DB );
mDeleteButton->setVisible( type == QgsVectorLayerProperties::DB && mLayer->dataProvider()->isDeleteStyleFromDatabaseSupported() );
mStyleCategoriesListWidget->setEnabled( currentStyleType() != QgsVectorLayerProperties::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() )
mStyleTypeComboBox->addItem( tr( "from database (%1)" ).arg( providerName ), QgsVectorLayerProperties::DB );

// fill style categories
QgsMapLayer::StyleCategories lastStyleCategories = settings.flagValue( QStringLiteral( "style/lastStyleCategories" ), QgsMapLayer::AllStyleCategories );
for ( QgsMapLayer::StyleCategory category : qgsEnumMap<QgsMapLayer::StyleCategory>().keys() )
{
if ( category == QgsMapLayer::AllStyleCategories )
continue;

QgsMapLayer::ReadableStyleCategory readableCategory = QgsMapLayer::readableStyleCategory( category );

QListWidgetItem *item = new QListWidgetItem( readableCategory.icon(), readableCategory.name(), mStyleCategoriesListWidget );
item->setFlags( ( item->flags() | Qt::ItemIsUserCheckable ) & ~Qt::ItemIsSelectable );
item->setCheckState( lastStyleCategories.testFlag( category ) ? Qt::Checked : Qt::Unchecked );
item->setData( Qt::UserRole, category );
}

// load from file setup
mFileWidget->setFilter( tr( "QGIS Layer Style File, SLD File" ) + QStringLiteral( " (*.qml *.sld)" ) );
mFileWidget->setStorageMode( QgsFileWidget::GetFile );
mFileWidget->setDefaultRoot( myLastUsedDir );
connect( mFileWidget, &QgsFileWidget::fileChanged, this, [ = ]( const QString & )
{
mStyleCategoriesListWidget->setEnabled( currentStyleType() != QgsVectorLayerProperties::SLD );
updateLoadButtonState();
} );

// load from DB
mLoadButton->setDisabled( true );
mDeleteButton->setDisabled( true );
mRelatedTable->setEditTriggers( QTableWidget::NoEditTriggers );
mRelatedTable->horizontalHeader()->setStretchLastSection( true );
mRelatedTable->setSelectionBehavior( QTableWidget::SelectRows );
mRelatedTable->verticalHeader()->setVisible( false );

mOthersTable->setEditTriggers( QTableWidget::NoEditTriggers );
mOthersTable->horizontalHeader()->setStretchLastSection( true );
mOthersTable->setSelectionBehavior( QTableWidget::SelectRows );
mOthersTable->verticalHeader()->setVisible( false );

connect( mRelatedTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsLoadStyleFromDBDialog::onRelatedTableSelectionChanged );
connect( mOthersTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsLoadStyleFromDBDialog::onOthersTableSelectionChanged );
connect( mRelatedTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsVectorLayerLoadStyleDialog::onRelatedTableSelectionChanged );
connect( mOthersTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsVectorLayerLoadStyleDialog::onOthersTableSelectionChanged );
connect( mRelatedTable, &QTableWidget::doubleClicked, this, &QDialog::accept );
connect( mOthersTable, &QTableWidget::doubleClicked, this, &QDialog::accept );
connect( mCancelButton, &QPushButton::clicked, this, &QDialog::reject );
connect( mLoadButton, &QPushButton::clicked, this, &QDialog::accept );
connect( mDeleteButton, &QPushButton::clicked, this, &QgsLoadStyleFromDBDialog::deleteStyleFromDB );

connect( mDeleteButton, &QPushButton::clicked, this, &QgsVectorLayerLoadStyleDialog::deleteStyleFromDB );
setTabOrder( mRelatedTable, mOthersTable );
setTabOrder( mOthersTable, mCancelButton );
setTabOrder( mCancelButton, mDeleteButton );
setTabOrder( mDeleteButton, mLoadButton );

QgsSettings settings;
restoreGeometry( settings.value( QStringLiteral( "Windows/loadStyleFromDb/geometry" ) ).toByteArray() );
restoreGeometry( settings.value( QStringLiteral( "Windows/vectorLayerLoadStyle/geometry" ) ).toByteArray() );
mStyleCategoriesListWidget->adjustSize();
}

QgsLoadStyleFromDBDialog::~QgsLoadStyleFromDBDialog()
QgsVectorLayerLoadStyleDialog::~QgsVectorLayerLoadStyleDialog()
{
QgsSettings settings;
settings.setValue( QStringLiteral( "Windows/loadStyleFromDb/geometry" ), saveGeometry() );
settings.setValue( QStringLiteral( "Windows/vectorLayerLoadStyle/geometry" ), saveGeometry() );
}

void QgsLoadStyleFromDBDialog::initializeLists( const QStringList &ids, const QStringList &names, const QStringList &descriptions, int sectionLimit )
QgsMapLayer::StyleCategories QgsVectorLayerLoadStyleDialog::styleCategories() const
{
QgsMapLayer::StyleCategories categories;
for ( int row = 0; row < mStyleCategoriesListWidget->count(); ++row )
{
QListWidgetItem *item = mStyleCategoriesListWidget->item( row );
if ( item->checkState() == Qt::Checked )
categories |= item->data( Qt::UserRole ).value<QgsMapLayer::StyleCategory>();
}
return categories;
}

QgsVectorLayerProperties::StyleType QgsVectorLayerLoadStyleDialog::currentStyleType() const
{
QgsVectorLayerProperties::StyleType type = mStyleTypeComboBox->currentData().value<QgsVectorLayerProperties::StyleType>();
if ( type == QgsVectorLayerProperties::QML )
{
QFileInfo fi( mFileWidget->filePath() );
if ( fi.exists() && fi.suffix().compare( QStringLiteral( "sld" ), Qt::CaseInsensitive ) == 0 )
type = QgsVectorLayerProperties::SLD;
}
return type;
}

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

void QgsVectorLayerLoadStyleDialog::initializeLists( const QStringList &ids, const QStringList &names, const QStringList &descriptions, int sectionLimit )
{
mSectionLimit = sectionLimit;
int relatedTableNOfCols = sectionLimit > 0 ? 2 : 1;
Expand Down Expand Up @@ -98,48 +177,42 @@ void QgsLoadStyleFromDBDialog::initializeLists( const QStringList &ids, const QS
}
}

QString QgsLoadStyleFromDBDialog::getSelectedStyleId()
QString QgsVectorLayerLoadStyleDialog::selectedStyleId()
{
return mSelectedStyleId;
}

void QgsLoadStyleFromDBDialog::setLayer( QgsVectorLayer *l )
{
mLayer = l;
mDeleteButton->setVisible( mLayer->dataProvider()->isDeleteStyleFromDatabaseSupported() );
}

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

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

void QgsLoadStyleFromDBDialog::selectionChanged( QTableWidget *styleTable )
void QgsVectorLayerLoadStyleDialog::selectionChanged( QTableWidget *styleTable )
{
QTableWidgetItem *item = nullptr;
QList<QTableWidgetItem *> selected = styleTable->selectedItems();
Expand All @@ -159,9 +232,17 @@ void QgsLoadStyleFromDBDialog::selectionChanged( QTableWidget *styleTable )
mLoadButton->setEnabled( false );
mDeleteButton->setEnabled( false );
}

updateLoadButtonState();
}

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

void QgsLoadStyleFromDBDialog::deleteStyleFromDB()
void QgsVectorLayerLoadStyleDialog::deleteStyleFromDB()
{
QString msgError;
QString opInfo = QObject::tr( "Delete style %1 from %2" ).arg( mSelectedStyleName, mLayer->providerType() );
Expand Down Expand Up @@ -200,3 +281,15 @@ void QgsLoadStyleFromDBDialog::deleteStyleFromDB()
}
}
}

void QgsVectorLayerLoadStyleDialog::updateLoadButtonState()
{
QgsVectorLayerProperties::StyleType type = currentStyleType();
mLoadButton->setEnabled( ( type == QgsVectorLayerProperties::DB
&& ( mRelatedTable->selectionModel()->hasSelection() || mOthersTable->selectionModel()->hasSelection()
) ) ||
( type != QgsVectorLayerProperties::DB && !mFileWidget->filePath().isEmpty() ) );



}
@@ -1,5 +1,5 @@
/***************************************************************************
qgsloadstylefromdbdialog.h
qgsvectorlayerloadstyle.h
---------------------
begin : April 2013
copyright : (C) 2013 by Emilio Loi
Expand All @@ -16,35 +16,44 @@
#ifndef QGSLOADFILEFROMDBDIALOG_H
#define QGSLOADFILEFROMDBDIALOG_H

#include "ui_qgsloadstylefromdbdialog.h"
#include "ui_qgsvectorlayerloadstyledialog.h"
#include "qgsguiutils.h"
#include "qgis_app.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayerproperties.h"
#include "qgsmaplayer.h"

class APP_EXPORT QgsLoadStyleFromDBDialog : public QDialog, private Ui::QgsLoadStyleFromDBDialogLayout
class APP_EXPORT QgsVectorLayerLoadStyleDialog : public QDialog, private Ui::QgsVectorLayerLoadStyleDialog
{
QString mSelectedStyleId;
QString mSelectedStyleName;
int mSectionLimit = 0;
Q_OBJECT
public:
explicit QgsLoadStyleFromDBDialog( QWidget *parent = nullptr );
explicit QgsVectorLayerLoadStyleDialog( QgsVectorLayer *layer, QWidget *parent = nullptr );

~QgsVectorLayerLoadStyleDialog() override;

QgsMapLayer::StyleCategories styleCategories() const;

QgsVectorLayerProperties::StyleType currentStyleType() const;

~QgsLoadStyleFromDBDialog() override;
QString filePath() const;

void initializeLists( const QStringList &ids, const QStringList &names, const QStringList &descriptions, int sectionLimit );
QString getSelectedStyleId();
QString selectedStyleId();
void selectionChanged( QTableWidget *styleTable );
void setLayer( QgsVectorLayer *l );

public slots:
void accept() override;

private slots:
void updateLoadButtonState();
void onRelatedTableSelectionChanged();
void onOthersTableSelectionChanged();
void deleteStyleFromDB();

private:
QgsVectorLayer *mLayer = nullptr;

QgsVectorLayer *mLayer;
QString mSelectedStyleId;
QString mSelectedStyleName;
int mSectionLimit = 0;
};

#endif //QGSLOADFILEFROMDBDIALOG_H

0 comments on commit 97376b8

Please sign in to comment.