Skip to content

Commit

Permalink
new class QgsDbSourceSelectBase as base class for database source wid…
Browse files Browse the repository at this point in the history
…get selector

this will allow regrouping similar code
  • Loading branch information
3nids committed Nov 9, 2021
1 parent 5fb2236 commit 90f9541
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -361,6 +361,7 @@ set(QGIS_GUI_SRCS
processing/models/qgsmodelviewtooltemporarymousepan.cpp
processing/models/qgsmodelviewtoolzoom.cpp

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

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

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

Expand Down
125 changes: 125 additions & 0 deletions src/gui/providers/qgsdbsourceselectbase.cpp
@@ -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 );
}
}

59 changes: 59 additions & 0 deletions src/gui/providers/qgsdbsourceselectbase.h
@@ -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

0 comments on commit 90f9541

Please sign in to comment.