41 b.
setId( element.attribute( QStringLiteral(
"id" ) ) );
42 b.
setName( element.attribute( QStringLiteral(
"name" ) ) );
43 b.
setGroup( element.attribute( QStringLiteral(
"group" ) ) );
53 QDomElement bookmarkElem = doc.createElement( QStringLiteral(
"Bookmark" ) );
54 bookmarkElem.setAttribute( QStringLiteral(
"id" ), mId );
55 bookmarkElem.setAttribute( QStringLiteral(
"name" ), mName );
56 bookmarkElem.setAttribute( QStringLiteral(
"group" ), mGroup );
57 bookmarkElem.setAttribute( QStringLiteral(
"extent" ), mExtent.
asWktPolygon() );
64 return mId == other.mId && mName == other.mName && mExtent == other.mExtent && mGroup == other.mGroup;
69 return !( *
this == other );
110 res->mProject = project;
131 if ( bookmark.
id().isEmpty() )
132 bookmark.
setId( QUuid::createUuid().toString() );
136 for (
const QgsBookmark &b : qgis::as_const( mBookmarks ) )
138 if ( b.
id() == bookmark.
id() )
149 mBookmarks << bookmark;
150 if ( !mGroups.contains( bookmark.group() ) )
151 mGroups << bookmark.group();
158 return bookmark.id();
169 for (
const QgsBookmark &b : qgis::as_const( mBookmarks ) )
184 mBookmarks.removeAt( pos );
186 mGroups.removeOne( group );
200 for (
const QgsBookmark &b : qgis::as_const( mBookmarks ) )
202 if ( b.id() == bookmark.
id() )
204 if ( mBookmarks[i].group() != bookmark.
group() )
207 mGroups.removeOne( mBookmarks[i].group() );
208 if ( !mGroups.contains( bookmark.
group() ) )
209 mGroups << bookmark.
group();
211 mBookmarks[i] = bookmark;
227 const QList< QgsBookmark >
bookmarks = mBookmarks;
241 for (
int i = 0; i < mBookmarks.count(); ++i )
243 if ( mBookmarks.at( i ).group() == oldName )
245 mBookmarks[ i ].setGroup( newName );
271 if ( b.group() == group )
281 QDomElement bookmarksElem = element;
282 if ( element.tagName() != QStringLiteral(
"Bookmarks" ) )
284 bookmarksElem = element.firstChildElement( QStringLiteral(
"Bookmarks" ) );
287 if ( mProject && bookmarksElem.isNull() )
290 const int count = mProject->
readNumEntry( QStringLiteral(
"Bookmarks" ), QStringLiteral(
"/count" ) );
291 for (
int i = 0; i < count; ++i )
293 const double minX = mProject->
readDoubleEntry( QStringLiteral(
"Bookmarks" ), QStringLiteral(
"/Row-%1/MinX" ).arg( i ) );
294 const double minY = mProject->
readDoubleEntry( QStringLiteral(
"Bookmarks" ), QStringLiteral(
"/Row-%1/MinY" ).arg( i ) );
295 const double maxX = mProject->
readDoubleEntry( QStringLiteral(
"Bookmarks" ), QStringLiteral(
"/Row-%1/MaxX" ).arg( i ) );
296 const double maxY = mProject->
readDoubleEntry( QStringLiteral(
"Bookmarks" ), QStringLiteral(
"/Row-%1/MaxY" ).arg( i ) );
297 const long srid = mProject->
readNumEntry( QStringLiteral(
"Bookmarks" ), QStringLiteral(
"/Row-%1/SRID" ).arg( i ) );
299 b.
setId( QStringLiteral(
"bookmark_%1" ).arg( i ) );
300 b.
setName( mProject->
readEntry( QStringLiteral(
"Bookmarks" ), QStringLiteral(
"/Row-%1/Name" ).arg( i ) ) );
305 result = added && result;
311 QDomNodeList bookmarkNodes = element.elementsByTagName( QStringLiteral(
"Bookmark" ) );
312 for (
int i = 0; i < bookmarkNodes.size(); ++i )
317 result = added && result;
325 QDomElement bookmarksElem = doc.createElement( QStringLiteral(
"Bookmarks" ) );
329 QDomElement bookmarkElem = b.writeXml( doc );
330 bookmarksElem.appendChild( bookmarkElem );
332 return bookmarksElem;
338 if ( b.
id().isEmpty() )
350 QDomDocument doc( QStringLiteral(
"qgis_bookmarks" ) );
351 QDomElement root = doc.createElement( QStringLiteral(
"qgis_bookmarks" ) );
352 doc.appendChild( root );
354 QList<QString> headerList;
356 << QStringLiteral(
"project" )
357 << QStringLiteral(
"xmin" )
358 << QStringLiteral(
"ymin" )
359 << QStringLiteral(
"xmax" )
360 << QStringLiteral(
"ymax" )
361 << QStringLiteral(
"sr_id" );
365 const QList< QgsBookmark >
bookmarks = manager->bookmarks();
368 if ( !group.isEmpty() && b.group() != group )
371 QDomElement bookmark = doc.createElement( QStringLiteral(
"bookmark" ) );
372 root.appendChild( bookmark );
374 QDomElement
id = doc.createElement( QStringLiteral(
"id" ) );
375 id.appendChild( doc.createTextNode( b.id() ) );
376 bookmark.appendChild(
id );
378 QDomElement name = doc.createElement( QStringLiteral(
"name" ) );
379 name.appendChild( doc.createTextNode( b.name() ) );
380 bookmark.appendChild( name );
382 QDomElement group = doc.createElement( QStringLiteral(
"project" ) );
383 group.appendChild( doc.createTextNode( b.group() ) );
384 bookmark.appendChild( group );
386 QDomElement xMin = doc.createElement( QStringLiteral(
"xmin" ) );
387 xMin.appendChild( doc.createTextNode(
qgsDoubleToString( b.extent().xMinimum() ) ) );
388 bookmark.appendChild( xMin );
389 QDomElement yMin = doc.createElement( QStringLiteral(
"ymin" ) );
390 yMin.appendChild( doc.createTextNode(
qgsDoubleToString( b.extent().yMinimum() ) ) );
391 bookmark.appendChild( yMin );
392 QDomElement xMax = doc.createElement( QStringLiteral(
"xmax" ) );
393 xMax.appendChild( doc.createTextNode(
qgsDoubleToString( b.extent().xMaximum() ) ) );
394 bookmark.appendChild( xMax );
395 QDomElement yMax = doc.createElement( QStringLiteral(
"ymax" ) );
396 yMax.appendChild( doc.createTextNode(
qgsDoubleToString( b.extent().yMaximum() ) ) );
397 bookmark.appendChild( yMax );
399 QDomElement
crs = doc.createElement( QStringLiteral(
"sr_id" ) );
400 crs.appendChild( doc.createTextNode( QString::number( b.extent().crs().srsid() ) ) );
401 bookmark.appendChild( crs );
406 if ( !f.open( QFile::WriteOnly | QIODevice::Truncate ) )
412 QTextStream out( &f );
413 out.setCodec(
"UTF-8" );
422 if ( path.isEmpty() )
428 if ( !f.open( QIODevice::ReadOnly | QIODevice::Text ) )
434 if ( !doc.setContent( &f ) )
440 QDomElement docElem = doc.documentElement();
441 QDomNodeList nodeList = docElem.elementsByTagName( QStringLiteral(
"bookmark" ) );
444 for (
int i = 0; i < nodeList.count(); i++ )
446 QDomNode bookmark = nodeList.at( i );
447 QDomElement name = bookmark.firstChildElement( QStringLiteral(
"name" ) );
448 QDomElement prjname = bookmark.firstChildElement( QStringLiteral(
"project" ) );
449 QDomElement xmin = bookmark.firstChildElement( QStringLiteral(
"xmin" ) );
450 QDomElement ymin = bookmark.firstChildElement( QStringLiteral(
"ymin" ) );
451 QDomElement xmax = bookmark.firstChildElement( QStringLiteral(
"xmax" ) );
452 QDomElement ymax = bookmark.firstChildElement( QStringLiteral(
"ymax" ) );
453 QDomElement srid = bookmark.firstChildElement( QStringLiteral(
"sr_id" ) );
462 ymin.text().toDouble(),
463 xmax.text().toDouble(),
464 ymax.text().toDouble() ), crs ) );
472 void QgsBookmarkManager::store()
474 if ( !mFilePath.isEmpty() )
476 QFile f( mFilePath );
477 if ( !f.open( QFile::WriteOnly | QIODevice::Truncate ) )
485 doc.appendChild( elem );
487 QTextStream out( &f );
488 out.setCodec(
"UTF-8" );
499 mFilePath = filePath;
504 if ( !QFileInfo::exists( mFilePath ) )
509 if ( result != SQLITE_OK )
514 sqlite3_statement_unique_ptr preparedStatement = database.
prepare( QStringLiteral(
"SELECT name,project_name,xmin,ymin,xmax,ymax,projection_srid FROM tbl_bookmarks" ), result );
515 if ( result == SQLITE_OK )
517 while ( preparedStatement.
step() == SQLITE_ROW )
519 const QString name = preparedStatement.
columnAsText( 0 );
520 const QString group = preparedStatement.
columnAsText( 1 );
538 QFile f( mFilePath );
539 if ( !f.open( QIODevice::ReadOnly | QIODevice::Text ) )
545 if ( !doc.setContent( &f ) )
551 QDomElement elem = doc.documentElement();
void setDirty(bool b=true)
Flag the project as dirty (modified).
bool updateBookmark(const QgsBookmark &bookmark)
Updates the definition of a bookmark in the manager.
A rectangle specified with double values.
QgsBookmark bookmarkById(const QString &id) const
Returns the bookmark with a matching id, or an empty bookmark if no matching bookmarks were found...
QString readEntry(const QString &scope, const QString &key, const QString &def=QString(), bool *ok=nullptr) const
static QString qgisUserDatabaseFilePath()
Returns the path to the user qgis.db file.
Unique pointer for sqlite3 prepared statements, which automatically finalizes the statement when the ...
static QgsRectangle fromWkt(const QString &wkt)
Creates a new rectangle from a wkt string.
QList< QgsBookmark > bookmarksByGroup(const QString &group)
Returns a list of bookmark with a matching group, or an empty list if no matching bookmarks were foun...
QStringList groups() const
Returns a list of all bookmark groups contained in the manager.
Manages storage of a set of bookmarks.
void setName(const QString &name)
Sets the bookmark's name, which is a user-visible string identifying the bookmark.
bool moveBookmark(const QString &id, QgsBookmarkManager *destination)
Moves the bookmark with matching id from this manager to a destination manager.
const QgsCoordinateReferenceSystem & crs
void bookmarkAboutToBeRemoved(const QString &id)
Emitted when a bookmark is about to be removed from the manager.
QString group() const
Returns the bookmark's group, which is a user-visible string identifying the bookmark's category...
static bool exportToFile(const QString &path, const QList< const QgsBookmarkManager *> &managers, const QString &group=QString())
Exports all bookmarks from a list of managers to an xml file at the specified path.
static QgsBookmarkManager * createProjectBasedManager(QgsProject *project)
Returns a newly created QgsBookmarkManager using a project-based bookmark store, linked to the specif...
int readNumEntry(const QString &scope, const QString &key, int def=0, bool *ok=nullptr) const
QString asWktPolygon() const
Returns a string representation of the rectangle as a WKT Polygon.
QString id() const
Returns the bookmark's unique ID.
QgsCoordinateReferenceSystem crs() const
Returns the associated coordinate reference system, or an invalid CRS if no reference system is set...
void bookmarkAdded(const QString &id)
Emitted when a bookmark has been added to the manager.
~QgsBookmarkManager() override
void setGroup(const QString &group)
Sets the bookmark's group, which is a user-visible string identifying the bookmark's category...
bool readXml(const QDomElement &element, const QDomDocument &doc)
Reads the manager's state from a DOM element, restoring all bookmarks present in the XML document...
int step()
Steps to the next record in the statement, returning the sqlite3 result code.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
void bookmarkAboutToBeAdded(const QString &id)
Emitted when a bookmark is about to be added to the manager.
QString name() const
Returns the bookmark's name, which is a user-visible string identifying the bookmark.
bool operator!=(const QgsBookmark &other)
QString addBookmark(const QgsBookmark &bookmark, bool *ok=nullptr)
Adds a bookmark to the manager.
QString columnAsText(int column) const
Returns the column value from the current statement row as a string.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
A QgsRectangle with associated coordinate reference system.
void bookmarkChanged(const QString &id)
Emitted when a bookmark is changed.
sqlite3_statement_unique_ptr prepare(const QString &sql, int &resultCode) const
Prepares a sql statement, returning the result.
void clear()
Removes and deletes all bookmarks from the manager.
QList< QgsBookmark > bookmarks() const
Returns a list of all bookmarks contained in the manager.
static QgsBookmark fromXml(const QDomElement &element, const QDomDocument &doc)
Creates a bookmark using the properties from a DOM element.
int open(const QString &path)
Opens the database at the specified file path.
QgsBookmarkManager(QObject *parent=nullptr)
Constructor for QgsBookmarkManager, with the specified parent object.
Unique pointer for sqlite3 databases, which automatically closes the database when the pointer goes o...
bool operator==(const QgsBookmark &other)
bool createFromSrsId(long srsId)
Sets this CRS by lookup of internal QGIS CRS ID in the CRS database.
QgsReferencedRectangle extent() const
Returns the bookmark's spatial extent.
void bookmarkRemoved(const QString &id)
Emitted when a bookmark was removed from the manager.
void renameGroup(const QString &oldName, const QString &newName)
Renames an existing group from oldName to newName.
This class represents a coordinate reference system (CRS).
bool readXml(const QDomNode &node)
Restores state from the given DOM node.
bool importFromFile(const QString &path)
Imports the bookmarks from an xml file at the specified path.
double readDoubleEntry(const QString &scope, const QString &key, double def=0, bool *ok=nullptr) const
void setId(const QString &id)
Sets the bookmark's unique id.
bool removeBookmark(const QString &id)
Removes the bookmark with matching id from the manager.
QDomElement writeXml(QDomDocument &doc) const
Returns a DOM element representing the state of the manager.
bool writeXml(QDomNode &node, QDomDocument &doc) const
Stores state to the given Dom node in the given document.
double columnAsDouble(int column) const
Gets column value from the current statement row as a double.
qlonglong columnAsInt64(int column) const
Gets column value from the current statement row as a long long integer (64 bits).
QDomElement writeXml(QDomDocument &doc) const
Returns a DOM element representing the bookmark's properties.
void initialize(const QString &filePath)
initialize
Represents a spatial bookmark, with a name, CRS and extent.
void setExtent(const QgsReferencedRectangle &extent)
Sets the bookmark's spatial extent.
static QgsCoordinateReferenceSystem fromSrsId(long srsId)
Creates a CRS from a specified QGIS SRS ID.