1
/***************************************************************************
2
QgsBookmarks.cpp - Spatial Bookmarks
5
copyright : (C) 2005 Gary Sherman
6
email : sherman at mrcc dot com
7
***************************************************************************/
9
/***************************************************************************
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. *
16
***************************************************************************/
17
/* $Id: qgsbookmarks.cpp 5829 2006-09-16 06:45:44Z g_j_m $ */
18
#include "qgsbookmarks.h"
20
#include "qgsapplication.h"
21
#include "qgscontexthelp.h"
22
#include "qgsmapcanvas.h"
23
#include "qgslogger.h"
27
#include <QMessageBox>
35
QgsBookmarks::QgsBookmarks(QWidget *parent, Qt::WFlags fl)
36
: QDialog(parent, fl),
40
connect(btnClose, SIGNAL(clicked()), this, SLOT(reject()));
42
// user database is created at QGIS startup in QgisApp::createDB
43
// we just check whether there is our database [MD]
45
myFileInfo.setFile(QgsApplication::qgisSettingsDirPath());
46
if ( !myFileInfo.exists( ) )
49
std::cout << "The qgis.db does not exist" << std::endl;
53
// Note proper queens english on next line
56
// connect the slot up to catch when a new bookmark is added
57
connect(mParent, SIGNAL(bookmarkAdded()), this, SLOT(refreshBookmarks()));
61
QgsBookmarks::~QgsBookmarks()
65
void QgsBookmarks::refreshBookmarks()
67
lstBookmarks->clear();
70
// Initialise the bookmark tree from the database
71
void QgsBookmarks::initialise()
76
// prepare the sql statement
79
QString sql = "select * from tbl_bookmarks";
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
85
// get the first row of the result set
86
while(sqlite3_step(ppStmt) == SQLITE_ROW)
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)));
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));
100
lvi->setText(2, xMin + ", " + yMin + ", " + xMax + ", " + yMax);
102
lvi->setText(3, QString::fromUtf8((const char *)sqlite3_column_text(ppStmt, 0)));
107
// XXX query failed -- warn the user some how
108
std::cout << "Failed to get bookmarks: " << sqlite3_errmsg(db) << std::endl;
110
// close the statement
111
sqlite3_finalize(ppStmt);
112
// close the database
114
// return the srs wkt
120
void QgsBookmarks::on_btnDelete_clicked()
122
// get the current item
123
Q3ListViewItem *lvi = lstBookmarks->currentItem();
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))
131
// remove it from the listview
132
lstBookmarks->takeItem(lvi);
133
// delete it from the database
134
int rc = connectDb();
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);
143
// XXX Provide popup message on failure?
144
QMessageBox::warning(this, tr("Error deleting bookmark"),
145
tr("Failed to delete the ") +
147
tr(" bookmark from the database. The "
148
"database said:\n") + QString(errmsg),
149
QMessageBox::Ok, QMessageBox::NoButton);
150
sqlite3_free(errmsg);
152
// close the database
159
void QgsBookmarks::on_btnZoomTo_clicked()
164
void QgsBookmarks::on_lstBookmarks_doubleClicked(Q3ListViewItem *lvi)
169
void QgsBookmarks::zoomToBookmark()
171
// Need to fetch the extent for the selected bookmark and then redraw
173
// get the current item
174
Q3ListViewItem *lvi = lstBookmarks->currentItem();
179
// get the extent from the database
180
int rc = connectDb();
183
sqlite3_stmt *ppStmt;
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);
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(),
202
dynamic_cast<QgisApp*>(mParent)->getInterface()->getMapCanvas()->refresh();
208
// close the statement
209
sqlite3_finalize(ppStmt);
210
// close the database
216
int QgsBookmarks::connectDb()
220
rc = sqlite3_open(QgsApplication::qgisUserDbFilePath(), &db);
223
std::cout << "Can't open database: " << sqlite3_errmsg(db) << std::endl;
225
// XXX This will likely never happen since on open, sqlite creates the
226
// database if it does not exist.
232
void QgsBookmarks::on_btnHelp_clicked()
234
QgsContextHelp::run(context_id);