1 /**************************************************************************
2 * Otter Browser: Web browser controlled by the user, not vice-versa.
3 * Copyright (C) 2013 - 2018 Michal Dutkiewicz aka Emdek <michal@emdek.pl>
4 * Copyright (C) 2014 - 2017 Jan Bajer aka bajasoft <jbajer@gmail.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 **************************************************************************/
20 
21 #ifndef OTTER_SEARCHWIDGET_H
22 #define OTTER_SEARCHWIDGET_H
23 
24 #include "../../../core/SessionsManager.h"
25 #include "../../../ui/LineEditWidget.h"
26 #include "../../../ui/WebWidget.h"
27 
28 #include <QtCore/QPointer>
29 #include <QtWidgets/QItemDelegate>
30 
31 namespace Otter
32 {
33 
34 class SearchSuggester;
35 class Window;
36 
37 class SearchDelegate final : public QItemDelegate
38 {
39 public:
40 	explicit SearchDelegate(QObject *parent = nullptr);
41 
42 	void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
43 	QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
44 };
45 
46 class SearchWidget final : public LineEditWidget
47 {
48 	Q_OBJECT
49 
50 public:
51 	explicit SearchWidget(Window *window, QWidget *parent = nullptr);
52 
53 	QVariantMap getOptions() const;
54 	bool event(QEvent *event) override;
55 
56 public slots:
57 	void setWindow(Window *window);
58 	void setOptions(const QVariantMap &options);
59 	void setQuery(const QString &query);
60 
61 protected:
62 	void changeEvent(QEvent *event) override;
63 	void paintEvent(QPaintEvent *event) override;
64 	void resizeEvent(QResizeEvent *event) override;
65 	void focusInEvent(QFocusEvent *event) override;
66 	void keyPressEvent(QKeyEvent *event) override;
67 	void mouseMoveEvent(QMouseEvent *event) override;
68 	void mouseReleaseEvent(QMouseEvent *event) override;
69 	void wheelEvent(QWheelEvent *event) override;
70 	QModelIndex getCurrentIndex() const;
71 
72 protected slots:
73 	void sendRequest(const QString &query = {});
74 	void showSearchEngines();
75 	void showSearchSuggestions();
76 	void addSearchEngine(QAction *action);
77 	void storeCurrentSearchEngine();
78 	void restoreCurrentSearchEngine();
79 	void handleOptionChanged(int identifier, const QVariant &value);
80 	void handleWindowOptionChanged(int identifier, const QVariant &value);
81 	void handleWatchedDataChanged(WebWidget::ChangeWatcher watcher);
82 	void handleLoadingStateChanged();
83 	void updateGeometries();
84 	void setSearchEngine(const QString &searchEngine);
85 	void setSearchEngine(const QModelIndex &index, bool canSendRequest = true);
86 
87 private:
88 	QPointer<Window> m_window;
89 	SearchSuggester *m_suggester;
90 	QString m_query;
91 	QString m_searchEngine;
92 	QRect m_iconRectangle;
93 	QRect m_dropdownArrowRectangle;
94 	QRect m_addButtonRectangle;
95 	QRect m_searchButtonRectangle;
96 	QVariantMap m_options;
97 	bool m_hasAllWindowSearchEngines;
98 	bool m_isSearchEngineLocked;
99 
100 signals:
101 	void requestedSearch(const QString &query, const QString &searchEngine, SessionsManager::OpenHints hints);
102 };
103 
104 }
105 
106 #endif
107