From 483c84729c82f784dd10f7a3e74bd2ff18a27a69 Mon Sep 17 00:00:00 2001 From: SZinedine Date: Fri, 15 Mar 2024 22:26:29 +0100 Subject: [PATCH] centralize the implementation of Return key press in MainWindow implemented Return for the search bar to rerun the search pressing return for a tag is equivalent to a click --- src/DocumentsListView.cpp | 16 +++++++++------- src/DocumentsListView.h | 3 +++ src/MainWindow.cpp | 20 +++++++++++++++++--- src/MainWindow.h | 2 +- src/TagsTreeView.h | 2 +- 5 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/DocumentsListView.cpp b/src/DocumentsListView.cpp index 0453d16..fc64667 100644 --- a/src/DocumentsListView.cpp +++ b/src/DocumentsListView.cpp @@ -17,11 +17,10 @@ using Ui::Delegates::DocumentListDelegate; using std::make_unique; namespace Settings = Ui::Settings; -const auto ctrl_p = QKeySequence::fromString(QStringLiteral("Ctrl+p")); -const auto ctrl_s = QKeySequence::fromString(QStringLiteral("Ctrl+s")); -const auto ctrl_e = QKeySequence::fromString(QStringLiteral("Ctrl+e")); -const auto return_ = QKeySequence::fromString(QStringLiteral("Return")); -const auto space_ = QKeySequence::fromString(QStringLiteral("Space")); +const auto ctrl_p = QKeySequence::fromString(QStringLiteral("Ctrl+p")); +const auto ctrl_s = QKeySequence::fromString(QStringLiteral("Ctrl+s")); +const auto ctrl_e = QKeySequence::fromString(QStringLiteral("Ctrl+e")); +const auto space_ = QKeySequence::fromString(QStringLiteral("Space")); constexpr QSize iconSize_(35, 20); DocumentsListView::DocumentsListView(QWidget* parent) @@ -42,12 +41,10 @@ DocumentsListView::DocumentsListView(QWidget* parent) connect(this, &DocumentsListView::customContextMenuRequested, this, &DocumentsListView::onCustomContextMenuRequested); - auto openDoc_ = [this] { openDocumentAt(currentIndex()); }; auto toggleBoolPin = [this] { toggleBool(currentIndex(), DocumentListModel::PinnedRole); }; auto toggleBoolFav = [this] { toggleBool(currentIndex(), DocumentListModel::FavoritedRole); }; auto launchDID = [this] { launchDocumentInfoDialog(currentIndex()); }; auto docSelected = [this] { onDocumentSelected(currentIndex()); }; - mShortcuts.push_back(make_unique(return_, this, openDoc_)); mShortcuts.push_back(make_unique(ctrl_p, this, toggleBoolPin)); mShortcuts.push_back(make_unique(ctrl_s, this, toggleBoolFav)); mShortcuts.push_back(make_unique(ctrl_e, this, launchDID)); @@ -60,6 +57,11 @@ DocumentsListView::~DocumentsListView() { } +void DocumentsListView::openCurrentDocument() { + openDocumentAt(currentIndex()); +} + + void DocumentsListView::onCustomContextMenuRequested(const QPoint& pos) { const auto index = indexAt(pos); if (!index.isValid()) { diff --git a/src/DocumentsListView.h b/src/DocumentsListView.h index 815b606..eb93fba 100644 --- a/src/DocumentsListView.h +++ b/src/DocumentsListView.h @@ -30,6 +30,9 @@ class DocumentsListView : public QListView { explicit DocumentsListView(QWidget* parent = nullptr); ~DocumentsListView() override; +public slots: + void openCurrentDocument(); + private slots: void onCustomContextMenuRequested(const QPoint& pos); void onDocumentSelected(const QModelIndex& index); diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index f888893..671c5f1 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -177,7 +177,7 @@ void MainWindow::setupMenu() { ui->nativeThemeAction->setChecked(true); } - connect(themesActionGroup, &QActionGroup::triggered, this, [=](QAction* act) { + connect(themesActionGroup, &QActionGroup::triggered, this, [this](QAction* act) { applyTheme(act->data().toString()); Settings::saveTheme(act->data().toString()); act->setChecked(true); @@ -197,8 +197,22 @@ void MainWindow::setupMenu() { void MainWindow::setupKeyboardShortcuts() { - const auto ctrl_f = QKeySequence::fromString(QStringLiteral("Ctrl+f")); - mSearchShortcut = make_unique(ctrl_f, this, [this] { mSearchBar->setFocus(); }); + const auto ctrl_f = QKeySequence::fromString(QStringLiteral("Ctrl+f")); + const auto return_ = QKeySequence::fromString(QStringLiteral("Return")); + + new QShortcut(ctrl_f, this, [this] { mSearchBar->setFocus(); }); + new QShortcut(return_, this, [this] { on_ReturnPressed(); }); +} + + +void MainWindow::on_ReturnPressed() { + if (ui->documentsListView->hasFocus()) { + ui->documentsListView->openCurrentDocument(); + } else if (ui->tagsTreeView->hasFocus()) { + ui->tagsTreeView->onClicked({}); + } else if (mSearchBar->hasFocus()) { + search(mSearchBar->text()); + } } diff --git a/src/MainWindow.h b/src/MainWindow.h index a4dd98e..094cc0c 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -57,6 +57,7 @@ private slots: void onDocumentPermanentlyDeleted(Doc::Document* document); void onSystemTryIconActivated(int reason); void applyTheme(const QString& theme); + void on_ReturnPressed(); void about(); signals: @@ -70,7 +71,6 @@ private slots: std::unique_ptr mSearchBar; std::unique_ptr mSearchBarEraseText; std::unique_ptr mNumberOfFilesLabel; - std::unique_ptr mSearchShortcut; std::unique_ptr mSystemTrayIcon; std::unique_ptr mSystemTrayMenu; }; diff --git a/src/TagsTreeView.h b/src/TagsTreeView.h index 256fc6d..91da91f 100644 --- a/src/TagsTreeView.h +++ b/src/TagsTreeView.h @@ -25,7 +25,7 @@ public slots: void changeTagColor(const QModelIndex& index, const QString& color); void pinTag(const QModelIndex& index, bool pin); -private slots: +public slots: void onClicked(const QModelIndex& index); private: