29#include "moc_qgsbookmarkmanager.cpp"
31using namespace Qt::StringLiterals;
50 b.
setId( element.attribute( u
"id"_s ) );
51 b.
setName( element.attribute( u
"name"_s ) );
52 b.
setGroup( element.attribute( u
"group"_s ) );
54 b.
setRotation( element.attribute( u
"rotation"_s ).toDouble() );
63 QDomElement bookmarkElem = doc.createElement( u
"Bookmark"_s );
64 bookmarkElem.setAttribute( u
"id"_s, mId );
65 bookmarkElem.setAttribute( u
"name"_s, mName );
66 bookmarkElem.setAttribute( u
"group"_s, mGroup );
67 bookmarkElem.setAttribute( u
"extent"_s, mExtent.asWktPolygon() );
68 bookmarkElem.setAttribute( u
"rotation"_s, mRotation );
69 mExtent.crs().
writeXml( bookmarkElem, doc );
75 return mId == other.mId && mName == other.mName && mExtent == other.mExtent && mGroup == other.mGroup &&
qgsDoubleNear( mRotation, other.mRotation );
80 return !( *
this == other );
131 res->mProject = project;
152 if ( bookmark.
id().isEmpty() )
153 bookmark.
setId( QUuid::createUuid().toString() );
157 for (
const QgsBookmark &b : std::as_const( mBookmarks ) )
159 if ( b.
id() == bookmark.
id() )
170 mBookmarks << bookmark;
171 if ( !mGroups.contains( bookmark.
group() ) )
172 mGroups << bookmark.
group();
176 mProject->setDirty(
true );
179 return bookmark.
id();
190 for (
const QgsBookmark &b : std::as_const( mBookmarks ) )
205 mBookmarks.removeAt( pos );
207 mGroups.removeOne( group );
211 mProject->setDirty(
true );
221 for (
const QgsBookmark &b : std::as_const( mBookmarks ) )
223 if ( b.id() == bookmark.
id() )
225 if ( mBookmarks[i].group() != bookmark.
group() )
228 mGroups.removeOne( mBookmarks[i].group() );
229 if ( !mGroups.contains( bookmark.
group() ) )
230 mGroups << bookmark.
group();
232 mBookmarks[i] = bookmark;
236 mProject->setDirty(
true );
248 const QList< QgsBookmark >
bookmarks = mBookmarks;
262 for (
int i = 0; i < mBookmarks.count(); ++i )
264 if ( mBookmarks.at( i ).group() == oldName )
266 mBookmarks[i].setGroup( newName );
292 if ( b.group() == group )
302 QDomElement bookmarksElem = element;
303 if ( element.tagName() !=
"Bookmarks"_L1 )
305 bookmarksElem = element.firstChildElement( u
"Bookmarks"_s );
308 if ( mProject && bookmarksElem.isNull() )
311 const int count = mProject->readNumEntry( u
"Bookmarks"_s, u
"/count"_s );
312 for (
int i = 0; i < count; ++i )
314 const double minX = mProject->readDoubleEntry( u
"Bookmarks"_s, u
"/Row-%1/MinX"_s.arg( i ) );
315 const double minY = mProject->readDoubleEntry( u
"Bookmarks"_s, u
"/Row-%1/MinY"_s.arg( i ) );
316 const double maxX = mProject->readDoubleEntry( u
"Bookmarks"_s, u
"/Row-%1/MaxX"_s.arg( i ) );
317 const double maxY = mProject->readDoubleEntry( u
"Bookmarks"_s, u
"/Row-%1/MaxY"_s.arg( i ) );
318 const long srid = mProject->readNumEntry( u
"Bookmarks"_s, u
"/Row-%1/SRID"_s.arg( i ) );
320 b.
setId( u
"bookmark_%1"_s.arg( i ) );
321 b.
setName( mProject->readEntry( u
"Bookmarks"_s, u
"/Row-%1/Name"_s.arg( i ) ) );
326 result = added && result;
332 QDomNodeList bookmarkNodes = element.elementsByTagName( u
"Bookmark"_s );
333 for (
int i = 0; i < bookmarkNodes.size(); ++i )
338 result = added && result;
346 QDomElement bookmarksElem = doc.createElement( u
"Bookmarks"_s );
350 QDomElement bookmarkElem = b.writeXml( doc );
351 bookmarksElem.appendChild( bookmarkElem );
353 return bookmarksElem;
359 if ( b.
id().isEmpty() )
371 QDomDocument doc( u
"qgis_bookmarks"_s );
372 QDomElement root = doc.createElement( u
"qgis_bookmarks"_s );
373 doc.appendChild( root );
375 QList<QString> headerList;
376 headerList << u
"project"_s << u
"xmin"_s << u
"ymin"_s << u
"xmax"_s << u
"ymax"_s << u
"rotation"_s << u
"sr_id"_s;
380 const QList< QgsBookmark >
bookmarks = manager->bookmarks();
383 if ( !group.isEmpty() && b.group() != group )
386 QDomElement bookmark = doc.createElement( u
"bookmark"_s );
387 root.appendChild( bookmark );
389 QDomElement
id = doc.createElement( u
"id"_s );
390 id.appendChild( doc.createTextNode( b.id() ) );
391 bookmark.appendChild(
id );
393 QDomElement name = doc.createElement( u
"name"_s );
394 name.appendChild( doc.createTextNode( b.name() ) );
395 bookmark.appendChild( name );
397 QDomElement group = doc.createElement( u
"project"_s );
398 group.appendChild( doc.createTextNode( b.group() ) );
399 bookmark.appendChild( group );
401 QDomElement xMin = doc.createElement( u
"xmin"_s );
402 xMin.appendChild( doc.createTextNode(
qgsDoubleToString( b.extent().xMinimum() ) ) );
403 bookmark.appendChild( xMin );
404 QDomElement yMin = doc.createElement( u
"ymin"_s );
405 yMin.appendChild( doc.createTextNode(
qgsDoubleToString( b.extent().yMinimum() ) ) );
406 bookmark.appendChild( yMin );
407 QDomElement xMax = doc.createElement( u
"xmax"_s );
408 xMax.appendChild( doc.createTextNode(
qgsDoubleToString( b.extent().xMaximum() ) ) );
409 bookmark.appendChild( xMax );
410 QDomElement yMax = doc.createElement( u
"ymax"_s );
411 yMax.appendChild( doc.createTextNode(
qgsDoubleToString( b.extent().yMaximum() ) ) );
412 bookmark.appendChild( yMax );
414 QDomElement rotation = doc.createElement( u
"rotation"_s );
416 bookmark.appendChild( rotation );
418 QDomElement crs = doc.createElement( u
"sr_id"_s );
419 crs.appendChild( doc.createTextNode( QString::number( b.extent().crs().srsid() ) ) );
420 bookmark.appendChild( crs );
425 if ( !f.open( QFile::WriteOnly | QIODevice::Truncate ) )
431 QTextStream out( &f );
440 if ( path.isEmpty() )
446 if ( !f.open( QIODevice::ReadOnly | QIODevice::Text ) )
452 if ( !doc.setContent( &f ) )
458 QDomElement docElem = doc.documentElement();
459 QDomNodeList nodeList = docElem.elementsByTagName( u
"bookmark"_s );
462 for (
int i = 0; i < nodeList.count(); i++ )
464 QDomNode bookmark = nodeList.at( i );
465 QDomElement name = bookmark.firstChildElement( u
"name"_s );
466 QDomElement prjname = bookmark.firstChildElement( u
"project"_s );
467 QDomElement xmin = bookmark.firstChildElement( u
"xmin"_s );
468 QDomElement ymin = bookmark.firstChildElement( u
"ymin"_s );
469 QDomElement xmax = bookmark.firstChildElement( u
"xmax"_s );
470 QDomElement ymax = bookmark.firstChildElement( u
"ymax"_s );
471 QDomElement rotation = bookmark.firstChildElement( u
"rotation"_s );
472 QDomElement srid = bookmark.firstChildElement( u
"sr_id"_s );
489void QgsBookmarkManager::store()
491 if ( !mFilePath.isEmpty() )
493 QFile f( mFilePath );
494 if ( !f.open( QFile::WriteOnly | QIODevice::Truncate ) )
502 doc.appendChild( elem );
504 QTextStream out( &f );
515 mFilePath = filePath;
520 if ( !QFileInfo::exists( mFilePath ) )
525 if ( result != SQLITE_OK )
531 if ( result == SQLITE_OK )
533 while ( preparedStatement.
step() == SQLITE_ROW )
535 const QString name = preparedStatement.
columnAsText( 0 );
553 QFile f( mFilePath );
554 if ( !f.open( QIODevice::ReadOnly | QIODevice::Text ) )
560 if ( !doc.setContent( &f ) )
566 QDomElement elem = doc.documentElement();
static QString qgisUserDatabaseFilePath()
Returns the path to the user qgis.db file.
void renameGroup(const QString &oldName, const QString &newName)
Renames an existing group from oldName to newName.
bool removeBookmark(const QString &id)
Removes the bookmark with matching id from the manager.
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...
void initialize(const QString &filePath)
Initializes the bookmark manager.
void bookmarkAboutToBeRemoved(const QString &id)
Emitted when a bookmark is about to be removed from the manager.
~QgsBookmarkManager() override
void bookmarkChanged(const QString &id)
Emitted when a bookmark is changed.
static QgsBookmarkManager * createProjectBasedManager(QgsProject *project)
Returns a newly created QgsBookmarkManager using a project-based bookmark store, linked to the specif...
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.
void clear()
Removes and deletes all bookmarks from the manager.
void bookmarkAdded(const QString &id)
Emitted when a bookmark has been added to the manager.
bool updateBookmark(const QgsBookmark &bookmark)
Updates the definition of a bookmark in the manager.
void bookmarkAboutToBeAdded(const QString &id)
Emitted when a bookmark is about to be added to the manager.
bool moveBookmark(const QString &id, QgsBookmarkManager *destination)
Moves the bookmark with matching id from this manager to a destination manager.
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.
QgsBookmark bookmarkById(const QString &id) const
Returns the bookmark with a matching id, or an empty bookmark if no matching bookmarks were found.
QStringList groups() const
Returns a list of all bookmark groups contained in the manager.
QString addBookmark(const QgsBookmark &bookmark, bool *ok=nullptr)
Adds a bookmark to the manager.
QDomElement writeXml(QDomDocument &doc) const
Returns a DOM element representing the state of the manager.
QgsBookmarkManager(QObject *parent=nullptr)
Constructor for QgsBookmarkManager, with the specified parent object.
QList< QgsBookmark > bookmarks() const
Returns a list of all bookmarks contained in the manager.
void bookmarkRemoved(const QString &id)
Emitted when a bookmark was removed from the manager.
bool importFromFile(const QString &path)
Imports the bookmarks from an xml file at the specified path.
Represents a spatial bookmark, with a name, CRS and extent.
static QgsBookmark fromXml(const QDomElement &element, const QDomDocument &doc)
Creates a bookmark using the properties from a DOM element.
void setGroup(const QString &group)
Sets the bookmark's group, which is a user-visible string identifying the bookmark's category.
bool operator!=(const QgsBookmark &other) const
void setRotation(double rotation)
Sets the bookmark's spatial map rotation.
QgsBookmark()=default
Default constructor, creates an empty bookmark.
QString id() const
Returns the bookmark's unique ID.
QgsReferencedRectangle extent() const
Returns the bookmark's spatial extent.
void setExtent(const QgsReferencedRectangle &extent)
Sets the bookmark's spatial extent.
double rotation() const
Returns the bookmark's map rotation.
void setId(const QString &id)
Sets the bookmark's unique id.
QDomElement writeXml(QDomDocument &doc) const
Returns a DOM element representing the bookmark's properties.
bool operator==(const QgsBookmark &other) const
QString group() const
Returns the bookmark's group, which is a user-visible string identifying the bookmark's category.
void setName(const QString &name)
Sets the bookmark's name, which is a user-visible string identifying the bookmark.
QString name() const
Returns the bookmark's name, which is a user-visible string identifying the bookmark.
Represents a coordinate reference system (CRS).
bool readXml(const QDomNode &node)
Restores state from the given DOM node.
bool createFromSrsId(long srsId)
Sets this CRS by lookup of internal QGIS CRS ID in the CRS database.
static QgsCoordinateReferenceSystem fromSrsId(long srsId)
Creates a CRS from a specified QGIS SRS ID.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
A rectangle specified with double values.
static QgsRectangle fromWkt(const QString &wkt)
Creates a new rectangle from a wkt string.
A QgsRectangle with associated coordinate reference system.
Unique pointer for sqlite3 databases, which automatically closes the database when the pointer goes o...
sqlite3_statement_unique_ptr prepare(const QString &sql, int &resultCode) const
Prepares a sql statement, returning the result.
int open(const QString &path)
Opens the database at the specified file path.
Unique pointer for sqlite3 prepared statements, which automatically finalizes the statement when the ...
QString columnAsText(int column) const
Returns the column value from the current statement row as a string.
double columnAsDouble(int column) const
Gets column value from the current statement row as a double.
int step()
Steps to the next record in the statement, returning the sqlite3 result code.
qlonglong columnAsInt64(int column) const
Gets column value from the current statement row as a long long integer (64 bits).
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).