1 /*
2     SPDX-FileCopyrightText: 2007-2009 Sergio Pistone <sergio_pistone@yahoo.com.ar>
3     SPDX-FileCopyrightText: 2010-2019 Mladen Milinkovic <max@smoothware.net>
4 
5     SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #ifndef SIMPLERICHTEXTEDIT_H
9 #define SIMPLERICHTEXTEDIT_H
10 
11 #include "core/sstring.h"
12 
13 #include <KTextEdit>
14 
15 #include <QKeySequence>
16 #include <QVector>
17 #include <QAction>
18 
19 QT_FORWARD_DECLARE_CLASS(QEvent)
QT_FORWARD_DECLARE_CLASS(QKeyEvent)20 QT_FORWARD_DECLARE_CLASS(QKeyEvent)
21 QT_FORWARD_DECLARE_CLASS(QFocusEvent)
22 QT_FORWARD_DECLARE_CLASS(QMenu)
23 
24 namespace SubtitleComposer {
25 
26 class SimpleRichTextEdit : public KTextEdit
27 {
28 	Q_OBJECT
29 
30 public:
31 	typedef enum {
32 		Undo = 0, Redo,
33 		Cut, Copy, Paste, Delete, Clear, SelectAll,
34 		ToggleBold, ToggleItalic, ToggleUnderline, ToggleStrikeOut,
35 		CheckSpelling, ToggleAutoSpellChecking,
36 		AllowTabulations, ChangeTextColor,
37 		ActionCount
38 	} Action;
39 
40 	explicit SimpleRichTextEdit(QWidget *parent = 0);
41 	virtual ~SimpleRichTextEdit();
42 
43 	inline bool hasSelection() const { return textCursor().hasSelection(); }
44 	inline QString selectedText() const { return textCursor().selectedText(); }
45 
46 	inline bool fontBold() { return fontWeight() == QFont::Bold; }
47 	inline bool fontStrikeOut() { return currentFont().strikeOut(); }
48 
49 	inline QAction * action(int action) const { return action >= 0 && action < ActionCount ? m_actions[action] : nullptr; }
50 	inline QList<QAction *> actions() const { return m_actions.toList(); }
51 
52 	bool event(QEvent *event) override;
53 
54 public slots:
55 	void setSelection(int startIndex, int endIndex);
56 	void clearSelection();
57 
58 	inline void setFontBold(bool enabled) { setFontWeight(enabled ? QFont::Bold : QFont::Normal); }
59 	inline void setFontStrikeOut(bool enabled) { QTextCharFormat f; f.setFontStrikeOut(enabled); textCursor().mergeCharFormat(f); }
60 
61 	inline void toggleFontBold() { setFontBold(!fontBold()); }
62 	inline void toggleFontItalic() { setFontItalic(!fontItalic()); }
63 	inline void toggleFontUnderline() { setFontUnderline(!fontUnderline()); }
64 	inline void toggleFontStrikeOut() { setFontStrikeOut(!fontStrikeOut()); }
65 	void changeTextColor();
66 
67 	void deleteText();
68 	void undoableClear();
69 
70 	inline void toggleTabChangesFocus() { setTabChangesFocus(!tabChangesFocus()); }
71 	inline void toggleAutoSpellChecking() { setCheckSpellingEnabled(!checkSpellingEnabled()); }
72 
73 protected:
74 	void setupActions();
75 	QMenu * createContextMenu(const QPoint &mousePos);
76 
77 	void contextMenuEvent(QContextMenuEvent *event) override;
78 	void keyPressEvent(QKeyEvent *event) override;
79 
80 	void setupWordUnderPositionCursor(const QPoint &globalPos);
81 
82 protected slots:
83 	void addToIgnoreList();
84 	void addToDictionary();
85 	void replaceWithSuggestion();
86 
87 protected:
88 	QVector<QAction *> m_actions = QVector<QAction *>(ActionCount, nullptr);
89 	QMenu *m_insertUnicodeControlCharMenu;
90 	QTextCursor m_selectedWordCursor;
91 	bool m_ignoreTextUpdate = false;
92 };
93 
94 }
95 
96 #endif
97