~ubuntu-branches/ubuntu/trusty/qgis/trusty

« back to all changes in this revision

Viewing changes to src/app/ogr/qgsopenvectorlayerdialog.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
/***************************************************************************
 
2
                          qgsopenvectorlayerdialog.cpp
 
3
 Dialog to select the type and source for ogr vectors, supports
 
4
 file, database, directory and protocol sources.
 
5
                             -------------------
 
6
    begin                : Mon Jan 2 2009
 
7
    copyright            : (C) 2009 by Godofredo Contreras Nava
 
8
    email                : frdcn at hotmail.com
 
9
 ***************************************************************************/
 
10
 
 
11
/***************************************************************************
 
12
 *                                                                         *
 
13
 *   This program is free software; you can redistribute it and/or modify  *
 
14
 *   it under the terms of the GNU General Public License as published by  *
 
15
 *   the Free Software Foundation; either version 2 of the License, or     *
 
16
 *   (at your option) any later version.                                   *
 
17
 *                                                                         *
 
18
 ***************************************************************************/
 
19
/* $Id:$ */
 
20
#include <QSettings>
 
21
#include <QFileDialog>
 
22
#include <QMessageBox>
 
23
#include <QInputDialog>
 
24
#include <QTextCodec>
 
25
#include "qgslogger.h"
 
26
#include "qgsencodingfiledialog.h"
 
27
#include "qgsopenvectorlayerdialog.h"
 
28
#include <ogr_api.h>
 
29
#include "qgsproviderregistry.h"
 
30
#include "qgsnewogrconnection.h"
 
31
#include "qgsogrhelperfunctions.h"
 
32
#include "qgscontexthelp.h"
 
33
 
 
34
QgsOpenVectorLayerDialog::QgsOpenVectorLayerDialog( QWidget* parent, Qt::WFlags fl )
 
35
    : QDialog( parent, fl )
 
36
{
 
37
  setupUi( this );
 
38
 
 
39
  cmbDatabaseTypes->blockSignals( true );
 
40
  cmbConnections->blockSignals( true );
 
41
  radioSrcFile->setChecked( true );
 
42
  mDataSourceType = "file";
 
43
  //set encoding
 
44
  // cmbEncodings->setItemText( cmbEncodings->currentIndex(), QString( QTextCodec::codecForLocale()->name() ) );
 
45
  QSettings settings;
 
46
  QString enc = settings.value( "/UI/encoding", QString( "System" ) ).toString();
 
47
 
 
48
  // The specified decoding is added if not existing alread, and then set current.
 
49
  // This should select it.
 
50
  int encindex = cmbEncodings->findText( enc );
 
51
  if ( encindex < 0 )
 
52
  {
 
53
    cmbEncodings->insertItem( 0, enc );
 
54
    encindex = 0;
 
55
  }
 
56
  cmbEncodings->setCurrentIndex( encindex );
 
57
 
 
58
  //add database drivers
 
59
  mVectorFileFilter = QgsProviderRegistry::instance()->fileVectorFilters();
 
60
  QgsDebugMsg( "Database drivers :" + QgsProviderRegistry::instance()->databaseDrivers() );
 
61
  QStringList dbDrivers = QgsProviderRegistry::instance()->databaseDrivers().split( ";" );
 
62
 
 
63
  for ( int i = 0; i < dbDrivers.count(); i++ )
 
64
  {
 
65
    QString dbDriver = dbDrivers.at( i );
 
66
    if (( !dbDriver.isEmpty() ) && ( !dbDriver.isNull() ) )
 
67
      cmbDatabaseTypes->addItem( dbDriver.split( "," ).at( 0 ) );
 
68
  }
 
69
 
 
70
  //add directory drivers
 
71
  QStringList dirDrivers = QgsProviderRegistry::instance()->directoryDrivers().split( ";" );
 
72
  for ( int i = 0; i < dirDrivers.count(); i++ )
 
73
  {
 
74
    QString dirDriver = dirDrivers.at( i );
 
75
    if (( !dirDriver.isEmpty() ) && ( !dirDriver.isNull() ) )
 
76
      cmbDirectoryTypes->addItem( dirDriver.split( "," ).at( 0 ) );
 
77
  }
 
78
 
 
79
  //add protocol drivers
 
80
  QStringList proDrivers = QgsProviderRegistry::instance()->protocolDrivers().split( ";" );
 
81
  for ( int i = 0; i < proDrivers.count(); i++ )
 
82
  {
 
83
    QString proDriver = proDrivers.at( i );
 
84
    if (( !proDriver.isEmpty() ) && ( !proDriver.isNull() ) )
 
85
      cmbProtocolTypes->addItem( proDriver.split( "," ).at( 0 ) );
 
86
  }
 
87
  cmbDatabaseTypes->blockSignals( false );
 
88
  cmbConnections->blockSignals( false );
 
89
}
 
