|
| 1 | +#include "notebookmarkdialog.h" |
| 2 | + |
| 3 | +#include <QDialogButtonBox> |
| 4 | +#include <QKeyEvent> |
| 5 | +#include <QMessageBox> |
| 6 | +#include <QPushButton> |
| 7 | +#include <QTableWidgetItem> |
| 8 | +#include <algorithm> |
| 9 | + |
| 10 | +#include "entities/note.h" |
| 11 | +#include "ui_notebookmarkdialog.h" |
| 12 | +#include "utils/gui.h" |
| 13 | + |
| 14 | +NoteBookmarkDialog::NoteBookmarkDialog(QWidget *parent) |
| 15 | + : MasterDialog(parent), ui(new Ui::NoteBookmarkDialog) { |
| 16 | + ui->setupUi(this); |
| 17 | + afterSetupUI(); |
| 18 | + |
| 19 | + // Set up table column widths |
| 20 | + ui->bookmarkTableWidget->horizontalHeader()->setSectionResizeMode( |
| 21 | + 0, QHeaderView::ResizeToContents); |
| 22 | + ui->bookmarkTableWidget->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch); |
| 23 | + ui->bookmarkTableWidget->horizontalHeader()->setSectionResizeMode( |
| 24 | + 2, QHeaderView::ResizeToContents); |
| 25 | + |
| 26 | + // Add Jump and Delete action buttons |
| 27 | + QPushButton *jumpButton = |
| 28 | + ui->buttonBox->addButton(tr("Jump to bookmark"), QDialogButtonBox::ActionRole); |
| 29 | + jumpButton->setIcon( |
| 30 | + QIcon::fromTheme(QStringLiteral("go-next"), |
| 31 | + QIcon(QStringLiteral(":/icons/breeze-qownnotes/16x16/go-next.svg")))); |
| 32 | + jumpButton->setToolTip(tr("Jump to the selected bookmark in the main window")); |
| 33 | + |
| 34 | + QPushButton *deleteButton = |
| 35 | + ui->buttonBox->addButton(tr("Delete bookmark"), QDialogButtonBox::ActionRole); |
| 36 | + deleteButton->setIcon( |
| 37 | + QIcon::fromTheme(QStringLiteral("edit-delete"), |
| 38 | + QIcon(QStringLiteral(":/icons/breeze-qownnotes/16x16/edit-delete.svg")))); |
| 39 | + deleteButton->setToolTip(tr("Delete the selected bookmark")); |
| 40 | + |
| 41 | + connect(jumpButton, &QPushButton::clicked, this, &NoteBookmarkDialog::onJumpButtonClicked); |
| 42 | + connect(deleteButton, &QPushButton::clicked, this, &NoteBookmarkDialog::onDeleteButtonClicked); |
| 43 | + connect(ui->bookmarkTableWidget, &QTableWidget::cellDoubleClicked, this, |
| 44 | + &NoteBookmarkDialog::onBookmarkTableDoubleClicked); |
| 45 | + |
| 46 | + // Install event filter on the table to handle the Del key |
| 47 | + ui->bookmarkTableWidget->installEventFilter(this); |
| 48 | +} |
| 49 | + |
| 50 | +NoteBookmarkDialog::~NoteBookmarkDialog() { delete ui; } |
| 51 | + |
| 52 | +/** |
| 53 | + * Populates the bookmark table from the given bookmarks hash |
| 54 | + */ |
| 55 | +void NoteBookmarkDialog::setBookmarks(const QHash<int, NoteHistoryItem> &bookmarks) { |
| 56 | + // Collect and sort by slot number for a predictable display order |
| 57 | + QList<int> slotNumbers = bookmarks.keys(); |
| 58 | + std::sort(slotNumbers.begin(), slotNumbers.end()); |
| 59 | + |
| 60 | + ui->bookmarkTableWidget->setRowCount(0); |
| 61 | + |
| 62 | + for (int slot : slotNumbers) { |
| 63 | + const NoteHistoryItem &item = bookmarks[slot]; |
| 64 | + const Note note = item.getNote(); |
| 65 | + |
| 66 | + if (!note.isFetched()) { |
| 67 | + // Skip bookmarks whose note no longer exists |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + const int row = ui->bookmarkTableWidget->rowCount(); |
| 72 | + ui->bookmarkTableWidget->insertRow(row); |
| 73 | + |
| 74 | + auto *slotItem = new QTableWidgetItem(QString::number(slot)); |
| 75 | + slotItem->setData(Qt::UserRole, slot); |
| 76 | + slotItem->setTextAlignment(Qt::AlignCenter); |
| 77 | + |
| 78 | + auto *noteItem = new QTableWidgetItem(item.getNoteName()); |
| 79 | + auto *posItem = new QTableWidgetItem(QString::number(item.getCursorPosition())); |
| 80 | + posItem->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter); |
| 81 | + |
| 82 | + ui->bookmarkTableWidget->setItem(row, 0, slotItem); |
| 83 | + ui->bookmarkTableWidget->setItem(row, 1, noteItem); |
| 84 | + ui->bookmarkTableWidget->setItem(row, 2, posItem); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Returns the slot number of the currently selected row, or -1 if none |
| 90 | + */ |
| 91 | +int NoteBookmarkDialog::selectedSlot() const { |
| 92 | + const QList<QTableWidgetItem *> selected = ui->bookmarkTableWidget->selectedItems(); |
| 93 | + |
| 94 | + if (selected.isEmpty()) { |
| 95 | + return -1; |
| 96 | + } |
| 97 | + |
| 98 | + // The slot is stored in column 0 via UserRole |
| 99 | + const int row = ui->bookmarkTableWidget->row(selected.first()); |
| 100 | + QTableWidgetItem *slotItem = ui->bookmarkTableWidget->item(row, 0); |
| 101 | + |
| 102 | + if (!slotItem) { |
| 103 | + return -1; |
| 104 | + } |
| 105 | + |
| 106 | + return slotItem->data(Qt::UserRole).toInt(); |
| 107 | +} |
| 108 | + |
| 109 | +void NoteBookmarkDialog::onJumpButtonClicked() { |
| 110 | + const int slot = selectedSlot(); |
| 111 | + |
| 112 | + if (slot >= 0) { |
| 113 | + emit jumpToBookmarkRequested(slot); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +void NoteBookmarkDialog::onDeleteButtonClicked() { |
| 118 | + const int slot = selectedSlot(); |
| 119 | + |
| 120 | + if (slot < 0) { |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + // Ask the user to confirm before deleting the bookmark, with a "Don't ask again!" option |
| 125 | + const auto answer = Utils::Gui::question( |
| 126 | + this, tr("Delete bookmark"), |
| 127 | + tr("Are you sure you want to delete the bookmark at slot %1?").arg(slot), |
| 128 | + QStringLiteral("delete-note-bookmark")); |
| 129 | + |
| 130 | + if (answer == QMessageBox::Yes) { |
| 131 | + emit deleteBookmarkRequested(slot); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +void NoteBookmarkDialog::onBookmarkTableDoubleClicked(int /*row*/, int /*column*/) { |
| 136 | + onJumpButtonClicked(); |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * Intercepts key events on the bookmark table to allow deletion with the Del key |
| 141 | + */ |
| 142 | +bool NoteBookmarkDialog::eventFilter(QObject *watched, QEvent *event) { |
| 143 | + if (watched == ui->bookmarkTableWidget && event->type() == QEvent::KeyPress) { |
| 144 | + auto *keyEvent = static_cast<QKeyEvent *>(event); |
| 145 | + |
| 146 | + if (keyEvent->key() == Qt::Key_Delete) { |
| 147 | + onDeleteButtonClicked(); |
| 148 | + return true; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return MasterDialog::eventFilter(watched, event); |
| 153 | +} |
0 commit comments