Skip to content

Commit f3bcaf4

Browse files
committed
Merge pull request #1706 from slarosa/share_bookmarks
Allows to import/export bookmarks to XML file
2 parents 07182b6 + c4c36dd commit f3bcaf4

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

src/app/qgsbookmarks.cpp

+138
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "qgslogger.h"
2626

2727
#include <QFileInfo>
28+
#include <QFileDialog>
2829
#include <QMessageBox>
2930
#include <QSettings>
3031
#include <QPushButton>
@@ -47,10 +48,20 @@ QgsBookmarks::QgsBookmarks( QWidget *parent, Qt::WindowFlags fl )
4748
QPushButton *btnAdd = new QPushButton( tr( "&Add" ) );
4849
QPushButton *btnDelete = new QPushButton( tr( "&Delete" ) );
4950
QPushButton *btnZoomTo = new QPushButton( tr( "&Zoom to" ) );
51+
QPushButton *btnImpExp = new QPushButton( tr( "&Share" ) );
52+
5053
btnZoomTo->setDefault( true );
5154
buttonBox->addButton( btnAdd, QDialogButtonBox::ActionRole );
5255
buttonBox->addButton( btnDelete, QDialogButtonBox::ActionRole );
5356
buttonBox->addButton( btnZoomTo, QDialogButtonBox::ActionRole );
57+
buttonBox->addButton( btnImpExp, QDialogButtonBox::ActionRole );
58+
59+
QMenu *share = new QMenu();
60+
QAction *btnExport = share->addAction( tr( "&Export" ) );
61+
QAction *btnImport = share->addAction( tr( "&Import" ) );
62+
connect( btnExport, SIGNAL( triggered() ), this, SLOT( exportToXML() ) );
63+
connect( btnImport, SIGNAL( triggered() ), this, SLOT( importFromXML() ) );
64+
btnImpExp->setMenu( share );
5465

5566
connect( btnAdd, SIGNAL( clicked() ), this, SLOT( addClicked() ) );
5667
connect( btnDelete, SIGNAL( clicked() ), this, SLOT( deleteClicked() ) );
@@ -251,3 +262,130 @@ void QgsBookmarks::zoomToBookmark()
251262
QgisApp::instance()->setExtent( rect );
252263
QgisApp::instance()->mapCanvas()->refresh();
253264
}
265+
266+
void QgsBookmarks::importFromXML()
267+
{
268+
QString fileName = QFileDialog::getOpenFileName( this, tr( "Import Bookmarks" ), ".",
269+
tr( "XML files (*.xml *XML)" ) );
270+
if ( fileName.isEmpty() )
271+
{
272+
return;
273+
}
274+
275+
QFile f( fileName );
276+
if ( !f.open( QIODevice::ReadOnly | QIODevice::Text ) )
277+
{
278+
return;
279+
}
280+
281+
QDomDocument doc;
282+
if ( !doc.setContent( &f ) )
283+
{
284+
return;
285+
}
286+
f.close();
287+
288+
QDomElement docElem = doc.documentElement();
289+
QDomNodeList nodeList = docElem.elementsByTagName( "bookmark" );
290+
291+
QSqlTableModel *model = qobject_cast<QSqlTableModel *>( lstBookmarks->model() );
292+
Q_ASSERT( model );
293+
294+
QString queries;
295+
296+
for ( int i = 0;i < nodeList.count(); i++ )
297+
{
298+
QDomNode bookmark = nodeList.at( i );
299+
QDomElement name = bookmark.firstChildElement( "Name" );
300+
QDomElement prjname = bookmark.firstChildElement( "Project" );
301+
QDomElement xmin = bookmark.firstChildElement( "xMin" );
302+
QDomElement xmax = bookmark.firstChildElement( "xMax" );
303+
QDomElement ymin = bookmark.firstChildElement( "yMin" );
304+
QDomElement ymax = bookmark.firstChildElement( "yMax" );
305+
QDomElement srid = bookmark.firstChildElement( "SRID" );
306+
307+
queries += "INSERT INTO tbl_bookmarks(bookmark_id,name,project_name,xmin,ymin,xmax,ymax,projection_srid)"
308+
" VALUES (NULL,"
309+
"'" + name.text() + "',"
310+
"'" + prjname.text() + "',"
311+
+ xmin.text() + ","
312+
+ xmax.text() + ","
313+
+ ymin.text() + ","
314+
+ ymax.text() + ","
315+
+ srid.text() + ");";
316+
}
317+
318+
QStringList queriesList = queries.split( ";" );
319+
QSqlQuery query( model->database() );
320+
321+
foreach ( QString queryTxt, queriesList )
322+
{
323+
if ( queryTxt.trimmed().isEmpty() )
324+
{
325+
continue;
326+
}
327+
if ( !query.exec( queryTxt ) )
328+
{
329+
QMessageBox::warning( this, tr( "Error" ), tr( "Unable to create the bookmark.\nDriver: %1\nDatabase: %2" )
330+
.arg( query.lastError().driverText() )
331+
.arg( query.lastError().databaseText() ) );
332+
}
333+
query.finish();
334+
}
335+
model->setSort( 0, Qt::AscendingOrder );
336+
model->select();
337+
}
338+
339+
void QgsBookmarks::exportToXML()
340+
{
341+
QString fileName = QFileDialog::getSaveFileName( this, tr( "Export bookmarks" ), ".",
342+
tr( "XML files( *.xml *.XML )" ) );
343+
if ( fileName.isEmpty() )
344+
{
345+
return;
346+
}
347+
348+
// ensure the user never ommited the extension from the file name
349+
if ( !fileName.toLower().endsWith( ".xml" ) )
350+
{
351+
fileName += ".xml";
352+
}
353+
354+
QDomDocument doc( "qgis_bookmarks" );
355+
QDomElement root = doc.createElement( "qgis_bookmarks" );
356+
doc.appendChild( root );
357+
358+
int rowCount = lstBookmarks->model()->rowCount();
359+
int colCount = lstBookmarks->model()->columnCount();
360+
361+
for ( int i = 0; i < rowCount; ++i )
362+
{
363+
QDomElement bookmark = doc.createElement( "bookmark" );
364+
root.appendChild( bookmark );
365+
for ( int j = 0; j < colCount; j++ )
366+
{
367+
QModelIndex idx = lstBookmarks->model()->index( i, j );
368+
if ( idx.isValid() )
369+
{
370+
QString value = idx.data( Qt::DisplayRole ).toString();
371+
QDomText idText = doc.createTextNode( value );
372+
QVariant header = lstBookmarks->model()->headerData( j, Qt::Horizontal );
373+
QDomElement id = doc.createElement( header.toString() );
374+
id.appendChild( idText );
375+
bookmark.appendChild( id );
376+
}
377+
}
378+
}
379+
380+
QFile f( fileName );
381+
if ( !f.open( QFile::WriteOnly ) )
382+
{
383+
f.close();
384+
return;
385+
}
386+
387+
QTextStream out( &f );
388+
out.setCodec( "UTF - 8" );
389+
doc.save( out, 2 );
390+
f.close();
391+
}

src/app/qgsbookmarks.h

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class APP_EXPORT QgsBookmarks : public QDialog, private Ui::QgsBookmarksBase
3333
void addClicked();
3434
void deleteClicked();
3535
void zoomToBookmark();
36+
void exportToXML();
37+
void importFromXML();
3638

3739
void on_lstBookmarks_doubleClicked( const QModelIndex & );
3840
void on_buttonBox_helpRequested() { QgsContextHelp::run( metaObject()->className() ); }

0 commit comments

Comments
 (0)