90
 
 
91
QgsOpenVectorLayerDialog::~QgsOpenVectorLayerDialog()
 
92
{
 
93
}
 
94
 
 
95
QStringList QgsOpenVectorLayerDialog::openFile()
 
96
{
 
97
 
 
98
  QStringList selectedFiles;
 
99
  QgsDebugMsg( "Vector file filters: " + mVectorFileFilter );
 
100
  QString enc = encoding();
 
101
  QString title = tr( "Open an OGR Supported Vector Layer" );
 
102
  QgisGui::openFilesRememberingFilter( "lastVectorFileFilter", mVectorFileFilter, selectedFiles, enc,
 
103
                                       title );
 
104
 
 
105
  return selectedFiles;
 
106
}
 
107
 
 
108
QString QgsOpenVectorLayerDialog::openDirectory()
 
109
{
 
110
  QSettings settings;
 
111
 
 
112
  bool haveLastUsedDir = settings.contains( "/UI/LastUsedDirectory" );
 
113
  QString lastUsedDir = settings.value( "/UI/LastUsedDirectory", QVariant() ).toString();
 
114
  if ( !haveLastUsedDir )
 
115
    lastUsedDir = "";
 
116
 
 
117
  QString path = QFileDialog::getExistingDirectory( this,
 
118
                 tr( "Open Directory" ), lastUsedDir,
 
119
                 QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks );
 
120
 
 
121
  settings.setValue( "/UI/LastUsedDirectory", path );
 
122
  //process path if it is grass
 
123
  if ( cmbDirectoryTypes->currentText() == "Grass Vector" )
 
124
  {
 
125
#ifdef WIN32
 
126
    //replace backslashes with forward slashes
 
127
    path.replace( "\\", "/" );
 
128
#endif
 
129
    path = path + "/head";
 
130
  }
 
131
  return path;
 
132
}
 
133
 
 
134
QStringList QgsOpenVectorLayerDialog::dataSources()
 
135
{
 
136
  return mDataSources;
 
137
}
 
138
 
 
139
QString QgsOpenVectorLayerDialog::encoding()
 
140
{
 
141
  return cmbEncodings->currentText();
 
142
}
 
143
 
 
144
QString QgsOpenVectorLayerDialog::dataSourceType()
 
145
{
 
146
  return mDataSourceType;
 
147
}
 
148
 
 
149
void QgsOpenVectorLayerDialog::addNewConnection()
 
150
{
 
151
  QgsNewOgrConnection *nc = new QgsNewOgrConnection( this );
 
152
  nc->exec();
 
153
  delete nc;
 
154
 
 
155
  populateConnectionList();
 
156
}
 
157
 
 
158
void QgsOpenVectorLayerDialog::editConnection()
 
159
{
 
160
  QgsNewOgrConnection *nc = new QgsNewOgrConnection( this, cmbDatabaseTypes->currentText(), cmbConnections->currentText() );
 
161
  nc->exec();
 
162
  delete nc;
 
163
 
 
164
  populateConnectionList();
 
165
}
 
166
 
 
167
void QgsOpenVectorLayerDialog::deleteConnection()
 
