mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-15 00:04:00 -04:00
improve bad layer handling: make all datasource editable (fixes #7039)
This commit is contained in:
parent
d308ee5201
commit
1532048430
@ -70,21 +70,20 @@ QgsHandleBadLayers::QgsHandleBadLayers( const QList<QDomNode> &layers, const QDo
|
|||||||
mBrowseButton->setDisabled( true );
|
mBrowseButton->setDisabled( true );
|
||||||
|
|
||||||
connect( mLayerList, SIGNAL( itemSelectionChanged() ), this, SLOT( selectionChanged() ) );
|
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( 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->clear();
|
||||||
mLayerList->setSortingEnabled( true );
|
mLayerList->setSortingEnabled( true );
|
||||||
mLayerList->setSelectionBehavior( QAbstractItemView::SelectRows );
|
mLayerList->setSelectionBehavior( QAbstractItemView::SelectRows );
|
||||||
mLayerList->setColumnCount( 5 );
|
mLayerList->setColumnCount( 4 );
|
||||||
|
|
||||||
mLayerList->setHorizontalHeaderLabels( QStringList()
|
mLayerList->setHorizontalHeaderLabels( QStringList()
|
||||||
<< tr( "Layer name" )
|
<< tr( "Layer name" )
|
||||||
<< tr( "Type" )
|
<< tr( "Type" )
|
||||||
<< tr( "Provider" )
|
<< tr( "Provider" )
|
||||||
<< tr( "New file" )
|
<< tr( "Datasource" )
|
||||||
<< tr( "New datasource" )
|
|
||||||
);
|
);
|
||||||
|
|
||||||
int j = 0;
|
int j = 0;
|
||||||
@ -95,42 +94,12 @@ QgsHandleBadLayers::QgsHandleBadLayers( const QList<QDomNode> &layers, const QDo
|
|||||||
QString name = node.namedItem( "layername" ).toElement().text();
|
QString name = node.namedItem( "layername" ).toElement().text();
|
||||||
QString type = node.toElement().attribute( "type" );
|
QString type = node.toElement().attribute( "type" );
|
||||||
QString datasource = node.namedItem( "datasource" ).toElement().text();
|
QString datasource = node.namedItem( "datasource" ).toElement().text();
|
||||||
QString provider;
|
QString provider = type == "vector" ? node.namedItem( "provider" ).toElement().text() : tr( "none" );
|
||||||
|
|
||||||
QString filename = datasource;
|
QgsDebugMsg( QString( "name=%1 type=%2 provider=%3 datasource='%4'" )
|
||||||
|
|
||||||
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'" )
|
|
||||||
.arg( name )
|
.arg( name )
|
||||||
.arg( type )
|
.arg( type )
|
||||||
.arg( provider )
|
.arg( provider )
|
||||||
.arg( filename )
|
|
||||||
.arg( datasource ) );
|
.arg( datasource ) );
|
||||||
|
|
||||||
mLayerList->setRowCount( j + 1 );
|
mLayerList->setRowCount( j + 1 );
|
||||||
@ -150,42 +119,19 @@ QgsHandleBadLayers::QgsHandleBadLayers( const QList<QDomNode> &layers, const QDo
|
|||||||
item->setFlags( item->flags() & ~Qt::ItemIsEditable );
|
item->setFlags( item->flags() & ~Qt::ItemIsEditable );
|
||||||
mLayerList->setItem( j, 2, item );
|
mLayerList->setItem( j, 2, item );
|
||||||
|
|
||||||
item = new QTableWidgetItem( filename );
|
|
||||||
mLayerList->setItem( j, 3, item );
|
|
||||||
|
|
||||||
item = new QTableWidgetItem( datasource );
|
item = new QTableWidgetItem( datasource );
|
||||||
item->setFlags( item->flags() & ~Qt::ItemIsEditable );
|
mLayerList->setItem( j, 3, item );
|
||||||
mLayerList->setItem( j, 4, item );
|
|
||||||
|
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
|
|
||||||
mLayerList->resizeColumnsToContents();
|
// mLayerList->resizeColumnsToContents();
|
||||||
}
|
}
|
||||||
|
|
||||||
QgsHandleBadLayers::~QgsHandleBadLayers()
|
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()
|
void QgsHandleBadLayers::selectionChanged()
|
||||||
{
|
{
|
||||||
QgsDebugMsg( "entered." );
|
QgsDebugMsg( "entered." );
|
||||||
@ -203,18 +149,89 @@ void QgsHandleBadLayers::selectionChanged()
|
|||||||
mBrowseButton->setEnabled( mRows.size() > 0 );
|
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()
|
void QgsHandleBadLayers::browseClicked()
|
||||||
{
|
{
|
||||||
QgsDebugMsg( "entered." );
|
QgsDebugMsg( "entered." );
|
||||||
|
|
||||||
if ( mRows.size() == 1 )
|
if ( mRows.size() == 1 )
|
||||||
{
|
{
|
||||||
QString memoryQualifier;
|
int row = mRows[0];
|
||||||
QString fileFilter;
|
QString type = mLayerList->item( row, 1 )->text();
|
||||||
int idx = mLayerList->item( mRows[0], 0 )->data( Qt::UserRole ).toInt();
|
|
||||||
QTableWidgetItem *fileItem = mLayerList->item( mRows[0], 3 );
|
|
||||||
|
|
||||||
if ( mLayers[ idx ].toElement().attribute( "type" ) == "vector" )
|
QString memoryQualifier, fileFilter;
|
||||||
|
if ( type == "vector" )
|
||||||
{
|
{
|
||||||
memoryQualifier = "lastVectorFileFilter";
|
memoryQualifier = "lastVectorFileFilter";
|
||||||
fileFilter = mVectorFileFilter;
|
fileFilter = mVectorFileFilter;
|
||||||
@ -225,19 +242,22 @@ void QgsHandleBadLayers::browseClicked()
|
|||||||
fileFilter = mRasterFileFilter;
|
fileFilter = mRasterFileFilter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString fn = filename( row );
|
||||||
|
if ( fn.isNull() )
|
||||||
|
return;
|
||||||
|
|
||||||
QStringList selectedFiles;
|
QStringList selectedFiles;
|
||||||
QString enc;
|
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 );
|
QgisGui::openFilesRememberingFilter( memoryQualifier, fileFilter, selectedFiles, enc, title );
|
||||||
|
|
||||||
if ( selectedFiles.size() != 1 )
|
if ( selectedFiles.size() != 1 )
|
||||||
{
|
{
|
||||||
QMessageBox::information( this, title, tr( "Please select exactly one file." ) );
|
QMessageBox::information( this, title, tr( "Please select exactly one file." ) );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
fileItem->setText( selectedFiles[0] );
|
setFilename( row, selectedFiles[0] );
|
||||||
}
|
}
|
||||||
else if ( mRows.size() > 1 )
|
else if ( mRows.size() > 1 )
|
||||||
{
|
{
|
||||||
@ -246,7 +266,6 @@ void QgsHandleBadLayers::browseClicked()
|
|||||||
QString title = tr( "Select new directory of selected files" );
|
QString title = tr( "Select new directory of selected files" );
|
||||||
|
|
||||||
QgisGui::openFilesRememberingFilter( "missingDirectory", tr( "All files (*)" ), selectedFiles, enc, title );
|
QgisGui::openFilesRememberingFilter( "missingDirectory", tr( "All files (*)" ), selectedFiles, enc, title );
|
||||||
|
|
||||||
if ( selectedFiles.isEmpty() )
|
if ( selectedFiles.isEmpty() )
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
@ -258,17 +277,19 @@ void QgsHandleBadLayers::browseClicked()
|
|||||||
return;
|
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() );
|
fi.setFile( path.dir(), fi.fileName() );
|
||||||
|
|
||||||
if ( !fi.exists() )
|
if ( !fi.exists() )
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
fileItem->setText( fi.absoluteFilePath() );
|
setFilename( row, fi.absoluteFilePath() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -283,41 +304,8 @@ void QgsHandleBadLayers::apply()
|
|||||||
|
|
||||||
QString type = mLayerList->item( i, 1 )->text();
|
QString type = mLayerList->item( i, 1 )->text();
|
||||||
QString provider = mLayerList->item( i, 2 )->text();
|
QString provider = mLayerList->item( i, 2 )->text();
|
||||||
QTableWidgetItem *fileItem = mLayerList->item( i, 3 );
|
QTableWidgetItem *item = mLayerList->item( i, 3 );
|
||||||
QString datasource = mLayerList->item( i, 4 )->text();
|
QString datasource = item->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;
|
|
||||||
}
|
|
||||||
|
|
||||||
node.namedItem( "datasource" ).toElement().firstChild().toText().setData( datasource );
|
node.namedItem( "datasource" ).toElement().firstChild().toText().setData( datasource );
|
||||||
if ( QgsProject::instance()->read( node ) )
|
if ( QgsProject::instance()->read( node ) )
|
||||||
@ -326,7 +314,7 @@ void QgsHandleBadLayers::apply()
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fileItem->setForeground( QBrush( Qt::red ) );
|
item->setForeground( QBrush( Qt::red ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,8 +54,6 @@ class APP_EXPORT QgsHandleBadLayers
|
|||||||
void apply();
|
void apply();
|
||||||
void accept();
|
void accept();
|
||||||
void rejected();
|
void rejected();
|
||||||
void itemChanged( QTableWidgetItem * );
|
|
||||||
void cellDoubleClicked( int row, int column );
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QPushButton *mBrowseButton;
|
QPushButton *mBrowseButton;
|
||||||
@ -63,6 +61,9 @@ class APP_EXPORT QgsHandleBadLayers
|
|||||||
QList<int> mRows;
|
QList<int> mRows;
|
||||||
QString mVectorFileFilter;
|
QString mVectorFileFilter;
|
||||||
QString mRasterFileFilter;
|
QString mRasterFileFilter;
|
||||||
|
|
||||||
|
QString filename( int row );
|
||||||
|
void setFilename( int row, QString filename );
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -15,43 +15,7 @@
|
|||||||
</property>
|
</property>
|
||||||
<layout class="QVBoxLayout" name="verticalLayout">
|
<layout class="QVBoxLayout" name="verticalLayout">
|
||||||
<item>
|
<item>
|
||||||
<widget class="QTableWidget" name="mLayerList">
|
<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>
|
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QDialogButtonBox" name="buttonBox">
|
<widget class="QDialogButtonBox" name="buttonBox">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user