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
76 && mName == other.mName
77 && mExtent == other.mExtent
78 && mGroup == other.mGroup
84 return !( *
this == other );
135 res->mProject = project;
156 if ( bookmark.
id().isEmpty() )
157 bookmark.
setId( QUuid::createUuid().toString() );
161 for (
const QgsBookmark &b : std::as_const( mBookmarks ) )
163 if ( b.
id() == bookmark.
id() )
174 mBookmarks << bookmark;
175 if ( !mGroups.contains( bookmark.
group() ) )
176 mGroups << bookmark.
group();
180 mProject->setDirty(
true );
183 return bookmark.
id();
194 for (
const QgsBookmark &b : std::as_const( mBookmarks ) )
209 mBookmarks.removeAt( pos );
211 mGroups.removeOne( group );
215 mProject->setDirty(
true );
225 for (
const QgsBookmark &b : std::as_const( mBookmarks ) )
227 if ( b.id() == bookmark.
id() )
229 if ( mBookmarks[i].group() != bookmark.
group() )
232 mGroups.removeOne( mBookmarks[i].group() );
233 if ( !mGroups.contains( bookmark.
group() ) )
234 mGroups << bookmark.
group();
236 mBookmarks[i] = bookmark;
240 mProject->setDirty(
true );
252 const QList< QgsBookmark >
bookmarks = mBookmarks;
266 for (
int i = 0; i < mBookmarks.count(); ++i )
268 if ( mBookmarks.at( i ).group() == oldName )
270 mBookmarks[ i ].setGroup( newName );
296 if ( b.group() == group )
306 QDomElement bookmarksElem = element;
307 if ( element.tagName() !=
"Bookmarks"_L1 )
309 bookmarksElem = element.firstChildElement( u
"Bookmarks"_s );
312 if ( mProject && bookmarksElem.isNull() )
315 const int count = mProject->readNumEntry( u
"Bookmarks"_s, u
"/count"_s );
316 for (
int i = 0; i < count; ++i )
318 const double minX = mProject->readDoubleEntry( u
"Bookmarks"_s, u
"/Row-%1/MinX"_s.arg( i ) );
319 const double minY = mProject->readDoubleEntry( u
"Bookmarks"_s, u
"/Row-%1/MinY"_s.arg( i ) );
320 const double maxX = mProject->readDoubleEntry( u
"Bookmarks"_s, u
"/Row-%1/MaxX"_s.arg( i ) );
321 const double maxY = mProject->readDoubleEntry( u
"Bookmarks"_s, u
"/Row-%1/MaxY"_s.arg( i ) );
322 const long srid = mProject->readNumEntry( u
"Bookmarks"_s, u
"/Row-%1/SRID"_s.arg( i ) );
324 b.
setId( u
"bookmark_%1"_s.arg( i ) );
325 b.
setName( mProject->readEntry( u
"Bookmarks"_s, u
"/Row-%1/Name"_s.arg( i ) ) );
330 result = added && result;
336 QDomNodeList bookmarkNodes = element.elementsByTagName( u
"Bookmark"_s );
337 for (
int i = 0; i < bookmarkNodes.size(); ++i )
342 result = added && result;
350 QDomElement bookmarksElem = doc.createElement( u
"Bookmarks"_s );
354 QDomElement bookmarkElem = b.writeXml( doc );
355 bookmarksElem.appendChild( bookmarkElem );
357 return bookmarksElem;
363 if ( b.
id().isEmpty() )
375 QDomDocument doc( u
"qgis_bookmarks"_s );
376 QDomElement root = doc.createElement( u
"qgis_bookmarks"_s );
377 doc.appendChild( root );
379 QList<QString> headerList;
391 const QList< QgsBookmark >
bookmarks = manager->bookmarks();
394 if ( !group.isEmpty() && b.group() != group )
397 QDomElement bookmark = doc.createElement( u
"bookmark"_s );
398 root.appendChild( bookmark );
400 QDomElement
id = doc.createElement( u
"id"_s );
401 id.appendChild( doc.createTextNode( b.id() ) );
402 bookmark.appendChild(
id );
404 QDomElement name = doc.createElement( u
"name"_s );
405 name.appendChild( doc.createTextNode( b.name() ) );
406 bookmark.appendChild( name );
408 QDomElement group = doc.createElement( u
"project"_s );
409 group.appendChild( doc.createTextNode( b.group() ) );
410 bookmark.appendChild( group );
412 QDomElement xMin = doc.createElement( u
"xmin"_s );
413 xMin.appendChild( doc.createTextNode(
qgsDoubleToString( b.extent().xMinimum() ) ) );
414 bookmark.appendChild( xMin );
415 QDomElement yMin = doc.createElement( u
"ymin"_s );
416 yMin.appendChild( doc.createTextNode(
qgsDoubleToString( b.extent().yMinimum() ) ) );
417 bookmark.appendChild( yMin );
418 QDomElement xMax = doc.createElement( u
"xmax"_s );
419 xMax.appendChild( doc.createTextNode(
qgsDoubleToString( b.extent().xMaximum() ) ) );
420 bookmark.appendChild( xMax );
421 QDomElement yMax = doc.createElement( u
"ymax"_s );
422 yMax.appendChild( doc.createTextNode(
qgsDoubleToString( b.extent().yMaximum() ) ) );
423 bookmark.appendChild( yMax );
425 QDomElement rotation = doc.createElement( u
"rotation"_s );
427 bookmark.appendChild( rotation );
429 QDomElement crs = doc.createElement( u
"sr_id"_s );
430 crs.appendChild( doc.createTextNode( QString::number( b.extent().crs().srsid() ) ) );
431 bookmark.appendChild( crs );
436 if ( !f.open( QFile::WriteOnly | QIODevice::Truncate ) )
442 QTextStream out( &f );
451 if ( path.isEmpty() )
457 if ( !f.open( QIODevice::ReadOnly | QIODevice::Text ) )
463 if ( !doc.setContent( &f ) )
469 QDomElement docElem = doc.documentElement();
470 QDomNodeList nodeList = docElem.elementsByTagName( u
"bookmark"_s );
473 for (
int i = 0; i < nodeList.count(); i++ )
475 QDomNode bookmark = nodeList.at( i );
476 QDomElement name = bookmark.firstChildElement( u
"name"_s );
477 QDomElement prjname = bookmark.firstChildElement( u
"project"_s );
478 QDomElement xmin = bookmark.firstChildElement( u
"xmin"_s );
479 QDomElement ymin = bookmark.firstChildElement( u
"ymin"_s );
480 QDomElement xmax = bookmark.firstChildElement( u
"xmax"_s );
481 QDomElement ymax = bookmark.firstChildElement( u
"ymax"_s );
482 QDomElement rotation = bookmark.firstChildElement( u
"rotation"_s );
483 QDomElement srid = bookmark.firstChildElement( u
"sr_id"_s );
492 ymin.text().toDouble(),
493 xmax.text().toDouble(),
494 ymax.text().toDouble() ), crs ) );
503void QgsBookmarkManager::store()
505 if ( !mFilePath.isEmpty() )
507 QFile f( mFilePath );
508 if ( !f.open( QFile::WriteOnly | QIODevice::Truncate ) )
516 doc.appendChild( elem );
518 QTextStream out( &f );
529 mFilePath = filePath;
534 if ( !QFileInfo::exists( mFilePath ) )
539 if ( result != SQLITE_OK )
545 if ( result == SQLITE_OK )
547 while ( preparedStatement.
step() == SQLITE_ROW )
549 const QString name = preparedStatement.
columnAsText( 0 );
567 QFile f( mFilePath );
568 if ( !f.open( QIODevice::ReadOnly | QIODevice::Text ) )
574 if ( !doc.setContent( &f ) )
580 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).