Skip to content
Permalink
Browse files
Merge pull request #45992 from 3nids/prov2
a bit more of code duplication removal in provider GUIs
  • Loading branch information
3nids committed Nov 12, 2021
2 parents 5e671f4 + 1b96632 commit 23036461dd23846b1e820114a83588ba5c269373
Showing with 141 additions and 291 deletions.
  1. +5 −0 python/core/auto_generated/providers/qgsabstractdbtablemodel.sip.in
  2. +17 −8 ...on/gui/auto_generated/providers/{qgsdbsourceselectbase.sip.in → qgsabstractdbsourceselect.sip.in}
  3. +1 −1 python/gui/gui_auto.sip
  4. +3 −0 src/core/providers/qgsabstractdbtablemodel.h
  5. +2 −2 src/gui/CMakeLists.txt
  6. +4 −20 src/gui/providers/ogr/qgsogrdbsourceselect.cpp
  7. +7 −6 src/gui/providers/ogr/qgsogrdbsourceselect.h
  8. +1 −1 src/gui/providers/ogr/qgsogrdbtablemodel.h
  9. +31 −6 src/gui/providers/{qgsdbsourceselectbase.cpp → qgsabstractdbsourceselect.cpp}
  10. +18 −9 src/gui/providers/{qgsdbsourceselectbase.h → qgsabstractdbsourceselect.h}
  11. +2 −34 src/providers/db2/qgsdb2sourceselect.cpp
  12. +5 −10 src/providers/db2/qgsdb2sourceselect.h
  13. +1 −1 src/providers/db2/qgsdb2tablemodel.h
  14. +3 −30 src/providers/hana/qgshanasourceselect.cpp
  15. +6 −9 src/providers/hana/qgshanasourceselect.h
  16. +1 −1 src/providers/hana/qgshanatablemodel.h
  17. +2 −35 src/providers/mssql/qgsmssqlsourceselect.cpp
  18. +5 −6 src/providers/mssql/qgsmssqlsourceselect.h
  19. +1 −1 src/providers/mssql/qgsmssqltablemodel.h
  20. +2 −32 src/providers/oracle/qgsoraclesourceselect.cpp
  21. +5 −6 src/providers/oracle/qgsoraclesourceselect.h
  22. +1 −1 src/providers/oracle/qgsoracletablemodel.h
  23. +2 −35 src/providers/postgres/qgspgsourceselect.cpp
  24. +5 −6 src/providers/postgres/qgspgsourceselect.h
  25. +1 −1 src/providers/postgres/qgspgtablemodel.h
  26. +3 −23 src/providers/spatialite/qgsspatialitesourceselect.cpp
  27. +6 −6 src/providers/spatialite/qgsspatialitesourceselect.h
  28. +1 −1 src/providers/spatialite/qgsspatialitetablemodel.h
@@ -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
};

@@ -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 *
************************************************************************/
@@ -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 *
************************************************************************/
@@ -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
@@ -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
@@ -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

@@ -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

@@ -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 )
@@ -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 );

@@ -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 );

@@ -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 );
}
@@ -20,7 +20,7 @@

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

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

@@ -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
@@ -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
@@ -1,5 +1,5 @@
/***************************************************************************
qgsdbsourceselectbase.h
qgsabstractdbsourceselect.h
--------------------------------------
Date : 08.11.2021
Copyright : (C) 2021 Denis Rouzaud
@@ -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 );
@@ -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();
@@ -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();
@@ -1,5 +1,5 @@
/***************************************************************************
qgsdbsourceselectbase.h
qgsabstractdbsourceselect.h
--------------------------------------
Date : 08.11.2021
Copyright : (C) 2021 Denis Rouzaud
@@ -13,8 +13,8 @@
* *
***************************************************************************/

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


#include "qgis_gui.h"
@@ -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();

@@ -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
Loading

0 comments on commit 2303646

Please sign in to comment.