Skip to content

Commit

Permalink
use source model indexes in virtual protected slots
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids authored and nyalldawson committed Jan 21, 2022
1 parent 0bccb17 commit 5f33b4f
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 21 deletions.
8 changes: 4 additions & 4 deletions src/gui/providers/ogr/qgsogrdbsourceselect.cpp
Expand Up @@ -93,7 +93,8 @@ void QgsOgrDbSourceSelect::cbxAllowGeometrylessTables_stateChanged( int )

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

void QgsOgrDbSourceSelect::treeviewDoubleClicked( const QModelIndex &index )
Expand Down Expand Up @@ -305,11 +306,10 @@ void QgsOgrDbSourceSelect::btnConnect_clicked()

void QgsOgrDbSourceSelect::setSql( const QModelIndex &index )
{
QModelIndex idx = proxyModel()->mapToSource( index );
QString tableName = mTableModel->itemFromIndex( idx.sibling( idx.row(), 0 ) )->text();
QString tableName = mTableModel->itemFromIndex( index.sibling( index.row(), 0 ) )->text();

QgsVectorLayer::LayerOptions options { QgsProject::instance()->transformContext() };
std::unique_ptr<QgsVectorLayer> vlayer = std::make_unique<QgsVectorLayer>( layerURI( idx ), tableName, QStringLiteral( "ogr" ), options );
std::unique_ptr<QgsVectorLayer> vlayer = std::make_unique<QgsVectorLayer>( layerURI( index ), tableName, QStringLiteral( "ogr" ), options );

if ( !vlayer->isValid() )
{
Expand Down
12 changes: 8 additions & 4 deletions src/gui/providers/qgsabstractdbsourceselect.cpp
Expand Up @@ -45,10 +45,14 @@ QgsAbstractDbSourceSelect::QgsAbstractDbSourceSelect( QWidget *parent, Qt::Windo
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() );} );
connect( mTablesTreeView, &QTreeView::clicked, this, [ = ](const QModelIndex &index){
treeviewClicked( mProxyModel->mapToSource( index ) );
});
connect( mTablesTreeView, &QTreeView::doubleClicked, this, [ = ](const QModelIndex &index){
treeviewDoubleClicked( mProxyModel->mapToSource( index ) );
});

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

void QgsAbstractDbSourceSelect::init( QgsAbstractDbTableModel *model, QItemDelegate *delegate )
Expand Down
7 changes: 6 additions & 1 deletion src/gui/providers/qgsabstractdbsourceselect.h
Expand Up @@ -47,10 +47,15 @@ class GUI_EXPORT QgsAbstractDbSourceSelect : public QgsAbstractDataSourceWidget,
QPushButton *mBuildQueryButton = nullptr;

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

//! Called on click for the source \a index
virtual void treeviewClicked( const QModelIndex &index );

//! Called on double click for the source \a index
virtual void treeviewDoubleClicked( const QModelIndex &index );

private:
Expand Down
5 changes: 2 additions & 3 deletions src/providers/db2/qgsdb2sourceselect.cpp
Expand Up @@ -460,11 +460,10 @@ void QgsDb2SourceSelect::setSql( const QModelIndex &index )
return;
}

const QModelIndex idx = proxyModel()->mapToSource( index );
const QString tableName = mTableModel->itemFromIndex( idx.sibling( idx.row(), QgsDb2TableModel::DbtmTable ) )->text();
const QString tableName = mTableModel->itemFromIndex( index.sibling( index.row(), QgsDb2TableModel::DbtmTable ) )->text();

const QgsVectorLayer::LayerOptions options { QgsProject::instance()->transformContext() };
std::unique_ptr< QgsVectorLayer > vlayer = std::make_unique< QgsVectorLayer >( mTableModel->layerURI( idx, mConnInfo, mUseEstimatedMetadata ), tableName, QStringLiteral( "DB2" ), options );
std::unique_ptr< QgsVectorLayer > vlayer = std::make_unique< QgsVectorLayer >( mTableModel->layerURI( index, mConnInfo, mUseEstimatedMetadata ), tableName, QStringLiteral( "DB2" ), options );

