Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
After fixing a broken layer path, also auto-fix any other broken laye…
…rs pointing to same folder
  • Loading branch information
github-actions[bot] authored and nirvn committed Mar 18, 2020
1 parent 015e1b7 commit 4e732c4
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions src/app/qgisapp.cpp
Expand Up @@ -7801,20 +7801,45 @@ void QgisApp::changeDataSource( QgsMapLayer *layer )
const QVariantMap fixedUriParts = QgsProviderRegistry::instance()->decodeUri( layer->providerType(), layer->source() );

// next, we loop through to see if we can auto-fix any other layers with the same source
const QMap< QString, QgsMapLayer * > layers = QgsProject::instance()->mapLayers( false );
for ( auto it = layers.begin(); it != layers.end(); ++it )
if ( originalSourceParts.contains( QStringLiteral( "path" ) ) )
{
if ( it.value()->isValid() )
continue;
const QString originalPath = originalSourceParts.value( QStringLiteral( "path" ) ).toString();
const QFileInfo originalPathFi( originalPath );

QVariantMap thisParts = QgsProviderRegistry::instance()->decodeUri( it.value()->providerType(), it.value()->source() );
if ( thisParts.contains( QStringLiteral( "path" ) ) && thisParts.value( QStringLiteral( "path" ) ) == originalSourceParts.value( QStringLiteral( "path" ) ) )
const QMap< QString, QgsMapLayer * > layers = QgsProject::instance()->mapLayers( false );
for ( auto it = layers.begin(); it != layers.end(); ++it )
{
// found a broken layer with the same original path, fix this one too
uri.uri = it.value()->source().replace( thisParts.value( QStringLiteral( "path" ) ).toString(),
fixedUriParts.value( QStringLiteral( "path" ) ).toString() );
uri.providerKey = it.value()->providerType();
fixLayer( it.value(), uri );
if ( it.value()->isValid() )
continue;

QVariantMap thisParts = QgsProviderRegistry::instance()->decodeUri( it.value()->providerType(), it.value()->source() );
if ( thisParts.contains( QStringLiteral( "path" ) ) )
{
const QString thisBrokenPath = thisParts.value( QStringLiteral( "path" ) ).toString();
QString fixedPath;

const QFileInfo thisBrokenPathFi( thisBrokenPath );
if ( thisBrokenPath == originalPath )
{
// found a broken layer with the same original path, fix this one too
fixedPath = fixedUriParts.value( QStringLiteral( "path" ) ).toString();
}
else if ( thisBrokenPathFi.path() == originalPathFi.path() )
{
// file from same original directory
QDir fixedDir = QFileInfo( fixedUriParts.value( QStringLiteral( "path" ) ).toString() ).dir();
const QString newCandidatePath = fixedDir.filePath( thisBrokenPathFi.fileName() );
if ( QFileInfo::exists( newCandidatePath ) )
fixedPath = newCandidatePath;
}

if ( !fixedPath.isEmpty() )
{
uri.uri = it.value()->source().replace( thisBrokenPath, fixedPath );
uri.providerKey = it.value()->providerType();
fixLayer( it.value(), uri );
}
}
}
}
}
Expand Down

0 comments on commit 4e732c4

Please sign in to comment.