168
{
 
169
  QSettings settings;
 
170
  QString key = "/" + cmbDatabaseTypes->currentText() + "/connections/" + cmbConnections->currentText();
 
171
  QString msg = tr( "Are you sure you want to remove the %1 connection and all associated settings?" )
 
172
                .arg( cmbConnections->currentText() );
 
173
  QMessageBox::StandardButton result = QMessageBox::information( this, tr( "Confirm Delete" ), msg, QMessageBox::Ok | QMessageBox::Cancel );
 
174
  if ( result == QMessageBox::Ok )
 
175
  {
 
176
    settings.remove( key + "/host" );
 
177
    settings.remove( key + "/database" );
 
178
    settings.remove( key + "/username" );
 
179
    settings.remove( key + "/password" );
 
180
    settings.remove( key + "/port" );
 
181
    settings.remove( key + "/save" );
 
182
    settings.remove( key );
 
183
    cmbConnections->removeItem( cmbConnections->currentIndex() );  // populateConnectionList();
 
184
    setConnectionListPosition();
 
185
  }
 
186
}
 
187
 
 
188
void QgsOpenVectorLayerDialog::populateConnectionList()
 
189
{
 
190
  QSettings settings;
 
191
  settings.beginGroup( "/" + cmbDatabaseTypes->currentText() + "/connections" );
 
192
  QStringList keys = settings.childGroups();
 
193
  QStringList::Iterator it = keys.begin();
 
194
  cmbConnections->clear();
 
195
  while ( it != keys.end() )
 
196
  {
 
197
    cmbConnections->addItem( *it );
 
198
    ++it;
 
199
  }
 
200
  settings.endGroup();
 
201
  setConnectionListPosition();
 
202
}
 
203
 
 
204
void QgsOpenVectorLayerDialog::setConnectionListPosition()
 
205
{
 
206
  QSettings settings;
 
207
  // If possible, set the item currently displayed database
 
208
 
 
209
  QString toSelect = settings.value( "/" + cmbDatabaseTypes->currentText() + "/connections/selected" ).toString();
 
210
  // Does toSelect exist in cmbConnections?
 
211
  bool set = false;
 
212
  for ( int i = 0; i < cmbConnections->count(); ++i )
 
213
    if ( cmbConnections->itemText( i ) == toSelect )
 
214
    {
 
215
      cmbConnections->setCurrentIndex( i );
 
216
      set = true;
 
217
      break;
 
218
    }
 
219
  // If we couldn't find the stored item, but there are some,
 
220
  // default to the last item (this makes some sense when deleting
 
221
  // items as it allows the user to repeatidly click on delete to
 
222
  // remove a whole lot of items).
 
223
  if ( !set && cmbConnections->count() > 0 )
 
224
  {
 
225
    // If toSelect is null, then the selected connection wasn't found
 
226
    // by QSettings, which probably means that this is the first time
 
227
    // the user has used qgis with database connections, so default to
 
228
    // the first in the list of connetions. Otherwise default to the last.
 
229
    if ( toSelect.isNull() )
 
230
      cmbConnections->setCurrentIndex( 0 );
 
231
    else
 
232
      cmbConnections->setCurrentIndex( cmbConnections->count() - 1 );
 
233
  }
 
234
}
 
235
 
 
236
void QgsOpenVectorLayerDialog::setConnectionTypeListPosition()
 
237
{
 
238
  QSettings settings;
 
239
 
 
240
  QString toSelect = settings.value( "/ogr/connections/selectedtype" ).toString();
 
241
  bool set = false;
 
242
  for ( int i = 0; i < cmbDatabaseTypes->count(); ++i )
 
243
    if ( cmbDatabaseTypes->itemText( i ) == toSelect )
 
244
    {
 
245
      cmbDatabaseTypes->setCurrentIndex( i );
 
246
      set = true;
 
247
      break;
 
248
    }
 
249
}
 
250
 
 
251
void QgsOpenVectorLayerDialog::setSelectedConnectionType()
 
252
{
 
253
  QSettings settings;
 
254
  QString baseKey = "/ogr/connections/";
 
255
  settings.setValue( baseKey + "selectedtype", cmbDatabaseTypes->currentText() );
 
256
  QgsDebugMsg( "Setting selected type to" + cmbDatabaseTypes->currentText() );
 
257
}
 
