Skip to content

Commit

Permalink
SearchBar shortcuts work now
Browse files Browse the repository at this point in the history
Refactored shortcut to search bar and removed redundant code. Fix #967
  • Loading branch information
ShaopengLin authored and kelson42 committed Apr 12, 2024
1 parent 0b18cc6 commit 366ab1d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 16 deletions.
3 changes: 1 addition & 2 deletions src/kiwixapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,7 @@ void KiwixApp::createActions()
HIDE_ACTION(SavePageAsAction);
*/

CREATE_ACTION_SHORTCUT(SearchArticleAction, gt("search-article"), QKeySequence(Qt::CTRL | Qt::Key_L));
HIDE_ACTION(SearchArticleAction);
CREATE_ACTION_SHORTCUTS(SearchArticleAction, gt("search-article"), QList<QKeySequence>({QKeySequence(Qt::Key_F6), QKeySequence(Qt::CTRL | Qt::Key_L), QKeySequence(Qt::ALT | Qt::Key_D)}));

CREATE_ACTION_SHORTCUT(SearchLibraryAction, gt("search-in-library"), QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_R));
HIDE_ACTION(SearchLibraryAction);
Expand Down
12 changes: 0 additions & 12 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,6 @@ void MainWindow::when_libraryPageDisplayed(bool showed)
}
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
auto key = event->key();
auto modifier = event->modifiers();
if (key == Qt::Key_F6 ||
(key == Qt::Key_L && modifier == Qt::ControlModifier) ||
(key == Qt::Key_D && modifier == Qt::AltModifier)) {
getTopWidget()->getSearchBar().selectAll();
getTopWidget()->getSearchBar().setFocus();
}
return QWidget::keyPressEvent(event);
}
TabBar* MainWindow::getTabBar()
{
return mp_ui->tabBar;
Expand Down
1 change: 0 additions & 1 deletion src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class MainWindow : public QMainWindow
QWidget getMainView();

protected:
void keyPressEvent(QKeyEvent *event) override;
bool eventFilter(QObject* object, QEvent* event) override;

private slots:
Expand Down
10 changes: 9 additions & 1 deletion src/searchbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ SearchBar::SearchBar(QWidget *parent) :
connect(this, &QLineEdit::returnPressed, this, [=]() {
m_returnPressed = true;
});

auto app = KiwixApp::instance();
connect(app->getAction(KiwixApp::SearchArticleAction), &QAction::triggered,
this, [=]() {
this->selectAll();
this->setFocus(Qt::ShortcutFocusReason);
});
}

void SearchBar::hideSuggestions()
Expand Down Expand Up @@ -129,7 +136,8 @@ void SearchBar::focusInEvent( QFocusEvent* event)
clear();
}
if (event->reason() == Qt::ActiveWindowFocusReason ||
event->reason() == Qt::MouseFocusReason) {
event->reason() == Qt::MouseFocusReason ||
event->reason() == Qt::ShortcutFocusReason) {
connect(&m_completer, QOverload<const QModelIndex &>::of(&QCompleter::activated),
this, QOverload<const QModelIndex &>::of(&SearchBar::openCompletion));
}
Expand Down

0 comments on commit 366ab1d

Please sign in to comment.