diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-04-17 16:37:25 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-04-17 16:37:25 +0200 |
commit | 5152fc15d128821b842ade2c8fa7e2fcb16c0e94 (patch) | |
tree | 446658345a873ede716006bfe10d0470b8181aa8 /src/mainwindow | |
parent | Clear navigation bar and address bar when last subwindow is closed (diff) | |
download | smolbote-5152fc15d128821b842ade2c8fa7e2fcb16c0e94.tar.xz |
Search box works again
Diffstat (limited to 'src/mainwindow')
-rw-r--r-- | src/mainwindow/mainwindow.cpp | 20 | ||||
-rw-r--r-- | src/mainwindow/mainwindow.h | 5 | ||||
-rw-r--r-- | src/mainwindow/widgets/searchform.cpp | 55 | ||||
-rw-r--r-- | src/mainwindow/widgets/searchform.h | 16 | ||||
-rw-r--r-- | src/mainwindow/widgets/searchform.ui | 20 |
5 files changed, 83 insertions, 33 deletions
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 <about/aboutdialog.h> #include <configuration/configuration.h> #include "addressbar/addressbar.h" +#include <QStatusBar> +#include "widgets/searchform.h" MainWindow::MainWindow(std::shared_ptr<Configuration> &config, QWidget *parent) : QMainWindow(parent) @@ -54,10 +56,17 @@ MainWindow::MainWindow(std::shared_ptr<Configuration> &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 *>(window); if(w == nullptr) { @@ -65,6 +74,7 @@ MainWindow::MainWindow(std::shared_ptr<Configuration> &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<Configuration> &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<std::string>("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<std::string>("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<Configuration> 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 <QAction> +#include <QWebEngineView> +#include <QFocusEvent> -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<std::string>("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 <QWidget> @@ -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 @@ <x>0</x> <y>0</y> <width>600</width> - <height>32</height> + <height>22</height> </rect> </property> <property name="windowTitle"> <string>Form</string> </property> <layout class="QHBoxLayout" name="horizontalLayout"> + <property name="spacing"> + <number>0</number> + </property> + <property name="leftMargin"> + <number>0</number> + </property> <property name="topMargin"> <number>0</number> </property> + <property name="rightMargin"> + <number>0</number> + </property> <property name="bottomMargin"> <number>0</number> </property> <item> - <widget class="QLineEdit" name="lineEdit"/> + <widget class="QLineEdit" name="lineEdit"> + <property name="maximumSize"> + <size> + <width>16777215</width> + <height>20</height> + </size> + </property> + </widget> </item> <item> <widget class="QCheckBox" name="caseSensitivity_checkBox"> |