~ubuntu-branches/ubuntu/hardy/qgis/hardy

« back to all changes in this revision

Viewing changes to src/gui/qgsoptions.cpp

  • Committer: Bazaar Package Importer
  • Author(s): William Grant
  • Date: 2007-05-06 13:42:32 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070506134232-pyli6t388w5asd8x
Tags: 0.8.0-3ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
  - debian/rules, debian/qgis.install, debian/qgis.dirs debian/qgis.desktop:
    Add and install .desktop.
* debian/qgis.desktop: Remove Applications category; it's not real.
* Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                          qgsoptions.cpp
 
3
                    Set user options and preferences
 
4
                             -------------------
 
5
    begin                : May 28, 2004
 
6
    copyright            : (C) 2004 by Gary E.Sherman
 
7
    email                : sherman at mrcc.com
 
8
 ***************************************************************************/
 
9
 
 
10
/***************************************************************************
 
11
 *                                                                         *
 
12
 *   This program is free software; you can redistribute it and/or modify  *
 
13
 *   it under the terms of the GNU General Public License as published by  *
 
14
 *   the Free Software Foundation; either version 2 of the License, or     *
 
15
 *   (at your option) any later version.                                   *
 
16
 *                                                                         *
 
17
 ***************************************************************************/
 
18
/* $Id: qgsoptions.cpp 6251 2006-12-13 23:23:50Z telwertowski $ */
 
19
#include "qgsapplication.h"
 
20
#include "qgsoptions.h"
 
21
#include "qgis.h"
 
22
#include "qgisapp.h"
 
23
#include "qgslayerprojectionselector.h"
 
24
#include "qgsspatialrefsys.h"
 
25
 
 
26
#include <QFileDialog>
 
27
#include <QSettings>
 
28
#include <QColorDialog>
 
29
 
 
30
#include <cassert>
 
31
#include <iostream>
 
32
#include <sqlite3.h>
 
33
 
 
34
 
 
35
/**
 
36
 * \class QgsOptions - Set user options and preferences
 
37
 * Constructor
 
38
 */
 
39
QgsOptions::QgsOptions(QWidget *parent, Qt::WFlags fl) :
 
40
  QDialog(parent, fl)
 
41
{
 
42
  setupUi(this);
 
43
  connect(cmbTheme, SIGNAL(activated(const QString&)), this, SLOT(themeChanged(const QString&)));
 
44
  connect(cmbTheme, SIGNAL(highlighted(const QString&)), this, SLOT(themeChanged(const QString&)));
 
45
  connect(cmbTheme, SIGNAL(textChanged(const QString&)), this, SLOT(themeChanged(const QString&)));
 
46
  connect(buttonOk, SIGNAL(clicked()), this, SLOT(saveOptions()));
 
47
  connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
 
48
 
 
49
  qparent = parent;
 
50
  // read the current browser and set it
 
51
  QSettings settings;
 
52
  QString browser = settings.readEntry("/qgis/browser");
 
53
  cmbBrowser->setCurrentText(browser);
 
54
#ifdef QGISDEBUG
 
55
  std::cout << "Standard Identify radius setting: " << QGis::DEFAULT_IDENTIFY_RADIUS << std::endl;
 
56
#endif
 
57
  double identifyValue = settings.value("/Map/identifyRadius",QGis::DEFAULT_IDENTIFY_RADIUS).toDouble();
 
58
#ifdef QGISDEBUG
 
59
  std::cout << "Standard Identify radius setting read from settings file: " << identifyValue << std::endl;
 
60
#endif
 
61
  spinBoxIdentifyValue->setValue(identifyValue);
 
62
 
 
63
  // set the current theme
 
64
  cmbTheme->setCurrentText(settings.readEntry("/Themes"));
 
65
  // set the display update threshold
 
66
  spinBoxUpdateThreshold->setValue(settings.readNumEntry("/Map/updateThreshold"));
 
67
  //set the default projection behaviour radio buttongs
 
68
  if (settings.readEntry("/Projections/defaultBehaviour")=="prompt")
 
69
  {
 
70
    radPromptForProjection->setChecked(true);
 
71
  }
 
72
  else if (settings.readEntry("/Projections/defaultBehaviour")=="useProject")
 
73
  {
 
74
    radUseProjectProjection->setChecked(true);
 
75
  }
 
76
  else //useGlobal
 
77
  {
 
78
    radUseGlobalProjection->setChecked(true);
 
79
  }
 
80
  mGlobalSRSID = settings.readNumEntry("/Projections/defaultProjectionSRSID",GEOSRS_ID);
 
81
  //! @todo changes this control name in gui to txtGlobalProjString
 
82
  QString myProjString = QgsSpatialRefSys::getProj4FromSrsId(mGlobalSRSID);
 
83
  txtGlobalWKT->setText(myProjString);
 
84
 
 
85
  // populate combo box with ellipsoids
 
86
  getEllipsoidList();
 
87
  QString myEllipsoidId = settings.readEntry("/qgis/measure/ellipsoid", "WGS84");
 
88
  cmbEllipsoid->setCurrentText(getEllipsoidName(myEllipsoidId));
 
89
  // add the themes to the combo box on the option dialog
 
90
  QDir myThemeDir( QgsApplication::pkgDataPath()+"/themes/" );
 
91
  myThemeDir.setFilter(QDir::Dirs);
 
92
  QStringList myDirList = myThemeDir.entryList("*");
 
93
  cmbTheme->clear();
 
94
  for(int i=0; i < myDirList.count(); i++)
 
95
  {
 
96
    if(myDirList[i] != "." && myDirList[i] != "..")
 
97
    {
 
98
      cmbTheme->insertItem(myDirList[i]);
 
99
    }
 
100
  }
 
101
 
 
102
  // set the theme combo
 
103
  cmbTheme->setCurrentText(settings.readEntry("/Themes","default"));
 
104
 
 
105
  //set the state of the checkboxes
 
106
  chkAntiAliasing->setChecked(settings.value("/qgis/enable_anti_aliasing",false).toBool());
 
107
 
 
108
  // Slightly awkard here at the settings value is true to use QImage,
 
109
  // but the checkbox is true to use QPixmap
 
110
  chkUseQPixmap->setChecked(!(settings.value("/qgis/use_qimage_to_render", true).toBool()));
 
111
  chkAddedVisibility->setChecked(settings.value("/qgis/new_layers_visible",true).toBool());
 
112
  cbxHideSplash->setChecked(settings.value("/qgis/hideSplash",false).toBool());
 
113
 
 
114
  //set the colour for selections
 
115
  int myRed = settings.value("/qgis/default_selection_color_red",255).toInt();
 
116
  int myGreen = settings.value("/qgis/default_selection_color_green",255).toInt();
 
117
  int myBlue = settings.value("/qgis/default_selection_color_blue",0).toInt();
 
118
  pbnSelectionColour->setColor( QColor(myRed,myGreen,myBlue) );
 
119
 
 
120
  //set the default color for canvas background
 
121
  myRed = settings.value("/qgis/default_canvas_color_red",255).toInt();
 
122
  myGreen = settings.value("/qgis/default_canvas_color_green",255).toInt();
 
123
  myBlue = settings.value("/qgis/default_canvas_color_blue",255).toInt();
 
124
  pbnCanvasColor->setColor( QColor(myRed,myGreen,myBlue) );
 
125
 
 
126
  capitaliseCheckBox->setChecked(settings.value("qgis/capitaliseLayerName", QVariant(false)).toBool());
 
127
  
 
128
  cmbWheelAction->setCurrentIndex(settings.value("/qgis/wheel_action", 0).toInt());
 
129
  spinZoomFactor->setValue(settings.value("/qgis/zoom_factor", 2).toDouble());
 
130
 
 
131
  cbxSplitterRedraw->setChecked(settings.value("/qgis/splitterRedraw", QVariant(true)).toBool());
 
132
}
 
133
 
 
134
//! Destructor
 
135
QgsOptions::~QgsOptions(){}
 
136
 
 
137
void QgsOptions::on_pbnSelectionColour_clicked()
 
138
{
 
139
  QColor color = QColorDialog::getColor(pbnSelectionColour->color(), this);
 
140
  if (color.isValid())
 
141
  {
 
142
    pbnSelectionColour->setColor(color);
 
143
  }
 
144
}
 
