00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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
00043
00044 QFileInfo myFileInfo;
00045 myFileInfo.setFile( QgsApplication::qgisSettingsDirPath() );
00046 if ( !myFileInfo.exists( ) )
00047 {
00048 QgsDebugMsg( "The qgis.db does not exist" );
00049 }
00050
00051
00052 initialise();
00053
00054
00055
00056
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
00064 connect( btnDelete, SIGNAL( clicked() ), this, SLOT( on_btnDelete_clicked() ) );
00065
00066 connect( btnZoomTo, SIGNAL( clicked() ), this, SLOT( on_btnZoomTo_clicked() ) );
00067
00068 connect( mParent, SIGNAL( bookmarkAdded() ), this, SLOT( refreshBookmarks() ) );
00069
00070 connect( buttonBox, SIGNAL( helpRequested() ), this, SLOT( helpRequested() ) );
00071 }
00072
00073
00074 QgsBookmarks::~QgsBookmarks()
00075 {
00076 saveWindowLocation();
00077 }
00078
00079 void QgsBookmarks::refreshBookmarks()
00080 {
00081 lstBookmarks->clear();
00082 initialise();
00083 }
00084
00085 void QgsBookmarks::initialise()
00086 {
00087 int rc = connectDb();
00088 if ( rc == SQLITE_OK )
00089 {
00090
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
00097 if ( rc == SQLITE_OK )
00098 {
00099
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
00105
00106 item->setText( 0, name );
00107
00108 item->setText( 1, QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 2 ) ) );
00109
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
00115 item->setText( 2, xMin + ", " + yMin + ", " + xMax + ", " + yMax );
00116
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
00128 QgsDebugMsg( QString( "Failed to get bookmarks: %1" ).arg( sqlite3_errmsg( db ) ) );
00129 }
00130
00131 sqlite3_finalize( ppStmt );
00132
00133 sqlite3_close( db );
00134
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
00154 QTreeWidgetItem *item = lstBookmarks->currentItem();
00155 if ( item )
00156 {
00157
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
00163 item = lstBookmarks->takeTopLevelItem( lstBookmarks->indexOfTopLevelItem( item ) );
00164
00165 int rc = connectDb();
00166 if ( rc == SQLITE_OK )
00167 {
00168 char *errmsg;
00169
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
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
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
00203
00204
00205 QTreeWidgetItem *item = lstBookmarks->currentItem();
00206 if ( !item )
00207 {
00208 return;
00209 }
00210
00211 int rc = connectDb();
00212 if ( rc == SQLITE_OK )
00213 {
00214 sqlite3_stmt *ppStmt;
00215 const char *pzTail;
00216
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
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
00229 QgisApp::instance()->setExtent( QgsRect( xmin.toDouble(),
00230 ymin.toDouble(),
00231 xmax.toDouble(),
00232 ymax.toDouble() ) );
00233
00234 QgisApp::instance()->mapCanvas()->refresh();
00235 }
00236 }
00237
00238
00239 sqlite3_finalize( ppStmt );
00240
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
00256
00257 assert( rc == 0 );
00258 }
00259 return rc;
00260 }
00261
00262 void QgsBookmarks::helpRequested()
00263 {
00264 QgsContextHelp::run( context_id );
00265 }