19
19
#include " qgsvectorlayersaveasdialog.h"
20
20
#include " qgsgenericprojectionselector.h"
21
21
#include " qgsvectordataprovider.h"
22
- #include " qgsvectorfilewriter.h"
23
22
#include " qgscoordinatereferencesystem.h"
24
23
25
24
#include < QSettings>
@@ -88,6 +87,75 @@ void QgsVectorLayerSaveAsDialog::setup()
88
87
mSymbologyExportComboBox ->addItem ( tr ( " Feature symbology" ), QgsVectorFileWriter::FeatureSymbology );
89
88
mSymbologyExportComboBox ->addItem ( tr ( " Symbol layer symbology" ), QgsVectorFileWriter::SymbolLayerSymbology );
90
89
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;
91
159
}
92
160
93
161
QgsVectorLayerSaveAsDialog::~QgsVectorLayerSaveAsDialog ()
@@ -102,6 +170,7 @@ void QgsVectorLayerSaveAsDialog::accept()
102
170
settings.setValue ( " /UI/lastVectorFileFilterDir" , QFileInfo ( filename () ).absolutePath () );
103
171
settings.setValue ( " /UI/lastVectorFormat" , format () );
104
172
settings.setValue ( " /UI/encoding" , encoding () );
173
+ settings.setValue ( " /UI/vectorLayerSaveAsOptionsVisible" , mOptionsButton ->isChecked () );
105
174
QDialog::accept ();
106
175
}
107
176
@@ -133,6 +202,62 @@ void QgsVectorLayerSaveAsDialog::on_mFormatComboBox_currentIndexChanged( int idx
133
202
mEncodingComboBox ->setEnabled ( true );
134
203
mSkipAttributeCreation ->setEnabled ( true );
135
204
}
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
+ }
136
261
}
137
262
138
263
void QgsVectorLayerSaveAsDialog::on_browseFilename_clicked ()
@@ -199,12 +324,103 @@ long QgsVectorLayerSaveAsDialog::crs() const
199
324
200
325
QStringList QgsVectorLayerSaveAsDialog::datasourceOptions () const
201
326
{
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 " );
203
375
}
204
376
205
377
QStringList QgsVectorLayerSaveAsDialog::layerOptions () const
206
378
{
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 " );
208
424
}
209
425
210
426
bool QgsVectorLayerSaveAsDialog::skipAttributeCreation () const
@@ -237,3 +453,8 @@ void QgsVectorLayerSaveAsDialog::on_mSymbologyExportComboBox_currentIndexChanged
237
453
mScaleSpinBox ->setEnabled ( scaleEnabled );
238
454
mScaleLabel ->setEnabled ( scaleEnabled );
239
455
}
456
+
457
+ void QgsVectorLayerSaveAsDialog::on_mOptionsButton_toggled ( bool checked )
458
+ {
459
+ mOptionsGroupBox ->setVisible ( checked );
460
+ }
0 commit comments