Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add a context menu to QgsBookmarks
  • Loading branch information
YoannQDQ authored and nyalldawson committed Mar 29, 2023
1 parent 7ea839b commit 9d3140c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
57 changes: 51 additions & 6 deletions src/app/qgsbookmarks.cpp
Expand Up @@ -49,6 +49,8 @@ QgsBookmarks::QgsBookmarks( QWidget *parent )
QgsGui::enableAutoGeometryRestore( this );

connect( lstBookmarks, &QTreeView::doubleClicked, this, &QgsBookmarks::lstBookmarks_doubleClicked );
lstBookmarks->setContextMenuPolicy( Qt::CustomContextMenu );
connect( lstBookmarks, &QTreeView::customContextMenuRequested, this, &QgsBookmarks::lstBookmarks_customContextMenuRequested );

bookmarksDockContents->layout()->setContentsMargins( 0, 0, 0, 0 );
static_cast< QGridLayout * >( bookmarksDockContents->layout() )->setVerticalSpacing( 0 );
Expand All @@ -60,17 +62,15 @@ QgsBookmarks::QgsBookmarks( QWidget *parent )
btnImpExp->setPopupMode( QToolButton::InstantPopup );

QMenu *share = new QMenu( this );
QAction *btnExport = share->addAction( tr( "&Export" ) );
QAction *btnImport = share->addAction( tr( "&Import" ) );
btnExport->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionSharingExport.svg" ) ) );
btnImport->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionSharingImport.svg" ) ) );
connect( btnExport, &QAction::triggered, this, &QgsBookmarks::exportToXml );
connect( btnImport, &QAction::triggered, this, &QgsBookmarks::importFromXml );
share->addAction( actionExport );
share->addAction( actionImport );
btnImpExp->setMenu( share );

connect( actionAdd, &QAction::triggered, this, &QgsBookmarks::addClicked );
connect( actionDelete, &QAction::triggered, this, &QgsBookmarks::deleteClicked );
connect( actionZoomTo, &QAction::triggered, this, &QgsBookmarks::zoomToBookmark );
connect( actionExport, &QAction::triggered, this, &QgsBookmarks::exportToXml );
connect( actionImport, &QAction::triggered, this, &QgsBookmarks::importFromXml );

mBookmarkToolbar->addWidget( btnImpExp );

Expand Down Expand Up @@ -145,6 +145,51 @@ void QgsBookmarks::lstBookmarks_doubleClicked( const QModelIndex &index )
zoomToBookmark();
}

void QgsBookmarks::lstBookmarks_customContextMenuRequested( QPoint pos )
{
// Get index of item under mouse
QModelIndex index = lstBookmarks->indexAt( pos );
if ( !index.isValid() )
{
// No bookmark under mouse, display generic menu
QMenu menu;
menu.addAction( actionAdd );
menu.addSeparator();
menu.addAction( actionExport );
menu.addAction( actionImport );
menu.exec( lstBookmarks->mapToGlobal( pos ) );
return;
}

// Create the context menu
QMenu menu;

// Add zoom and delete actions
menu.addAction( actionZoomTo );
menu.addAction( actionDelete );

// Get the bookmark
const QString id = lstBookmarks->model()->data( index, QgsBookmarkManagerModel::RoleId ).toString();
QgsBookmark bookmark = QgsApplication::bookmarkManager()->bookmarkById( id );
bool inProject = false;
if ( bookmark.id().isEmpty() )
{
inProject = true;
bookmark = QgsProject::instance()->bookmarkManager()->bookmarkById( id );
}

// Add an edit action (similar to the one in QgsBookmarksItemGuiProvider)
QAction *actionEdit = new QAction( tr( "Edit Spatial Bookmark…" ), &menu );
connect( actionEdit, &QAction::triggered, this, [bookmark, inProject]
{
QgsBookmarkEditorDialog *dlg = new QgsBookmarkEditorDialog( bookmark, inProject, QgisApp::instance(), QgisApp::instance()->mapCanvas() );
dlg->setAttribute( Qt::WA_DeleteOnClose );
dlg->show();
} );
menu.addAction( actionEdit );
menu.exec( lstBookmarks->viewport()->mapToGlobal( pos ) );
}

void QgsBookmarks::zoomToBookmark()
{
const QModelIndex index = lstBookmarks->currentIndex();
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsbookmarks.h
Expand Up @@ -73,6 +73,7 @@ class APP_EXPORT QgsBookmarks : public QgsDockWidget, private Ui::QgsBookmarksBa
void importFromXml();

void lstBookmarks_doubleClicked( const QModelIndex & );
void lstBookmarks_customContextMenuRequested( QPoint pos );

private:
QgsBookmarkManagerProxyModel *mBookmarkModel = nullptr;
Expand Down
18 changes: 18 additions & 0 deletions src/ui/qgsbookmarksbase.ui
Expand Up @@ -103,6 +103,24 @@
<string>Zoom to bookmark</string>
</property>
</action>
<action name="actionExport">
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionSharingExport.svg</normaloff>:/images/themes/default/mActionSharingExport.svg</iconset>
</property>
<property name="text">
<string>&amp;Export</string>
</property>
</action>
<action name="actionImport">
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionSharingImport.svg</normaloff>:/images/themes/default/mActionSharingImport.svg</iconset>
</property>
<property name="text">
<string>&amp;Import</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
Expand Down

0 comments on commit 9d3140c

Please sign in to comment.