src: Yearly copyright bump
[quassel.git] / src / uisupport / multilineedit.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #ifndef MULTILINEEDIT_H_
22 #define MULTILINEEDIT_H_
23
24 #include <QKeyEvent>
25 #include <QHash>
26
27 #ifdef HAVE_KDE4
28 #  include <KDE/KTextEdit>
29 #  define MultiLineEditParent KTextEdit
30 #elif defined HAVE_KF5
31 #  include <KTextWidgets/KTextEdit>
32 #  define MultiLineEditParent KTextEdit
33 #else
34 #  include <QTextEdit>
35 #  define MultiLineEditParent QTextEdit
36 #endif
37
38 #if defined HAVE_SONNET && !defined HAVE_KDE
39 #  include <QContextMenuEvent>
40 #  include <Sonnet/Highlighter>
41 #  include <Sonnet/SpellCheckDecorator>
42 #endif
43
44 class MultiLineEdit : public MultiLineEditParent
45 {
46     Q_OBJECT
47
48 public:
49     enum Mode {
50         SingleLine,
51         MultiLine
52     };
53
54     MultiLineEdit(QWidget *parent = 0);
55     ~MultiLineEdit();
56
57     void setCustomFont(const QFont &); // should be used instead setFont(), so we can set our size correctly
58
59     // Compatibility methods with the rest of the classes which still expect this to be a QLineEdit
60     inline QString text() const { return toPlainText(); }
61     inline QString html() const { return toHtml(); }
62     inline int cursorPosition() const { return textCursor().position(); }
63     inline void insert(const QString &newText) { insertPlainText(newText); }
64     inline void backspace() { keyPressEvent(new QKeyEvent(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier)); }
65     inline bool hasSelectedText() const { return textCursor().hasSelection(); }
66
67     inline bool isSingleLine() const { return _singleLine; }
68     inline bool pasteProtectionEnabled() const { return _pasteProtectionEnabled; }
69
70     QSize sizeHint() const override;
71     QSize minimumSizeHint() const override;
72
73     inline QString mircColorFromRGB(QString rgbColor) const { return _mircColorMap.key(rgbColor); }
74     inline QString rgbColorFromMirc(QString mircColor) const { return _mircColorMap[mircColor]; }
75     inline QMap<QString, QString>  mircColorMap() const { return _mircColorMap; }
76
77     inline QStringList history() const { return _history; }
78     inline QHash<int, QString> tempHistory() const { return _tempHistory; }
79     inline qint32 idx() const { return _idx; }
80     inline bool emacsMode() const { return _emacsMode; }
81
82     void addCompletionSpace();
83
84 public slots:
85     void setMode(Mode mode);
86     void setMinHeight(int numLines);
87     void setMaxHeight(int numLines);
88     void setEmacsMode(bool enable = true);
89     void setScrollBarsEnabled(bool enable = true);
90     void setPasteProtectionEnabled(bool enable = true, QWidget *msgBoxParent = 0);
91     void setLineWrapEnabled(bool enable = false);
92
93     inline void setHistory(QStringList history) { _history = history; }
94     inline void setTempHistory(QHash<int, QString> tempHistory) { _tempHistory = tempHistory; }
95     inline void setIdx(qint32 idx) { _idx = idx; }
96
97 signals:
98     void textEntered(const QString &text);
99     void noTextEntered();
100
101 protected:
102     bool event(QEvent *e) override;
103     void keyPressEvent(QKeyEvent *event) override;
104     void resizeEvent(QResizeEvent *event) override;
105
106 #if defined HAVE_SONNET && !defined HAVE_KDE
107     void contextMenuEvent(QContextMenuEvent *event) override;
108 #endif
109
110 private slots:
111     void on_returnPressed();
112     void on_returnPressed(QString text);
113     void on_textChanged();
114     void on_documentHeightChanged(qreal height);
115
116     bool addToHistory(const QString &text, bool temporary = false);
117     void historyMoveForward();
118     void historyMoveBack();
119
120     QString convertRichtextToMircCodes();
121     QString convertMircCodesToHtml(const QString &text);
122     bool mircCodesChanged(QTextCursor &cursor, QTextCursor &peekcursor);
123
124 private:
125     void reset();
126     void showHistoryEntry();
127     void updateScrollBars();
128     void updateSizeHint();
129
130 private:
131     QStringList _history;
132     QHash<int, QString> _tempHistory;
133     qint32 _idx;
134     Mode _mode;
135     bool _singleLine;
136     int _minHeight;
137     int _maxHeight;
138     bool _scrollBarsEnabled;
139     bool _pasteProtectionEnabled;
140     bool _emacsMode;
141     int _completionSpace;
142
143     QSize _sizeHint;
144     qreal _lastDocumentHeight;
145
146     QMap<QString, QString> _mircColorMap;
147
148 #if defined HAVE_SONNET && !defined HAVE_KDE
149     // This member function is provided by KTextEdit
150     Sonnet::Highlighter *highlighter() const;
151
152 private slots:
153     void setSpellCheckEnabled(bool enabled);
154
155 private:
156     Sonnet::SpellCheckDecorator *_spellCheckDecorator{nullptr};
157 #endif
158 };
159
160
161 #endif