145
 
 
146
void QgsOptions::on_pbnCanvasColor_clicked()
 
147
{
 
148
  QColor color = QColorDialog::getColor(pbnCanvasColor->color(), this);
 
149
  if (color.isValid())
 
150
  {
 
151
    pbnCanvasColor->setColor(color);
 
152
  }
 
153
}
 
154
void QgsOptions::themeChanged(const QString &newThemeName)
 
155
{
 
156
  // Slot to change the theme as user scrolls through the choices
 
157
  QString newt = newThemeName;
 
158
  ((QgisApp*)qparent)->setTheme(newt);
 
159
}
 
160
QString QgsOptions::theme()
 
161
{
 
162
  // returns the current theme (as selected in the cmbTheme combo box)
 
163
  return cmbTheme->currentText();
 
164
}
 
165
 
 
166
void QgsOptions::saveOptions()
 
167
{
 
168
  QSettings settings;
 
169
  settings.writeEntry("/qgis/browser", cmbBrowser->currentText());
 
170
  settings.writeEntry("/Map/identifyRadius", spinBoxIdentifyValue->value());
 
171
  settings.writeEntry("/qgis/hideSplash",cbxHideSplash->isChecked());
 
172
  settings.writeEntry("/qgis/new_layers_visible",chkAddedVisibility->isChecked());
 
173
  settings.writeEntry("/qgis/enable_anti_aliasing",chkAntiAliasing->isChecked());
 
174
  settings.writeEntry("/qgis/use_qimage_to_render", !(chkUseQPixmap->isChecked()));
 
175
  settings.setValue("qgis/capitaliseLayerName", capitaliseCheckBox->isChecked());
 
176
 
 
177
  if(cmbTheme->currentText().length() == 0)
 
178
  {
 
179
    settings.writeEntry("/Themes", "default");
 
180
  }else{
 
181
    settings.writeEntry("/Themes",cmbTheme->currentText());
 
182
  }
 
183
  settings.writeEntry("/Map/updateThreshold", spinBoxUpdateThreshold->value());
 
184
  //check behaviour so default projection when new layer is added with no
 
185
  //projection defined...
 
186
  if (radPromptForProjection->isChecked())
 
187
  {
 
188
    //
 
189
    settings.writeEntry("/Projections/defaultBehaviour", "prompt");
 
190
  }
 
191
  else if(radUseProjectProjection->isChecked())
 
192
  {
 
193
    //
 
194
    settings.writeEntry("/Projections/defaultBehaviour", "useProject");
 
195
  }
 
196
  else //assumes radUseGlobalProjection is checked
 
197
  {
 
198
    //
 
199
    settings.writeEntry("/Projections/defaultBehaviour", "useGlobal");
 
200
  }
 
201
  settings.writeEntry("/Projections/defaultProjectionSRSID",(int)mGlobalSRSID);
 
202
 
 
203
  settings.writeEntry("/qgis/measure/ellipsoid", getEllipsoidAcronym(cmbEllipsoid->currentText()));
 
204
 
 
205
  //set the colour for selections
 
206
  QColor myColor = pbnSelectionColour->color();
 
207
  int myRed = settings.writeEntry("/qgis/default_selection_color_red",myColor.red());
 
208
  int myGreen = settings.writeEntry("/qgis/default_selection_color_green",myColor.green());
 
209
  int myBlue = settings.writeEntry("/qgis/default_selection_color_blue",myColor.blue());
 
210
 
 
211
  //set the default color for canvas background
 
212
  myColor = pbnCanvasColor->color();
 
213
  myRed = settings.writeEntry("/qgis/default_canvas_color_red",myColor.red());
 
214
  myGreen = settings.writeEntry("/qgis/default_canvas_color_green",myColor.green());
 
215
  myBlue = settings.writeEntry("/qgis/default_canvas_color_blue",myColor.blue());
 
216
 
 
217
  settings.writeEntry("/qgis/wheel_action", cmbWheelAction->currentIndex());
 
218
  settings.writeEntry("/qgis/zoom_factor", spinZoomFactor->value());
 
219
 
 
220
  settings.setValue("/qgis/splitterRedraw", cbxSplitterRedraw->isChecked());  
 
221
 
 
222
  //all done
 
223
  accept();
 
224
}
 
