Skip to content

Commit 9604456

Browse files
committed
#3459 navigationwidget: add context menu
Signed-off-by: Patrizio Bekerle <patrizio@bekerle.com>
1 parent 0e222b3 commit 9604456

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
- Added ability to rename headings directly in the Navigation panel
66
(for [#3459](https://github.com/pbek/QOwnNotes/issues/3459))
7-
- You can now double-click or press `F2` on any heading in the "Headings" tab of the Navigation panel to rename it
7+
- You can now double-click, press `F2`, or right-click and select "Rename heading"
8+
on any heading in the "Headings" tab of the Navigation panel to rename it
89
- The heading text in your note will update automatically while preserving the heading level
910
- This provides a quick way to reorganize your note structure without manually editing the text
1011

src/widgets/navigationwidget.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <libraries/qmarkdowntextedit/markdownhighlighter.h>
1717

1818
#include <QDebug>
19+
#include <QMenu>
1920
#include <QRegularExpression>
2021
#include <QTextBlock>
2122
#include <QTextDocument>
@@ -29,6 +30,11 @@ NavigationWidget::NavigationWidget(QWidget *parent) : QTreeWidget(parent) {
2930
QObject::connect(this, &NavigationWidget::itemClicked, this, &NavigationWidget::onItemClicked);
3031
// We want to handle itemChanged to allow renaming headings
3132
QObject::connect(this, &NavigationWidget::itemChanged, this, &NavigationWidget::onItemChanged);
33+
34+
// Enable context menu
35+
setContextMenuPolicy(Qt::CustomContextMenu);
36+
connect(this, &QTreeWidget::customContextMenuRequested, this,
37+
&NavigationWidget::showContextMenu);
3238
}
3339

3440
/**
@@ -228,3 +234,31 @@ QTreeWidgetItem *NavigationWidget::findSuitableParentItem(int elementType) const
228234
? findSuitableParentItem(elementType)
229235
: lastHigherItem;
230236
}
237+
238+
/**
239+
* Shows the context menu for the navigation widget
240+
*/
241+
void NavigationWidget::showContextMenu(const QPoint &pos) {
242+
QTreeWidgetItem *item = itemAt(pos);
243+
if (item == nullptr) {
244+
return;
245+
}
246+
247+
QMenu menu(this);
248+
249+
// Add "Rename heading" action
250+
QAction *renameAction = menu.addAction(tr("&Rename heading"));
251+
connect(renameAction, &QAction::triggered, this, &NavigationWidget::renameHeadingTriggered);
252+
253+
menu.exec(mapToGlobal(pos));
254+
}
255+
256+
/**
257+
* Triggers the rename action for the currently selected heading
258+
*/
259+
void NavigationWidget::renameHeadingTriggered() {
260+
QTreeWidgetItem *item = currentItem();
261+
if (item != nullptr) {
262+
editItem(item, 0);
263+
}
264+
}

src/widgets/navigationwidget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class NavigationWidget : public QTreeWidget {
4444
void onCurrentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous);
4545
void onItemClicked(QTreeWidgetItem *current, int column);
4646
void onItemChanged(QTreeWidgetItem *item, int column);
47+
void showContextMenu(const QPoint &pos);
48+
void renameHeadingTriggered();
4749

4850
private:
4951
void buildNavTree(const QVector<Node> &nodes);

0 commit comments

Comments
 (0)