Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new class QgsDbSourceSelectBase as base class for database source wid…
…get selector this will allow regrouping similar code
- Loading branch information
Showing
3 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/*************************************************************************** | ||
qgsdbsourceselectbase.h | ||
-------------------------------------- | ||
Date : 08.11.2021 | ||
Copyright : (C) 2021 Denis Rouzaud | ||
Email : denis@opengis.ch | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsabstractdbtablemodel.h" | ||
#include "qgsdbsourceselectbase.h" | ||
#include "qgsdbfilterproxymodel.h" | ||
|
||
#include <QMenu> | ||
|
||
|
||
QgsDbSourceSelectBase::QgsDbSourceSelectBase( QWidget *parent, Qt::WindowFlags fl, QgsProviderRegistry::WidgetMode widgetMode ) | ||
: QgsAbstractDataSourceWidget( parent, fl, widgetMode ) | ||
{ | ||
setupUi( this ); | ||
|
||
mProxyModel = new QgsDatabaseFilterProxyModel( this ); | ||
mProxyModel->setParent( this ); | ||
mProxyModel->setFilterKeyColumn( -1 ); | ||
mProxyModel->setFilterCaseSensitivity( Qt::CaseInsensitive ); | ||
|
||
// Do not do dynamic sorting - otherwise whenever user selects geometry type / srid / pk columns, | ||
// that item suddenly jumps to the end of the list (because the item gets changed) which is very annoying. | ||
// The list gets sorted in finishList() method when the listing of tables and views has finished. | ||
mProxyModel->setDynamicSortFilter( false ); | ||
|
||
} | ||
|
||
void QgsDbSourceSelectBase::setSourceModel( QgsAbstractDbTableModel *model ) | ||
{ | ||
mProxyModel->setSourceModel( model ); | ||
|
||
// setting the search coluns in search settings menu using the model header data | ||
|
||
if ( mSearchSettingsMenu ) | ||
mSearchSettingsMenu->deleteLater(); | ||
mSearchColumnActions.clear(); | ||
mSearchSettingsMenu = new QMenu( this ); | ||
// columns | ||
QActionGroup *columnActionGroup = new QActionGroup( this ); | ||
mSearchColumnAllAction = new QAction( tr( "All" ) ); | ||
mSearchColumnAllAction->setCheckable( true ); | ||
mSearchSettingsMenu->addAction( mSearchColumnAllAction ); | ||
columnActionGroup->addAction( mSearchColumnAllAction ); | ||
bool hasDefaultSearchColumn = false; | ||
const QStringList columns = model->columns(); | ||
for ( int i = 0; i < columns.count(); i++ ) | ||
{ | ||
if ( !model->searchableColumn( i ) ) | ||
continue; | ||
QAction *action = new QAction( columns.at( i ) ); | ||
action->setCheckable( true ); | ||
if ( model->defaultSearchColumn() == i ) | ||
{ | ||
action->setChecked( true ); | ||
hasDefaultSearchColumn = true; | ||
} | ||
mSearchSettingsMenu->addAction( action ); | ||
columnActionGroup->addAction( action ); | ||
mSearchColumnActions << action; | ||
} | ||
mSearchColumnAllAction->setChecked( !hasDefaultSearchColumn ); | ||
mSearchSettingsMenu->addSeparator(); | ||
QActionGroup *modeActionGroup = new QActionGroup( this ); | ||
// mode: wildcard | ||
QAction *wildcardAction = new QAction( tr( "Wildcard" ) ); | ||
wildcardAction->setCheckable( true ); | ||
wildcardAction->setChecked( true ); | ||
mSearchSettingsMenu->addAction( wildcardAction ); | ||
modeActionGroup->addAction( wildcardAction ); | ||
// mode: regexp | ||
mSearchModeRegexAction = new QAction( tr( "Regular expression" ) ); | ||
mSearchModeRegexAction->setCheckable( true ); | ||
mSearchModeRegexAction->setChecked( false ); | ||
mSearchSettingsMenu->addAction( mSearchModeRegexAction ); | ||
modeActionGroup->addAction( mSearchModeRegexAction ); | ||
|
||
mSearchSettingsButton->setMenu( mSearchSettingsMenu ); | ||
|
||
connect( mSearchSettingsMenu, &QMenu::triggered, this, [ = ]() {filterResults();} ); | ||
connect( mSearchTableEdit, &QLineEdit::textChanged, this, [ = ]() {filterResults();} ); | ||
} | ||
|
||
|
||
void QgsDbSourceSelectBase::filterResults() | ||
{ | ||
QString searchText = mSearchTableEdit->text(); | ||
bool regex = mSearchModeRegexAction->isChecked(); | ||
|
||
if ( mSearchColumnAllAction->isChecked() ) | ||
{ | ||
mProxyModel->setFilterKeyColumn( -1 ); | ||
} | ||
else | ||
{ | ||
for ( int i = 0; i < mSearchColumnActions.count(); i++ ) | ||
{ | ||
if ( mSearchColumnActions.at( i )->isChecked() ) | ||
{ | ||
mProxyModel->setFilterKeyColumn( i ); | ||
} | ||
} | ||
} | ||
|
||
if ( regex ) | ||
{ | ||
mProxyModel->_setFilterRegExp( searchText ); | ||
} | ||
else | ||
{ | ||
mProxyModel->_setFilterWildcard( searchText ); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/*************************************************************************** | ||
qgsdbsourceselectbase.h | ||
-------------------------------------- | ||
Date : 08.11.2021 | ||
Copyright : (C) 2021 Denis Rouzaud | ||
Email : denis@opengis.ch | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSDBSOURCESELECTBASE_H | ||
#define QGSDBSOURCESELECTBASE_H | ||
|
||
|
||
#include "qgis_gui.h" | ||
#include "ui_qgsdbsourceselectbase.h" | ||
#include "qgsabstractdatasourcewidget.h" | ||
|
||
class QgsDatabaseFilterProxyModel; | ||
class QgsAbstractDbTableModel; | ||
|
||
/** | ||
* \ingroup gui | ||
* \brief The QgsDbSourceSelectBase class is a base class for database source widget selector | ||
* \since QGIS 3.24 | ||
*/ | ||
class GUI_EXPORT QgsDbSourceSelectBase : public QgsAbstractDataSourceWidget, protected Ui::QgsDbSourceSelectBase | ||
{ | ||
Q_OBJECT | ||
public: | ||
//! Constructor | ||
QgsDbSourceSelectBase( 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 ); | ||
|
||
//! Returns the proxy model used to filter the results | ||
QgsDatabaseFilterProxyModel *proxyModel() {return mProxyModel;} | ||
|
||
private: | ||
void filterResults(); | ||
|
||
QgsDatabaseFilterProxyModel *mProxyModel = nullptr; | ||
QMenu *mSearchSettingsMenu = nullptr; | ||
|
||
QAction *mSearchColumnAllAction = nullptr; | ||
QList<QAction *> mSearchColumnActions; | ||
QAction *mSearchModeWildCardAction = nullptr; | ||
QAction *mSearchModeRegexAction = nullptr; | ||
|
||
}; | ||
|
||
#endif // QGSDBSOURCESELECTBASE_H |