File indexing completed on 2024-05-26 04:59:46

0001 /*
0002     SPDX-FileCopyrightText: 2007-2009 Sergio Pistone <sergio_pistone@yahoo.com.ar>
0003     SPDX-FileCopyrightText: 2010-2022 Mladen Milinkovic <max@smoothware.net>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef SIMPLERICHTEXTEDIT_H
0009 #define SIMPLERICHTEXTEDIT_H
0010 
0011 #include "core/richstring.h"
0012 
0013 #include <KTextEdit>
0014 
0015 #include <QKeySequence>
0016 #include <QVector>
0017 #include <QAction>
0018 
0019 QT_FORWARD_DECLARE_CLASS(QEvent)
0020 QT_FORWARD_DECLARE_CLASS(QKeyEvent)
0021 QT_FORWARD_DECLARE_CLASS(QFocusEvent)
0022 QT_FORWARD_DECLARE_CLASS(QMenu)
0023 
0024 namespace SubtitleComposer {
0025 
0026 class SimpleRichTextEdit : public KTextEdit
0027 {
0028     Q_OBJECT
0029 
0030 public:
0031     typedef enum {
0032         Undo = 0, Redo,
0033         Cut, Copy, Paste, Delete, Clear, SelectAll,
0034         ToggleBold, ToggleItalic, ToggleUnderline, ToggleStrikeOut,
0035         CheckSpelling, ToggleAutoSpellChecking,
0036         AllowTabulations, ChangeTextColor,
0037         ActionCount
0038     } Action;
0039 
0040     explicit SimpleRichTextEdit(QWidget *parent = nullptr);
0041     virtual ~SimpleRichTextEdit();
0042 
0043     inline bool hasSelection() const { return textCursor().hasSelection(); }
0044     inline QString selectedText() const { return textCursor().selectedText(); }
0045 
0046     const QTextCharFormat charFormat() const;
0047 
0048     inline QAction * action(int action) const { return action >= 0 && action < ActionCount ? m_actions[action] : nullptr; }
0049     inline QList<QAction *> actions() const { return m_actions.toList(); }
0050 
0051     bool event(QEvent *event) override;
0052 
0053 public slots:
0054     void setSelection(int startIndex, int endIndex);
0055     void clearSelection();
0056 
0057     inline void setFontBold(bool enabled) { setFontWeight(enabled ? QFont::Bold : QFont::Normal); }
0058     inline void setFontStrikeOut(bool enabled) { QTextCharFormat f; f.setFontStrikeOut(enabled); textCursor().mergeCharFormat(f); }
0059 
0060     inline void toggleFontBold() { setFontBold(charFormat().fontWeight() != QFont::Bold); }
0061     inline void toggleFontItalic() { setFontItalic(!charFormat().fontItalic()); }
0062     inline void toggleFontUnderline() { setFontUnderline(!charFormat().fontUnderline()); }
0063     inline void toggleFontStrikeOut() { setFontStrikeOut(!charFormat().fontStrikeOut()); }
0064     void changeTextColor();
0065     void changeTextClass();
0066     void changeTextVoice();
0067 
0068     void deleteText();
0069     void undoableClear();
0070 
0071     inline void toggleTabChangesFocus() { setTabChangesFocus(!tabChangesFocus()); }
0072     inline void toggleAutoSpellChecking() { setCheckSpellingEnabled(!checkSpellingEnabled()); }
0073 
0074 protected:
0075     void setupActions();
0076     QMenu * createContextMenu(const QPoint &mousePos);
0077 
0078     void contextMenuEvent(QContextMenuEvent *event) override;
0079     void keyPressEvent(QKeyEvent *event) override;
0080 
0081     void setupWordUnderPositionCursor(const QPoint &globalPos);
0082 
0083 protected slots:
0084     void addToIgnoreList();
0085     void addToDictionary();
0086     void replaceWithSuggestion();
0087 
0088 protected:
0089     QVector<QAction *> m_actions = QVector<QAction *>(ActionCount, nullptr);
0090     QMenu *m_insertUnicodeControlCharMenu;
0091     QTextCursor m_selectedWordCursor;
0092     bool m_ignoreTextUpdate = false;
0093 };
0094 
0095 }
0096 
0097 #endif