Skip to content

Commit 5ac587d

Browse files
committed
Merge pull request #1000 from matthias-kuhn/filewriter
Visual candy for the "Save as" GUI
2 parents 55f8606 + c8c2ba2 commit 5ac587d

5 files changed

+1712
-249
lines changed

src/app/ogr/qgsvectorlayersaveasdialog.cpp

+224-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "qgsvectorlayersaveasdialog.h"
2020
#include "qgsgenericprojectionselector.h"
2121
#include "qgsvectordataprovider.h"
22-
#include "qgsvectorfilewriter.h"
2322
#include "qgscoordinatereferencesystem.h"
2423

2524
#include <QSettings>
@@ -88,6 +87,75 @@ void QgsVectorLayerSaveAsDialog::setup()
8887
mSymbologyExportComboBox->addItem( tr( "Feature symbology" ), QgsVectorFileWriter::FeatureSymbology );
8988
mSymbologyExportComboBox->addItem( tr( "Symbol layer symbology" ), QgsVectorFileWriter::SymbolLayerSymbology );
9089
on_mSymbologyExportComboBox_currentIndexChanged( mSymbologyExportComboBox->currentText() );
90+
mOptionsButton->setChecked( settings.value( "/UI/vectorLayerSaveAsOptionsVisible" ).toBool() );
91+
}
92+
93+
QList<QPair<QLabel*, QWidget*> > QgsVectorLayerSaveAsDialog::createControls( const QMap<QString, QgsVectorFileWriter::Option*>& options )
94+
{
95+
QList<QPair<QLabel*, QWidget*> > controls;
96+
QMap<QString, QgsVectorFileWriter::Option*>::ConstIterator it;
97+
98+
for ( it = options.constBegin(); it != options.constEnd(); ++it )
99+
{
100+
QgsVectorFileWriter::Option* option = it.value();
101+
QLabel* label = new QLabel( it.key() );
102+
QWidget* control;
103+
switch ( option->type )
104+
{
105+
case QgsVectorFileWriter::Int:
106+
{
107+
QgsVectorFileWriter::IntOption* opt = dynamic_cast<QgsVectorFileWriter::IntOption*>( option );
108+
QSpinBox* sb = new QSpinBox();
109+
sb->setObjectName( it.key() );
110+
sb->setValue( opt->defaultValue );
111+
control = sb;
112+
break;
113+
}
114+
115+
case QgsVectorFileWriter::Set:
116+
{
117+
QgsVectorFileWriter::SetOption* opt = dynamic_cast<QgsVectorFileWriter::SetOption*>( option );
118+
QComboBox* cb = new QComboBox();
119+
cb->setObjectName( it.key() );
120+
Q_FOREACH( const QString& val, opt->values )
121+
{
122+
cb->addItem( val, val );
123+
}
124+
if ( opt->allowNone )
125+
cb->addItem( tr( "<Default>" ), QVariant( QVariant::String ) );
126+
int idx = cb->findText( opt->defaultValue );
127+
if ( idx == -1 )
128+
idx = cb->findData( QVariant( QVariant::String ) );
129+
cb->setCurrentIndex( idx );
130+
control = cb;
131+
break;
132+
}
133+
134+
case QgsVectorFileWriter::String:
135+
{
136+
QgsVectorFileWriter::StringOption* opt = dynamic_cast<QgsVectorFileWriter::StringOption*>( option );
137+
QLineEdit* le = new QLineEdit( opt->defaultValue );
138+
le->setObjectName( it.key() );
139+
control = le;
140+
break;
141+
}
142+
143+
case QgsVectorFileWriter::Hidden:
144+
control = 0;
145+
break;
146+
}
147+
148+
if ( control )
149+
{
150+
// Pack the tooltip in some html element, so it gets linebreaks.
151+
label->setToolTip( QString( "<p>%1</p>" ).arg( option->docString ) );
152+
control->setToolTip( QString( "<p>%1</p>" ).arg( option->docString ) );
153+
154+
controls << QPair<QLabel*, QWidget*>( label, control );
155+
}
156+
}
157+
158+
return controls;
91159
}
92160

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

