Skip to content
Permalink
Browse files

Merge pull request #1000 from matthias-kuhn/filewriter

Visual candy for the "Save as" GUI
  • Loading branch information
jef-n committed Nov 26, 2013
2 parents 55f8606 + c8c2ba2 commit 5ac587d63984c527ee26c785dc0c8297f3273280
@@ -19,7 +19,6 @@
#include "qgsvectorlayersaveasdialog.h"
#include "qgsgenericprojectionselector.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorfilewriter.h"
#include "qgscoordinatereferencesystem.h"

#include <QSettings>
@@ -88,6 +87,75 @@ void QgsVectorLayerSaveAsDialog::setup()
mSymbologyExportComboBox->addItem( tr( "Feature symbology" ), QgsVectorFileWriter::FeatureSymbology );
mSymbologyExportComboBox->addItem( tr( "Symbol layer symbology" ), QgsVectorFileWriter::SymbolLayerSymbology );
on_mSymbologyExportComboBox_currentIndexChanged( mSymbologyExportComboBox->currentText() );
mOptionsButton->setChecked( settings.value( "/UI/vectorLayerSaveAsOptionsVisible" ).toBool() );
}

QList<QPair<QLabel*, QWidget*> > QgsVectorLayerSaveAsDialog::createControls( const QMap<QString, QgsVectorFileWriter::Option*>& options )
{
QList<QPair<QLabel*, QWidget*> > controls;
QMap<QString, QgsVectorFileWriter::Option*>::ConstIterator it;

for ( it = options.constBegin(); it != options.constEnd(); ++it )
{
QgsVectorFileWriter::Option* option = it.value();
QLabel* label = new QLabel( it.key() );
QWidget* control;
switch ( option->type )
{
case QgsVectorFileWriter::Int:
{
QgsVectorFileWriter::IntOption* opt = dynamic_cast<QgsVectorFileWriter::IntOption*>( option );
QSpinBox* sb = new QSpinBox();
sb->setObjectName( it.key() );
sb->setValue( opt->defaultValue );
control = sb;
break;
}

case QgsVectorFileWriter::Set:
{
QgsVectorFileWriter::SetOption* opt = dynamic_cast<QgsVectorFileWriter::SetOption*>( option );
QComboBox* cb = new QComboBox();
cb->setObjectName( it.key() );
Q_FOREACH( const QString& val, opt->values )
{
cb->addItem( val, val );
}
if ( opt->allowNone )
cb->addItem( tr( "<Default>" ), QVariant( QVariant::String ) );
int idx = cb->findText( opt->defaultValue );
if ( idx == -1 )
idx = cb->findData( QVariant( QVariant::String ) );
cb->setCurrentIndex( idx );
control = cb;
break;
}

case QgsVectorFileWriter::String:
{
QgsVectorFileWriter::StringOption* opt = dynamic_cast<QgsVectorFileWriter::StringOption*>( option );
QLineEdit* le = new QLineEdit( opt->defaultValue );
le->setObjectName( it.key() );
control = le;
break;
}

case QgsVectorFileWriter::Hidden:
control = 0;
break;
}

if ( control )
{
// Pack the tooltip in some html element, so it gets linebreaks.
label->setToolTip( QString( "<p>%1</p>" ).arg( option->docString ) );
control->setToolTip( QString( "<p>%1</p>" ).arg( option->docString ) );

controls << QPair<QLabel*, QWidget*>( label, control );
}
}

return controls;
}

QgsVectorLayerSaveAsDialog::~QgsVectorLayerSaveAsDialog()
@@ -102,6 +170,7 @@ void QgsVectorLayerSaveAsDialog::accept()
settings.setValue( "/UI/lastVectorFileFilterDir", QFileInfo( filename() ).absolutePath() );
settings.setValue( "/UI/lastVectorFormat", format() );
settings.setValue( "/UI/encoding", encoding() );
settings.setValue( "/UI/vectorLayerSaveAsOptionsVisible", mOptionsButton->isChecked() );
QDialog::accept();
}

@@ -133,6 +202,62 @@ void QgsVectorLayerSaveAsDialog::on_mFormatComboBox_currentIndexChanged( int idx
mEncodingComboBox->setEnabled( true );
mSkipAttributeCreation->setEnabled( true );
}

QgsVectorFileWriter::MetaData driverMetaData;

while ( mDatasourceOptionsGroupBox->layout()->count() )
{
QLayoutItem* item = mDatasourceOptionsGroupBox->layout()->takeAt( 0 );
delete item->widget();
delete item;
}

while ( mLayerOptionsGroupBox->layout()->count() )
{
QLayoutItem* item = mLayerOptionsGroupBox->layout()->takeAt( 0 );
delete item->widget();
delete item;
}

// workaround so the Q_FOREACH macro does not get confused by the ','
typedef QPair<QLabel*, QWidget*> LabelControlPair;

if ( QgsVectorFileWriter::driverMetadata( format(), driverMetaData ) )
{
if ( driverMetaData.driverOptions.size() != 0 )
{
mDatasourceOptionsGroupBox->setVisible( true );
QList<QPair<QLabel*, QWidget*> > controls = createControls( driverMetaData.driverOptions );

QFormLayout* datasourceLayout = dynamic_cast<QFormLayout*>( mDatasourceOptionsGroupBox->layout() );

Q_FOREACH( const LabelControlPair& control, controls )
{
datasourceLayout->addRow( control.first, control.second );
}
}
else
{
mDatasourceOptionsGroupBox->setVisible( false );
}

if ( driverMetaData.layerOptions.size() != 0 )
{
mLayerOptionsGroupBox->setVisible( true );
QList<QPair<QLabel*, QWidget*> > controls = createControls( driverMetaData.layerOptions );

QFormLayout* layerOptionsLayout = dynamic_cast<QFormLayout*>( mLayerOptionsGroupBox->layout() );

Q_FOREACH( const LabelControlPair& control, controls )
{
layerOptionsLayout->addRow( control.first, control.second );
}
}
else
{
mLayerOptionsGroupBox->setVisible( false );
}
}
}

