~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to src/plugins/ogr_converter/dialog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// $Id$
 
2
//////////////////////////////////////////////////////////////////////////////
 
3
//
 
4
// Copyright (C) 2008 by Mateusz Loskot <mateusz@loskot.net>
 
5
//
 
6
// This program is free software; you can redistribute it and/or modify
 
7
// it under the terms of the GNU General Public License as published by
 
8
// the Free Software Foundation; either version 2 of the License,
 
9
// or (at your option) any later version.
 
10
//
 
11
//////////////////////////////////////////////////////////////////////////////
 
12
 
 
13
// qgis::plugin::ogrconv
 
14
#include "dialog.h"
 
15
#include "format.h"
 
16
#include "translator.h"
 
17
// QGIS includes
 
18
#include <qgsapplication.h>
 
19
#include <qgslogger.h>
 
20
#include <qgscontexthelp.h>
 
21
// TODO: Add support of QGIS projection selector
 
22
//#include <qgsprojectionselector.h>
 
23
// Qt4
 
24
#include <QtAlgorithms>
 
25
#include <QtDebug>
 
26
#include <QFileDialog>
 
27
#include <QMessageBox>
 
28
#include <QSettings>
 
29
#include <QString>
 
30
#include <QStringList>
 
31
#include <QVariant>
 
32
 
 
33
#include <ogr_api.h>
 
34
 
 
35
Dialog::Dialog( QWidget* parent, Qt::WFlags fl )
 
36
    : QDialog( parent, fl )
 
37
{
 
38
  setupUi( this );
 
39
  populateFormats();
 
40
  resetSrcUi();
 
41
  resetDstUi();
 
42
}
 
43
 
 
44
Dialog::~Dialog()
 
45
{
 
46
}
 
47
 
 
48
void Dialog::resetSrcUi()
 
49
{
 
50
  // Clear all settings and states
 
51
  inputSrcDataset->clear();
 
52
  // TODO: Transformation support
 
53
  //inputSrcSrs->clear();
 
54
  comboSrcLayer->clear();
 
55
  radioSrcFile->setDisabled( true );
 
56
  radioSrcFile->setChecked( false );
 
57
  radioSrcDirectory->setDisabled( true );
 
58
  radioSrcDirectory->setChecked( false );
 
59
  radioSrcProtocol->setDisabled( true );
 
60
  radioSrcProtocol->setChecked( false );
 
61
 
 
62
  // Configure types of input sources
 
63
  unsigned char const& type = mSrcFormat.type();
 
64
 
 
65
  if ( isFormatType( type, Format::eFile ) )
 
66
  {
 
67
    radioSrcFile->setDisabled( false );
 
68
    radioSrcFile->setChecked( true );
 
69
  }
 
70
 
 
71
  if ( isFormatType( type, Format::eDirectory ) )
 
72
  {
 
73
    radioSrcDirectory->setDisabled( false );
 
74
    if ( !radioSrcFile->isEnabled() )
 
75
      radioSrcDirectory->setChecked( true );
 
76
  }
 
77
 
 
78
  if ( isFormatType( type, Format::eProtocol ) )
 
79
  {
 
80
    radioSrcProtocol->setDisabled( false );
 
81
 
 
82
    if ( !radioSrcFile->isEnabled() && !radioSrcDirectory->isEnabled() )
 
83
    {
 
84
      radioSrcProtocol->setChecked( true );
 
85
      inputSrcDataset->setText( mSrcFormat.protocol() );
 
86
    }
 
87
  }
 
88
 
 
89
  setButtonState( buttonSelectSrc, isFormatType( type, Format::eProtocol ) );
 
90
}
 
91
 
 
92
void Dialog::resetDstUi()
 
93
{
 
94
  inputDstDataset->clear();
 
95
  // TODO: Transformation support
 
96
  //inputDstSrs->clear();
 
97
 
 
98
  unsigned char const& type = mDstFormat.type();
 
99
  bool isProtocol = isFormatType( type, Format::eProtocol );
 
100
 
 
101
  if ( isProtocol )
 
102
  {
 
103
    inputDstDataset->setText( mDstFormat.protocol() );
 
104
  }
 
105
 
 
106
  setButtonState( buttonSelectDst, isProtocol );
 
107
}
 
