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

« back to all changes in this revision

Viewing changes to src/gui/qgsbookmarks.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
               QgsBookmarks.cpp  - Spatial Bookmarks
 
3
                             -------------------
 
4
    begin                : 2005-04-23
 
5
    copyright            : (C) 2005 Gary Sherman
 
6
    email                : sherman at mrcc dot com
 
7
 ***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
 *                                                                         *
 
11
 *   This program is free software; you can redistribute it and/or modify  *
 
12
 *   it under the terms of the GNU General Public License as published by  *
 
13
 *   the Free Software Foundation; either version 2 of the License, or     *
 
14
 *   (at your option) any later version.                                   *
 
15
 *                                                                         *
 
16
 ***************************************************************************/
 
17
 /* $Id: qgsbookmarks.cpp 5829 2006-09-16 06:45:44Z g_j_m $ */
 
18
#include "qgsbookmarks.h"
 
19
#include "qgisapp.h"
 
20
#include "qgsapplication.h"
 
21
#include "qgscontexthelp.h"
 
22
#include "qgsmapcanvas.h"
 
23
#include "qgslogger.h"
 
24
 
 
25
#include <QDir>
 
26
#include <QFileInfo>
 
27
#include <QMessageBox>
 
28
 
 
29
//standard includes
 
30
#include <iostream>
 
31
#include <cassert>
 
32
#include <sqlite3.h>
 
33
#include <fstream>
 
34
 
 
35
QgsBookmarks::QgsBookmarks(QWidget *parent, Qt::WFlags fl)
 
36
  : QDialog(parent, fl),
 
37
  mParent(parent)
 
38
{
 
39
  setupUi(this);
 
40
  connect(btnClose, SIGNAL(clicked()), this, SLOT(reject()));
 
41
 
 
42
  // user database is created at QGIS startup in QgisApp::createDB
 
43
  // we just check whether there is our database [MD]
 
44
  QFileInfo myFileInfo;
 
45
  myFileInfo.setFile(QgsApplication::qgisSettingsDirPath());
 
46
  if ( !myFileInfo.exists( ) )
 
47
  {
 
48
#ifdef QGISDEBUG 
 
49
    std::cout << "The qgis.db does not exist" << std::endl; 
 
50
#endif 
 
51
  }
 
52
  
 
53
  // Note proper queens english on next line
 
54
  initialise();
 
55
 
 
56
  // connect the slot up to catch when a new bookmark is added
 
57
  connect(mParent, SIGNAL(bookmarkAdded()), this, SLOT(refreshBookmarks()));
 
58
}
 
59
 
 
60
// Destructor
 
61
QgsBookmarks::~QgsBookmarks()
 
62
{
 
63
}
 
64
 
 
65
void QgsBookmarks::refreshBookmarks()
 
66
{
 
67
  lstBookmarks->clear();
 
68
  initialise();
 
69
}
 
70
// Initialise the bookmark tree from the database
 
71
void QgsBookmarks::initialise()
 
72
{
 
73
  int rc = connectDb();
 
74
  if(rc == SQLITE_OK)
 
75
  {
 
76
    // prepare the sql statement
 
77
    const char *pzTail;
 
78
    sqlite3_stmt *ppStmt;
 
79
    QString sql = "select * from tbl_bookmarks";
 
80
 
 
81
    rc = sqlite3_prepare(db, sql.utf8(), sql.length(), &ppStmt, &pzTail);
 
82
    // XXX Need to free memory from the error msg if one is set
 
83
    if(rc == SQLITE_OK)
 
84
    {
 
85
      // get the first row of the result set
 
86
      while(sqlite3_step(ppStmt) == SQLITE_ROW)
 
87
      {
 
88
        QString name = QString::fromUtf8((const char *)sqlite3_column_text(ppStmt, 1));
 
89
        //        sqlite3_bind_parameter_index(ppStmt, "name"));
 
90
        //QgsDebugMsg("Bookmark name: " + name.toLocal8Bit().data()); 
 
91
        Q3ListViewItem *lvi = new Q3ListViewItem(lstBookmarks, name);
 
92
        // set the project name
 
93
        lvi->setText(1, QString::fromUtf8((const char *)sqlite3_column_text(ppStmt, 2))); 
 
94
        // get the extents
 
95
        QString xMin = QString::fromUtf8((const char *)sqlite3_column_text(ppStmt, 3));
 
96
        QString yMin = QString::fromUtf8((const char *)sqlite3_column_text(ppStmt, 4));
 
97
        QString xMax = QString::fromUtf8((const char *)sqlite3_column_text(ppStmt, 5));
 
98
        QString yMax = QString::fromUtf8((const char *)sqlite3_column_text(ppStmt, 6));
 
99
        // set the extents
 
100
        lvi->setText(2, xMin + ", " + yMin + ", " + xMax + ", " + yMax); 
 
101
        // set the id
 
102
        lvi->setText(3, QString::fromUtf8((const char *)sqlite3_column_text(ppStmt, 0)));
 
103
      }
 
104
    }
 
105
    else
 
106
    {
 
107
      // XXX query failed -- warn the user some how
 
108
      std::cout << "Failed to get bookmarks: " << sqlite3_errmsg(db) << std::endl; 
 
109
    }
 
110
    // close the statement
 
111
    sqlite3_finalize(ppStmt);
 
112
    // close the database
 
113
    sqlite3_close(db);
 
114
    // return the srs wkt
 
115
 
 
116
  }
 
117
}
 
