/
qgsbookmarks.cpp
403 lines (346 loc) · 12.8 KB
/
qgsbookmarks.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/***************************************************************************
QgsBookmarks.cpp - Spatial Bookmarks
-------------------
begin : 2005-04-23
copyright : (C) 2005 Gary Sherman
email : sherman at mrcc dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsbookmarks.h"
#include "qgisapp.h"
#include "qgsapplication.h"
#include "qgscontexthelp.h"
#include "qgsmapcanvas.h"
#include "qgsmaprenderer.h"
#include "qgsproject.h"
#include "qgslogger.h"
#include <QFileInfo>
#include <QFileDialog>
#include <QMessageBox>
#include <QSettings>
#include <QPushButton>
#include <QSqlTableModel>
#include <QSqlError>
#include <QSqlQuery>
QgsBookmarks *QgsBookmarks::sInstance = 0;
QgsBookmarks::QgsBookmarks( QWidget *parent, Qt::WindowFlags fl )
: QDialog( parent, fl )
{
setupUi( this );
restorePosition();
//
// Create the zoomto and delete buttons and add them to the
// toolbar
//
QPushButton *btnAdd = new QPushButton( tr( "&Add" ) );
QPushButton *btnDelete = new QPushButton( tr( "&Delete" ) );
QPushButton *btnZoomTo = new QPushButton( tr( "&Zoom to" ) );
QPushButton *btnImpExp = new QPushButton( tr( "&Share" ) );
btnZoomTo->setDefault( true );
buttonBox->addButton( btnAdd, QDialogButtonBox::ActionRole );
buttonBox->addButton( btnDelete, QDialogButtonBox::ActionRole );
buttonBox->addButton( btnZoomTo, QDialogButtonBox::ActionRole );
buttonBox->addButton( btnImpExp, QDialogButtonBox::ActionRole );
QMenu *share = new QMenu();
QAction *btnExport = share->addAction( tr( "&Export" ) );
QAction *btnImport = share->addAction( tr( "&Import" ) );
connect( btnExport, SIGNAL( triggered() ), this, SLOT( exportToXML() ) );
connect( btnImport, SIGNAL( triggered() ), this, SLOT( importFromXML() ) );
btnImpExp->setMenu( share );
connect( btnAdd, SIGNAL( clicked() ), this, SLOT( addClicked() ) );
connect( btnDelete, SIGNAL( clicked() ), this, SLOT( deleteClicked() ) );
connect( btnZoomTo, SIGNAL( clicked() ), this, SLOT( zoomToBookmark() ) );
// open the database
QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE", "bookmarks" );
db.setDatabaseName( QgsApplication::qgisUserDbFilePath() );
if ( !db.open() )
{
QMessageBox::warning( this, tr( "Error" ),
tr( "Unable to open bookmarks database.\nDatabase: %1\nDriver: %2\nDatabase: %3" )
.arg( QgsApplication::qgisUserDbFilePath() )
.arg( db.lastError().driverText() )
.arg( db.lastError().databaseText() )
);
deleteLater();
return;
}
QSqlTableModel *model = new QSqlTableModel( this, db );
model->setTable( "tbl_bookmarks" );
model->setSort( 0, Qt::AscendingOrder );
model->setEditStrategy( QSqlTableModel::OnFieldChange );
model->select();
// set better headers then column names from table
model->setHeaderData( 0, Qt::Horizontal, tr( "ID" ) );
model->setHeaderData( 1, Qt::Horizontal, tr( "Name" ) );
model->setHeaderData( 2, Qt::Horizontal, tr( "Project" ) );
model->setHeaderData( 3, Qt::Horizontal, tr( "xMin" ) );
model->setHeaderData( 4, Qt::Horizontal, tr( "yMin" ) );
model->setHeaderData( 5, Qt::Horizontal, tr( "xMax" ) );
model->setHeaderData( 6, Qt::Horizontal, tr( "yMax" ) );
model->setHeaderData( 7, Qt::Horizontal, tr( "SRID" ) );
lstBookmarks->setModel( model );
QSettings settings;
lstBookmarks->header()->restoreState( settings.value( "/Windows/Bookmarks/headerstate" ).toByteArray() );
#ifndef QGISDEBUG
lstBookmarks->setColumnHidden( 0, true );
#endif
}
QgsBookmarks::~QgsBookmarks()
{
saveWindowLocation();
sInstance = 0;
}
void QgsBookmarks::restorePosition()
{
QSettings settings;
restoreGeometry( settings.value( "/Windows/Bookmarks/geometry" ).toByteArray() );
}
void QgsBookmarks::saveWindowLocation()
{
QSettings settings;
settings.setValue( "/Windows/Bookmarks/geometry", saveGeometry() );
settings.setValue( "/Windows/Bookmarks/headerstate", lstBookmarks->header()->saveState() );
}
void QgsBookmarks::newBookmark()
{
showBookmarks();
sInstance->addClicked();
}
void QgsBookmarks::showBookmarks()
{
if ( !sInstance )
{
sInstance = new QgsBookmarks( QgisApp::instance() );
sInstance->setAttribute( Qt::WA_DeleteOnClose );
}
sInstance->show();
sInstance->raise();
sInstance->setWindowState( sInstance->windowState() & ~Qt::WindowMinimized );
sInstance->activateWindow();
}
void QgsBookmarks::addClicked()
{
QSqlTableModel *model = qobject_cast<QSqlTableModel *>( lstBookmarks->model() );
Q_ASSERT( model );
QgsMapCanvas *canvas = QgisApp::instance()->mapCanvas();
Q_ASSERT( canvas );
QSqlQuery query( "INSERT INTO tbl_bookmarks(bookmark_id,name,project_name,xmin,ymin,xmax,ymax,projection_srid)"
" VALUES (NULL,:name,:project_name,:xmin,:xmax,:ymin,:ymax,:projection_srid)",
model->database() );
QString projStr( "" );
if ( QgsProject::instance() )
{
if ( !QgsProject::instance()->title().isEmpty() )
{
projStr = QgsProject::instance()->title();
}
else if ( !QgsProject::instance()->fileName().isEmpty() )
{
QFileInfo fi( QgsProject::instance()->fileName() );
projStr = fi.exists() ? fi.fileName() : "";
}
}
query.bindValue( ":name", tr( "New bookmark" ) );
query.bindValue( ":project_name", projStr );
query.bindValue( ":xmin", canvas->extent().xMinimum() );
query.bindValue( ":ymin", canvas->extent().yMinimum() );
query.bindValue( ":xmax", canvas->extent().xMaximum() );
query.bindValue( ":ymax", canvas->extent().yMaximum() );
query.bindValue( ":projection_srid", QVariant::fromValue( canvas->mapSettings().destinationCrs().srsid() ) );
if ( query.exec() )
{
model->setSort( 0, Qt::AscendingOrder );
model->select();
lstBookmarks->scrollToBottom();
lstBookmarks->setCurrentIndex( model->index( model->rowCount() - 1, 1 ) );
lstBookmarks->edit( model->index( model->rowCount() - 1, 1 ) );
}
else
{
QMessageBox::warning( this, tr( "Error" ), tr( "Unable to create the bookmark.\nDriver:%1\nDatabase:%2" )
.arg( query.lastError().driverText() )
.arg( query.lastError().databaseText() ) );
}
}
void QgsBookmarks::deleteClicked()
{
QList<int> rows;
foreach ( const QModelIndex &idx, lstBookmarks->selectionModel()->selectedIndexes() )
{
if ( idx.column() == 1 )
{
rows << idx.row();
}
}
if ( rows.size() == 0 )
return;
// make sure the user really wants to delete these bookmarks
if ( QMessageBox::Cancel == QMessageBox::information( this, tr( "Really Delete?" ),
tr( "Are you sure you want to delete %n bookmark(s)?", "number of rows", rows.size() ),
QMessageBox::Ok | QMessageBox::Cancel ) )
return;
int i = 0;
foreach ( int row, rows )
{
lstBookmarks->model()->removeRow( row - i );
i++;
}
}
void QgsBookmarks::on_lstBookmarks_doubleClicked( const QModelIndex & index )
{
Q_UNUSED( index );
zoomToBookmark();
}
void QgsBookmarks::zoomToBookmark()
{
QModelIndex index = lstBookmarks->currentIndex();
if ( !index.isValid() )
return;
double xmin = index.sibling( index.row(), 3 ).data().toDouble();
double ymin = index.sibling( index.row(), 4 ).data().toDouble();
double xmax = index.sibling( index.row(), 5 ).data().toDouble();
double ymax = index.sibling( index.row(), 6 ).data().toDouble();
int srid = index.sibling( index.row(), 7 ).data().toInt();
QgsRectangle rect = QgsRectangle( xmin, ymin, xmax, ymax );
// backwards compatibility, older version had -1 in the srid column
if ( srid > 0 &&
srid != QgisApp::instance()->mapCanvas()->mapSettings().destinationCrs().srsid() )
{
QgsCoordinateTransform ct( QgsCoordinateReferenceSystem( srid, QgsCoordinateReferenceSystem::InternalCrsId ),
QgisApp::instance()->mapCanvas()->mapSettings().destinationCrs() );
rect = ct.transform( rect );
if ( rect.isEmpty() )
{
QMessageBox::warning( this, tr( "Empty extent" ), tr( "Reprojected extent is empty." ) );
return;
}
}
// set the extent to the bookmark and refresh
QgisApp::instance()->setExtent( rect );
QgisApp::instance()->mapCanvas()->refresh();
}
void QgsBookmarks::importFromXML()
{
QSettings settings;
QString lastUsedDir = settings.value( "/Bookmark/LastUsedDirectory", QVariant() ).toString();
QString fileName = QFileDialog::getOpenFileName( this, tr( "Import Bookmarks" ), lastUsedDir,
tr( "XML files (*.xml *XML)" ) );
if ( fileName.isEmpty() )
{
return;
}
QFile f( fileName );
if ( !f.open( QIODevice::ReadOnly | QIODevice::Text ) )
{
return;
}
QDomDocument doc;
if ( !doc.setContent( &f ) )
{
return;
}
f.close();
QDomElement docElem = doc.documentElement();
QDomNodeList nodeList = docElem.elementsByTagName( "bookmark" );
QSqlTableModel *model = qobject_cast<QSqlTableModel *>( lstBookmarks->model() );
Q_ASSERT( model );
QString queries;
for ( int i = 0;i < nodeList.count(); i++ )
{
QDomNode bookmark = nodeList.at( i );
QDomElement name = bookmark.firstChildElement( "name" );
QDomElement prjname = bookmark.firstChildElement( "project" );
QDomElement xmin = bookmark.firstChildElement( "xmin" );
QDomElement xmax = bookmark.firstChildElement( "xmax" );
QDomElement ymin = bookmark.firstChildElement( "ymin" );
QDomElement ymax = bookmark.firstChildElement( "ymax" );
QDomElement srid = bookmark.firstChildElement( "sr_id" );
queries += "INSERT INTO tbl_bookmarks(bookmark_id,name,project_name,xmin,ymin,xmax,ymax,projection_srid)"
" VALUES (NULL,"
"'" + name.text() + "',"
"'" + prjname.text() + "',"
+ xmin.text() + ","
+ ymin.text() + ","
+ xmax.text() + ","
+ ymax.text() + ","
+ srid.text() + ");";
}
QStringList queriesList = queries.split( ";" );
QSqlQuery query( model->database() );
foreach ( QString queryTxt, queriesList )
{
if ( queryTxt.trimmed().isEmpty() )
{
continue;
}
if ( !query.exec( queryTxt ) )
{
QMessageBox::warning( this, tr( "Error" ), tr( "Unable to create the bookmark.\nDriver: %1\nDatabase: %2" )
.arg( query.lastError().driverText() )
.arg( query.lastError().databaseText() ) );
}
query.finish();
}
model->setSort( 0, Qt::AscendingOrder );
model->select();
}
void QgsBookmarks::exportToXML()
{
QSettings settings;
QString lastUsedDir = settings.value( "/Bookmark/LastUsedDirectory", QVariant() ).toString();
QString fileName = QFileDialog::getSaveFileName( this, tr( "Export bookmarks" ), lastUsedDir,
tr( "XML files( *.xml *.XML )" ) );
if ( fileName.isEmpty() )
{
return;
}
// ensure the user never ommited the extension from the file name
if ( !fileName.toLower().endsWith( ".xml" ) )
{
fileName += ".xml";
}
QDomDocument doc( "qgis_bookmarks" );
QDomElement root = doc.createElement( "qgis_bookmarks" );
doc.appendChild( root );
int rowCount = lstBookmarks->model()->rowCount();
int colCount = lstBookmarks->model()->columnCount();
QList<QString> headerList;
headerList << "id" << "name" << "project" << "xmin"
<< "ymin" << "xmax" << "ymax" << "sr_id";
for ( int i = 0; i < rowCount; ++i )
{
QDomElement bookmark = doc.createElement( "bookmark" );
root.appendChild( bookmark );
for ( int j = 0; j < colCount; j++ )
{
QModelIndex idx = lstBookmarks->model()->index( i, j );
if ( idx.isValid() )
{
QString value = idx.data( Qt::DisplayRole ).toString();
QDomText idText = doc.createTextNode( value );
QString header = headerList.at( j );
QDomElement id = doc.createElement( header );
id.appendChild( idText );
bookmark.appendChild( id );
}
}
}
QFile f( fileName );
if ( !f.open( QFile::WriteOnly ) )
{
f.close();
return;
}
QTextStream out( &f );
out.setCodec( "UTF - 8" );
doc.save( out, 2 );
f.close();
settings.setValue( "/Bookmark/LastUsedDirectory", QFileInfo( fileName ).path() );
}