Skip to content

Commit c85b986

Browse files
committedMar 11, 2021
Cleanup handling of selected file based rows in bad layer dialog
(cherry picked from commit 9a02c7d)
1 parent ece98c3 commit c85b986

File tree

2 files changed

+58
-44
lines changed

2 files changed

+58
-44
lines changed
 

‎src/app/qgshandlebadlayers.cpp

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -188,41 +188,20 @@ QgsHandleBadLayers::QgsHandleBadLayers( const QList<QDomNode> &layers )
188188

189189
void QgsHandleBadLayers::selectionChanged()
190190
{
191-
192-
mRows.clear();
193-
194-
const auto constSelectedItems = mLayerList->selectedItems();
195-
for ( QTableWidgetItem *item : constSelectedItems )
196-
{
197-
if ( item->column() != 0 )
198-
continue;
199-
200-
const bool providerFileBased = mLayerList->item( item->row(), 0 )->data( static_cast< int >( CustomRoles::ProviderIsFileBased ) ).toBool();
201-
if ( !providerFileBased )
202-
continue;
203-
204-
mRows << item->row();
205-
}
206-
207-
mBrowseButton->setEnabled( !mRows.isEmpty() );
191+
mBrowseButton->setEnabled( !fileBasedRows( true ).isEmpty() );
208192
}
209193

210194
QString QgsHandleBadLayers::filename( int row )
211195
{
212-
QString type = mLayerList->item( row, 1 )->text();
213-
QString provider = mLayerList->item( row, 2 )->text();
214-
QString datasource = mLayerList->item( row, 4 )->text();
196+
const bool providerFileBased = mLayerList->item( row, 0 )->data( static_cast< int >( CustomRoles::ProviderIsFileBased ) ).toBool();
197+
if ( !providerFileBased )
198+
return QString();
215199

216-
if ( type == QLatin1String( "vector" ) )
217-
{
218-
const QVariantMap parts = QgsProviderRegistry::instance()->decodeUri( provider, datasource );
219-
// if parts is empty then provider doesn't handle this method!
220-
return parts.empty() ? datasource : parts.value( QStringLiteral( "path" ) ).toString();
221-
}
222-
else
223-
{
224-
return datasource;
225-
}
200+
const QString provider = mLayerList->item( row, 2 )->text();
201+
const QString datasource = mLayerList->item( row, 4 )->text();
202+
203+
const QVariantMap parts = QgsProviderRegistry::instance()->decodeUri( provider, datasource );
204+
return parts.value( QStringLiteral( "path" ) ).toString();
226205
}
227206

228207
void QgsHandleBadLayers::setFilename( int row, const QString &filename )
@@ -266,12 +245,50 @@ void QgsHandleBadLayers::setFilename( int row, const QString &filename )
266245
item->setText( datasource );
267246
}
268247

248+
QList< int > QgsHandleBadLayers::fileBasedRows( bool selectedOnly )
249+
{
250+
QList< int > res;
251+
if ( selectedOnly )
252+
{
253+
const QList<QTableWidgetItem *> selectedItems = mLayerList->selectedItems();
254+
255+
for ( QTableWidgetItem *item : selectedItems )
256+
{
257+
if ( item->column() != 0 )
258+
continue;
259+
260+
const bool providerFileBased = mLayerList->item( item->row(), 0 )->data( static_cast< int >( CustomRoles::ProviderIsFileBased ) ).toBool();
261+
if ( !providerFileBased )
262+
continue;
263+
264+
res << item->row();
265+
}
266+
267+
}
268+
else
269+
{
270+
for ( int row = 0; row < mLayerList->rowCount(); row++ )
271+
{
272+
const bool providerFileBased = mLayerList->item( row, 0 )->data( static_cast< int >( CustomRoles::ProviderIsFileBased ) ).toBool();
273+
if ( !providerFileBased )
274+
continue;
275+
276+
res << row;
277+
}
278+
}
279+
return res;
280+
}
281+
269282
void QgsHandleBadLayers::browseClicked()
270283
{
284+
const QList< int > selectedRows = fileBasedRows( true );
271285

272-
if ( mRows.size() == 1 )
286+
if ( selectedRows.empty() )
287+
return;
288+
289+
if ( selectedRows.size() == 1 )
273290
{
274-
int row = mRows.at( 0 );
291+
int row = selectedRows.at( 0 );
275292
QString type = mLayerList->item( row, 1 )->text();
276293

277294
QString memoryQualifier, fileFilter;
@@ -303,7 +320,7 @@ void QgsHandleBadLayers::browseClicked()
303320

304321
setFilename( row, selectedFiles[0] );
305322
}
306-
else if ( mRows.size() > 1 )
323+
else
307324
{
308325
QString title = tr( "Select New Directory of Selected Files" );
309326

@@ -321,7 +338,7 @@ void QgsHandleBadLayers::browseClicked()
321338
return;
322339
}
323340

324-
for ( int row : qgis::as_const( mRows ) )
341+
for ( int row : selectedRows )
325342
{
326343
const bool providerFileBased = mLayerList->item( row, 0 )->data( static_cast< int >( CustomRoles::ProviderIsFileBased ) ).toBool();
327344
if ( !providerFileBased )
@@ -550,14 +567,7 @@ void QgsHandleBadLayers::autoFind()
550567
QDir::setCurrent( QgsProject::instance()->absolutePath() );
551568
QgsProject::instance()->layerTreeRegistryBridge()->setEnabled( true );
552569

553-
QList<int> layersToFind;
554-
if ( mRows.size() > 0 )
555-
layersToFind = mRows;
556-
else
557-
{
558-
for ( int i = 0; i < mLayerList->rowCount(); i++ )
559-
layersToFind.append( i );
560-
}
570+
const QList<int> layersToFind = fileBasedRows( !mLayerList->selectedItems().isEmpty() );
561571

562572
QProgressDialog progressDialog( QObject::tr( "Searching files" ), 0, 1, layersToFind.size(), this, Qt::Dialog );
563573

‎src/app/qgshandlebadlayers.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ class APP_EXPORT QgsHandleBadLayers
8484
QPushButton *mApplyButton = nullptr;
8585
QPushButton *mAutoFindButton = nullptr;
8686
const QList<QDomNode> &mLayers;
87-
QList<int> mRows;
8887
QString mVectorFileFilter;
8988
QString mRasterFileFilter;
9089
// Registry of the original paths associated with a file as a backup
@@ -102,7 +101,12 @@ class APP_EXPORT QgsHandleBadLayers
102101
*/
103102
QString checkBasepath( const QString &layerId, const QString &newPath, const QString &fileName );
104103

105-
104+
/**
105+
* Returns a list of all rows associated with file-based providers.
106+
*
107+
* If \a selectedOnly is true then only currently selected rows will be considered.
108+
*/
109+
QList<int> fileBasedRows( bool selectedOnly );
106110
};
107111

108112
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.