258
 
 
259
void QgsOpenVectorLayerDialog::setSelectedConnection()
 
260
{
 
261
  QSettings settings;
 
262
  settings.setValue( "/" + cmbDatabaseTypes->currentText() + "/connections/selected", cmbConnections->currentText() );
 
263
  QgsDebugMsg( "Setting selected connection to " + cmbConnections->currentText() );
 
264
}
 
265
 
 
266
 
 
267
void QgsOpenVectorLayerDialog::on_buttonSelectSrc_clicked()
 
268
{
 
269
  if ( radioSrcFile->isChecked() )
 
270
  {
 
271
    inputSrcDataset->setText( openFile().join( ";" ) );
 
272
  }
 
273
  else if ( radioSrcDirectory->isChecked() )
 
274
  {
 
275
    inputSrcDataset->setText( openDirectory() );
 
276
  }
 
277
  else if ( !radioSrcDatabase->isChecked() )
 
278
  {
 
279
    Q_ASSERT( !"SHOULD NEVER GET HERE" );
 
280
  }
 
281
}
 
282
 
 
283
 
 
284
 
 
285
//********************auto connected slots *****************/
 
286
void QgsOpenVectorLayerDialog::accept()
 
287
{
 
288
  QSettings settings;
 
289
  QgsDebugMsg( "dialog button accepted" );
 
290
 
 
291
  mDataSources.clear();
 
292
 
 
293
  if ( radioSrcDatabase->isChecked() )
 
294
  {
 
295
    if ( !settings.contains( "/" + cmbDatabaseTypes->currentText()
 
296
                             + "/connections/" + cmbConnections->currentText()
 
297
                             + "/host" ) )
 
298
    {
 
299
      QMessageBox::information( this,
 
300
                                tr( "Add vector layer" ),
 
301
                                tr( "No database selected." ) );
 
302
      return;
 
303
    }
 
304
 
 
305
    QString baseKey = "/" + cmbDatabaseTypes->currentText() + "/connections/";
 
306
    baseKey += cmbConnections->currentText();
 
307
    QString host = settings.value( baseKey + "/host" ).toString();
 
308
    QString database = settings.value( baseKey + "/database" ).toString();
 
309
    QString port = settings.value( baseKey + "/port" ).toString();
 
310
    QString user = settings.value( baseKey + "/username" ).toString();
 
311
    QString pass = settings.value( baseKey + "/password" ).toString();
 
312
 
 
313
    bool makeConnection = false;
 
314
    if ( pass.isEmpty() )
 
315
    {
 
316
      pass = QInputDialog::getText( this,
 
317
                                    tr( "Password for " ) + user,
 
318
                                    tr( "Please enter your password:" ),
 
319
                                    QLineEdit::Password, QString::null,
 
320
                                    &makeConnection );
 
321
    }
 
322
 
 
323
    if ( makeConnection || !pass.isEmpty() )
 
324
    {
 
325
      mDataSources << createDatabaseURI(
 
326
        cmbDatabaseTypes->currentText(),
 
327
        host,
 
328
        database,
 
329
        port,
 
330
        user,
 
331
        pass
 
332
      );
 
333
    }
 
334
  }
 
335
  else if ( radioSrcProtocol->isChecked() )
 
336
  {
 
337
    if ( protocolURI->text().isEmpty() )
 
338
    {
 
339
      QMessageBox::information( this,
 
340
                                tr( "Add vector layer" ),
 
341
                                tr( "No protocol URI entered." ) );
 
342
      return;
 
343
    }
 
344
 
 
345
    mDataSources << createProtocolURI( cmbProtocolTypes->currentText(), protocolURI->text() );
 
346
  }
 
347
  else if ( radioSrcFile->isChecked() )
 
348
  {
 
349
    if ( inputSrcDataset->text().isEmpty() )
 
350
    {
 
351
      QMessageBox::information( this,
 
352
                                tr( "Add vector layer" ),
 
353
                                tr( "No layers selected." ) );
 
354
      return;
 
355
    }
 
356
 
 
357
    mDataSources << inputSrcDataset->text().split( ";" );
 
358
  }
 
359
  else if ( radioSrcDirectory->isChecked() )
 
360
  {
 
361
    if ( inputSrcDataset->text().isEmpty() )
 
362
    {
 
363
      QMessageBox::information( this,
 
364
                                tr( "Add vector layer" ),
 
365
                                tr( "No directory selected." ) );
 
366
      return;
 
367
    }
 
368
 
 
369
    mDataSources << inputSrcDataset->text();
 
370
  }
 
371
 
 
372
  // Save the used encoding
 
373
  settings.setValue( "/UI/encoding", encoding() );
 
374
 
 
375
  QDialog::accept();
 
376
}
 
