Skip to content

Commit

Permalink
follow-up ogr
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Nov 10, 2021
1 parent 54eb158 commit a4439c1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
43 changes: 21 additions & 22 deletions src/gui/providers/ogr/qgsogrdbsourceselect.cpp
Expand Up @@ -27,17 +27,18 @@
#include "qgsogrproviderutils.h"
#include "qgsprovidermetadata.h"
#include "qgsprovidersublayerdetails.h"
#include "qgsogrdbtablemodel.h"


#include <QMessageBox>

QgsOgrDbSourceSelect::QgsOgrDbSourceSelect( const QString &theSettingsKey, const QString &theName,
const QString &theExtensions, QWidget *parent, Qt::WindowFlags fl, QgsProviderRegistry::WidgetMode theWidgetMode )
: QgsAbstractDataSourceWidget( parent, fl, theWidgetMode )
: QgsDbSourceSelectBase( parent, fl, theWidgetMode )
, mOgrDriverName( theSettingsKey )
, mName( theName )
, mExtension( theExtensions )
{
setupUi( this );
QgsGui::instance()->enableAutoGeometryRestore( this );

connect( btnConnect, &QPushButton::clicked, this, &QgsOgrDbSourceSelect::btnConnect_clicked );
Expand Down Expand Up @@ -71,12 +72,10 @@ QgsOgrDbSourceSelect::QgsOgrDbSourceSelect( const QString &theSettingsKey, const

populateConnectionList();

mProxyModel.setParent( this );
mProxyModel.setFilterKeyColumn( -1 );
mProxyModel.setFilterCaseSensitivity( Qt::CaseInsensitive );
mProxyModel.setDynamicSortFilter( true );
mProxyModel.setSourceModel( &mTableModel );
mTablesTreeView->setModel( &mProxyModel );
mTableModel = new QgsOgrDbTableModel( this );
setSourceModel( mTableModel );

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

connect( mTablesTreeView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsOgrDbSourceSelect::treeWidgetSelectionChanged );
Expand Down Expand Up @@ -148,9 +147,9 @@ void QgsOgrDbSourceSelect::btnNew_clicked()

QString QgsOgrDbSourceSelect::layerURI( const QModelIndex &index )
{
QStandardItem *item = mTableModel.itemFromIndex( index );
QStandardItem *item = mTableModel->itemFromIndex( index );
QString uri( item->data().toString() );
QString sql = mTableModel.itemFromIndex( index.sibling( index.row(), 3 ) )->text();
QString sql = mTableModel->itemFromIndex( index.sibling( index.row(), 3 ) )->text();
if ( ! sql.isEmpty() )
{
uri += QStringLiteral( "|subset=%1" ).arg( sql );
Expand Down Expand Up @@ -200,7 +199,7 @@ void QgsOgrDbSourceSelect::addButtonClicked()
//top level items only contain the schema names
continue;
}
currentItem = mTableModel.itemFromIndex( mProxyModel.mapToSource( *selected_it ) );
currentItem = mTableModel->itemFromIndex( proxyModel()->mapToSource( *selected_it ) );
if ( !currentItem )
{
continue;
Expand All @@ -214,11 +213,11 @@ void QgsOgrDbSourceSelect::addButtonClicked()
dbInfo[currentSchemaName][currentRow] = true;
if ( currentItem->data( Qt::UserRole + 2 ).toString().contains( QStringLiteral( "Raster" ), Qt::CaseInsensitive ) )
{
selectedRasters << LayerInfo( layerURI( mProxyModel.mapToSource( *selected_it ) ), currentItem->data( Qt::DisplayRole ).toString() );
selectedRasters << LayerInfo( layerURI( proxyModel()->mapToSource( *selected_it ) ), currentItem->data( Qt::DisplayRole ).toString() );
}
else
{
selectedVectors << LayerInfo( layerURI( mProxyModel.mapToSource( *selected_it ) ), currentItem->data( Qt::DisplayRole ).toString() );
selectedVectors << LayerInfo( layerURI( proxyModel()->mapToSource( *selected_it ) ), currentItem->data( Qt::DisplayRole ).toString() );
}
}
}
Expand Down Expand Up @@ -260,13 +259,13 @@ void QgsOgrDbSourceSelect::btnConnect_clicked()

const QList< QgsProviderSublayerDetails > sublayers = QgsProviderRegistry::instance()->providerMetadata( QStringLiteral( "ogr" ) )->querySublayers( mPath );

QModelIndex rootItemIndex = mTableModel.indexFromItem( mTableModel.invisibleRootItem() );
mTableModel.removeRows( 0, mTableModel.rowCount( rootItemIndex ), rootItemIndex );
QModelIndex rootItemIndex = mTableModel->indexFromItem( mTableModel->invisibleRootItem() );
mTableModel->removeRows( 0, mTableModel->rowCount( rootItemIndex ), rootItemIndex );

// populate the table list
// get the list of suitable tables and columns and populate the UI

mTableModel.setPath( mPath );
mTableModel->setPath( mPath );


for ( const QgsProviderSublayerDetails &layer : sublayers )
Expand Down Expand Up @@ -298,17 +297,17 @@ void QgsOgrDbSourceSelect::btnConnect_clicked()
break;
}

mTableModel.addTableEntry( layerType, layer.name(), layer.uri(), layer.geometryColumnName(), QgsWkbTypes::displayString( layer.wkbType() ), QString() );
mTableModel->addTableEntry( layerType, layer.name(), layer.uri(), layer.geometryColumnName(), QgsWkbTypes::displayString( layer.wkbType() ), QString() );
}
}