void QgsVectorLayerSaveAsDialog::on_browseFilename_clicked()
@@ -199,12 +324,103 @@ long QgsVectorLayerSaveAsDialog::crs() const

QStringList QgsVectorLayerSaveAsDialog::datasourceOptions() const
{
return mOgrDatasourceOptions->toPlainText().split( "\n" );
QStringList options;

QgsVectorFileWriter::MetaData driverMetaData;

if ( QgsVectorFileWriter::driverMetadata( format(), driverMetaData ) )
{
QMap<QString, QgsVectorFileWriter::Option*>::ConstIterator it;

for ( it = driverMetaData.driverOptions.constBegin(); it != driverMetaData.driverOptions.constEnd(); ++it )
{
switch ( it.value()->type )
{
case QgsVectorFileWriter::Int:
{
QSpinBox* sb = mDatasourceOptionsGroupBox->findChild<QSpinBox*>( it.key() );
if ( sb )
options << QString( "%1=%2" ).arg( it.key() ).arg( sb->value() );
break;
}

case QgsVectorFileWriter::Set:
{
QComboBox* cb = mDatasourceOptionsGroupBox->findChild<QComboBox*>( it.key() );
if ( cb && !cb->itemData( cb->currentIndex() ).isNull() )
options << QString( "%1=%2" ).arg( it.key() ).arg( cb->currentText() );
break;
}

case QgsVectorFileWriter::String:
{
QLineEdit* le = mDatasourceOptionsGroupBox->findChild<QLineEdit*>( it.key() );
if ( le )
options << QString( "%1=%2" ).arg( it.key() ).arg( le->text() );
break;
}

case QgsVectorFileWriter::Hidden:
{
QgsVectorFileWriter::HiddenOption* opt
= dynamic_cast<QgsVectorFileWriter::HiddenOption*>( it.value() );
options << QString( "%1=%2" ).arg( it.key() ).arg( opt->mValue );
break;
}
}
}
}

return options + mOgrDatasourceOptions->toPlainText().split( "\n" );
}

QStringList QgsVectorLayerSaveAsDialog::layerOptions() const
{
return mOgrLayerOptions->toPlainText().split( "\n" );
QStringList options;

QgsVectorFileWriter::MetaData driverMetaData;

if ( QgsVectorFileWriter::driverMetadata( format(), driverMetaData ) )
{
QMap<QString, QgsVectorFileWriter::Option*>::ConstIterator it;

for ( it = driverMetaData.layerOptions.constBegin(); it != driverMetaData.layerOptions.constEnd(); ++it )
{
switch ( it.value()->type )
{
case QgsVectorFileWriter::Int:
{
QSpinBox* sb = mLayerOptionsGroupBox->findChild<QSpinBox*>( it.key() );
options << QString( "%1=%2" ).arg( it.key() ).arg( sb->value() );
break;
}

case QgsVectorFileWriter::Set:
{
QComboBox* cb = mLayerOptionsGroupBox->findChild<QComboBox*>( it.key() );
options << QString( "%1=%2" ).arg( it.key() ).arg( cb->currentText() );
break;
}

case QgsVectorFileWriter::String:
{
QLineEdit* le = mLayerOptionsGroupBox->findChild<QLineEdit*>( it.key() );
options << QString( "%1=%2" ).arg( it.key() ).arg( le->text() );
break;
}

case QgsVectorFileWriter::Hidden:
{
QgsVectorFileWriter::HiddenOption* opt
= dynamic_cast<QgsVectorFileWriter::HiddenOption*>( it.value() );
options << QString( "%1=%2" ).arg( it.key() ).arg( opt->mValue );
break;
}
}
}
}

return options + mOgrLayerOptions->toPlainText().split( "\n" );
}

bool QgsVectorLayerSaveAsDialog::skipAttributeCreation() const
@@ -237,3 +453,8 @@ void QgsVectorLayerSaveAsDialog::on_mSymbologyExportComboBox_currentIndexChanged
mScaleSpinBox->setEnabled( scaleEnabled );
mScaleLabel->setEnabled( scaleEnabled );
}

void QgsVectorLayerSaveAsDialog::on_mOptionsButton_toggled( bool checked )
{
mOptionsGroupBox->setVisible( checked );
}
@@ -21,6 +21,7 @@
#include <ui_qgsvectorlayersaveasdialogbase.h>
#include <QDialog>
#include "qgscontexthelp.h"
#include "qgsvectorfilewriter.h"

/**
* Class to select destination file, type and CRS for ogr layrs
@@ -63,10 +64,12 @@ class QgsVectorLayerSaveAsDialog : public QDialog, private Ui::QgsVectorLayerSav
void on_browseCRS_clicked();
void on_buttonBox_helpRequested() { QgsContextHelp::run( metaObject()->className() ); }
void on_mSymbologyExportComboBox_currentIndexChanged( const QString& text );
void on_mOptionsButton_toggled( bool checked );
void accept();

private:
void setup();
QList< QPair< QLabel*, QWidget* > > createControls( const QMap<QString, QgsVectorFileWriter::Option*>& options );

long mCRS;
};

0 comments on commit 5ac587d

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