1 /**************************************************************************
2 * Otter Browser: Web browser controlled by the user, not vice-versa.
3 * Copyright (C) 2015 - 2017 Michal Dutkiewicz aka Emdek <michal@emdek.pl>
4 * Copyright (C) 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_LINEEDITWIDGET_H
22 #define OTTER_LINEEDITWIDGET_H
23 
24 #include "ItemViewWidget.h"
25 #include "../core/ActionExecutor.h"
26 
27 #include <QtWidgets/QCompleter>
28 #include <QtWidgets/QLineEdit>
29 
30 namespace Otter
31 {
32 
33 class LineEditWidget;
34 
35 class PopupViewWidget final : public ItemViewWidget
36 {
37 	Q_OBJECT
38 
39 public:
40 	explicit PopupViewWidget(LineEditWidget *parent);
41 
42 	bool event(QEvent *event) override;
43 
44 public slots:
45 	void updateHeight();
46 
47 protected:
48 	void keyPressEvent(QKeyEvent *event) override;
49 
50 protected slots:
51 	void handleIndexEntered(const QModelIndex &index);
52 
53 private:
54 	LineEditWidget *m_lineEditWidget;
55 };
56 
57 class LineEditWidget : public QLineEdit, public ActionExecutor
58 {
59 	Q_OBJECT
60 
61 public:
62 	enum DropMode
63 	{
64 		PasteDropMode = 0,
65 		ReplaceDropMode,
66 		ReplaceAndNotifyDropMode
67 	};
68 
69 	explicit LineEditWidget(const QString &text, QWidget *parent = nullptr);
70 	explicit LineEditWidget(QWidget *parent = nullptr);
71 	~LineEditWidget();
72 
73 	void activate(Qt::FocusReason reason);
74 	virtual void showPopup();
75 	virtual void hidePopup();
76 	void setDropMode(DropMode mode);
77 	void setClearOnEscape(bool clear);
78 	void setSelectAllOnFocus(bool select);
79 	PopupViewWidget* getPopup();
80 	ActionsManager::ActionDefinition::State getActionState(int identifier, const QVariantMap &parameters) const override;
81 	bool isPopupVisible() const;
82 
83 public slots:
84 	void triggerAction(int identifier, const QVariantMap &parameters, ActionsManager::TriggerType trigger = ActionsManager::UnknownTrigger) override;
85 	void setCompletion(const QString &completion);
86 
87 protected:
88 	void resizeEvent(QResizeEvent *event) override;
89 	void focusInEvent(QFocusEvent *event) override;
90 	void keyPressEvent(QKeyEvent *event) override;
91 	void contextMenuEvent(QContextMenuEvent *event) override;
92 	void mousePressEvent(QMouseEvent *event) override;
93 	void mouseReleaseEvent(QMouseEvent *event) override;
94 	void dropEvent(QDropEvent *event) override;
95 	void initialize();
96 
97 protected slots:
98 	void clearSelectAllOnRelease();
99 	void handleSelectionChanged();
100 	void handleTextChanged(const QString &text);
101 	void notifyPasteActionStateChanged();
102 
103 private:
104 	PopupViewWidget *m_popupViewWidget;
105 	QCompleter *m_completer;
106 	QString m_completion;
107 	DropMode m_dropMode;
108 	int m_selectionStart;
109 	bool m_shouldClearOnEscape;
110 	bool m_shouldIgnoreCompletion;
111 	bool m_shouldSelectAllOnFocus;
112 	bool m_shouldSelectAllOnRelease;
113 	bool m_hadSelection;
114 	bool m_wasEmpty;
115 
116 signals:
117 	void arbitraryActionsStateChanged(const QVector<int> &identifiers);
118 	void popupClicked(const QModelIndex &index);
119 	void textDropped(const QString &text);
120 };
121 
122 }
123 
124 #endif
125