From 5152fc15d128821b842ade2c8fa7e2fcb16c0e94 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Tue, 17 Apr 2018 16:37:25 +0200 Subject: Search box works again --- src/CMakeLists.txt | 6 ++-- src/mainwindow/mainwindow.cpp | 20 ++++++++++++- src/mainwindow/mainwindow.h | 5 +++- src/mainwindow/widgets/searchform.cpp | 55 ++++++++++++++++++++--------------- src/mainwindow/widgets/searchform.h | 16 ++++++---- src/mainwindow/widgets/searchform.ui | 20 +++++++++++-- 6 files changed, 86 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0ffb768..edc8f49 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -26,9 +26,9 @@ add_executable(poi #mainwindow/widgets/loadingbar.h mainwindow/widgets/navigationbar.cpp mainwindow/widgets/navigationbar.h - #mainwindow/widgets/searchform.cpp - #mainwindow/widgets/searchform.h - #mainwindow/widgets/searchform.ui + mainwindow/widgets/searchform.cpp + mainwindow/widgets/searchform.h + mainwindow/widgets/searchform.ui mainwindow/widgets/tabwidget.cpp mainwindow/widgets/tabwidget.h diff --git a/src/mainwindow/mainwindow.cpp b/src/mainwindow/mainwindow.cpp index b68ddbe..9b1db30 100644 --- a/src/mainwindow/mainwindow.cpp +++ b/src/mainwindow/mainwindow.cpp @@ -24,6 +24,8 @@ #include #include #include "addressbar/addressbar.h" +#include +#include "widgets/searchform.h" MainWindow::MainWindow(std::shared_ptr &config, QWidget *parent) : QMainWindow(parent) @@ -54,10 +56,17 @@ MainWindow::MainWindow(std::shared_ptr &config, QWidget *parent) setCentralWidget(mdiArea); mdiArea->setFocus(); + // status bar + searchBox = new SearchForm(this); + statusBar()->addPermanentWidget(searchBox); + searchBox->setVisible(false); + + // connect signlas connect(mdiArea, &QMdiArea::subWindowActivated, this, [this, navigationToolBar](QMdiSubWindow *window) { disconnect(titleChangedConnection); disconnect(addressBarConnection); disconnect(navigationBarConnection); + disconnect(searchBoxConnection); auto *w = qobject_cast(window); if(w == nullptr) { @@ -65,6 +74,7 @@ MainWindow::MainWindow(std::shared_ptr &config, QWidget *parent) setWindowTitle(tr("smolbote")); addressBar->connectWebView(nullptr); navigationToolBar->connectWebView(nullptr); + searchBox->setView(nullptr); } else { setWindowTitle(w->windowTitle() + titleSuffix); titleChangedConnection = connect(w, &Window::windowTitleChanged, this, [this](const QString &title) { @@ -75,13 +85,20 @@ MainWindow::MainWindow(std::shared_ptr &config, QWidget *parent) addressBarConnection = connect(w, &Window::currentViewChanged, addressBar, &AddressBar::connectWebView); navigationToolBar->connectWebView(w->currentView()); navigationBarConnection = connect(w, &Window::currentViewChanged, navigationToolBar, &NavigationBar::connectWebView); + searchBox->setView(w->currentView()); + searchBoxConnection = connect(w, &Window::currentViewChanged, searchBox, &SearchForm::setView); } }); auto *tileShortcut = new QShortcut(QKeySequence(config->value("mainwindow.shortcuts.tileWindows").value().c_str()), this); - connect(tileShortcut, &QShortcut::activated, this, [this]() { + connect(tileShortcut, &QShortcut::activated, this, [=]() { mdiArea->tileSubWindows(); }); + + auto *searchShortcut = new QShortcut(QKeySequence(config->value("mainwindow.shortcuts.search").value().c_str()), this); + connect(searchShortcut, &QShortcut::activated, this, [=]() { + searchBox->setVisible(!searchBox->isVisible()); + }); } MainWindow::~MainWindow() @@ -89,6 +106,7 @@ MainWindow::~MainWindow() disconnect(titleChangedConnection); disconnect(addressBarConnection); disconnect(navigationBarConnection); + disconnect(searchBoxConnection); } void MainWindow::createMenuBar() diff --git a/src/mainwindow/mainwindow.h b/src/mainwindow/mainwindow.h index 38ab746..581cd0c 100644 --- a/src/mainwindow/mainwindow.h +++ b/src/mainwindow/mainwindow.h @@ -17,6 +17,7 @@ class QMdiArea; class Configuration; class Window; class AddressBar; +class SearchForm; class MainWindow : public QMainWindow { friend class Browser; @@ -47,13 +48,15 @@ protected: private: QString titleSuffix; QMenu *toolsMenu = nullptr; - AddressBar *addressBar; + AddressBar *addressBar = nullptr; + SearchForm *searchBox = nullptr; QMdiArea *mdiArea; std::shared_ptr m_config; QMetaObject::Connection titleChangedConnection; QMetaObject::Connection addressBarConnection, navigationBarConnection; + QMetaObject::Connection searchBoxConnection; }; #endif // SMOLBOTE_MAINWINDOW_H diff --git a/src/mainwindow/widgets/searchform.cpp b/src/mainwindow/widgets/searchform.cpp index 07b8426..80e3848 100644 --- a/src/mainwindow/widgets/searchform.cpp +++ b/src/mainwindow/widgets/searchform.cpp @@ -7,45 +7,30 @@ */ #include "searchform.h" -#include "configuration/configuration.h" -#include "mainwindow/mainwindow.h" #include "ui_searchform.h" -#include "webengine/webview.h" -#include +#include +#include -SearchForm::SearchForm(MainWindow *parentWindow, QWidget *parent) +SearchForm::SearchForm(QWidget *parent) : QWidget(parent) , ui(new Ui::SearchForm) { - Q_CHECK_PTR(parentWindow); - ui->setupUi(this); ui->lineEdit->setPlaceholderText(tr("Search")); ui->lineEdit->setClearButtonEnabled(true); ui->result_label->setVisible(false); - // show/hide action - auto *toggleSearchBox = new QAction(this); - toggleSearchBox->setShortcut(QKeySequence(QString::fromStdString(parentWindow->m_config->value("browser.shortcuts.toggleSearchBox").value()))); - connect(toggleSearchBox, &QAction::triggered, this, [this, parentWindow]() { - if(isVisible()) { - setVisible(false); - // remove highlighting by passing an empty string - parentWindow->m_currentView->findText(""); - } else { - setVisible(true); - setFocus(); - } - }); - parentWindow->addAction(toggleSearchBox); + connect(ui->lineEdit, &QLineEdit::returnPressed, this, [this]() { + Q_ASSERT_X(view != nullptr, "SearchForm::returnPressed::lambda", "No view set"); - connect(ui->lineEdit, &QLineEdit::returnPressed, this, [this, parentWindow]() { + if(view) { QWebEnginePage::FindFlags searchFlags; searchFlags.setFlag(QWebEnginePage::FindCaseSensitively, ui->caseSensitivity_checkBox->isChecked()); searchFlags.setFlag(QWebEnginePage::FindBackward, ui->reverseSearch_checkBox->isChecked()); - parentWindow->m_currentView->findText(ui->lineEdit->text(), searchFlags, [this](bool found) { + view->findText(ui->lineEdit->text(), searchFlags, [this](bool found) { ui->result_label->setVisible(!found); }); + } }); } @@ -54,8 +39,30 @@ SearchForm::~SearchForm() delete ui; } +void SearchForm::setView(QWebEngineView* view) +{ + qDebug("setting view: %s", view == nullptr ? "nullptr" : "valid"); + this->view = view; +} + +void SearchForm::showEvent(QShowEvent* e) +{ + e->accept(); + ui->lineEdit->setFocus(); + ui->lineEdit->selectAll(); +} + +void SearchForm::hideEvent(QHideEvent* e) +{ + e->accept(); + // clear highlighted text when hiding + if(view) { + view->findText(""); + } +} + void SearchForm::focusInEvent(QFocusEvent *e) { ui->lineEdit->setFocus(); - QWidget::focusInEvent(e); + e->accept(); } diff --git a/src/mainwindow/widgets/searchform.h b/src/mainwindow/widgets/searchform.h index 94cc2a9..59c4ef5 100644 --- a/src/mainwindow/widgets/searchform.h +++ b/src/mainwindow/widgets/searchform.h @@ -6,8 +6,8 @@ * SPDX-License-Identifier: GPL-3.0 */ -#ifndef SEARCHFORM_H -#define SEARCHFORM_H +#ifndef SMOLBOTE_SEARCHFORM_H +#define SMOLBOTE_SEARCHFORM_H #include @@ -16,20 +16,26 @@ namespace Ui class SearchForm; } -class MainWindow; +class QWebEngineView; class SearchForm : public QWidget { Q_OBJECT public: - explicit SearchForm(MainWindow *parentWindow, QWidget *parent = nullptr); + explicit SearchForm(QWidget *parent = nullptr); ~SearchForm() override; +public slots: + void setView(QWebEngineView *view); + protected: + void showEvent(QShowEvent *e) override; + void hideEvent(QHideEvent *e) override; void focusInEvent(QFocusEvent *e) override; private: Ui::SearchForm *ui; + QWebEngineView *view = nullptr; }; -#endif // SEARCHFORM_H +#endif // SMOLBOTE_SEARCHFORM_H diff --git a/src/mainwindow/widgets/searchform.ui b/src/mainwindow/widgets/searchform.ui index 9d1d108..5ad2009 100644 --- a/src/mainwindow/widgets/searchform.ui +++ b/src/mainwindow/widgets/searchform.ui @@ -7,21 +7,37 @@ 0 0 600 - 32 + 22 Form + + 0 + + + 0 + 0 + + 0 + 0 - + + + + 16777215 + 20 + + + -- cgit v1.2.1