Skip to content

Commit

Permalink
centralize the implementation of Return key press in MainWindow
Browse files Browse the repository at this point in the history
implemented Return for the search bar to rerun the search
pressing return for a tag is equivalent to a click
  • Loading branch information
SZinedine committed Mar 15, 2024
1 parent 403d8b0 commit 483c847
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
16 changes: 9 additions & 7 deletions src/DocumentsListView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<QShortcut>(return_, this, openDoc_));
mShortcuts.push_back(make_unique<QShortcut>(ctrl_p, this, toggleBoolPin));
mShortcuts.push_back(make_unique<QShortcut>(ctrl_s, this, toggleBoolFav));
mShortcuts.push_back(make_unique<QShortcut>(ctrl_e, this, launchDID));
Expand All @@ -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()) {
Expand Down
3 changes: 3 additions & 0 deletions src/DocumentsListView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 17 additions & 3 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -197,8 +197,22 @@ void MainWindow::setupMenu() {


void MainWindow::setupKeyboardShortcuts() {
const auto ctrl_f = QKeySequence::fromString(QStringLiteral("Ctrl+f"));
mSearchShortcut = make_unique<QShortcut>(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());
}
}


Expand Down
2 changes: 1 addition & 1 deletion src/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -70,7 +71,6 @@ private slots:
std::unique_ptr<QLineEdit> mSearchBar;
std::unique_ptr<QAction> mSearchBarEraseText;
std::unique_ptr<QLabel> mNumberOfFilesLabel;
std::unique_ptr<QShortcut> mSearchShortcut;
std::unique_ptr<QSystemTrayIcon> mSystemTrayIcon;
std::unique_ptr<QMenu> mSystemTrayMenu;
};
Expand Down
2 changes: 1 addition & 1 deletion src/TagsTreeView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 483c847

Please sign in to comment.