Skip to content
Permalink
Browse files

improve bad layer handling: make all datasource editable (fixes #7039)

  • Loading branch information
jef-n committed Jun 17, 2014
1 parent d308ee5 commit 1532048430421dc2c5747d23b497b4cdbfef3e7e
Showing with 104 additions and 151 deletions.
  1. +100 −112 src/app/qgshandlebadlayers.cpp
  2. +3 −2 src/app/qgshandlebadlayers.h
  3. +1 −37 src/ui/qgshandlebadlayersbase.ui
@@ -70,21 +70,20 @@ QgsHandleBadLayers::QgsHandleBadLayers( const QList<QDomNode> &layers, const QDo
mBrowseButton->setDisabled( true );

connect( mLayerList, SIGNAL( itemSelectionChanged() ), this, SLOT( selectionChanged() ) );
connect( mLayerList, SIGNAL( itemChanged( QTableWidgetItem * ) ), this, SLOT( itemChanged( QTableWidgetItem * ) ) );
connect( mLayerList, SIGNAL( cellDoubleClicked( int, int ) ), this, SLOT( cellDoubleClicked( int, int ) ) );
connect( mBrowseButton, SIGNAL( clicked() ), this, SLOT( browseClicked() ) );
connect( buttonBox, SIGNAL( accepted() ), this, SLOT( accept() ) );
connect( buttonBox->button( QDialogButtonBox::Apply ), SIGNAL( clicked() ), this, SLOT( apply() ) );

mLayerList->clear();
mLayerList->setSortingEnabled( true );
mLayerList->setSelectionBehavior( QAbstractItemView::SelectRows );
mLayerList->setColumnCount( 5 );
mLayerList->setColumnCount( 4 );

mLayerList->setHorizontalHeaderLabels( QStringList()
<< tr( "Layer name" )
<< tr( "Type" )
<< tr( "Provider" )
<< tr( "New file" )
<< tr( "New datasource" )
<< tr( "Datasource" )
);

int j = 0;
@@ -95,42 +94,12 @@ QgsHandleBadLayers::QgsHandleBadLayers( const QList<QDomNode> &layers, const QDo
QString name = node.namedItem( "layername" ).toElement().text();
QString type = node.toElement().attribute( "type" );
QString datasource = node.namedItem( "datasource" ).toElement().text();
QString provider;
QString provider = type == "vector" ? node.namedItem( "provider" ).toElement().text() : tr( "none" );

QString filename = datasource;

if ( type == "vector" )
{
provider = node.namedItem( "provider" ).toElement().text();
if ( provider == "spatialite" )
{
QgsDataSourceURI uri( datasource );
filename = uri.database();
}
else if ( provider == "ogr" )
{
QStringList theURIParts = datasource.split( "|" );
filename = theURIParts[0];
}
else if ( provider == "delimitedtext" )
{
filename = QUrl::fromEncoded( datasource.toAscii() ).toLocalFile();
}
else if ( provider == "postgres" || provider == "sqlanywhere" )
{
continue;
}
}
else
{
provider = tr( "none" );
}

QgsDebugMsg( QString( "name=%1 type=%2 provider=%3 filename=%4 datasource='%5'" )
QgsDebugMsg( QString( "name=%1 type=%2 provider=%3 datasource='%4'" )
.arg( name )
.arg( type )
.arg( provider )
.arg( filename )
.arg( datasource ) );

mLayerList->setRowCount( j + 1 );
@@ -150,42 +119,19 @@ QgsHandleBadLayers::QgsHandleBadLayers( const QList<QDomNode> &layers, const QDo
item->setFlags( item->flags() & ~Qt::ItemIsEditable );
mLayerList->setItem( j, 2, item );

item = new QTableWidgetItem( filename );
mLayerList->setItem( j, 3, item );

item = new QTableWidgetItem( datasource );
item->setFlags( item->flags() & ~Qt::ItemIsEditable );
mLayerList->setItem( j, 4, item );
mLayerList->setItem( j, 3, item );

j++;
}

mLayerList->resizeColumnsToContents();
// mLayerList->resizeColumnsToContents();
}

QgsHandleBadLayers::~QgsHandleBadLayers()
{
}

void QgsHandleBadLayers::itemChanged( QTableWidgetItem *item )
{
if ( item->column() != 3 )
return;

QFileInfo fi( item->text() );

item->setForeground( fi.exists() ? QBrush( Qt::green ) : QBrush( Qt::red ) );
}

void QgsHandleBadLayers::cellDoubleClicked( int row, int column )
{
Q_UNUSED( row );
if ( column != 3 || !mBrowseButton->isEnabled() )
return;

mBrowseButton->click();
}

void QgsHandleBadLayers::selectionChanged()
{
QgsDebugMsg( "entered." );
@@ -203,18 +149,89 @@ void QgsHandleBadLayers::selectionChanged()
mBrowseButton->setEnabled( mRows.size() > 0 );
}

QString QgsHandleBadLayers::filename( int row )
{
QString type = mLayerList->item( row, 1 )->text();
QString provider = mLayerList->item( row, 2 )->text();
QString datasource = mLayerList->item( row, 3 )->text();

if ( type == "vector" )
{
if ( provider == "spatialite" )
{
QgsDataSourceURI uri( datasource );
return uri.database();
}
else if ( provider == "ogr" )
{
QStringList theURIParts = datasource.split( "|" );
return theURIParts[0];
}
else if ( provider == "delimitedtext" )
{
return QUrl::fromEncoded( datasource.toAscii() ).toLocalFile();
}
}
else
{
return datasource;
}

return QString::null;
}

void QgsHandleBadLayers::setFilename( int row, QString filename )
{
if ( !QFileInfo( filename ).exists() )
return;

QString type = mLayerList->item( row, 1 )->text();
QString provider = mLayerList->item( row, 2 )->text();
QTableWidgetItem *item = mLayerList->item( row, 3 );

QString datasource = item->text();

if ( type == "vector" )
{
if ( provider == "spatialite" )
{
QgsDataSourceURI uri( datasource );
uri.setDatabase( filename );
datasource = uri.uri();
}
else if ( provider == "ogr" )
{
QStringList theURIParts = datasource.split( "|" );
theURIParts[0] = filename;
datasource = theURIParts.join( "|" );
}
else if ( provider == "delimitedtext" )
{
QUrl uriSource = QUrl::fromEncoded( datasource.toAscii() );
QUrl uriDest = QUrl::fromLocalFile( filename );
uriDest.setQueryItems( uriSource.queryItems() );
datasource = QString::fromAscii( uriDest.toEncoded() );
}
}
else
{
datasource = filename;
}

item->setText( datasource );
}

void QgsHandleBadLayers::browseClicked()
{
QgsDebugMsg( "entered." );

if ( mRows.size() == 1 )
{
QString memoryQualifier;
QString fileFilter;
int idx = mLayerList->item( mRows[0], 0 )->data( Qt::UserRole ).toInt();
QTableWidgetItem *fileItem = mLayerList->item( mRows[0], 3 );
int row = mRows[0];
QString type = mLayerList->item( row, 1 )->text();

if ( mLayers[ idx ].toElement().attribute( "type" ) == "vector" )
QString memoryQualifier, fileFilter;
if ( type == "vector" )
{
memoryQualifier = "lastVectorFileFilter";
fileFilter = mVectorFileFilter;
@@ -225,19 +242,22 @@ void QgsHandleBadLayers::browseClicked()
fileFilter = mRasterFileFilter;
}

QString fn = filename( row );
if ( fn.isNull() )
return;

QStringList selectedFiles;
QString enc;
QString title = tr( "Select file to replace '%1'" ).arg( fileItem->text() );
QString title = tr( "Select file to replace '%1'" ).arg( fn );

QgisGui::openFilesRememberingFilter( memoryQualifier, fileFilter, selectedFiles, enc, title );

if ( selectedFiles.size() != 1 )
{
QMessageBox::information( this, title, tr( "Please select exactly one file." ) );
return;
}

fileItem->setText( selectedFiles[0] );
setFilename( row, selectedFiles[0] );
}
else if ( mRows.size() > 1 )
{
@@ -246,7 +266,6 @@ void QgsHandleBadLayers::browseClicked()
QString title = tr( "Select new directory of selected files" );

QgisGui::openFilesRememberingFilter( "missingDirectory", tr( "All files (*)" ), selectedFiles, enc, title );

if ( selectedFiles.isEmpty() )
{
return;
@@ -258,17 +277,19 @@ void QgsHandleBadLayers::browseClicked()
return;
}

foreach ( int i, mRows )
foreach ( int row, mRows )
{
QTableWidgetItem *fileItem = mLayerList->item( i, 3 );
QString type = mLayerList->item( row, 1 )->text();
QString fn = filename( row );
if ( fn.isEmpty() )
continue;

QFileInfo fi( fileItem->text() );
QFileInfo fi( fn );
fi.setFile( path.dir(), fi.fileName() );

if ( !fi.exists() )
continue;

fileItem->setText( fi.absoluteFilePath() );
setFilename( row, fi.absoluteFilePath() );
}
}
}
@@ -283,41 +304,8 @@ void QgsHandleBadLayers::apply()

QString type = mLayerList->item( i, 1 )->text();
QString provider = mLayerList->item( i, 2 )->text();
QTableWidgetItem *fileItem = mLayerList->item( i, 3 );
QString datasource = mLayerList->item( i, 4 )->text();

QString filename = fileItem->text();
if ( !QFileInfo( filename ).exists() )
{
continue;
}

if ( type == "vector" )
{
if ( provider == "spatialite" )
{
QgsDataSourceURI uri( datasource );
uri.setDatabase( filename );
datasource = uri.uri();
}
else if ( provider == "ogr" )
{
QStringList theURIParts = datasource.split( "|" );
theURIParts[0] = filename;
datasource = theURIParts.join( "|" );
}
else if ( provider == "delimitedtext" )
{
QUrl uriSource = QUrl::fromEncoded( datasource.toAscii() );
QUrl uriDest = QUrl::fromLocalFile( filename );
uriDest.setQueryItems( uriSource.queryItems() );
datasource = QString::fromAscii( uriDest.toEncoded() );
}
}
else
{
datasource = filename;
}
QTableWidgetItem *item = mLayerList->item( i, 3 );
QString datasource = item->text();

node.namedItem( "datasource" ).toElement().firstChild().toText().setData( datasource );
if ( QgsProject::instance()->read( node ) )
@@ -326,7 +314,7 @@ void QgsHandleBadLayers::apply()
}
else
{
fileItem->setForeground( QBrush( Qt::red ) );
item->setForeground( QBrush( Qt::red ) );
}
}
}
@@ -54,15 +54,16 @@ class APP_EXPORT QgsHandleBadLayers
void apply();
void accept();
void rejected();
void itemChanged( QTableWidgetItem * );
void cellDoubleClicked( int row, int column );

private:
QPushButton *mBrowseButton;
const QList<QDomNode> &mLayers;
QList<int> mRows;
QString mVectorFileFilter;
QString mRasterFileFilter;

QString filename( int row );
void setFilename( int row, QString filename );
};

#endif
@@ -15,43 +15,7 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTableWidget" name="mLayerList">
<column>
<property name="text">
<string>Layer name</string>
</property>
</column>
<column>
<property name="text">
<string>Type</string>
</property>
</column>
<column>
<property name="text">
<string>Provider</string>
</property>
</column>
<column>
<property name="text">
<string>Original filename</string>
</property>
</column>
<column>
<property name="text">
<string>New filename</string>
</property>
</column>
<column>
<property name="text">
<string>Original datasource</string>
</property>
</column>
<column>
<property name="text">
<string>New datasource</string>
</property>
</column>
</widget>
<widget class="QTableWidget" name="mLayerList"/>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">

0 comments on commit 1532048

Please sign in to comment.
You can’t perform that action at this time.