225
 
 
226
 
 
227
 
 
228
 
 
229
 
 
230
 
 
231
 
 
232
void QgsOptions::on_btnFindBrowser_clicked()
 
233
{
 
234
  QString filter;
 
235
#ifdef WIN32
 
236
  filter = "Applications (*.exe)";
 
237
#else
 
238
  filter = "All Files (*)";
 
239
#endif
 
240
  QString browser = QFileDialog::getOpenFileName(
 
241
          this,
 
242
          "Choose a browser",
 
243
          "./",
 
244
          filter );
 
245
  if(browser.length() > 0)
 
246
  {
 
247
    cmbBrowser->setCurrentText(browser);
 
248
  }
 
249
}
 
250
 
 
251
 
 
252
void QgsOptions::on_pbnSelectProjection_clicked()
 
253
{
 
254
  QSettings settings;
 
255
  QgsLayerProjectionSelector * mySelector = new QgsLayerProjectionSelector(this);
 
256
  mySelector->setSelectedSRSID(mGlobalSRSID);
 
257
  if(mySelector->exec())
 
258
  {
 
259
#ifdef QGISDEBUG
 
260
    std::cout << "------ Global Default Projection Selection Set ----------" << std::endl;
 
261
#endif
 
262
    mGlobalSRSID = mySelector->getCurrentSRSID();  
 
263
    //! @todo changes this control name in gui to txtGlobalProjString
 
264
    txtGlobalWKT->setText(mySelector->getCurrentProj4String());
 
265
#ifdef QGISDEBUG
 
266
    std::cout << "------ Global Default Projection now set to ----------\n" << mGlobalSRSID << std::endl;
 
267
#endif
 
268
  }
 
269
  else
 
270
  {
 
271
#ifdef QGISDEBUG
 
272
    std::cout << "------ Global Default Projection Selection change cancelled ----------" << std::endl;
 
273
#endif
 
274
    QApplication::restoreOverrideCursor();
 
275
  }
 
276
 
 
277
}
 
278
 
 
279
void QgsOptions::on_chkAntiAliasing_stateChanged()
 
280
{
 
281
  // We can't have the anti-aliasing turned on when QPixmap is being
 
282
  // used (we we can. but it then doesn't do anti-aliasing, and this
 
283
  // will confuse people).
 
284
  if (chkAntiAliasing->isChecked())
 
285
    chkUseQPixmap->setChecked(false);
 
286
 
 
287
}
 
288
 
 
289
void QgsOptions::on_chkUseQPixmap_stateChanged()
 
290
{
 
291
  // We can't have the anti-aliasing turned on when QPixmap is being
 
292
  // used (we we can. but it then doesn't do anti-aliasing, and this
 
293
  // will confuse people).
 
294
  if (chkUseQPixmap->isChecked())
 
295
    chkAntiAliasing->setChecked(false);
 
296
 
 
297
}
 
298
 
 
299
// Return state of the visibility flag for newly added layers. If
 
300
 
 
301
bool QgsOptions::newVisible()
 
302
{
 
303
  return chkAddedVisibility->isChecked();
 
304
}
 
305
 
 
306
void QgsOptions::getEllipsoidList()
 
307
{
 
308
  // (copied from qgscustomprojectiondialog.cpp)
 
309
 
 
310
  // 
 
311
  // Populate the ellipsoid combo
 
312
  // 
 
313
  sqlite3      *myDatabase;
 
314
  const char   *myTail;
 
315
  sqlite3_stmt *myPreparedStatement;
 
316
  int           myResult;
 
317
  //check the db is available
 
318
  myResult = sqlite3_open(QgsApplication::qgisUserDbFilePath(), &myDatabase);
 
319
  if(myResult) 
 
320
  {
 
321
    std::cout <<  "Can't open database: " <<  sqlite3_errmsg(myDatabase) << std::endl; 
 
322
    // XXX This will likely never happen since on open, sqlite creates the 
 
323
    //     database if it does not exist.
 
324
    assert(myResult == 0);
 
325
  }
 
326
 
 
327
  // Set up the query to retreive the projection information needed to populate the ELLIPSOID list
 
328
  QString mySql = "select * from tbl_ellipsoid order by name";
 
329
  myResult = sqlite3_prepare(myDatabase, (const char *)mySql, mySql.length(), &myPreparedStatement, &myTail);
 
330
  // XXX Need to free memory from the error msg if one is set
 
331
  if(myResult == SQLITE_OK)
 
332
  {
 
333
    while(sqlite3_step(myPreparedStatement) == SQLITE_ROW)
 
334
    {
 
335
      cmbEllipsoid->insertItem((char *)sqlite3_column_text(myPreparedStatement,1));
 
336
    }
 
337
  }
 
338
  // close the sqlite3 statement
 
339
  sqlite3_finalize(myPreparedStatement);
 
340
  sqlite3_close(myDatabase);
 
341
}
 
