patch-for-3863.txt

patch Bookmarks - Steven Mizuno, 2011-06-02 07:58 AM

Download (9.85 KB)

 
1
diff --git a/resources/context_help/QgsBookmarks-en_US b/resources/context_help/QgsBookmarks-en_US
2
index d516a1d..8b89bf2 100644
3
--- a/resources/context_help/QgsBookmarks-en_US
4
+++ b/resources/context_help/QgsBookmarks-en_US
5
@@ -5,6 +5,7 @@ Spatial Bookmarks allow you to "bookmark" a geographic location and return to it
6
 <a href="#working">Working with Bookmarks</a><br/>
7
 <a href="#zooming">Zooming to a Bookmark</a><br/>
8
 <a href="#deleting">Deleting a Bookmark</a><br/>
9
+<a href="#updating">Updating a Bookmark</a><br/>
10
 
11
 <a name="creating">
12
 <h4>Creating a Bookmark</h4>
13
@@ -31,4 +32,8 @@ You can also zoom to a bookmark by double-clicking on it.
14
 <h5>Deleting a Bookmark</h5>
15
 </a>
16
 To delete a bookmark from the Bookmarks dialog, click on it then click the <label>Delete</label> button. Confirm your choice by clicking <label>OK</label> or cancel the delete by clicking <label>Cancel</label>.
17
+<a name="updating">
18
+<h5>Updating a Bookmark</h5>
19
+</a>
20
+To update the extent of a bookmark, click on it then click the <label>Update</label> button. Confirm your choice by clicking <label>OK</label> or cancel the update by clicking <label>Cancel</label>.
21
 
22
diff --git a/src/app/qgisapp.cpp b/src/app/qgisapp.cpp
23
index 93910a6..b5ea879 100644
24
--- a/src/app/qgisapp.cpp
25
+++ b/src/app/qgisapp.cpp
26
@@ -6339,7 +6339,6 @@ void QgisApp::showBookmarks()
27
   {
28
     bookmarks = new QgsBookmarks( this, Qt::WindowMinMaxButtonsHint );
29
   }
30
-  bookmarks->restorePosition();
31
   bookmarks->show();
32
   bookmarks->raise();
33
   bookmarks->setWindowState( bookmarks->windowState() & ~Qt::WindowMinimized );
34
diff --git a/src/app/qgsbookmarkitem.cpp b/src/app/qgsbookmarkitem.cpp
35
index fd6e5ed..3a0165d 100644
36
--- a/src/app/qgsbookmarkitem.cpp
37
+++ b/src/app/qgsbookmarkitem.cpp
38
@@ -43,48 +43,41 @@ void QgsBookmarkItem::store()
39
   int rc;
40
   QgsDebugMsg( QString( "Opening user database: %1" ).arg( mUserDbPath ) );
41
   rc = sqlite3_open( mUserDbPath.toUtf8().data(), &db );
42
-  if ( rc )
43
+  if ( SQLITE_OK == rc )
44
   {
45
-    QgsDebugMsg( QString( "Can't open database: %1" ).arg( sqlite3_errmsg( db ) ) );
46
-
47
-    // XXX This will likely never happen since on open, sqlite creates the
48
-    //     database if it does not exist.
49
-    assert( rc == 0 );
50
-  }
51
-  // prepare the sql statement
52
-  const char *pzTail;
53
-  sqlite3_stmt *ppStmt;
54
-  QString sql;
55
-  QTextStream sqlStream( &sql );
56
-  sqlStream << "insert into tbl_bookmarks values(null,'" <<
57
-  mName << "','" <<
58
-  mProjectTitle << "'," <<
59
-  mViewExtent.xMinimum() << "," <<
60
-  mViewExtent.yMinimum() << "," <<
61
-  mViewExtent.xMaximum() << "," <<
62
-  mViewExtent.yMaximum() << "," <<
63
-  mSrid << ")";
64
+    // prepare the sql statement
65
+    QString sql;
66
+    QTextStream sqlStream( &sql );
67
+    // use '17 g' format; SmartNotation is default
68
+    sqlStream.setRealNumberPrecision( 17 );
69
+    sqlStream << "insert into tbl_bookmarks values(null,'" <<
70
+    // fix occurrences of single-quote
71
+    mName.replace( '\'', "''" ) << "','" <<
72
+    mProjectTitle.replace( '\'', "''" ) << "'," <<
73
+    mViewExtent.xMinimum() << "," <<
74
+    mViewExtent.yMinimum() << "," <<
75
+    mViewExtent.xMaximum() << "," <<
76
+    mViewExtent.yMaximum() << "," <<
77
+    mSrid << ")";
78
 