108
 
 
109
void Dialog::setButtonState( QPushButton* btn, bool isProtocol )
 
110
{
 
111
  Q_CHECK_PTR( btn );
 
112
 
 
113
  if ( isProtocol )
 
114
  {
 
115
    btn->setText( tr( "Connect" ) );
 
116
  }
 
117
  else
 
118
  {
 
119
    btn->setText( tr( "Browse" ) );
 
120
  }
 
121
}
 
122
 
 
123
void Dialog::populateFormats()
 
124
{
 
125
  comboSrcFormats->clear();
 
126
  comboDstFormats->clear();
 
127
 
 
128
  QStringList drvSrcList;
 
129
  QStringList drvDstList;
 
130
  QString drvName;
 
131
 
 
132
  QgsApplication::registerOgrDrivers();
 
133
  int const drvCount = OGRGetDriverCount();
 
134
 
 
135
  for ( int i = 0; i < drvCount; ++i )
 
136
  {
 
137
    OGRSFDriverH drv = OGRGetDriver( i );
 
138
    Q_CHECK_PTR( drv );
 
139
    if ( 0 != drv )
 
140
    {
 
141
      drvName = OGR_Dr_GetName( drv );
 
142
      drvSrcList.append( drvName );
 
143
 
 
144
      if ( 0 != OGR_Dr_TestCapability( drv, ODrCCreateDataSource ) )
 
145
      {
 
146
        drvDstList.append( drvName );
 
147
      }
 
148
    }
 
149
  }
 
150
 
 
151
  qSort( drvSrcList.begin(), drvSrcList.end() );
 
152
  qSort( drvDstList.begin(), drvDstList.end() );
 
153
  comboSrcFormats->addItems( drvSrcList );
 
154
  comboDstFormats->addItems( drvDstList );
 
155
}
 
156
 
 
157
void Dialog::populateLayers( QString const& url )
 
158
{
 
159
  comboSrcLayer->clear();
 
160
 
 
161
  OGRDataSourceH ds = OGROpen( url.toAscii().constData(), 0, 0 );
 
162
  if ( 0 != ds )
 
163
  {
 
164
    QString lyrName;
 
165
    QString lyrType;
 
166
 
 
167
    int const size = OGR_DS_GetLayerCount( ds );
 
168
    for ( int i = 0; i < size; ++i )
 
169
    {
 
170
      OGRLayerH lyr = OGR_DS_GetLayer( ds, i );
 
171
      if ( 0 != lyr )
 
172
      {
 
173
        OGRFeatureDefnH lyrDef = OGR_L_GetLayerDefn( lyr );
 
174
        Q_ASSERT( 0 != lyrDef );
 
175
 
 
176
        lyrName = OGR_FD_GetName( lyrDef );
 
177
 
 
178
        OGRwkbGeometryType const geomType = OGR_FD_GetGeomType( lyrDef );
 
179
        lyrType = OGRGeometryTypeToName( geomType );
 
180
 
 
181
        // FIXME: Appending type to layer name prevents from layer finding
 
182
        //comboSrcLayer->addItem(lyrName + " (" + lyrType.toUpper() + ")");
 
183
        comboSrcLayer->addItem( lyrName );
 
184
      }
 
185
    }
 
186
 
 
187
    OGR_DS_Destroy( ds );
 
188
  }
 
189
  else
 
190
  {
 
191
    QMessageBox::warning( this,
 
192
                          tr( "OGR Converter" ),
 
193
                          tr( "Could not establish connection to: '%1'" ).arg( url ),
 
194
                          QMessageBox::Close );
 
195
  }
 
196
}
 
197
 
 
198
bool Dialog::testConnection( QString const& url )
 
199
{
 
200
  bool success = false;
 
201
 
 
202
  OGRDataSourceH ds = OGROpen( url.toAscii().constData(), 0, 0 );
 
203
  if ( 0 != ds )
 
204
  {
 
205
    success = true;
 
206
    OGR_DS_Destroy( ds );
 
207
  }
 
208
 
 
209
  return success;
 
210
}
 
211
 
 
212
QString Dialog::openFile()
 
213
{
 
214
  QSettings sets;
 
215
  QString path = QFileDialog::getOpenFileName( this,
 
216
                 tr( "Open OGR file" ),
 
217
                 sets.value( "/Plugin-OGR/ogr-file", "./" ).toString(),
 
218
                 tr( "OGR File Data Source (*.*)" ) );
 
219
 
 
220
  return path;
 
221
}
 