@@ -133,6 +202,62 @@ void QgsVectorLayerSaveAsDialog::on_mFormatComboBox_currentIndexChanged( int idx
133202
mEncodingComboBox->setEnabled( true );
134203
mSkipAttributeCreation->setEnabled( true );
135204
}
205+
206+
QgsVectorFileWriter::MetaData driverMetaData;
207+
208+
while ( mDatasourceOptionsGroupBox->layout()->count() )
209+
{
210+
QLayoutItem* item = mDatasourceOptionsGroupBox->layout()->takeAt( 0 );
211+
delete item->widget();
212+
delete item;
213+
}
214+
215+
while ( mLayerOptionsGroupBox->layout()->count() )
216+
{
217+
QLayoutItem* item = mLayerOptionsGroupBox->layout()->takeAt( 0 );
218+
delete item->widget();
219+
delete item;
220+
}
221+
222+
// workaround so the Q_FOREACH macro does not get confused by the ','
223+
typedef QPair<QLabel*, QWidget*> LabelControlPair;
224+
225+
if ( QgsVectorFileWriter::driverMetadata( format(), driverMetaData ) )
226+
{
227+
if ( driverMetaData.driverOptions.size() != 0 )
228+
{
229+
mDatasourceOptionsGroupBox->setVisible( true );
230+
QList<QPair<QLabel*, QWidget*> > controls = createControls( driverMetaData.driverOptions );
231+
232+
QFormLayout* datasourceLayout = dynamic_cast<QFormLayout*>( mDatasourceOptionsGroupBox->layout() );
233+
234+
Q_FOREACH( const LabelControlPair& control, controls )
235+
{
236+
datasourceLayout->addRow( control.first, control.second );
237+
}
238+
}
239+
else
240+
{
241+
mDatasourceOptionsGroupBox->setVisible( false );
242+
}
243+
244+
if ( driverMetaData.layerOptions.size() != 0 )
245+
{
246+
mLayerOptionsGroupBox->setVisible( true );
247+
QList<QPair<QLabel*, QWidget*> > controls = createControls( driverMetaData.layerOptions );
248+
249+
QFormLayout* layerOptionsLayout = dynamic_cast<QFormLayout*>( mLayerOptionsGroupBox->layout() );
250+
251+
Q_FOREACH( const LabelControlPair& control, controls )
252+
{
253+
layerOptionsLayout->addRow( control.first, control.second );
254+
}
255+
}
256+
else
257+
{
258+
mLayerOptionsGroupBox->setVisible( false );
259+
}
260+
}
136261
}
137262

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

200325
QStringList QgsVectorLayerSaveAsDialog::datasourceOptions() const
201326
{
202-
return mOgrDatasourceOptions->toPlainText().split( "\n" );
327+
QStringList options;
328+
329+
QgsVectorFileWriter::MetaData driverMetaData;
330+
331+
if ( QgsVectorFileWriter::driverMetadata( format(), driverMetaData ) )
332+
{
333+
QMap<QString, QgsVectorFileWriter::Option*>::ConstIterator it;
334+
335+
for ( it = driverMetaData.driverOptions.constBegin(); it != driverMetaData.driverOptions.constEnd(); ++it )
336+
{
337+
switch ( it.value()->type )
338+
{
339+
case QgsVectorFileWriter::Int:
340+
{
341+
QSpinBox* sb = mDatasourceOptionsGroupBox->findChild<QSpinBox*>( it.key() );
342+
if ( sb )
343+
options << QString( "%1=%2" ).arg( it.key() ).arg( sb->value() );
344+
break;
345+
}
346+
347+
case QgsVectorFileWriter::Set:
348+
{
349+
QComboBox* cb = mDatasourceOptionsGroupBox->findChild<QComboBox*>( it.key() );
350+
if ( cb && !cb->itemData( cb->currentIndex() ).isNull() )
351+
options << QString( "%1=%2" ).arg( it.key() ).arg( cb->currentText() );
352+
break;
353+
}
354+
355+
case QgsVectorFileWriter::String:
356+
{
357+
QLineEdit* le = mDatasourceOptionsGroupBox->findChild<QLineEdit*>( it.key() );
358+
if ( le )
359+
options << QString( "%1=%2" ).arg( it.key() ).arg( le->text() );
360+
break;
361+
}
362+
363+
case QgsVectorFileWriter::Hidden:
364+
{
365+
QgsVectorFileWriter::HiddenOption* opt
366+
= dynamic_cast<QgsVectorFileWriter::HiddenOption*>( it.value() );
367+
options << QString( "%1=%2" ).arg( it.key() ).arg( opt->mValue );
368+
break;
369+
}
370+
}
371+
}
372+
}
373+
374+
return options + mOgrDatasourceOptions->toPlainText().split( "\n" );
203375
}
204376