mTablesTreeView->sortByColumn( 0, Qt::AscendingOrder );

//expand all the toplevel items
int numTopLevelItems = mTableModel.invisibleRootItem()->rowCount();
int numTopLevelItems = mTableModel->invisibleRootItem()->rowCount();
for ( int i = 0; i < numTopLevelItems; ++i )
{
mTablesTreeView->expand( mProxyModel.mapFromSource( mTableModel.indexFromItem( mTableModel.invisibleRootItem()->child( i ) ) ) );
mTablesTreeView->expand( proxyModel()->mapFromSource( mTableModel->indexFromItem( mTableModel->invisibleRootItem()->child( i ) ) ) );
}
mTablesTreeView->resizeColumnToContents( 0 );
mTablesTreeView->resizeColumnToContents( 1 );
Expand All @@ -322,8 +321,8 @@ void QgsOgrDbSourceSelect::btnConnect_clicked()

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

QgsVectorLayer::LayerOptions options { QgsProject::instance()->transformContext() };
std::unique_ptr<QgsVectorLayer> vlayer = std::make_unique<QgsVectorLayer>( layerURI( idx ), tableName, QStringLiteral( "ogr" ), options );
Expand All @@ -338,7 +337,7 @@ void QgsOgrDbSourceSelect::setSql( const QModelIndex &index )

if ( gb->exec() )
{
mTableModel.setSql( mProxyModel.mapToSource( index ), gb->sql() );
mTableModel->setSql( proxyModel()->mapToSource( index ), gb->sql() );
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/gui/providers/ogr/qgsogrdbsourceselect.h
Expand Up @@ -18,14 +18,15 @@
#ifndef QGSGOGRDBSOURCESELECT_H
#define QGSGOGRDBSOURCESELECT_H

#include "ui_qgsdbsourceselectbase.h"
#include "qgsguiutils.h"
#include "qgsdbfilterproxymodel.h"
#include "qgshelp.h"
#include "qgsabstractdatasourcewidget.h"
#include "qgsogrdbtablemodel.h"
#include "qgsdbsourceselectbase.h"
#include "qgsproviderregistry.h"
#include "qgis_sip.h"

class QPushButton;
class QgsOgrDbTableModel;

///@cond PRIVATE
#define SIP_NO_FILE

Expand All @@ -34,7 +35,7 @@
* source selects.
*
*/
class QgsOgrDbSourceSelect: public QgsAbstractDataSourceWidget, private Ui::QgsDbSourceSelectBase
class QgsOgrDbSourceSelect: public QgsDbSourceSelectBase
{
Q_OBJECT

Expand Down Expand Up @@ -100,8 +101,7 @@ class QgsOgrDbSourceSelect: public QgsAbstractDataSourceWidget, private Ui::QgsD
private:
void setConnectionListPosition();
//! Model that acts as datasource for mTableTreeWidget
QgsOgrDbTableModel mTableModel;
QgsDatabaseFilterProxyModel mProxyModel;
QgsOgrDbTableModel *mTableModel = nullptr;
QPushButton *mBuildQueryButton = nullptr;
QString mPath;
QString mOgrDriverName;
Expand Down

3 comments on commit a4439c1

@roya0045
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know if this could be related to #46463 ? Other than the timeline I am not sure how those change could be problematic unless mTableModel is a nullptr or proxyModel() returns a null ptr. Might not be cause either...

@nyalldawson
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@3nids @roya0045 it's 100% the cause of the crash. I spent some time looking into this last year and couldn't fix it, but this is the origin of the issue.

@roya0045
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@3nids @roya0045 it's 100% the cause of the crash. I spent some time looking into this last year and couldn't fix it, but this is the origin of the issue.

Thank you for the confirmation Nyall, I was worried I could've missed something and annoyed Denis for no reason.

Please sign in to comment.