From 5152fc15d128821b842ade2c8fa7e2fcb16c0e94 Mon Sep 17 00:00:00 2001
From: Aqua-sama <aqua@iserlohn-fortress.net>
Date: Tue, 17 Apr 2018 16:37:25 +0200
Subject: Search box works again

---
 src/mainwindow/widgets/searchform.cpp | 55 ++++++++++++++++++++---------------
 src/mainwindow/widgets/searchform.h   | 16 ++++++----
 src/mainwindow/widgets/searchform.ui  | 20 +++++++++++--
 3 files changed, 60 insertions(+), 31 deletions(-)

(limited to 'src/mainwindow/widgets')

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">
-- 
cgit v1.2.1