222
 
 
223
QString Dialog::openDirectory()
 
224
{
 
225
  QString path = QFileDialog::getExistingDirectory( this,
 
226
                 tr( "Open Directory" ), "",
 
227
                 QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks );
 
228
 
 
229
  return path;
 
230
}
 
231
 
 
232
void Dialog::on_buttonBox_accepted()
 
233
{
 
234
  // Validate input settings
 
235
  QString srcUrl( inputSrcDataset->text() );
 
236
  QString srcLayer( comboSrcLayer->currentText() );
 
237
 
 
238
  if ( srcUrl.isEmpty() )
 
239
  {
 
240
    QMessageBox::warning( this,
 
241
                          tr( "OGR Layer Converter" ),
 
242
                          tr( "Input OGR dataset is missing!" ) );
 
243
    return;
 
244
  }
 
245
 
 
246
  if ( srcLayer.isEmpty() )
 
247
  {
 
248
    QMessageBox::warning( this,
 
249
                          tr( "OGR Layer Converter" ),
 
250
                          tr( "Input OGR layer name is missing!" ) );
 
251
    return;
 
252
  }
 
253
 
 
254
  // Validate output settings
 
255
  QString dstFormat( comboDstFormats->currentText() );
 
256
  QString dstUrl( inputDstDataset->text() );
 
257
  QString dstLayer( inputDstLayer->text() );
 
258
  if ( dstLayer.isEmpty() )
 
259
    dstLayer = srcLayer;
 
260
 
 
261
  if ( dstFormat.isEmpty() )
 
262
  {
 
263
    QMessageBox::warning( this,
 
264
                          tr( "OGR Layer Converter" ),
 
265
                          tr( "Target OGR format not selected!" ) );
 
266
    return;
 
267
  }
 
268
 
 
269
  if ( dstUrl.isEmpty() )
 
270
  {
 
271
    QMessageBox::warning( this,
 
272
                          tr( "OGR Layer Converter" ),
 
273
                          tr( "Output OGR dataset is missing!" ) );
 
274
    return;
 
275
  }
 
276
 
 
277
  if ( dstLayer.isEmpty() )
 
278
  {
 
279
    QMessageBox::warning( this,
 
280
                          tr( "OGR Layer Converter" ),
 
281
                          tr( "Output OGR layer name is missing!" ) );
 
282
    return;
 
283
  }
 
284
 
 
285
  // TODO: SRS transformation support
 
286
  //QString srcSrs("EPSG:");
 
287
  //QString dstSrs("EPSG:");
 
288
  //srcSrs += inputSrcSrs->text();
 
289
  //dstSrs += inputDstSrs->text();
 
290
 
 
291
  // Execute layer translation
 
292
  bool success = false;
 
293
 
 
294
  // TODO: Use try-catch to display more meaningful error messages from Translator
 
295
  Translator worker( srcUrl, dstUrl, dstFormat );
 
296
  worker.setSourceLayer( srcLayer );
 
297
  worker.setTargetLayer( dstLayer );
 
298
  success = worker.translate();
 
299
 
 
300
  if ( success )
 
301
  {
 
302
    QMessageBox::information( this,
 
303
                              tr( "OGR Layer Converter" ),
 
304
                              tr( "Successfully translated layer '%1'" ).arg( srcLayer ) );
 
305
  }
 
306
  else
 
307
  {
 
308
    QMessageBox::information( this,
 
309
                              tr( "OGR Layer Converter" ),
 
310
                              tr( "Failed to translate layer '%1'" ).arg( srcLayer ) );
 
311
  }
 
312
 
 
313
  // Close dialog box
 
314
  accept();
 
315
}
 
316
 
 
317
void Dialog::on_buttonBox_rejected()
 
318
{
 
319
  reject();
 
320
}
 
321
 
 
322
void Dialog::on_radioSrcFile_toggled( bool checked )
 
323
{
 
324
  if ( checked )
 
325
  {
 
326
    unsigned char const& type = mSrcFormat.type();
 
327
    Q_ASSERT( isFormatType( type, Format::eFile ) );
 
328
 
 
329
    inputSrcDataset->clear();
 
330
    setButtonState( buttonSelectSrc, isFormatType( type, Format::eProtocol ) );
 
331
  }
 
332
}
 
