|
22 | 22 | #include <QTreeWidgetItem> |
23 | 23 |
|
24 | 24 | NavigationWidget::NavigationWidget(QWidget *parent) : QTreeWidget(parent) { |
25 | | - // we want to handle currentItemChanged because it also works with the keyboard |
| 25 | + // We want to handle currentItemChanged because it also works with the keyboard |
26 | 26 | QObject::connect(this, &NavigationWidget::currentItemChanged, this, |
27 | 27 | &NavigationWidget::onCurrentItemChanged); |
28 | | - // we want to handle itemClicked because it allows to click on an item a 2nd time |
| 28 | + // We want to handle itemClicked because it allows to click on an item a 2nd time |
29 | 29 | QObject::connect(this, &NavigationWidget::itemClicked, this, &NavigationWidget::onItemClicked); |
| 30 | + // We want to handle itemChanged to allow renaming headings |
| 31 | + QObject::connect(this, &NavigationWidget::itemChanged, this, &NavigationWidget::onItemChanged); |
30 | 32 | } |
31 | 33 |
|
32 | 34 | /** |
@@ -57,6 +59,35 @@ void NavigationWidget::onItemClicked(QTreeWidgetItem *current, int column) { |
57 | 59 | emit positionClicked(current->data(0, Qt::UserRole).toInt()); |
58 | 60 | } |
59 | 61 |
|
| 62 | +/** |
| 63 | + * Handles renaming of heading items |
| 64 | + * Emits the headingRenamed signal when a heading is renamed |
| 65 | + */ |
| 66 | +void NavigationWidget::onItemChanged(QTreeWidgetItem *item, int column) { |
| 67 | + Q_UNUSED(column) |
| 68 | + |
| 69 | + if (item == nullptr) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + // Get the position from the item |
| 74 | + int position = item->data(0, Qt::UserRole).toInt(); |
| 75 | + |
| 76 | + // Find the corresponding node in our cache |
| 77 | + for (const auto &node : _navigationTreeNodes) { |
| 78 | + if (node.pos == position) { |
| 79 | + QString oldText = node.text; |
| 80 | + QString newText = item->text(0); |
| 81 | + |
| 82 | + // Only emit if the text actually changed |
| 83 | + if (oldText != newText && !newText.isEmpty()) { |
| 84 | + emit headingRenamed(position, oldText, newText); |
| 85 | + } |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
60 | 91 | /** |
61 | 92 | * Parses a text document and builds the navigation tree for it |
62 | 93 | */ |
@@ -164,6 +195,8 @@ void NavigationWidget::buildNavTree(const QVector<Node> &nodes) { |
164 | 195 | item->setText(0, stripMarkdown(node.text)); |
165 | 196 | item->setData(0, Qt::UserRole, pos); |
166 | 197 | item->setToolTip(0, tr("headline %1").arg(elementType - MarkdownHighlighter::H1 + 1)); |
| 198 | + // Make the item editable to allow renaming headings |
| 199 | + item->setFlags(item->flags() | Qt::ItemIsEditable); |
167 | 200 |
|
168 | 201 | // attempt to find a suitable parent item for the element type |
169 | 202 | QTreeWidgetItem *lastHigherItem = findSuitableParentItem(elementType); |
|
0 commit comments