Skip to content

Commit

Permalink
Followup a9ca69b, port geometry type fix to DB2 provider
Browse files Browse the repository at this point in the history
Also cleanup and modernize some code
  • Loading branch information
nyalldawson committed Aug 9, 2018
1 parent a9ca69b commit 37f5b0c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 95 deletions.
51 changes: 20 additions & 31 deletions src/providers/db2/qgsdb2sourceselect.cpp
Expand Up @@ -97,7 +97,7 @@ void QgsDb2SourceSelectDelegate::setModelData( QWidget *editor, QAbstractItemMod
{
if ( index.column() == QgsDb2TableModel::DbtmType )
{
QgsWkbTypes::Type type = ( QgsWkbTypes::Type ) cb->currentData().toInt();
const QgsWkbTypes::Type type = static_cast< QgsWkbTypes::Type >( cb->currentData().toInt() );

model->setData( index, QgsDb2TableModel::iconForWkbType( type ), Qt::DecorationRole );
model->setData( index, type != QgsWkbTypes::Unknown ? QgsWkbTypes::displayString( type ) : tr( "Select…" ) );
Expand Down Expand Up @@ -209,19 +209,17 @@ QgsDb2SourceSelect::QgsDb2SourceSelect( QWidget *parent, Qt::WindowFlags fl, Qgs

cbxAllowGeometrylessTables->setDisabled( true );
}
//! Autoconnected SLOTS *
// Slot for adding a new connection

void QgsDb2SourceSelect::btnNew_clicked()
{
QgsDb2NewConnection *nc = new QgsDb2NewConnection( this );
if ( nc->exec() )
QgsDb2NewConnection nc( this );
if ( nc.exec() )
{
populateConnectionList();
emit connectionsChanged();
}
delete nc;
}
// Slot for deleting an existing connection

void QgsDb2SourceSelect::btnDelete_clicked()
{
QString msg = tr( "Are you sure you want to remove the %1 connection and all associated settings?" )
Expand Down Expand Up @@ -274,21 +272,16 @@ void QgsDb2SourceSelect::btnLoad_clicked()
populateConnectionList();
}

// Slot for editing a connection
void QgsDb2SourceSelect::btnEdit_clicked()
{
QgsDb2NewConnection *nc = new QgsDb2NewConnection( this, cmbConnections->currentText() );
if ( nc->exec() )
QgsDb2NewConnection nc( this, cmbConnections->currentText() );
if ( nc.exec() )
{
populateConnectionList();
emit connectionsChanged();
}
delete nc;
}

//! End Autoconnected SLOTS *

// Remember which database is selected
void QgsDb2SourceSelect::cmbConnections_activated( int )
{
// Remember which database was selected.
Expand Down Expand Up @@ -442,10 +435,11 @@ void QgsDb2SourceSelect::populateConnectionList()
// Slot for performing action when the Add button is clicked
void QgsDb2SourceSelect::addButtonClicked()
{
QgsDebugMsg( QString( "mConnInfo:%1" ).arg( mConnInfo ) );
QgsDebugMsg( QStringLiteral( "mConnInfo:%1" ).arg( mConnInfo ) );
mSelectedTables.clear();

Q_FOREACH ( const QModelIndex &idx, mTablesTreeView->selectionModel()->selection().indexes() )
const QModelIndexList selection = mTablesTreeView->selectionModel()->selection().indexes();
for ( const QModelIndex &idx : selection )
{
if ( idx.column() != QgsDb2TableModel::DbtmTable )
continue;
Expand Down Expand Up @@ -592,30 +586,26 @@ void QgsDb2SourceSelect::setSql( const QModelIndex &index )
{
if ( !index.parent().isValid() )
{
QgsDebugMsg( "schema item found" );
QgsDebugMsg( QStringLiteral( "schema item found" ) );
return;
}

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

QgsVectorLayer *vlayer = new QgsVectorLayer( mTableModel.layerURI( idx, mConnInfo, mUseEstimatedMetadata ), tableName, QStringLiteral( "DB2" ) );
std::unique_ptr< QgsVectorLayer > vlayer = qgis::make_unique< QgsVectorLayer >( mTableModel.layerURI( idx, mConnInfo, mUseEstimatedMetadata ), tableName, QStringLiteral( "DB2" ) );

if ( !vlayer->isValid() )
{
delete vlayer;
return;
}

// create a query builder object
QgsQueryBuilder *gb = new QgsQueryBuilder( vlayer, this );
if ( gb->exec() )
QgsQueryBuilder gb( vlayer.get(), this );
if ( gb.exec() )
{
mTableModel.setSql( mProxyModel.mapToSource( index ), gb->sql() );
mTableModel.setSql( mProxyModel.mapToSource( index ), gb.sql() );
}

delete gb;
delete vlayer;
}

void QgsDb2SourceSelect::addSearchGeometryColumn( const QString &connectionName, const QgsDb2LayerProperty &layerProperty, bool estimateMetadata )
Expand All @@ -641,7 +631,7 @@ QString QgsDb2SourceSelect::fullDescription( const QString &schema, const QStrin
{
QString full_desc;
if ( !schema.isEmpty() )
full_desc = schema + ".";
full_desc = schema + '.';
full_desc += table + " (" + column + ") " + type;
return full_desc;
}
Expand Down Expand Up @@ -677,7 +667,6 @@ void QgsDb2SourceSelect::treeWidgetSelectionChanged( const QItemSelection &selec
QgsDb2GeomColumnTypeThread::QgsDb2GeomColumnTypeThread( const QString &connectionName, bool useEstimatedMetadata )
: mConnectionName( connectionName )
, mUseEstimatedMetadata( useEstimatedMetadata )
, mStopped( false )
{
qRegisterMetaType<QgsDb2LayerProperty>( "QgsDb2LayerProperty" );
}
Expand Down Expand Up @@ -717,8 +706,8 @@ void QgsDb2GeomColumnTypeThread::run()
" GROUP BY [%1].STGeometryType(), [%1].STSrid" )
.arg( layerProperty.geometryColName,
table,
mUseEstimatedMetadata ? "TOP 1" : "",
layerProperty.sql.isEmpty() ? QLatin1String( "" ) : QStringLiteral( " AND %1" ).arg( layerProperty.sql ) );
mUseEstimatedMetadata ? QStringLiteral( "TOP 1" ) : QString(),
layerProperty.sql.isEmpty() ? QString() : QStringLiteral( " AND %1" ).arg( layerProperty.sql ) );

// issue the sql query
QSqlDatabase db = QSqlDatabase::database( mConnectionName );
Expand Down Expand Up @@ -755,8 +744,8 @@ void QgsDb2GeomColumnTypeThread::run()
srids << srid;
}

type = types.join( QStringLiteral( "," ) );
srid = srids.join( QStringLiteral( "," ) );
type = types.join( ',' );
srid = srids.join( ',' );
}

layerProperty.type = type;
Expand Down
4 changes: 2 additions & 2 deletions src/providers/db2/qgsdb2sourceselect.h
Expand Up @@ -75,8 +75,8 @@ class QgsDb2GeomColumnTypeThread : public QThread
QgsDb2GeomColumnTypeThread() = delete;

QString mConnectionName;
bool mUseEstimatedMetadata;
bool mStopped;
bool mUseEstimatedMetadata = false;
bool mStopped = false;
QList<QgsDb2LayerProperty> layerProperties;
};

Expand Down
38 changes: 17 additions & 21 deletions src/providers/db2/qgsdb2tablemodel.cpp
Expand Up @@ -38,13 +38,13 @@ QgsDb2TableModel::QgsDb2TableModel()

void QgsDb2TableModel::addTableEntry( const QgsDb2LayerProperty &layerProperty )
{
QgsDebugMsg( QString( " DB2 **** %1.%2.%3 type=%4 srid=%5 pk=%6 sql=%7" )
QgsDebugMsg( QStringLiteral( " DB2 **** %1.%2.%3 type=%4 srid=%5 pk=%6 sql=%7" )
.arg( layerProperty.schemaName )
.arg( layerProperty.tableName )
.arg( layerProperty.geometryColName )
.arg( layerProperty.type )
.arg( layerProperty.srid )
.arg( layerProperty.pkCols.join( "," ) )
.arg( layerProperty.pkCols.join( ',' ) )
.arg( layerProperty.sql ) );

// is there already a root item with the given scheme Name?
Expand Down Expand Up @@ -136,7 +136,7 @@ void QgsDb2TableModel::addTableEntry( const QgsDb2LayerProperty &layerProperty )
if ( detailsFromThread )
flags |= Qt::ItemIsEnabled;

Q_FOREACH ( QStandardItem *item, childItemList )
for ( QStandardItem *item : qgis::as_const( childItemList ) )
{
item->setFlags( item->flags() & ~flags );
}
Expand Down Expand Up @@ -211,15 +211,15 @@ void QgsDb2TableModel::setSql( const QModelIndex &index, const QString &sql )

void QgsDb2TableModel::setGeometryTypesForTable( QgsDb2LayerProperty layerProperty )
{
QStringList typeList = layerProperty.type.split( QStringLiteral( "," ), QString::SkipEmptyParts );
QStringList sridList = layerProperty.srid.split( QStringLiteral( "," ), QString::SkipEmptyParts );
QStringList typeList = layerProperty.type.split( ',', QString::SkipEmptyParts );
QStringList sridList = layerProperty.srid.split( ',', QString::SkipEmptyParts );
Q_ASSERT( typeList.size() == sridList.size() );

//find schema item and table item
QStandardItem *schemaItem = nullptr;
QList<QStandardItem *> schemaItems = findItems( layerProperty.schemaName, Qt::MatchExactly, DbtmSchema );

if ( schemaItems.size() < 1 )
if ( schemaItems.empty() )
{
return;
}
Expand Down Expand Up @@ -255,7 +255,7 @@ void QgsDb2TableModel::setGeometryTypesForTable( QgsDb2LayerProperty layerProper
row[ DbtmSrid ]->setText( tr( "Enter…" ) );
row[ DbtmSrid ]->setFlags( row[ DbtmSrid ]->flags() | Qt::ItemIsEditable );

Q_FOREACH ( QStandardItem *item, row )
for ( QStandardItem *item : qgis::as_const( row ) )
{
item->setFlags( item->flags() | Qt::ItemIsEnabled );
}
Expand All @@ -276,7 +276,7 @@ void QgsDb2TableModel::setGeometryTypesForTable( QgsDb2LayerProperty layerProper
if ( layerProperty.pkCols.size() < 2 )
flags |= Qt::ItemIsSelectable;

Q_FOREACH ( QStandardItem *item, row )
for ( QStandardItem *item : qgis::as_const( row ) )
{
item->setFlags( item->flags() | flags );
}
Expand All @@ -298,21 +298,17 @@ QIcon QgsDb2TableModel::iconForWkbType( QgsWkbTypes::Type type )

{
case QgsWkbTypes::PointGeometry:
return QgsApplication::getThemeIcon( "/mIconPointLayer.svg" );
break;
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconPointLayer.svg" ) );
case QgsWkbTypes::LineGeometry:
return QgsApplication::getThemeIcon( "/mIconLineLayer.svg" );
break;
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconLineLayer.svg" ) );
case QgsWkbTypes::PolygonGeometry:
return QgsApplication::getThemeIcon( "/mIconPolygonLayer.svg" );
break;
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconPolygonLayer.svg" ) );
case QgsWkbTypes::NullGeometry:
return QgsApplication::getThemeIcon( "/mIconTableLayer.svg" );
break;
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconTableLayer.svg" ) );
case QgsWkbTypes::UnknownGeometry:
break;
}
return QgsApplication::getThemeIcon( "/mIconLayer.png" );
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconLayer.png" ) );
}

bool QgsDb2TableModel::setData( const QModelIndex &idx, const QVariant &value, int role )
Expand All @@ -322,11 +318,11 @@ bool QgsDb2TableModel::setData( const QModelIndex &idx, const QVariant &value, i

if ( idx.column() == DbtmType || idx.column() == DbtmSrid || idx.column() == DbtmPkCol )
{
QgsWkbTypes::GeometryType geomType = ( QgsWkbTypes::GeometryType ) idx.sibling( idx.row(), DbtmType ).data( Qt::UserRole + 2 ).toInt();
const QgsWkbTypes::Type wkbType = static_cast< QgsWkbTypes::Type >( idx.sibling( idx.row(), DbtmType ).data( Qt::UserRole + 2 ).toInt() );

bool ok = geomType != QgsWkbTypes::UnknownGeometry;
bool ok = wkbType != QgsWkbTypes::Unknown;

if ( ok && geomType != QgsWkbTypes::NullGeometry )
if ( ok && wkbType != QgsWkbTypes::NoGeometry )
idx.sibling( idx.row(), DbtmSrid ).data().toInt( &ok );

QStringList pkCols = idx.sibling( idx.row(), DbtmPkCol ).data( Qt::UserRole + 1 ).toStringList();
Expand All @@ -351,7 +347,7 @@ QString QgsDb2TableModel::layerURI( const QModelIndex &index, const QString &con
if ( !index.isValid() )
return QString();

QgsWkbTypes::Type wkbType = ( QgsWkbTypes::Type ) itemFromIndex( index.sibling( index.row(), DbtmType ) )->data( Qt::UserRole + 2 ).toInt();
const QgsWkbTypes::Type wkbType = static_cast< QgsWkbTypes::Type >( itemFromIndex( index.sibling( index.row(), DbtmType ) )->data( Qt::UserRole + 2 ).toInt() );
if ( wkbType == QgsWkbTypes::Unknown )
// no geometry type selected
return QString();
Expand Down
42 changes: 16 additions & 26 deletions src/providers/mssql/qgsmssqlsourceselect.cpp
Expand Up @@ -99,7 +99,7 @@ void QgsMssqlSourceSelectDelegate::setModelData( QWidget *editor, QAbstractItemM
{
if ( index.column() == QgsMssqlTableModel::DbtmType )
{
QgsWkbTypes::Type type = ( QgsWkbTypes::Type ) cb->currentData().toInt();
QgsWkbTypes::Type type = static_cast< QgsWkbTypes::Type >( cb->currentData().toInt() );

model->setData( index, QgsMssqlTableModel::iconForWkbType( type ), Qt::DecorationRole );
model->setData( index, type != QgsWkbTypes::Unknown ? QgsWkbTypes::displayString( type ) : tr( "Select…" ) );
Expand Down Expand Up @@ -214,19 +214,17 @@ QgsMssqlSourceSelect::QgsMssqlSourceSelect( QWidget *parent, Qt::WindowFlags fl,

cbxAllowGeometrylessTables->setDisabled( true );
}
//! Autoconnected SLOTS *
// Slot for adding a new connection

void QgsMssqlSourceSelect::btnNew_clicked()
{
QgsMssqlNewConnection *nc = new QgsMssqlNewConnection( this );
if ( nc->exec() )
QgsMssqlNewConnection nc( this );
if ( nc.exec() )
{
populateConnectionList();
emit connectionsChanged();
}
delete nc;
}
// Slot for deleting an existing connection

void QgsMssqlSourceSelect::btnDelete_clicked()
{
QString msg = tr( "Are you sure you want to remove the %1 connection and all associated settings?" )
Expand Down Expand Up @@ -277,21 +275,16 @@ void QgsMssqlSourceSelect::btnLoad_clicked()
populateConnectionList();
}

// Slot for editing a connection
void QgsMssqlSourceSelect::btnEdit_clicked()
{
QgsMssqlNewConnection *nc = new QgsMssqlNewConnection( this, cmbConnections->currentText() );
if ( nc->exec() )
QgsMssqlNewConnection nc( this, cmbConnections->currentText() );
if ( nc.exec() )
{
populateConnectionList();
emit connectionsChanged();
}
delete nc;
}

//! End Autoconnected SLOTS *

// Remember which database is selected
void QgsMssqlSourceSelect::cmbConnections_activated( int )
{
// Remember which database was selected.
Expand Down Expand Up @@ -440,10 +433,11 @@ void QgsMssqlSourceSelect::populateConnectionList()
// Slot for performing action when the Add button is clicked
void QgsMssqlSourceSelect::addButtonClicked()
{
QgsDebugMsg( QString( "mConnInfo:%1" ).arg( mConnInfo ) );
QgsDebugMsg( QStringLiteral( "mConnInfo:%1" ).arg( mConnInfo ) );
mSelectedTables.clear();

Q_FOREACH ( const QModelIndex &idx, mTablesTreeView->selectionModel()->selection().indexes() )
const QModelIndexList selection = mTablesTreeView->selectionModel()->selection().indexes();
for ( const QModelIndex &idx : selection )
{
if ( idx.column() != QgsMssqlTableModel::DbtmTable )
continue;
Expand Down Expand Up @@ -516,7 +510,7 @@ void QgsMssqlSourceSelect::btnConnect_clicked()
if ( !service.isEmpty() )
mConnInfo += " service='" + service + '\'';

QgsDebugMsg( "GetDatabase" );
QgsDebugMsg( QStringLiteral( "GetDatabase" ) );
QSqlDatabase db = QgsMssqlProvider::GetDatabase( service, host, database, username, password );

if ( !QgsMssqlProvider::OpenDatabase( db ) )
Expand Down Expand Up @@ -674,30 +668,26 @@ void QgsMssqlSourceSelect::setSql( const QModelIndex &index )
{
if ( !index.parent().isValid() )
{
QgsDebugMsg( "schema item found" );
QgsDebugMsg( QStringLiteral( "schema item found" ) );
return;
}

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

QgsVectorLayer *vlayer = new QgsVectorLayer( mTableModel.layerURI( idx, mConnInfo, mUseEstimatedMetadata ), tableName, QStringLiteral( "mssql" ) );
std::unique_ptr< QgsVectorLayer > vlayer = qgis::make_unique< QgsVectorLayer>( mTableModel.layerURI( idx, mConnInfo, mUseEstimatedMetadata ), tableName, QStringLiteral( "mssql" ) );

if ( !vlayer->isValid() )
{
delete vlayer;
return;
}

// create a query builder object
QgsQueryBuilder *gb = new QgsQueryBuilder( vlayer, this );
if ( gb->exec() )
QgsQueryBuilder gb( vlayer.get(), this );
if ( gb.exec() )
{
mTableModel.setSql( mProxyModel.mapToSource( index ), gb->sql() );
mTableModel.setSql( mProxyModel.mapToSource( index ), gb.sql() );
}

delete gb;
delete vlayer;
}

void QgsMssqlSourceSelect::addSearchGeometryColumn( const QString &connectionName, const QgsMssqlLayerProperty &layerProperty, bool estimateMetadata )
Expand Down

0 comments on commit 37f5b0c

Please sign in to comment.