Skip to content

Commit

Permalink
Merge pull request #45992 from 3nids/prov2
Browse files Browse the repository at this point in the history
a bit more of code duplication removal in provider GUIs
  • Loading branch information
3nids committed Nov 12, 2021
2 parents 5e671f4 + 1b96632 commit 2303646
Show file tree
Hide file tree
Showing 28 changed files with 141 additions and 291 deletions.
Expand Up @@ -39,6 +39,11 @@ Returns the index of the column used by default to filter the results (probably
virtual bool searchableColumn( int column ) const;
%Docstring
Returns if the column should be searchable at the given index
%End

virtual void setSql( const QModelIndex &index, const QString &sql ) = 0;
%Docstring
Sets an sql statement that belongs to a cell specified by a model index
%End
};

Expand Down
@@ -1,7 +1,7 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/providers/qgsdbsourceselectbase.h *
* src/gui/providers/qgsabstractdbsourceselect.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
Expand All @@ -10,40 +10,49 @@



class QgsDbSourceSelectBase : QgsAbstractDataSourceWidget, protected Ui::QgsDbSourceSelectBase
class QgsAbstractDbSourceSelect : QgsAbstractDataSourceWidget, protected Ui::QgsDbSourceSelectBase
{
%Docstring(signature="appended")
The :py:class:`QgsDbSourceSelectBase` class is a base class for database source widget selector
The :py:class:`QgsAbstractDbSourceSelect` class is a base class for database source widget selector

.. versionadded:: 3.24
%End

%TypeHeaderCode
#include "qgsdbsourceselectbase.h"
#include "qgsabstractdbsourceselect.h"
%End
public:
QgsDbSourceSelectBase( QWidget *parent = 0, Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags, QgsProviderRegistry::WidgetMode widgetMode = QgsProviderRegistry::WidgetMode::None );
QgsAbstractDbSourceSelect( QWidget *parent = 0, Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags, QgsProviderRegistry::WidgetMode widgetMode = QgsProviderRegistry::WidgetMode::None );
%Docstring
Constructor
%End

protected:
void setSourceModel( QgsAbstractDbTableModel *model );
void init( QgsAbstractDbTableModel *model, QItemDelegate *delegate = 0 );
%Docstring
Sets the source model for the widget
Sets the source model for the table and optionally a delegate
%End

QSortFilterProxyModel *proxyModel();
%Docstring
Returns the proxy model used to filter the results
%End

protected slots:
virtual void setSql( const QModelIndex &index ) = 0;
%Docstring
This is called to define the SQL query and must be re-implemented. The implementation should call :py:class:`QgsAbstractDbTableModel`.setSql
%End

virtual void treeviewClicked( const QModelIndex &index );
virtual void treeviewDoubleClicked( const QModelIndex &index );

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/providers/qgsdbsourceselectbase.h *
* src/gui/providers/qgsabstractdbsourceselect.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
2 changes: 1 addition & 1 deletion python/gui/gui_auto.sip
Expand Up @@ -378,7 +378,7 @@
%Include auto_generated/processing/models/qgsmodelgraphicitem.sip
%Include auto_generated/processing/models/qgsmodelgraphicsscene.sip
%Include auto_generated/processing/models/qgsmodelgraphicsview.sip
%Include auto_generated/providers/qgsdbsourceselectbase.sip
%Include auto_generated/providers/qgsabstractdbsourceselect.sip
%Include auto_generated/raster/qgscolorrampshaderwidget.sip
%Include auto_generated/raster/qgshillshaderendererwidget.sip
%Include auto_generated/raster/qgsmultibandcolorrendererwidget.sip
Expand Down
3 changes: 3 additions & 0 deletions src/core/providers/qgsabstractdbtablemodel.h
Expand Up @@ -42,6 +42,9 @@ class CORE_EXPORT QgsAbstractDbTableModel : public QStandardItemModel

//! Returns if the column should be searchable at the given index
virtual bool searchableColumn( int column ) const {Q_UNUSED( column ) return true;}

//! Sets an sql statement that belongs to a cell specified by a model index
virtual void setSql( const QModelIndex &index, const QString &sql ) = 0;
};