205377
QStringList QgsVectorLayerSaveAsDialog::layerOptions() const
206378
{
207-
return mOgrLayerOptions->toPlainText().split( "\n" );
379+
QStringList options;
380+
381+
QgsVectorFileWriter::MetaData driverMetaData;
382+
383+
if ( QgsVectorFileWriter::driverMetadata( format(), driverMetaData ) )
384+
{
385+
QMap<QString, QgsVectorFileWriter::Option*>::ConstIterator it;
386+
387+
for ( it = driverMetaData.layerOptions.constBegin(); it != driverMetaData.layerOptions.constEnd(); ++it )
388+
{
389+
switch ( it.value()->type )
390+
{
391+
case QgsVectorFileWriter::Int:
392+
{
393+
QSpinBox* sb = mLayerOptionsGroupBox->findChild<QSpinBox*>( it.key() );
394+
options << QString( "%1=%2" ).arg( it.key() ).arg( sb->value() );
395+
break;
396+
}
397+
398+
case QgsVectorFileWriter::Set:
399+
{
400+
QComboBox* cb = mLayerOptionsGroupBox->findChild<QComboBox*>( it.key() );
401+
options << QString( "%1=%2" ).arg( it.key() ).arg( cb->currentText() );
402+
break;
403+
}
404+
405+
case QgsVectorFileWriter::String:
406+
{
407+
QLineEdit* le = mLayerOptionsGroupBox->findChild<QLineEdit*>( it.key() );
408+
options << QString( "%1=%2" ).arg( it.key() ).arg( le->text() );
409+
break;
410+
}
411+
412+
case QgsVectorFileWriter::Hidden:
413+
{
414+
QgsVectorFileWriter::HiddenOption* opt
415+
= dynamic_cast<QgsVectorFileWriter::HiddenOption*>( it.value() );
416+
options << QString( "%1=%2" ).arg( it.key() ).arg( opt->mValue );
417+
break;
418+
}
419+
}
420+
}
421+
}
422+
423+
return options + mOgrLayerOptions->toPlainText().split( "\n" );
208424
}
209425

210426
bool QgsVectorLayerSaveAsDialog::skipAttributeCreation() const
@@ -237,3 +453,8 @@ void QgsVectorLayerSaveAsDialog::on_mSymbologyExportComboBox_currentIndexChanged
237453
mScaleSpinBox->setEnabled( scaleEnabled );
238454
mScaleLabel->setEnabled( scaleEnabled );
239455
}
456+
457+
void QgsVectorLayerSaveAsDialog::on_mOptionsButton_toggled( bool checked )
458+
{
459+
mOptionsGroupBox->setVisible( checked );
460+
}

src/app/ogr/qgsvectorlayersaveasdialog.h

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <ui_qgsvectorlayersaveasdialogbase.h>
2222
#include <QDialog>
2323
#include "qgscontexthelp.h"
24+
#include "qgsvectorfilewriter.h"
2425

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

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

7174
long mCRS;
7275
};

0 commit comments

Comments
 (0)