1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
3 | |
4 | #ifndef MESSAGEEDITORWIDGETS_H |
5 | #define MESSAGEEDITORWIDGETS_H |
6 | |
7 | #include <QAbstractButton> |
8 | #include <QIcon> |
9 | #include <QImage> |
10 | #include <QLabel> |
11 | #include <QMap> |
12 | #include <QTextEdit> |
13 | #include <QUrl> |
14 | #include <QWidget> |
15 | |
16 | QT_BEGIN_NAMESPACE |
17 | |
18 | class QAction; |
19 | class ; |
20 | class QKeyEvent; |
21 | class ; |
22 | class QSizeF; |
23 | class QString; |
24 | class QVariant; |
25 | |
26 | class MessageHighlighter; |
27 | |
28 | /* |
29 | Automatically adapt height to document contents |
30 | */ |
31 | class ExpandingTextEdit : public QTextEdit |
32 | { |
33 | Q_OBJECT |
34 | |
35 | public: |
36 | ExpandingTextEdit(QWidget *parent = 0); |
37 | QSize sizeHint() const override; |
38 | QSize minimumSizeHint() const override; |
39 | |
40 | private slots: |
41 | void updateHeight(const QSizeF &documentSize); |
42 | void reallyEnsureCursorVisible(); |
43 | |
44 | private: |
45 | int m_minimumHeight; |
46 | }; |
47 | |
48 | /* |
49 | Format markup & control characters |
50 | */ |
51 | class FormatTextEdit : public ExpandingTextEdit |
52 | { |
53 | Q_OBJECT |
54 | public: |
55 | FormatTextEdit(QWidget *parent = 0); |
56 | ~FormatTextEdit(); |
57 | void setEditable(bool editable); |
58 | |
59 | signals: |
60 | void editorDestroyed(); |
61 | |
62 | public slots: |
63 | void setPlainText(const QString & text, bool userAction); |
64 | void setVisualizeWhitespace(bool value); |
65 | |
66 | private: |
67 | MessageHighlighter *m_highlighter; |
68 | }; |
69 | |
70 | /* |
71 | Displays text field & associated label |
72 | */ |
73 | class FormWidget : public QWidget |
74 | { |
75 | Q_OBJECT |
76 | public: |
77 | FormWidget(const QString &label, bool isEditable, QWidget *parent = 0); |
78 | void setLabel(const QString &label) { m_label->setText(label); } |
79 | void setTranslation(const QString &text, bool userAction = false); |
80 | void clearTranslation() { setTranslation(text: QString(), userAction: false); } |
81 | QString getTranslation() { return m_editor->toPlainText(); } |
82 | void setEditingEnabled(bool enable); |
83 | void setHideWhenEmpty(bool optional) { m_hideWhenEmpty = optional; } |
84 | FormatTextEdit *getEditor() { return m_editor; } |
85 | |
86 | signals: |
87 | void textChanged(QTextEdit *); |
88 | void selectionChanged(QTextEdit *); |
89 | void cursorPositionChanged(); |
90 | |
91 | private slots: |
92 | void slotSelectionChanged(); |
93 | void slotTextChanged(); |
94 | |
95 | private: |
96 | QLabel *m_label; |
97 | FormatTextEdit *m_editor; |
98 | bool m_hideWhenEmpty; |
99 | }; |
100 | |
101 | /* |
102 | Displays text fields & associated label |
103 | */ |
104 | class FormMultiWidget : public QWidget |
105 | { |
106 | Q_OBJECT |
107 | public: |
108 | FormMultiWidget(const QString &label, QWidget *parent = 0); |
109 | void setLabel(const QString &label) { m_label->setText(label); } |
110 | void setTranslation(const QString &text, bool userAction = false); |
111 | void clearTranslation() { setTranslation(text: QString(), userAction: false); } |
112 | QString getTranslation() const; |
113 | void setEditingEnabled(bool enable); |
114 | void setMultiEnabled(bool enable); |
115 | void setHideWhenEmpty(bool optional) { m_hideWhenEmpty = optional; } |
116 | const QList<FormatTextEdit *> &getEditors() const { return m_editors; } |
117 | |
118 | signals: |
119 | void editorCreated(QTextEdit *); |
120 | void textChanged(QTextEdit *); |
121 | void selectionChanged(QTextEdit *); |
122 | void cursorPositionChanged(); |
123 | |
124 | protected: |
125 | bool eventFilter(QObject *watched, QEvent *event) override; |
126 | |
127 | private slots: |
128 | void slotTextChanged(); |
129 | void slotSelectionChanged(); |
130 | void minusButtonClicked(); |
131 | void plusButtonClicked(); |
132 | |
133 | private: |
134 | void addEditor(int idx); |
135 | void updateLayout(); |
136 | |
137 | template<typename Func> |
138 | QAbstractButton *makeButton(const QIcon &icon, Func slot) |
139 | { |
140 | auto *button = makeButton(icon); |
141 | connect(button, &QAbstractButton::clicked, |
142 | this, slot); |
143 | return button; |
144 | } |
145 | QAbstractButton *makeButton(const QIcon &icon); |
146 | void insertEditor(int idx); |
147 | void deleteEditor(int idx); |
148 | |
149 | QLabel *m_label; |
150 | QList<FormatTextEdit *> m_editors; |
151 | QList<QWidget *> m_plusButtons; |
152 | QList<QAbstractButton *> m_minusButtons; |
153 | bool m_hideWhenEmpty; |
154 | bool m_multiEnabled; |
155 | QIcon m_plusIcon, m_minusIcon; |
156 | }; |
157 | |
158 | QT_END_NAMESPACE |
159 | |
160 | #endif // MESSAGEEDITORWIDGETS_H |
161 | |