mirror of
https://github.com/qgis/QGIS.git
synced 2025-10-07 00:15:48 -04:00
Add a context menu to QgsBookmarks
This commit is contained in:
parent
7ea839bd58
commit
9d3140c55a
@ -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 );
|
||||
@ -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 );
|
||||
|
||||
@ -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();
|
||||
|
@ -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;
|
||||
|
@ -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>&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>&Import</string>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
|
Loading…
x
Reference in New Issue
Block a user