#endif // QGSABSTRACTDBTABLEMODEL_H
4 changes: 2 additions & 2 deletions src/gui/CMakeLists.txt
Expand Up @@ -361,7 +361,7 @@ set(QGIS_GUI_SRCS
processing/models/qgsmodelviewtooltemporarymousepan.cpp
processing/models/qgsmodelviewtoolzoom.cpp

providers/qgsdbsourceselectbase.cpp
providers/qgsabstractdbsourceselect.cpp
providers/qgspointcloudproviderguimetadata.cpp
providers/qgspointcloudsourceselect.cpp

Expand Down Expand Up @@ -1181,7 +1181,7 @@ set(QGIS_GUI_HDRS
processing/models/qgsmodelviewtooltemporarymousepan.h
processing/models/qgsmodelviewtoolzoom.h

providers/qgsdbsourceselectbase.h
providers/qgsabstractdbsourceselect.h
providers/qgspointcloudsourceselect.h
providers/qgspointcloudproviderguimetadata.h

Expand Down
24 changes: 4 additions & 20 deletions src/gui/providers/ogr/qgsogrdbsourceselect.cpp
Expand Up @@ -34,7 +34,7 @@

QgsOgrDbSourceSelect::QgsOgrDbSourceSelect( const QString &theSettingsKey, const QString &theName,
const QString &theExtensions, QWidget *parent, Qt::WindowFlags fl, QgsProviderRegistry::WidgetMode theWidgetMode )
: QgsDbSourceSelectBase( parent, fl, theWidgetMode )
: QgsAbstractDbSourceSelect( parent, fl, theWidgetMode )
, mOgrDriverName( theSettingsKey )
, mName( theName )
, mExtension( theExtensions )
Expand All @@ -46,8 +46,6 @@ QgsOgrDbSourceSelect::QgsOgrDbSourceSelect( const QString &theSettingsKey, const
connect( btnDelete, &QPushButton::clicked, this, &QgsOgrDbSourceSelect::btnDelete_clicked );
connect( cbxAllowGeometrylessTables, &QCheckBox::stateChanged, this, &QgsOgrDbSourceSelect::cbxAllowGeometrylessTables_stateChanged );
connect( cmbConnections, static_cast<void ( QComboBox::* )( int )>( &QComboBox::activated ), this, &QgsOgrDbSourceSelect::cmbConnections_activated );
connect( mTablesTreeView, &QTreeView::clicked, this, &QgsOgrDbSourceSelect::mTablesTreeView_clicked );
connect( mTablesTreeView, &QTreeView::doubleClicked, this, &QgsOgrDbSourceSelect::mTablesTreeView_doubleClicked );
setupButtons( buttonBox );
connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsOgrDbSourceSelect::showHelp );

Expand All @@ -59,24 +57,15 @@ QgsOgrDbSourceSelect::QgsOgrDbSourceSelect( const QString &theSettingsKey, const
btnSave->hide();
btnLoad->hide();

mBuildQueryButton = new QPushButton( tr( "&Set Filter" ) );
connect( mBuildQueryButton, &QAbstractButton::clicked, this, &QgsOgrDbSourceSelect::buildQuery );
mBuildQueryButton->setEnabled( false );

if ( widgetMode() != QgsProviderRegistry::WidgetMode::None )
{
mHoldDialogOpen->hide();
}

buttonBox->addButton( mBuildQueryButton, QDialogButtonBox::ActionRole );

populateConnectionList();

mTableModel = new QgsOgrDbTableModel( this );
setSourceModel( mTableModel );

mTablesTreeView->setModel( proxyModel() );
mTablesTreeView->setSortingEnabled( true );
init( mTableModel );

connect( mTablesTreeView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsOgrDbSourceSelect::treeWidgetSelectionChanged );

Expand All @@ -96,23 +85,18 @@ void QgsOgrDbSourceSelect::cmbConnections_activated( int )
dbChanged();
}

void QgsOgrDbSourceSelect::buildQuery()
{
setSql( mTablesTreeView->currentIndex() );
}


void QgsOgrDbSourceSelect::cbxAllowGeometrylessTables_stateChanged( int )
{
btnConnect_clicked();
}

void QgsOgrDbSourceSelect::mTablesTreeView_clicked( const QModelIndex &index )
void QgsOgrDbSourceSelect::treeviewClicked( const QModelIndex &index )
{
mBuildQueryButton->setEnabled( index.parent().isValid() && mTablesTreeView->currentIndex().data( Qt::UserRole + 2 ) != QLatin1String( "Raster" ) );
}

void QgsOgrDbSourceSelect::mTablesTreeView_doubleClicked( const QModelIndex &index )
void QgsOgrDbSourceSelect::treeviewDoubleClicked( const QModelIndex &index )
{
setSql( index );
}
Expand Down
13 changes: 7 additions & 6 deletions src/gui/providers/ogr/qgsogrdbsourceselect.h
Expand Up @@ -20,7 +20,7 @@

#include "qgsguiutils.h"
#include "qgshelp.h"
#include "qgsdbsourceselectbase.h"
#include "qgsabstractdbsourceselect.h"
#include "qgsproviderregistry.h"
#include "qgis_sip.h"

Expand All @@ -35,7 +35,7 @@ class QgsOgrDbTableModel;
* source selects.
*
*/
class QgsOgrDbSourceSelect: public QgsDbSourceSelectBase
class QgsOgrDbSourceSelect: public QgsAbstractDbSourceSelect
{
Q_OBJECT

Expand Down Expand Up @@ -82,22 +82,23 @@ class QgsOgrDbSourceSelect: public QgsDbSourceSelectBase
* Once connected, available layers are displayed.
*/
void btnConnect_clicked();
void buildQuery();
//! Opens the create connection dialog to build a new connection
void btnNew_clicked();
//! Deletes the selected connection
void btnDelete_clicked();
void cbxAllowGeometrylessTables_stateChanged( int );
void setSql( const QModelIndex &index );
void cmbConnections_activated( int );
void mTablesTreeView_clicked( const QModelIndex &index );
void mTablesTreeView_doubleClicked( const QModelIndex &index );
void treeWidgetSelectionChanged( const QItemSelection &selected, const QItemSelection &deselected );
//!Sets a new regular expression to the model
void setSearchExpression( const QString &regexp );

void showHelp();

protected slots:
void treeviewClicked( const QModelIndex &index ) override;
void setSql( const QModelIndex &index ) override;
void treeviewDoubleClicked( const QModelIndex &index ) override;

private:
void setConnectionListPosition();
//! Model that acts as datasource for mTableTreeWidget
Expand Down
2 changes: 1 addition & 1 deletion src/gui/providers/ogr/qgsogrdbtablemodel.h
Expand Up @@ -46,7 +46,7 @@ class QgsOgrDbTableModel : public QgsAbstractDbTableModel
void addTableEntry( const Qgis::BrowserLayerType &layerType, const QString &tableName, const QString &uri, const QString &geometryColName, const QString &geometryType, const QString &sql );

//! Sets an sql statement that belongs to a cell specified by a model index
void setSql( const QModelIndex &index, const QString &sql );
void setSql( const QModelIndex &index, const QString &sql ) override;

//! Returns the number of tables in the model
int tableCount() const
Expand Down
@@ -1,5 +1,5 @@
/***************************************************************************
qgsdbsourceselectbase.h
qgsabstractdbsourceselect.h
--------------------------------------
Date : 08.11.2021
Copyright : (C) 2021 Denis Rouzaud
Expand All @@ -14,12 +14,13 @@
***************************************************************************/

#include "qgsabstractdbtablemodel.h"
#include "qgsdbsourceselectbase.h"
#include "qgsabstractdbsourceselect.h"

#include <QMenu>
#include <QSortFilterProxyModel>
#include <QItemDelegate>

QgsDbSourceSelectBase::QgsDbSourceSelectBase( QWidget *parent, Qt::WindowFlags fl, QgsProviderRegistry::WidgetMode widgetMode )
QgsAbstractDbSourceSelect::QgsAbstractDbSourceSelect( QWidget *parent, Qt::WindowFlags fl, QgsProviderRegistry::WidgetMode widgetMode )
: QgsAbstractDataSourceWidget( parent, fl, widgetMode )
{
setupUi( this );
Expand All @@ -35,14 +36,28 @@ QgsDbSourceSelectBase::QgsDbSourceSelectBase( QWidget *parent, Qt::WindowFlags f
// The list gets sorted in finishList() method when the listing of tables and views has finished.
mProxyModel->setDynamicSortFilter( false );

mTablesTreeView->setSortingEnabled( true );
mTablesTreeView->setUniformRowHeights( true );
mTablesTreeView->setEditTriggers( QAbstractItemView::CurrentChanged );

mBuildQueryButton = new QPushButton( tr( "&Set Filter" ) );
mBuildQueryButton->setToolTip( tr( "Set Filter" ) );
mBuildQueryButton->setDisabled( true );
buttonBox->addButton( mBuildQueryButton, QDialogButtonBox::ActionRole );

connect( mTablesTreeView, &QTreeView::clicked, this, &QgsAbstractDbSourceSelect::treeviewClicked );
connect( mTablesTreeView, &QTreeView::doubleClicked, this, &QgsAbstractDbSourceSelect::treeviewDoubleClicked );

connect( mBuildQueryButton, &QAbstractButton::clicked, this, [ = ]() {setSql( mTablesTreeView->currentIndex() );} );
}

void QgsDbSourceSelectBase::setSourceModel( QgsAbstractDbTableModel *model )
void QgsAbstractDbSourceSelect::init( QgsAbstractDbTableModel *model, QItemDelegate *delegate )
{
mProxyModel->setSourceModel( model );
mTablesTreeView->setModel( mProxyModel );
mTablesTreeView->setItemDelegate( delegate );

// setting the search coluns in search settings menu using the model header data

if ( mSearchSettingsMenu )
mSearchSettingsMenu->deleteLater();
mSearchColumnActions.clear();
Expand Down Expand Up @@ -92,8 +107,18 @@ void QgsDbSourceSelectBase::setSourceModel( QgsAbstractDbTableModel *model )
connect( mSearchTableEdit, &QLineEdit::textChanged, this, [ = ]() {filterResults();} );
}

void QgsAbstractDbSourceSelect::treeviewClicked( const QModelIndex &index )
{
mBuildQueryButton->setEnabled( index.parent().isValid() );
}

void QgsAbstractDbSourceSelect::treeviewDoubleClicked( const QModelIndex &index )
{
Q_UNUSED( index )
addButtonClicked();
}

void QgsDbSourceSelectBase::filterResults()
void QgsAbstractDbSourceSelect::filterResults()
{
QString searchText = mSearchTableEdit->text();
bool regex = mSearchModeRegexAction->isChecked();
Expand Down
@@ -1,5 +1,5 @@
/***************************************************************************
qgsdbsourceselectbase.h
qgsabstractdbsourceselect.h
--------------------------------------
Date : 08.11.2021
Copyright : (C) 2021 Denis Rouzaud
Expand All @@ -13,8 +13,8 @@
* *
***************************************************************************/

#ifndef QGSDBSOURCESELECTBASE_H
#define QGSDBSOURCESELECTBASE_H
#ifndef QGSABSTRACTDBSOURCESELECT_H
#define QGSABSTRACTDBSOURCESELECT_H


#include "qgis_gui.h"
Expand All @@ -23,26 +23,34 @@

class QSortFilterProxyModel;
class QgsAbstractDbTableModel;
class QItemDelegate;

/**
* \ingroup gui
* \brief The QgsDbSourceSelectBase class is a base class for database source widget selector
* \brief The QgsAbstractDbSourceSelect class is a base class for database source widget selector
* \since QGIS 3.24
*/
class GUI_EXPORT QgsDbSourceSelectBase : public QgsAbstractDataSourceWidget, protected Ui::QgsDbSourceSelectBase
class GUI_EXPORT QgsAbstractDbSourceSelect : public QgsAbstractDataSourceWidget, protected Ui::QgsDbSourceSelectBase
{
Q_OBJECT
public:
//! Constructor
QgsDbSourceSelectBase( QWidget *parent = nullptr, Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags, QgsProviderRegistry::WidgetMode widgetMode = QgsProviderRegistry::WidgetMode::None );
QgsAbstractDbSourceSelect( QWidget *parent = nullptr, Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags, QgsProviderRegistry::WidgetMode widgetMode = QgsProviderRegistry::WidgetMode::None );

protected:
//! Sets the source model for the widget
void setSourceModel( QgsAbstractDbTableModel *model );
//! Sets the source model for the table and optionally a delegate
void init( QgsAbstractDbTableModel *model, QItemDelegate *delegate = nullptr );

//! Returns the proxy model used to filter the results
QSortFilterProxyModel *proxyModel() {return mProxyModel;}

protected slots:
//! This is called to define the SQL query and must be re-implemented. The implementation should call QgsAbstractDbTableModel::setSql
virtual void setSql( const QModelIndex &index ) = 0;

virtual void treeviewClicked( const QModelIndex &index );
virtual void treeviewDoubleClicked( const QModelIndex &index );

private:
void filterResults();

Expand All @@ -54,6 +62,7 @@ class GUI_EXPORT QgsDbSourceSelectBase : public QgsAbstractDataSourceWidget, pro
QAction *mSearchModeWildCardAction = nullptr;
QAction *mSearchModeRegexAction = nullptr;

QPushButton *mBuildQueryButton = nullptr;
};

#endif // QGSDBSOURCESELECTBASE_H
#endif // QGSABSTRACTDBSOURCESELECT_H

0 comments on commit 2303646

Please sign in to comment.