79
-  QgsDebugMsg( QString( "Storing bookmark using: %1" ).arg( sql ) );
80
+    QgsDebugMsg( QString( "Storing bookmark using: %1" ).arg( sql ) );
81
 
82
-  QByteArray sqlData = sql.toUtf8();
83
-
84
-  rc = sqlite3_prepare( db, sqlData.constData(), sqlData.size(), &ppStmt, &pzTail );
85
-  // XXX Need to free memory from the error msg if one is set
86
-  if ( rc == SQLITE_OK )
87
-  {
88
-    // get the first row of the result set
89
-    if ( sqlite3_step( ppStmt ) != SQLITE_DONE )
90
+    char * errmsg = 0;
91
+    rc = sqlite3_exec( db, sql.toUtf8(), NULL, NULL, &errmsg );
92
+    if ( rc != SQLITE_OK )
93
     {
94
-
95
       // XXX query failed -- warn the user some how
96
-      QgsDebugMsg( QString( "Failed to store bookmark: %1" ).arg( sqlite3_errmsg( db ) ) );
97
+      QgsDebugMsg( QString( "Failed to store bookmark: %1" ).arg( errmsg ) );
98
+      sqlite3_free( errmsg );
99
     }
100
-    // close the statement
101
-    sqlite3_finalize( ppStmt );
102
-    // close the database
103
     sqlite3_close( db );
104
   }
105
+  else
106
+  {
107
+    QgsDebugMsg( QString( "Can't open database: %1" ).arg( sqlite3_errmsg( db ) ) );
108
 
109
-
110
+    // XXX This will likely never happen since on open, sqlite creates the
111
+    //     database if it does not exist.
112
+    assert( rc == 0 );
113
+  }
114
 }
115
diff --git a/src/app/qgsbookmarks.cpp b/src/app/qgsbookmarks.cpp
116
index e93ff83..d1403be 100644
117
--- a/src/app/qgsbookmarks.cpp
118
+++ b/src/app/qgsbookmarks.cpp
119
@@ -38,6 +38,9 @@ QgsBookmarks::QgsBookmarks( QWidget *parent, Qt::WFlags fl )
120
     mParent( parent )
121
 {
122
   setupUi( this );
123
+
124
+  restorePosition();
125
+
126
   // user database is created at QGIS startup in QgisApp::createDB
127
   // we just check whether there is our database [MD]
128
   QFileInfo myFileInfo;
129
@@ -54,11 +57,15 @@ QgsBookmarks::QgsBookmarks( QWidget *parent, Qt::WFlags fl )
130
   // Create the zoomto and delete buttons and add them to the
131
   // toolbar
132
   //
133
+  QPushButton * btnUpdate = new QPushButton( tr( "&Update" ) );
134
   QPushButton * btnDelete = new QPushButton( tr( "&Delete" ) );
135
   QPushButton * btnZoomTo = new QPushButton( tr( "&Zoom to" ) );
136
   btnZoomTo->setDefault( true );
137
+  buttonBox->addButton( btnUpdate, QDialogButtonBox::ActionRole );
138
   buttonBox->addButton( btnDelete, QDialogButtonBox::ActionRole );
139
   buttonBox->addButton( btnZoomTo, QDialogButtonBox::ActionRole );
140
+  // connect the slot up to catch when a bookmark is updated
141
+  connect( btnUpdate, SIGNAL( clicked() ), this, SLOT( on_btnUpdate_clicked() ) );
142
   // connect the slot up to catch when a bookmark is deleted
143
   connect( btnDelete, SIGNAL( clicked() ), this, SLOT( on_btnDelete_clicked() ) );
144
   // connect the slot up to catch when a bookmark is zoomed to
145
@@ -109,7 +116,7 @@ void QgsBookmarks::initialise()
146
         QString xMax = QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 5 ) );
147
         QString yMax = QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 6 ) );
148
         // set the extents
149
-        item->setText( 2, xMin + ", " + yMin + ", " + xMax + ", " + yMax );
150
+        item->setText( 2, xMin + ", " + yMin + " : " + xMax + ", " + yMax );   // use colon to separate ll from ur corners listed (be consistent with other displays of extent)
151
         // set the id
152
         item->setText( 3, QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 0 ) ) );
153
       }
