@@ -49,6 +49,8 @@ QgsBookmarks::QgsBookmarks( QWidget *parent )
49
49
QgsGui::enableAutoGeometryRestore ( this );
50
50
51
51
connect ( lstBookmarks, &QTreeView::doubleClicked, this , &QgsBookmarks::lstBookmarks_doubleClicked );
52
+ lstBookmarks->setContextMenuPolicy ( Qt::CustomContextMenu );
53
+ connect ( lstBookmarks, &QTreeView::customContextMenuRequested, this , &QgsBookmarks::lstBookmarks_customContextMenuRequested );
52
54
53
55
bookmarksDockContents->layout ()->setContentsMargins ( 0 , 0 , 0 , 0 );
54
56
static_cast < QGridLayout * >( bookmarksDockContents->layout () )->setVerticalSpacing ( 0 );
@@ -60,17 +62,15 @@ QgsBookmarks::QgsBookmarks( QWidget *parent )
60
62
btnImpExp->setPopupMode ( QToolButton::InstantPopup );
61
63
62
64
QMenu *share = new QMenu ( this );
63
- QAction *btnExport = share->addAction ( tr ( " &Export" ) );
64
- QAction *btnImport = share->addAction ( tr ( " &Import" ) );
65
- btnExport->setIcon ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mActionSharingExport.svg" ) ) );
66
- btnImport->setIcon ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mActionSharingImport.svg" ) ) );
67
- connect ( btnExport, &QAction::triggered, this , &QgsBookmarks::exportToXml );
68
- connect ( btnImport, &QAction::triggered, this , &QgsBookmarks::importFromXml );
65
+ share->addAction ( actionExport );
66
+ share->addAction ( actionImport );
69
67
btnImpExp->setMenu ( share );
70
68
71
69
connect ( actionAdd, &QAction::triggered, this , &QgsBookmarks::addClicked );
72
70
connect ( actionDelete, &QAction::triggered, this , &QgsBookmarks::deleteClicked );
73
71
connect ( actionZoomTo, &QAction::triggered, this , &QgsBookmarks::zoomToBookmark );
72
+ connect ( actionExport, &QAction::triggered, this , &QgsBookmarks::exportToXml );
73
+ connect ( actionImport, &QAction::triggered, this , &QgsBookmarks::importFromXml );
74
74
75
75
mBookmarkToolbar ->addWidget ( btnImpExp );
76
76
@@ -145,6 +145,51 @@ void QgsBookmarks::lstBookmarks_doubleClicked( const QModelIndex &index )
145
145
zoomToBookmark ();
146
146
}
147
147
148
+ void QgsBookmarks::lstBookmarks_customContextMenuRequested ( QPoint pos )
149
+ {
150
+ // Get index of item under mouse
151
+ QModelIndex index = lstBookmarks->indexAt ( pos );
152
+ if ( !index.isValid () )
153
+ {
154
+ // No bookmark under mouse, display generic menu
155
+ QMenu menu;
156
+ menu.addAction ( actionAdd );
157
+ menu.addSeparator ();
158
+ menu.addAction ( actionExport );
159
+ menu.addAction ( actionImport );
160
+ menu.exec ( lstBookmarks->mapToGlobal ( pos ) );
161
+ return ;
162
+ }
163
+
164
+ // Create the context menu
165
+ QMenu menu;
166
+
167
+ // Add zoom and delete actions
168
+ menu.addAction ( actionZoomTo );
169
+ menu.addAction ( actionDelete );
170
+
171
+ // Get the bookmark
172
+ const QString id = lstBookmarks->model ()->data ( index, QgsBookmarkManagerModel::RoleId ).toString ();
173
+ QgsBookmark bookmark = QgsApplication::bookmarkManager ()->bookmarkById ( id );
174
+ bool inProject = false ;
175
+ if ( bookmark.id ().isEmpty () )
176
+ {
177
+ inProject = true ;
178
+ bookmark = QgsProject::instance ()->bookmarkManager ()->bookmarkById ( id );
179
+ }
180
+
181
+ // Add an edit action (similar to the one in QgsBookmarksItemGuiProvider)
182
+ QAction *actionEdit = new QAction ( tr ( " Edit Spatial Bookmark…" ), &menu );
183
+ connect ( actionEdit, &QAction::triggered, this , [bookmark, inProject]
184
+ {
185
+ QgsBookmarkEditorDialog *dlg = new QgsBookmarkEditorDialog ( bookmark, inProject, QgisApp::instance (), QgisApp::instance ()->mapCanvas () );
186
+ dlg->setAttribute ( Qt::WA_DeleteOnClose );
187
+ dlg->show ();
188
+ } );
189
+ menu.addAction ( actionEdit );
190
+ menu.exec ( lstBookmarks->viewport ()->mapToGlobal ( pos ) );
191
+ }
192
+
148
193
void QgsBookmarks::zoomToBookmark ()
149
194
{
150
195
const QModelIndex index = lstBookmarks->currentIndex ();
0 commit comments