src/app/qgsbookmarks.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                QgsBookmarks.cpp  - Spatial Bookmarks
00003                              -------------------
00004     begin                : 2005-04-23
00005     copyright            : (C) 2005 Gary Sherman
00006     email                : sherman at mrcc dot com
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 /* $Id: qgsbookmarks.cpp 9231 2008-08-31 16:56:09Z timlinux $ */
00018 #include "qgsbookmarks.h"
00019 #include "qgisapp.h"
00020 #include "qgsapplication.h"
00021 #include "qgscontexthelp.h"
00022 #include "qgsmapcanvas.h"
00023 #include "qgslogger.h"
00024 
00025 #include <QDir>
00026 #include <QFileInfo>
00027 #include <QMessageBox>
00028 #include <QSettings>
00029 #include <QPushButton>
00030 
00031 //standard includes
00032 #include <cassert>
00033 #include <sqlite3.h>
00034 #include <fstream>
00035 #include "qgslogger.h"
00036 
00037 QgsBookmarks::QgsBookmarks( QWidget *parent, Qt::WFlags fl )
00038     : QDialog( parent, fl ),
00039     mParent( parent )
00040 {
00041   setupUi( this );
00042   // user database is created at QGIS startup in QgisApp::createDB
00043   // we just check whether there is our database [MD]
00044   QFileInfo myFileInfo;
00045   myFileInfo.setFile( QgsApplication::qgisSettingsDirPath() );
00046   if ( !myFileInfo.exists( ) )
00047   {
00048     QgsDebugMsg( "The qgis.db does not exist" );
00049   }
00050 
00051   // Note proper queens english on next line
00052   initialise();
00053 
00054   //
00055   // Create the zoomto and delete buttons and add them to the
00056   // toolbar
00057   //
00058   QPushButton * btnDelete = new QPushButton( tr( "&Delete" ) );
00059   QPushButton * btnZoomTo = new QPushButton( tr( "&Zoom to" ) );
00060   btnZoomTo->setDefault( true );
00061   buttonBox->addButton( btnDelete, QDialogButtonBox::ActionRole );
00062   buttonBox->addButton( btnZoomTo, QDialogButtonBox::ActionRole );
00063   // connect the slot up to catch when a bookmark is deleted
00064   connect( btnDelete, SIGNAL( clicked() ), this, SLOT( on_btnDelete_clicked() ) );
00065   // connect the slot up to catch when a bookmark is zoomed to
00066   connect( btnZoomTo, SIGNAL( clicked() ), this, SLOT( on_btnZoomTo_clicked() ) );
00067   // connect the slot up to catch when a new bookmark is added
00068   connect( mParent, SIGNAL( bookmarkAdded() ), this, SLOT( refreshBookmarks() ) );
00069   //and for help requested
00070   connect( buttonBox, SIGNAL( helpRequested() ), this, SLOT( helpRequested() ) );
00071 }
00072 
00073 // Destructor
00074 QgsBookmarks::~QgsBookmarks()
00075 {
00076   saveWindowLocation();
00077 }
00078 
00079 void QgsBookmarks::refreshBookmarks()
00080 {
00081   lstBookmarks->clear();
00082   initialise();
00083 }
00084 // Initialise the bookmark tree from the database
00085 void QgsBookmarks::initialise()
00086 {
00087   int rc = connectDb();
00088   if ( rc == SQLITE_OK )
00089   {
00090     // prepare the sql statement
00091     const char *pzTail;
00092     sqlite3_stmt *ppStmt;
00093     QString sql = "select * from tbl_bookmarks";
00094 
00095     rc = sqlite3_prepare( db, sql.toUtf8(), sql.length(), &ppStmt, &pzTail );
00096     // XXX Need to free memory from the error msg if one is set
00097     if ( rc == SQLITE_OK )
00098     {
00099       // get the first row of the result set
00100       while ( sqlite3_step( ppStmt ) == SQLITE_ROW )
00101       {
00102         QTreeWidgetItem *item = new QTreeWidgetItem( lstBookmarks );
00103         QString name = QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 1 ) );
00104         //        sqlite3_bind_parameter_index(ppStmt, "name"));
00105         //QgsDebugMsg("Bookmark name: " + name.toLocal8Bit().data());
00106         item->setText( 0, name );
00107         // set the project name
00108         item->setText( 1, QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 2 ) ) );
00109         // get the extents
00110         QString xMin = QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 3 ) );
00111         QString yMin = QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 4 ) );
00112         QString xMax = QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 5 ) );
00113         QString yMax = QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 6 ) );
00114         // set the extents
00115         item->setText( 2, xMin + ", " + yMin + ", " + xMax + ", " + yMax );
00116         // set the id
00117         item->setText( 3, QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 0 ) ) );
00118       }
00119       for ( int col = 0; col < 4; col++ )
00120       {
00121         lstBookmarks->resizeColumnToContents( col );
00122       }
00123       lstBookmarks->sortByColumn( 0, Qt::AscendingOrder );
00124     }
00125     else
00126     {
00127       // XXX query failed -- warn the user some how
00128       QgsDebugMsg( QString( "Failed to get bookmarks: %1" ).arg( sqlite3_errmsg( db ) ) );
00129     }
00130     // close the statement
00131     sqlite3_finalize( ppStmt );
00132     // close the database
00133     sqlite3_close( db );
00134     // return the srs wkt
00135 
00136   }
00137 }
00138 
00139 void QgsBookmarks::restorePosition()
00140 {
00141   QSettings settings;
00142   restoreGeometry( settings.value( "/Windows/Bookmarks/geometry" ).toByteArray() );
00143 }
00144 
00145 void QgsBookmarks::saveWindowLocation()
00146 {
00147   QSettings settings;
00148   settings.setValue( "/Windows/Bookmarks/geometry", saveGeometry() );
00149 }
00150 
00151 void QgsBookmarks::on_btnDelete_clicked()
00152 {
00153   // get the current item
00154   QTreeWidgetItem *item = lstBookmarks->currentItem();
00155   if ( item )
00156   {
00157     // make sure the user really wants to delete this bookmark
00158     if ( QMessageBox::Ok == QMessageBox::information( this, tr( "Really Delete?" ),
00159          tr( "Are you sure you want to delete the " ) + item->text( 0 ) + tr( " bookmark?" ),
00160          QMessageBox::Ok | QMessageBox::Cancel ) )
00161     {
00162       // remove it from the listview
00163       item = lstBookmarks->takeTopLevelItem( lstBookmarks->indexOfTopLevelItem( item ) );
00164       // delete it from the database
00165       int rc = connectDb();
00166       if ( rc == SQLITE_OK )
00167       {
00168         char *errmsg;
00169         // build the sql statement
00170         QString sql = "delete from tbl_bookmarks where bookmark_id = " + item->text( 3 );
00171         rc = sqlite3_exec( db, sql.toUtf8(), NULL, NULL, &errmsg );
00172         if ( rc != SQLITE_OK )
00173         {
00174           // XXX Provide popup message on failure?
00175           QMessageBox::warning( this, tr( "Error deleting bookmark" ),
00176                                 tr( "Failed to delete the " ) +
00177                                 item->text( 0 ) +
00178                                 tr( " bookmark from the database. The "
00179                                     "database said:\n" ) + QString( errmsg ) );
00180           sqlite3_free( errmsg );
00181         }
00182         // close the database
00183         sqlite3_close( db );
00184       }
00185       delete item;
00186     }
00187   }
00188 }
00189 
00190 void QgsBookmarks::on_btnZoomTo_clicked()
00191 {
00192   zoomToBookmark();
00193 }
00194 
00195 void QgsBookmarks::on_lstBookmarks_doubleClicked( QTreeWidgetItem *lvi )
00196 {
00197   zoomToBookmark();
00198 }
00199 
00200 void QgsBookmarks::zoomToBookmark()
00201 {
00202   // Need to fetch the extent for the selected bookmark and then redraw
00203   // the map
00204   // get the current item
00205   QTreeWidgetItem *item = lstBookmarks->currentItem();
00206   if ( !item )
00207   {
00208     return;
00209   }
00210   // get the extent from the database
00211   int rc = connectDb();
00212   if ( rc == SQLITE_OK )
00213   {
00214     sqlite3_stmt *ppStmt;
00215     const char *pzTail;
00216     // build the sql statement
00217     QString sql = "select xmin, ymin, xmax, ymax from tbl_bookmarks where bookmark_id = " + item->text( 3 );
00218     rc = sqlite3_prepare( db, sql.toUtf8(), sql.length(), &ppStmt, &pzTail );
00219     if ( rc == SQLITE_OK )
00220     {
00221       if ( sqlite3_step( ppStmt ) == SQLITE_ROW )
00222       {
00223         // get the extents from the resultset
00224         QString xmin  = QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 0 ) );
00225         QString ymin  = QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 1 ) );
00226         QString xmax  = QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 2 ) );
00227         QString ymax  = QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 3 ) );
00228         // set the extent to the bookmark
00229         QgisApp::instance()->setExtent( QgsRect( xmin.toDouble(),
00230                                         ymin.toDouble(),
00231                                         xmax.toDouble(),
00232                                         ymax.toDouble() ) );
00233         // redraw the map
00234         QgisApp::instance()->mapCanvas()->refresh();
00235       }
00236     }
00237 
00238     // close the statement
00239     sqlite3_finalize( ppStmt );
00240     // close the database
00241     sqlite3_close( db );
00242   }
00243 
00244 }
00245 
00246 int QgsBookmarks::connectDb()
00247 {
00248 
00249   int rc;
00250   rc = sqlite3_open( QgsApplication::qgisUserDbFilePath().toUtf8().data(), &db );
00251   if ( rc != SQLITE_OK )
00252   {
00253     QgsDebugMsg( QString( "Can't open database: %1" ).arg( sqlite3_errmsg( db ) ) );
00254 
00255     // XXX This will likely never happen since on open, sqlite creates the
00256     //     database if it does not exist.
00257     assert( rc == 0 );
00258   }
00259   return rc;
00260 }
00261 
00262 void QgsBookmarks::helpRequested()
00263 {
00264   QgsContextHelp::run( context_id );
00265 }

Generated on Tue Oct 28 16:51:26 2008 for Quantum GIS API Documentation by  doxygen 1.5.1