mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-13 00:03:09 -04:00
Allow individual groups of bookmarks to be exported
Also avoids the unfortunate situation where the first item shown in the right click menu for a bookmark group is the "delete" action
This commit is contained in:
parent
a426fdaea2
commit
b533fbaeb2
@ -258,10 +258,12 @@ Moves the bookmark with matching ``id`` from this manager to a ``destination`` m
|
||||
Returns ``True`` if the bookmark was successfully moved.
|
||||
%End
|
||||
|
||||
static bool exportToFile( const QString &path, const QList<const QgsBookmarkManager *> &managers );
|
||||
static bool exportToFile( const QString &path, const QList<const QgsBookmarkManager *> &managers, const QString &group = QString() );
|
||||
%Docstring
|
||||
Exports all bookmarks from a list of ``managers`` to an xml file at the specified ``path``.
|
||||
|
||||
If ``group`` is set then only bookmarks from the matching group will be exported.
|
||||
|
||||
Returns ``True`` if the export was successful.
|
||||
|
||||
.. seealso:: :py:func:`importFromFile`
|
||||
|
@ -979,13 +979,13 @@ void QgsBookmarksItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu
|
||||
} );
|
||||
menu->addAction( showBookmarksPanel );
|
||||
menu->addSeparator();
|
||||
QAction *importBookmarks = new QAction( tr( "Import Spatial Bookmarks…" ), menu );
|
||||
QAction *importBookmarks = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionSharingImport.svg" ) ), tr( "Import Spatial Bookmarks…" ), menu );
|
||||
connect( importBookmarks, &QAction::triggered, this, [ = ]
|
||||
{
|
||||
importBookmarksToManager( QgsApplication::bookmarkManager(), context.messageBar() );
|
||||
} );
|
||||
menu->addAction( importBookmarks );
|
||||
QAction *exportBookmarks = new QAction( tr( "Export Spatial Bookmarks…" ), menu );
|
||||
QAction *exportBookmarks = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionSharingExport.svg" ) ), tr( "Export Spatial Bookmarks…" ), menu );
|
||||
connect( exportBookmarks, &QAction::triggered, this, [ = ]
|
||||
{
|
||||
exportBookmarksFromManagers( QList< const QgsBookmarkManager * >() << QgsApplication::bookmarkManager() << QgsProject::instance()->bookmarkManager(), context.messageBar() );
|
||||
@ -1002,14 +1002,14 @@ void QgsBookmarksItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu
|
||||
} );
|
||||
menu->addAction( addBookmark );
|
||||
menu->addSeparator();
|
||||
QAction *importBookmarks = new QAction( tr( "Import Spatial Bookmarks…" ), menu );
|
||||
QAction *importBookmarks = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionSharingImport.svg" ) ), tr( "Import Spatial Bookmarks…" ), menu );
|
||||
connect( importBookmarks, &QAction::triggered, this, [ = ]
|
||||
{
|
||||
importBookmarksToManager( managerItem->manager(), context.messageBar() );
|
||||
} );
|
||||
menu->addAction( importBookmarks );
|
||||
|
||||
QAction *exportBookmarks = new QAction( tr( "Export Spatial Bookmarks…" ), menu );
|
||||
QAction *exportBookmarks = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionSharingExport.svg" ) ), tr( "Export Spatial Bookmarks…" ), menu );
|
||||
connect( exportBookmarks, &QAction::triggered, this, [ = ]
|
||||
{
|
||||
exportBookmarksFromManagers( QList< const QgsBookmarkManager * >() << managerItem->manager(), context.messageBar() );
|
||||
@ -1097,6 +1097,14 @@ void QgsBookmarksItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu
|
||||
}
|
||||
}
|
||||
|
||||
QAction *exportBookmarks = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionSharingExport.svg" ) ), tr( "Export Spatial Bookmarks…" ), menu );
|
||||
connect( exportBookmarks, &QAction::triggered, this, [ = ]
|
||||
{
|
||||
exportBookmarksFromManagers( QList< const QgsBookmarkManager * >() << groupItem->manager(), context.messageBar(), groupItem->group() );
|
||||
} );
|
||||
menu->addAction( exportBookmarks );
|
||||
menu->addSeparator();
|
||||
|
||||
QAction *actionDelete = new QAction( selectedItems.count() == 1 ? tr( "Delete Bookmark Group" ) : tr( "Delete Bookmark Groups" ), menu );
|
||||
connect( actionDelete, &QAction::triggered, this, [selectedItems, groups, manager]
|
||||
{
|
||||
@ -1181,7 +1189,7 @@ bool QgsBookmarksItemGuiProvider::rename( QgsDataItem *item, const QString &name
|
||||
return false;
|
||||
}
|
||||
|
||||
void QgsBookmarksItemGuiProvider::exportBookmarksFromManagers( const QList<const QgsBookmarkManager *> &managers, QgsMessageBar *messageBar )
|
||||
void QgsBookmarksItemGuiProvider::exportBookmarksFromManagers( const QList<const QgsBookmarkManager *> &managers, QgsMessageBar *messageBar, const QString &group )
|
||||
{
|
||||
QgsSettings settings;
|
||||
|
||||
@ -1196,7 +1204,7 @@ void QgsBookmarksItemGuiProvider::exportBookmarksFromManagers( const QList<const
|
||||
// ensure the user never omitted the extension from the file name
|
||||
fileName = QgsFileUtils::ensureFileNameHasExtension( fileName, QStringList() << QStringLiteral( "xml" ) );
|
||||
|
||||
if ( !QgsBookmarkManager::exportToFile( fileName, managers ) )
|
||||
if ( !QgsBookmarkManager::exportToFile( fileName, managers, group ) )
|
||||
{
|
||||
messageBar->pushWarning( tr( "Export Bookmarks" ), tr( "Error exporting bookmark file" ) );
|
||||
}
|
||||
|
@ -270,7 +270,7 @@ class QgsBookmarksItemGuiProvider : public QObject, public QgsDataItemGuiProvide
|
||||
|
||||
private:
|
||||
|
||||
void exportBookmarksFromManagers( const QList< const QgsBookmarkManager * > &managers, QgsMessageBar *messageBar );
|
||||
void exportBookmarksFromManagers( const QList< const QgsBookmarkManager * > &managers, QgsMessageBar *messageBar, const QString &group = QString() );
|
||||
void importBookmarksToManager( QgsBookmarkManager *manager, QgsMessageBar *messageBar );
|
||||
};
|
||||
|
||||
|
@ -344,7 +344,7 @@ bool QgsBookmarkManager::moveBookmark( const QString &id, QgsBookmarkManager *de
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool QgsBookmarkManager::exportToFile( const QString &path, const QList<const QgsBookmarkManager *> &managers )
|
||||
bool QgsBookmarkManager::exportToFile( const QString &path, const QList<const QgsBookmarkManager *> &managers, const QString &group )
|
||||
{
|
||||
// note - we don't use the other writeXml implementation, to maintain older format compatibility
|
||||
QDomDocument doc( QStringLiteral( "qgis_bookmarks" ) );
|
||||
@ -365,6 +365,9 @@ bool QgsBookmarkManager::exportToFile( const QString &path, const QList<const Qg
|
||||
const QList< QgsBookmark > bookmarks = manager->bookmarks();
|
||||
for ( const QgsBookmark &b : bookmarks )
|
||||
{
|
||||
if ( !group.isEmpty() && b.group() != group )
|
||||
continue;
|
||||
|
||||
QDomElement bookmark = doc.createElement( QStringLiteral( "bookmark" ) );
|
||||
root.appendChild( bookmark );
|
||||
|
||||
|
@ -266,11 +266,13 @@ class CORE_EXPORT QgsBookmarkManager : public QObject
|
||||
/**
|
||||
* Exports all bookmarks from a list of \a managers to an xml file at the specified \a path.
|
||||
*
|
||||
* If \a group is set then only bookmarks from the matching group will be exported.
|
||||
*
|
||||
* Returns TRUE if the export was successful.
|
||||
*
|
||||
* \see importFromFile()
|
||||
*/
|
||||
static bool exportToFile( const QString &path, const QList<const QgsBookmarkManager *> &managers );
|
||||
static bool exportToFile( const QString &path, const QList<const QgsBookmarkManager *> &managers, const QString &group = QString() );
|
||||
|
||||
|
||||
/**
|
||||
|
@ -544,6 +544,7 @@ class TestQgsBookmarkManager(unittest.TestCase):
|
||||
b = QgsBookmark()
|
||||
b.setId('1')
|
||||
b.setName('b1')
|
||||
b.setGroup('g1')
|
||||
b.setExtent(QgsReferencedRectangle(QgsRectangle(11, 21, 31, 41), QgsCoordinateReferenceSystem('EPSG:4326')))
|
||||
|
||||
b2 = QgsBookmark()
|
||||
@ -554,6 +555,7 @@ class TestQgsBookmarkManager(unittest.TestCase):
|
||||
b3 = QgsBookmark()
|
||||
b3.setId('3')
|
||||
b3.setName('b3')
|
||||
b3.setGroup('g1')
|
||||
b3.setExtent(QgsReferencedRectangle(QgsRectangle(32, 32, 33, 43), QgsCoordinateReferenceSystem('EPSG:4326')))
|
||||
|
||||
manager.addBookmark(b)
|
||||
@ -571,6 +573,12 @@ class TestQgsBookmarkManager(unittest.TestCase):
|
||||
self.assertTrue(manager3.importFromFile(tmpFile))
|
||||
self.assertEqual([(b.name(), b.extent()) for b in manager3.bookmarks()], [(b.name(), b.extent()) for b in [b, b2, b3]])
|
||||
|
||||
manager3.clear()
|
||||
# restrict to group
|
||||
self.assertTrue(QgsBookmarkManager.exportToFile(tmpFile, [manager, manager2], 'g1'))
|
||||
self.assertTrue(manager3.importFromFile(tmpFile))
|
||||
self.assertEqual([(b.name(), b.extent()) for b in manager3.bookmarks()], [(b.name(), b.extent()) for b in [b, b3]])
|
||||
|
||||
def testRenameGroup(self):
|
||||
"""
|
||||
Test renaming a bookmark group
|
||||
|
Loading…
x
Reference in New Issue
Block a user