377
 
 
378
void QgsOpenVectorLayerDialog::on_radioSrcFile_toggled( bool checked )
 
379
{
 
380
  if ( checked )
 
381
  {
 
382
    labelDirectoryType->hide();
 
383
    cmbDirectoryTypes->hide();
 
384
    fileGroupBox->show();
 
385
    dbGroupBox->hide();
 
386
    protocolGroupBox->hide();
 
387
    layout()->setSizeConstraint( QLayout::SetFixedSize );
 
388
    mDataSourceType = "file";
 
389
  }
 
390
}
 
391
 
 
392
void QgsOpenVectorLayerDialog::on_radioSrcDirectory_toggled( bool checked )
 
393
{
 
394
  if ( checked )
 
395
  {
 
396
    labelDirectoryType->show();
 
397
    cmbDirectoryTypes->show();
 
398
    fileGroupBox->show();
 
399
    dbGroupBox->hide();
 
400
    protocolGroupBox->hide();
 
401
    layout()->setSizeConstraint( QLayout::SetFixedSize );
 
402
    mDataSourceType = "directory";
 
403
  }
 
404
}
 
405
 
 
406
void QgsOpenVectorLayerDialog::on_radioSrcDatabase_toggled( bool checked )
 
407
{
 
408
  if ( checked )
 
409
  {
 
410
    layout()->blockSignals( true );
 
411
    fileGroupBox->hide();
 
412
    protocolGroupBox->hide();
 
413
    dbGroupBox->show();
 
414
    layout()->blockSignals( false );
 
415
    layout()->setSizeConstraint( QLayout::SetFixedSize );
 
416
    setConnectionTypeListPosition();
 
417
    populateConnectionList();
 
418
    setConnectionListPosition();
 
419
    mDataSourceType = "database";
 
420
  }
 
421
}
 
422
 
 
423
void QgsOpenVectorLayerDialog::on_radioSrcProtocol_toggled( bool checked )
 
424
{
 
425
  if ( checked )
 
426
  {
 
427
    fileGroupBox->hide();
 
428
    dbGroupBox->hide();
 
429
    protocolGroupBox->show();
 
430
    layout()->setSizeConstraint( QLayout::SetFixedSize );
 
431
    mDataSourceType = "protocol";
 
432
  }
 
433
}
 
434
 
 
435
// Slot for adding a new connection
 
436
void QgsOpenVectorLayerDialog::on_btnNew_clicked()
 
437
{
 
438
  addNewConnection();
 
439
}
 
440
// Slot for deleting an existing connection
 
441
void QgsOpenVectorLayerDialog::on_btnDelete_clicked()
 
442
{
 
443
  deleteConnection();
 
444
}
 
445
 
 
446
 
 
447
// Slot for editing a connection
 
448
void QgsOpenVectorLayerDialog::on_btnEdit_clicked()
 
449
{
 
450
  editConnection();
 
451
}
 
452
 
 
453
void QgsOpenVectorLayerDialog::on_cmbDatabaseTypes_currentIndexChanged( const QString & text )
 
454
{
 
455
  populateConnectionList();
 
456
  setSelectedConnectionType();
 
457
}
 
458
 
 
459
void QgsOpenVectorLayerDialog::on_cmbConnections_currentIndexChanged( const QString & text )
 
460
{
 
461
  setSelectedConnection();
 
462
}
 
463
//********************end auto connected slots *****************/
 
464
 
 
465