Skip to content
Permalink
Browse files

[saveas] Template for the driver options

  • Loading branch information
m-kuhn committed Sep 15, 2013
1 parent 584aba7 commit f78af8bd3ba5b55763e84c05864b986307d3effb
@@ -19,7 +19,6 @@
#include "qgsvectorlayersaveasdialog.h"
#include "qgsgenericprojectionselector.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorfilewriter.h"
#include "qgscoordinatereferencesystem.h"

#include <QSettings>
@@ -90,6 +89,68 @@ void QgsVectorLayerSaveAsDialog::setup()
on_mSymbologyExportComboBox_currentIndexChanged( mSymbologyExportComboBox->currentText() );
}

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;
}
}

// 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()
{
QSettings settings;
@@ -133,6 +194,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 +316,87 @@ 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;
}
}
}
}

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;
}
}
}
}

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

bool QgsVectorLayerSaveAsDialog::skipAttributeCreation() const
@@ -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
@@ -67,6 +68,7 @@ class QgsVectorLayerSaveAsDialog : public QDialog, private Ui::QgsVectorLayerSav

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

long mCRS;
};

0 comments on commit f78af8b

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