154
@@ -145,6 +152,66 @@ void QgsBookmarks::saveWindowLocation()
155
   settings.setValue( "/Windows/Bookmarks/geometry", saveGeometry() );
156
 }
157
 
158
+void QgsBookmarks::on_btnUpdate_clicked()
159
+{
160
+  // get the current item
161
+  QTreeWidgetItem *item = lstBookmarks->currentItem();
162
+  if ( item )
163
+  {
164
+    // make sure the user really wants to update this bookmark
165
+    if ( QMessageBox::Ok == QMessageBox::information( this, tr( "Really Update?" ),
166
+         tr( "Are you sure you want to update the %1 bookmark?" ).arg( item->text( 0 ) ),
167
+         QMessageBox::Ok | QMessageBox::Cancel ) )
168
+    {
169
+      // retrieve the current map extent
170
+      QgsRectangle viewExtent = QgisApp::instance()->mapCanvas()->extent();
171
+
172
+      int rc;
173
+      QgsDebugMsg( QString( "Opening user database: %1" ).arg( QgsApplication::qgisUserDbFilePath() ) );
174
+      rc = connectDb();
175
+      if ( SQLITE_OK == rc )
176
+      {
177
+        // prepare the sql statement
178
+        QString sql;
179
+        QTextStream sqlStream( &sql );
180
+        // use '17 g' format; SmartNotation is default
181
+        sqlStream.setRealNumberPrecision( 17 );
182
+        sqlStream << "update tbl_bookmarks set " <<
183
+        "xmin=" << viewExtent.xMinimum() << "," <<
184
+        "ymin=" << viewExtent.yMinimum() << "," <<
185
+        "xmax=" << viewExtent.xMaximum() << "," <<
186
+        "ymax=" << viewExtent.yMaximum() << " " <<
187
+        "where bookmark_id=" << item->text( 3 );
188
+        QgsDebugMsg( QString( "Storing bookmark using: %1" ).arg( sql ) );
189
+
190
+        char * errmsg;
191
+        rc = sqlite3_exec( db, sql.toUtf8(), NULL, NULL, &errmsg );
192
+        if ( rc != SQLITE_OK )
193
+        {
194
+          // XXX Provide popup message on failure?
195
+          QMessageBox::warning( this, tr( "Error updating bookmark" ),
196
+                                tr( "Failed to update the %1 bookmark. The database said:\n%2" )
197
+                                .arg( item->text( 0 ) ).arg( errmsg ) );
198
+          sqlite3_free( errmsg );
199
+        }
200
+        // close the database
201
+        sqlite3_close( db );
202
+
203
+        refreshBookmarks();
204
+
205
+      }
206
+      else
207
+      {
208
+        QgsDebugMsg( QString( "Can't open database: %1" ).arg( sqlite3_errmsg( db ) ) );
209
+
210
+        // XXX This will likely never happen since on open, sqlite creates the
211
+        //     database if it does not exist.
212
+        assert( rc == 0 );
213
+      }
214
+    }
215
+  }
216
+}
217
+
218
 void QgsBookmarks::on_btnDelete_clicked()
219
 {
220
   // get the current item
221
@@ -187,7 +254,7 @@ void QgsBookmarks::on_btnZoomTo_clicked()
222
   zoomToBookmark();
223
 }
224
 
225
-void QgsBookmarks::on_lstBookmarks_doubleClicked( QTreeWidgetItem *lvi )
226
+void QgsBookmarks::on_lstBookmarks_itemDoubleClicked( QTreeWidgetItem *lvi )
227
 {
228
   zoomToBookmark();
229
 }
230
diff --git a/src/app/qgsbookmarks.h b/src/app/qgsbookmarks.h
231
index 8fe9262..f5cfb37 100644
232
--- a/src/app/qgsbookmarks.h
233
+++ b/src/app/qgsbookmarks.h
234
@@ -35,9 +35,10 @@ class QgsBookmarks : public QDialog, private Ui::QgsBookmarksBase
235
     void restorePosition();
236
   private slots:
237
     void saveWindowLocation();
238
+    void on_btnUpdate_clicked();
239
     void on_btnDelete_clicked();
240
     void on_btnZoomTo_clicked();
241
-    void on_lstBookmarks_doubleClicked( QTreeWidgetItem * );
242
+    void on_lstBookmarks_itemDoubleClicked( QTreeWidgetItem * );
243
     void refreshBookmarks();
244
 
245
     void on_buttonBox_helpRequested() { QgsContextHelp::run( metaObject()->className() ); }