if ( !vlayer->isValid() )
{
Expand Down
5 changes: 2 additions & 3 deletions src/providers/mssql/qgsmssqlsourceselect.cpp
Expand Up @@ -532,11 +532,10 @@ void QgsMssqlSourceSelect::setSql( const QModelIndex &index )
return;
}

const QModelIndex idx = proxyModel()->mapToSource( index );
const QString tableName = mTableModel->itemFromIndex( idx.sibling( idx.row(), QgsMssqlTableModel::DbtmTable ) )->text();
const QString tableName = mTableModel->itemFromIndex( index.sibling( index.row(), QgsMssqlTableModel::DbtmTable ) )->text();
const bool disableInvalidGeometryHandling = QgsMssqlConnection::isInvalidGeometryHandlingDisabled( cmbConnections->currentText() );
const QgsVectorLayer::LayerOptions options { QgsProject::instance()->transformContext() };
std::unique_ptr< QgsVectorLayer > vlayer = std::make_unique< QgsVectorLayer>( mTableModel->layerURI( idx, mConnInfo, mUseEstimatedMetadata, disableInvalidGeometryHandling ), tableName, QStringLiteral( "mssql" ), options );
std::unique_ptr< QgsVectorLayer > vlayer = std::make_unique< QgsVectorLayer>( mTableModel->layerURI( index, mConnInfo, mUseEstimatedMetadata, disableInvalidGeometryHandling ), tableName, QStringLiteral( "mssql" ), options );

if ( !vlayer->isValid() )
{
Expand Down
5 changes: 2 additions & 3 deletions src/providers/postgres/qgspgsourceselect.cpp
Expand Up @@ -530,10 +530,9 @@ void QgsPgSourceSelect::setSql( const QModelIndex &index )
return;
}

QModelIndex idx = proxyModel()->mapToSource( index );
QString tableName = mTableModel->itemFromIndex( idx.sibling( idx.row(), QgsPgTableModel::DbtmTable ) )->text();
QString tableName = mTableModel->itemFromIndex( index.sibling( index.row(), QgsPgTableModel::DbtmTable ) )->text();

QString uri = mTableModel->layerURI( idx, connectionInfo( false ), mUseEstimatedMetadata );
QString uri = mTableModel->layerURI( index, connectionInfo( false ), mUseEstimatedMetadata );
if ( uri.isNull() )
{
QgsDebugMsg( QStringLiteral( "no uri" ) );
Expand Down
5 changes: 2 additions & 3 deletions src/providers/spatialite/qgsspatialitesourceselect.cpp
Expand Up @@ -431,8 +431,7 @@ QString QgsSpatiaLiteSourceSelect::connectionInfo()

void QgsSpatiaLiteSourceSelect::setSql( const QModelIndex &index )
{
const QModelIndex idx = proxyModel()->mapToSource( index );
const auto item { mTableModel->itemFromIndex( idx.sibling( idx.row(), 0 ) ) };
const auto item { mTableModel->itemFromIndex( index.sibling( index.row(), 0 ) ) };
if ( !item )
{
return;
Expand All @@ -441,7 +440,7 @@ void QgsSpatiaLiteSourceSelect::setSql( const QModelIndex &index )
const QString tableName = item->text();

const QgsVectorLayer::LayerOptions options { QgsProject::instance()->transformContext() };
QgsVectorLayer *vlayer = new QgsVectorLayer( layerURI( idx ), tableName, QStringLiteral( "spatialite" ), options );
QgsVectorLayer *vlayer = new QgsVectorLayer( layerURI( index ), tableName, QStringLiteral( "spatialite" ), options );

if ( !vlayer->isValid() )
{
Expand Down

0 comments on commit 5f33b4f

Please sign in to comment.