118
 
 
119
 
 
120
void QgsBookmarks::on_btnDelete_clicked()
 
121
{
 
122
  // get the current item
 
123
  Q3ListViewItem *lvi = lstBookmarks->currentItem();
 
124
  if(lvi)
 
125
  {
 
126
    // make sure the user really wants to delete this bookmark
 
127
    if(0 == QMessageBox::information(this,tr("Really Delete?"),
 
128
          tr("Are you sure you want to delete the ") + lvi->text(0) +
 
129
          tr(" bookmark?"), tr("&Yes"), tr("&No"), QString::null, 0, 1))  
 
130
    {
 
131
      // remove it from the listview
 
132
      lstBookmarks->takeItem(lvi);
 
133
      // delete it from the database
 
134
      int rc = connectDb();
 
135
      if(rc == SQLITE_OK)
 
136
      {
 
137
        char *errmsg;
 
138
        // build the sql statement
 
139
        QString sql = "delete from tbl_bookmarks where bookmark_id = " + lvi->text(3);
 
140
        rc = sqlite3_exec(db, sql.utf8(), NULL, NULL, &errmsg);
 
141
        if(rc != SQLITE_OK)
 
142
        {
 
143
          // XXX Provide popup message on failure?
 
144
          QMessageBox::warning(this, tr("Error deleting bookmark"),
 
145
                               tr("Failed to delete the ") + 
 
146
                               lvi->text(0) +
 
147
                               tr(" bookmark from the database. The "
 
148
                                  "database said:\n") + QString(errmsg),
 
149
                               QMessageBox::Ok, QMessageBox::NoButton);
 
150
          sqlite3_free(errmsg);
 
151
        }
 
152
        // close the database
 
153
        sqlite3_close(db);
 
154
      }
 
155
    }
 
156
  }
 
157
}
 
158
 
 
159
void QgsBookmarks::on_btnZoomTo_clicked()
 
160
{
 
161
  zoomToBookmark();
 
162
}
 
163
 
 
164
void QgsBookmarks::on_lstBookmarks_doubleClicked(Q3ListViewItem *lvi)
 
165
{
 
166
  zoomToBookmark();
 
167
}
 
168
 
 
169
void QgsBookmarks::zoomToBookmark()
 
170
{
 
171
        // Need to fetch the extent for the selected bookmark and then redraw
 
172
        // the map
 
173
  // get the current item
 
174
  Q3ListViewItem *lvi = lstBookmarks->currentItem();
 
175
  if(!lvi)
 
176
  {
 
177
      return;
 
178
  }
 
179
  // get the extent from the database
 
180
  int rc = connectDb();
 
181
  if(rc == SQLITE_OK)
 
182
  {
 
183
    sqlite3_stmt *ppStmt;
 
184
    const char *pzTail;
 
185
    // build the sql statement
 
186
    QString sql = "select xmin, ymin, xmax, ymax from tbl_bookmarks where bookmark_id = " + lvi->text(3);
 
187
    rc = sqlite3_prepare(db, sql.utf8(), sql.length(), &ppStmt, &pzTail);
 
188
    if(rc == SQLITE_OK)
 
189
    {
 
190
      if(sqlite3_step(ppStmt) == SQLITE_ROW){
 
191
        // get the extents from the resultset
 
192
        QString xmin  = QString::fromUtf8((const char *)sqlite3_column_text(ppStmt, 0));
 
193
        QString ymin  = QString::fromUtf8((const char *)sqlite3_column_text(ppStmt, 1));
 
194
        QString xmax  = QString::fromUtf8((const char *)sqlite3_column_text(ppStmt, 2));
 
195
        QString ymax  = QString::fromUtf8((const char *)sqlite3_column_text(ppStmt, 3));
 
196
        // set the extent to the bookmark
 
197
        dynamic_cast<QgisApp*>(mParent)->setExtent(QgsRect(xmin.toDouble(),
 
198
              ymin.toDouble(),
 
199
              xmax.toDouble(),
 
200
              ymax.toDouble()));
 
201
        // redraw the map
 
202
        dynamic_cast<QgisApp*>(mParent)->getInterface()->getMapCanvas()->refresh();
 
203
 
 
204
 
 
205
      }
 
206
    }
 
207
 
 
208
    // close the statement
 
209
    sqlite3_finalize(ppStmt);
 
210
    // close the database
 
211
    sqlite3_close(db);
 
212
  }
 
213
  
 
214
}
 
215
 
 
216
int QgsBookmarks::connectDb()
 
217
{
 
218
 
 
219
  int rc;
 
220
  rc = sqlite3_open(QgsApplication::qgisUserDbFilePath(), &db);
 
221
  if(rc)
 
222
  {
 
223
    std::cout <<  "Can't open database: " <<  sqlite3_errmsg(db) << std::endl;
 
224
 
 
225
    // XXX This will likely never happen since on open, sqlite creates the
 
226
    //     database if it does not exist.
 
227
    assert(rc == 0);
 
228
  }
 
229
  return rc;
 
230
}
 
231
 
 
232
void QgsBookmarks::on_btnHelp_clicked()
 
233
{
 
234
  QgsContextHelp::run(context_id);
 
235
}