Skip to content

Commit 02ef298

Browse files
committed
Followup a9ca69b, port geometry type fix to DB2 provider
Also cleanup and modernize some code (cherry-picked from 37f5b0c)
1 parent 25587f6 commit 02ef298

File tree

5 files changed

+65
-95
lines changed

5 files changed

+65
-95
lines changed

src/providers/db2/qgsdb2sourceselect.cpp

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void QgsDb2SourceSelectDelegate::setModelData( QWidget *editor, QAbstractItemMod
9797
{
9898
if ( index.column() == QgsDb2TableModel::DbtmType )
9999
{
100-
QgsWkbTypes::Type type = ( QgsWkbTypes::Type ) cb->currentData().toInt();
100+
const QgsWkbTypes::Type type = static_cast< QgsWkbTypes::Type >( cb->currentData().toInt() );
101101

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

210210
cbxAllowGeometrylessTables->setDisabled( true );
211211
}
212-
//! Autoconnected SLOTS *
213-
// Slot for adding a new connection
212+
214213
void QgsDb2SourceSelect::btnNew_clicked()
215214
{
216-
QgsDb2NewConnection *nc = new QgsDb2NewConnection( this );
217-
if ( nc->exec() )
215+
QgsDb2NewConnection nc( this );
216+
if ( nc.exec() )
218217
{
219218
populateConnectionList();
220219
emit connectionsChanged();
221220
}
222-
delete nc;
223221
}
224-
// Slot for deleting an existing connection
222+
225223
void QgsDb2SourceSelect::btnDelete_clicked()
226224
{
227225
QString msg = tr( "Are you sure you want to remove the %1 connection and all associated settings?" )
@@ -274,21 +272,16 @@ void QgsDb2SourceSelect::btnLoad_clicked()
274272
populateConnectionList();
275273
}
276274

277-
// Slot for editing a connection
278275
void QgsDb2SourceSelect::btnEdit_clicked()
279276
{
280-
QgsDb2NewConnection *nc = new QgsDb2NewConnection( this, cmbConnections->currentText() );
281-
if ( nc->exec() )
277+
QgsDb2NewConnection nc( this, cmbConnections->currentText() );
278+
if ( nc.exec() )
282279
{
283280
populateConnectionList();
284281
emit connectionsChanged();
285282
}
286-
delete nc;
287283
}
288284

289-
//! End Autoconnected SLOTS *
290-
291-
// Remember which database is selected
292285
void QgsDb2SourceSelect::cmbConnections_activated( int )
293286
{
294287
// Remember which database was selected.
@@ -442,10 +435,11 @@ void QgsDb2SourceSelect::populateConnectionList()
442435
// Slot for performing action when the Add button is clicked
443436
void QgsDb2SourceSelect::addButtonClicked()
444437
{
445-
QgsDebugMsg( QString( "mConnInfo:%1" ).arg( mConnInfo ) );
438+
QgsDebugMsg( QStringLiteral( "mConnInfo:%1" ).arg( mConnInfo ) );
446439
mSelectedTables.clear();
447440

448-
Q_FOREACH ( const QModelIndex &idx, mTablesTreeView->selectionModel()->selection().indexes() )
441+
const QModelIndexList selection = mTablesTreeView->selectionModel()->selection().indexes();
442+
for ( const QModelIndex &idx : selection )
449443
{
450444
if ( idx.column() != QgsDb2TableModel::DbtmTable )
451445
continue;
@@ -592,30 +586,26 @@ void QgsDb2SourceSelect::setSql( const QModelIndex &index )
592586
{
593587
if ( !index.parent().isValid() )
594588
{
595-
QgsDebugMsg( "schema item found" );
589+
QgsDebugMsg( QStringLiteral( "schema item found" ) );
596590
return;
597591
}
598592

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

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

604598
if ( !vlayer->isValid() )
605599
{
606-
delete vlayer;
607600
return;
608601
}
609602

610603
// create a query builder object
611-
QgsQueryBuilder *gb = new QgsQueryBuilder( vlayer, this );
612-
if ( gb->exec() )
604+
QgsQueryBuilder gb( vlayer.get(), this );
605+
if ( gb.exec() )
613606
{
614-
mTableModel.setSql( mProxyModel.mapToSource( index ), gb->sql() );
607+
mTableModel.setSql( mProxyModel.mapToSource( index ), gb.sql() );
615608
}
616-
617-
delete gb;
618-
delete vlayer;
619609
}
620610

621611
void QgsDb2SourceSelect::addSearchGeometryColumn( const QString &connectionName, const QgsDb2LayerProperty &layerProperty, bool estimateMetadata )
@@ -641,7 +631,7 @@ QString QgsDb2SourceSelect::fullDescription( const QString &schema, const QStrin
641631
{
642632
QString full_desc;
643633
if ( !schema.isEmpty() )
644-
full_desc = schema + ".";
634+
full_desc = schema + '.';
645635
full_desc += table + " (" + column + ") " + type;
646636
return full_desc;
647637
}
@@ -677,7 +667,6 @@ void QgsDb2SourceSelect::treeWidgetSelectionChanged( const QItemSelection &selec
677667
QgsDb2GeomColumnTypeThread::QgsDb2GeomColumnTypeThread( const QString &connectionName, bool useEstimatedMetadata )
678668
: mConnectionName( connectionName )
679669
, mUseEstimatedMetadata( useEstimatedMetadata )
680-
, mStopped( false )
681670
{
682671
qRegisterMetaType<QgsDb2LayerProperty>( "QgsDb2LayerProperty" );
683672
}
@@ -717,8 +706,8 @@ void QgsDb2GeomColumnTypeThread::run()
717706
" GROUP BY [%1].STGeometryType(), [%1].STSrid" )
718707
.arg( layerProperty.geometryColName,
719708
table,
720-
mUseEstimatedMetadata ? "TOP 1" : "",
721-
layerProperty.sql.isEmpty() ? QLatin1String( "" ) : QStringLiteral( " AND %1" ).arg( layerProperty.sql ) );
709+
mUseEstimatedMetadata ? QStringLiteral( "TOP 1" ) : QString(),
710+
layerProperty.sql.isEmpty() ? QString() : QStringLiteral( " AND %1" ).arg( layerProperty.sql ) );
722711

723712
// issue the sql query
724713
QSqlDatabase db = QSqlDatabase::database( mConnectionName );
@@ -755,8 +744,8 @@ void QgsDb2GeomColumnTypeThread::run()
755744
srids << srid;
756745
}
757746

758-
type = types.join( QStringLiteral( "," ) );
759-
srid = srids.join( QStringLiteral( "," ) );
747+
type = types.join( ',' );
748+
srid = srids.join( ',' );
760749
}
761750

762751
layerProperty.type = type;

src/providers/db2/qgsdb2sourceselect.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ class QgsDb2GeomColumnTypeThread : public QThread
7575
QgsDb2GeomColumnTypeThread() = delete;
7676

7777
QString mConnectionName;
78-
bool mUseEstimatedMetadata;
79-
bool mStopped;
78+
bool mUseEstimatedMetadata = false;
79+
bool mStopped = false;
8080
QList<QgsDb2LayerProperty> layerProperties;
8181
};
8282

src/providers/db2/qgsdb2tablemodel.cpp

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ QgsDb2TableModel::QgsDb2TableModel()
3838

3939
void QgsDb2TableModel::addTableEntry( const QgsDb2LayerProperty &layerProperty )
4040
{
41-
QgsDebugMsg( QString( " DB2 **** %1.%2.%3 type=%4 srid=%5 pk=%6 sql=%7" )
41+
QgsDebugMsg( QStringLiteral( " DB2 **** %1.%2.%3 type=%4 srid=%5 pk=%6 sql=%7" )
4242
.arg( layerProperty.schemaName )
4343
.arg( layerProperty.tableName )
4444
.arg( layerProperty.geometryColName )
4545
.arg( layerProperty.type )
4646
.arg( layerProperty.srid )
47-
.arg( layerProperty.pkCols.join( "," ) )
47+
.arg( layerProperty.pkCols.join( ',' ) )
4848
.arg( layerProperty.sql ) );
4949

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

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

212212
void QgsDb2TableModel::setGeometryTypesForTable( QgsDb2LayerProperty layerProperty )
213213
{
214-
QStringList typeList = layerProperty.type.split( QStringLiteral( "," ), QString::SkipEmptyParts );
215-
QStringList sridList = layerProperty.srid.split( QStringLiteral( "," ), QString::SkipEmptyParts );
214+
QStringList typeList = layerProperty.type.split( ',', QString::SkipEmptyParts );
215+
QStringList sridList = layerProperty.srid.split( ',', QString::SkipEmptyParts );
216216
Q_ASSERT( typeList.size() == sridList.size() );
217217

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

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

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

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

299299
{
300300
case QgsWkbTypes::PointGeometry:
301-
return QgsApplication::getThemeIcon( "/mIconPointLayer.svg" );
302-
break;
301+
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconPointLayer.svg" ) );
303302
case QgsWkbTypes::LineGeometry:
304-
return QgsApplication::getThemeIcon( "/mIconLineLayer.svg" );
305-
break;
303+
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconLineLayer.svg" ) );
306304
case QgsWkbTypes::PolygonGeometry:
307-
return QgsApplication::getThemeIcon( "/mIconPolygonLayer.svg" );
308-
break;
305+
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconPolygonLayer.svg" ) );
309306
case QgsWkbTypes::NullGeometry:
310-
return QgsApplication::getThemeIcon( "/mIconTableLayer.svg" );
311-
break;
307+
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconTableLayer.svg" ) );
312308
case QgsWkbTypes::UnknownGeometry:
313309
break;
314310
}
315-
return QgsApplication::getThemeIcon( "/mIconLayer.png" );
311+
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconLayer.png" ) );
316312
}
317313

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

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

327-
bool ok = geomType != QgsWkbTypes::UnknownGeometry;
323+
bool ok = wkbType != QgsWkbTypes::Unknown;
328324

329-
if ( ok && geomType != QgsWkbTypes::NullGeometry )
325+
if ( ok && wkbType != QgsWkbTypes::NoGeometry )
330326
idx.sibling( idx.row(), DbtmSrid ).data().toInt( &ok );
331327

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

354-
QgsWkbTypes::Type wkbType = ( QgsWkbTypes::Type ) itemFromIndex( index.sibling( index.row(), DbtmType ) )->data( Qt::UserRole + 2 ).toInt();
350+
const QgsWkbTypes::Type wkbType = static_cast< QgsWkbTypes::Type >( itemFromIndex( index.sibling( index.row(), DbtmType ) )->data( Qt::UserRole + 2 ).toInt() );
355351
if ( wkbType == QgsWkbTypes::Unknown )
356352
// no geometry type selected
357353
return QString();

src/providers/mssql/qgsmssqlsourceselect.cpp

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void QgsMssqlSourceSelectDelegate::setModelData( QWidget *editor, QAbstractItemM
9999
{
100100
if ( index.column() == QgsMssqlTableModel::DbtmType )
101101
{
102-
QgsWkbTypes::Type type = ( QgsWkbTypes::Type ) cb->currentData().toInt();
102+
QgsWkbTypes::Type type = static_cast< QgsWkbTypes::Type >( cb->currentData().toInt() );
103103

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

215215
cbxAllowGeometrylessTables->setDisabled( true );
216216
}
217-
//! Autoconnected SLOTS *
218-
// Slot for adding a new connection
217+
219218
void QgsMssqlSourceSelect::btnNew_clicked()
220219
{
221-
QgsMssqlNewConnection *nc = new QgsMssqlNewConnection( this );
222-
if ( nc->exec() )
220+
QgsMssqlNewConnection nc( this );
221+
if ( nc.exec() )
223222
{
224223
populateConnectionList();
225224
emit connectionsChanged();
226225
}
227-
delete nc;
228226
}
229-
// Slot for deleting an existing connection
227+
230228
void QgsMssqlSourceSelect::btnDelete_clicked()
231229
{
232230
QString msg = tr( "Are you sure you want to remove the %1 connection and all associated settings?" )
@@ -277,21 +275,16 @@ void QgsMssqlSourceSelect::btnLoad_clicked()
277275
populateConnectionList();
278276
}
279277

280-
// Slot for editing a connection
281278
void QgsMssqlSourceSelect::btnEdit_clicked()
282279
{
283-
QgsMssqlNewConnection *nc = new QgsMssqlNewConnection( this, cmbConnections->currentText() );
284-
if ( nc->exec() )
280+
QgsMssqlNewConnection nc( this, cmbConnections->currentText() );
281+
if ( nc.exec() )
285282
{
286283
populateConnectionList();
287284
emit connectionsChanged();
288285
}
289-
delete nc;
290286
}
291287

292-
//! End Autoconnected SLOTS *
293-
294-
// Remember which database is selected
295288
void QgsMssqlSourceSelect::cmbConnections_activated( int )
296289
{
297290
// Remember which database was selected.
@@ -440,10 +433,11 @@ void QgsMssqlSourceSelect::populateConnectionList()
440433
// Slot for performing action when the Add button is clicked
441434
void QgsMssqlSourceSelect::addButtonClicked()
442435
{
443-
QgsDebugMsg( QString( "mConnInfo:%1" ).arg( mConnInfo ) );
436+
QgsDebugMsg( QStringLiteral( "mConnInfo:%1" ).arg( mConnInfo ) );
444437
mSelectedTables.clear();
445438

446-
Q_FOREACH ( const QModelIndex &idx, mTablesTreeView->selectionModel()->selection().indexes() )
439+
const QModelIndexList selection = mTablesTreeView->selectionModel()->selection().indexes();
440+
for ( const QModelIndex &idx : selection )
447441
{
448442
if ( idx.column() != QgsMssqlTableModel::DbtmTable )
449443
continue;
@@ -516,7 +510,7 @@ void QgsMssqlSourceSelect::btnConnect_clicked()
516510
if ( !service.isEmpty() )
517511
mConnInfo += " service='" + service + '\'';
518512

519-
QgsDebugMsg( "GetDatabase" );
513+
QgsDebugMsg( QStringLiteral( "GetDatabase" ) );
520514
QSqlDatabase db = QgsMssqlProvider::GetDatabase( service, host, database, username, password );
521515

522516
if ( !QgsMssqlProvider::OpenDatabase( db ) )
@@ -674,30 +668,26 @@ void QgsMssqlSourceSelect::setSql( const QModelIndex &index )
674668
{
675669
if ( !index.parent().isValid() )
676670
{
677-
QgsDebugMsg( "schema item found" );
671+
QgsDebugMsg( QStringLiteral( "schema item found" ) );
678672
return;
679673
}
680674

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

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

686680
if ( !vlayer->isValid() )
687681
{
688-
delete vlayer;
689682
return;
690683
}
691684

692685
// create a query builder object
693-
QgsQueryBuilder *gb = new QgsQueryBuilder( vlayer, this );
694-
if ( gb->exec() )
686+
QgsQueryBuilder gb( vlayer.get(), this );
687+
if ( gb.exec() )
695688
{
696-
mTableModel.setSql( mProxyModel.mapToSource( index ), gb->sql() );
689+
mTableModel.setSql( mProxyModel.mapToSource( index ), gb.sql() );
697690
}
698-
699-
delete gb;
700-
delete vlayer;
701691
}
702692

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

0 commit comments

Comments
 (0)