342
 
 
343
QString QgsOptions::getEllipsoidAcronym(QString theEllipsoidName)
 
344
{
 
345
  sqlite3      *myDatabase;
 
346
  const char   *myTail;
 
347
  sqlite3_stmt *myPreparedStatement;
 
348
  int           myResult;
 
349
  QString       myName;
 
350
  //check the db is available
 
351
  myResult = sqlite3_open(QgsApplication::qgisUserDbFilePath(), &myDatabase);
 
352
  if(myResult) 
 
353
  {
 
354
    std::cout <<  "Can't open database: " <<  sqlite3_errmsg(myDatabase) << std::endl; 
 
355
    // XXX This will likely never happen since on open, sqlite creates the 
 
356
    //     database if it does not exist.
 
357
    assert(myResult == 0);
 
358
  }
 
359
  // Set up the query to retreive the projection information needed to populate the ELLIPSOID list
 
360
  QString mySql = "select acronym from tbl_ellipsoid where name='" + theEllipsoidName + "'";
 
361
  myResult = sqlite3_prepare(myDatabase, (const char *)mySql, mySql.length(), &myPreparedStatement, &myTail);
 
362
  // XXX Need to free memory from the error msg if one is set
 
363
  if(myResult == SQLITE_OK)
 
364
  {
 
365
    sqlite3_step(myPreparedStatement) == SQLITE_ROW;
 
366
    myName = QString((char *)sqlite3_column_text(myPreparedStatement,0));
 
367
  }
 
368
  // close the sqlite3 statement
 
369
  sqlite3_finalize(myPreparedStatement);
 
370
  sqlite3_close(myDatabase);
 
371
  return myName;
 
372
 
 
373
}
 
374
 
 
375
QString QgsOptions::getEllipsoidName(QString theEllipsoidAcronym)
 
376
{
 
377
  sqlite3      *myDatabase;
 
378
  const char   *myTail;
 
379
  sqlite3_stmt *myPreparedStatement;
 
380
  int           myResult;
 
381
  QString       myName;
 
382
  //check the db is available
 
383
  myResult = sqlite3_open(QgsApplication::qgisUserDbFilePath(), &myDatabase);
 
384
  if(myResult) 
 
385
  {
 
386
    std::cout <<  "Can't open database: " <<  sqlite3_errmsg(myDatabase) << std::endl; 
 
387
    // XXX This will likely never happen since on open, sqlite creates the 
 
388
    //     database if it does not exist.
 
389
    assert(myResult == 0);
 
390
  }
 
391
  // Set up the query to retreive the projection information needed to populate the ELLIPSOID list
 
392
  QString mySql = "select name from tbl_ellipsoid where acronym='" + theEllipsoidAcronym + "'";
 
393
  myResult = sqlite3_prepare(myDatabase, (const char *)mySql, mySql.length(), &myPreparedStatement, &myTail);
 
394
  // XXX Need to free memory from the error msg if one is set
 
395
  if(myResult == SQLITE_OK)
 
396
  {
 
397
    sqlite3_step(myPreparedStatement) == SQLITE_ROW;
 
398
    myName = QString((char *)sqlite3_column_text(myPreparedStatement,0));
 
399
  }
 
400
  // close the sqlite3 statement
 
401
  sqlite3_finalize(myPreparedStatement);
 
402
  sqlite3_close(myDatabase);
 
403
  return myName;
 
404
 
 
405
}