333
 
 
334
void Dialog::on_radioSrcDirectory_toggled( bool checked )
 
335
{
 
336
  if ( checked )
 
337
  {
 
338
    unsigned char const& type = mSrcFormat.type();
 
339
    Q_ASSERT( isFormatType( type, Format::eDirectory ) );
 
340
 
 
341
    inputSrcDataset->clear();
 
342
    setButtonState( buttonSelectSrc, isFormatType( type, Format::eProtocol ) );
 
343
  }
 
344
}
 
345
 
 
346
void Dialog::on_radioSrcProtocol_toggled( bool checked )
 
347
{
 
348
  if ( checked )
 
349
  {
 
350
    unsigned char const& type = mSrcFormat.type();
 
351
    Q_ASSERT( isFormatType( type, Format::eProtocol ) );
 
352
 
 
353
    inputSrcDataset->setText( mSrcFormat.protocol() );
 
354
    setButtonState( buttonSelectSrc, isFormatType( type, Format::eProtocol ) );
 
355
  }
 
356
}
 
357
 
 
358
void Dialog::on_comboSrcFormats_currentIndexChanged( int index )
 
359
{
 
360
  // Select source data format
 
361
  QString frmtCode = comboSrcFormats->currentText();
 
362
  mSrcFormat = mFrmts.find( frmtCode );
 
363
 
 
364
  resetSrcUi();
 
365
}
 
366
 
 
367
void Dialog::on_comboDstFormats_currentIndexChanged( int index )
 
368
{
 
369
  // Select destination data format
 
370
  QString frmtCode = comboDstFormats->currentText();
 
371
  mDstFormat = mFrmts.find( frmtCode );
 
372
 
 
373
  resetDstUi();
 
374
}
 
375
 
 
376
void Dialog::on_buttonSelectSrc_clicked()
 
377
{
 
378
  QSettings settings;
 
379
  QString src;
 
380
 
 
381
  if ( radioSrcFile->isChecked() )
 
382
  {
 
383
    src = openFile();
 
384
  }
 
385
  else if ( radioSrcDirectory->isChecked() )
 
386
  {
 
387
    src = openDirectory();
 
388
  }
 
389
  else if ( radioSrcProtocol->isChecked() )
 
390
  {
 
391
    src = inputSrcDataset->text();
 
392
  }
 
393
  else
 
394
  {
 
395
    Q_ASSERT( !"SHOULD NEVER GET HERE" );
 
396
  }
 
397
 
 
398
  inputSrcDataset->setText( src );
 
399
 
 
400
  if ( !src.isEmpty() )
 
401
  {
 
402
    populateLayers( src );
 
403
  }
 
404
}
 
405
 
 
406
void Dialog::on_buttonSelectDst_clicked()
 
407
{
 
408
  QSettings settings;
 
409
  QString dst;
 
410
  QString msg;
 
411
 
 
412
  unsigned char const& type = mDstFormat.type();
 
413
  if ( isFormatType( type, Format::eProtocol ) )
 
414
  {
 
415
    dst = inputDstDataset->text();
 
416
 
 
417
    if ( testConnection( dst ) )
 
418
    {
 
419
      msg = tr( "Successfully connected to: '%1'" ).arg( dst );
 
420
    }
 
421
    else
 
422
    {
 
423
      msg = tr( "Could not establish connection to: '%1'" ).arg( dst );
 
424
    }
 
425
 
 
426
    QMessageBox::information( this, tr( "OGR Converter" ), msg, QMessageBox::Close );
 
427
  }
 
428
  else if ( isFormatType( type, Format::eDirectory ) )
 
429
  {
 
430
    dst = openDirectory();
 
431
  }
 
432
  else if ( isFormatType( type, Format::eFile ) )
 
433
  {
 
434
    dst = QFileDialog::getSaveFileName( this,
 
435
                                        tr( "Choose a file name to save to" ),
 
436
                                        "output", tr( "OGR File Data Source (*.*)" ) );
 
437
  }
 
438
  else
 
439
  {
 
440
    Q_ASSERT( !"SHOULD NEVER GET HERE" );
 
441
  }
 
442
 
 
443
  inputDstDataset->setText( dst );
 
444
}