cmake: avoid de-duplication of user's CXXFLAGS
[quassel.git] / src / uisupport / multilineedit.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2022 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 #pragma once
22
23 #include "uisupport-export.h"
24
25 #include <QHash>
26 #include <QKeyEvent>
27
28 #if defined HAVE_KF5
29 #    include <KTextWidgets/KTextEdit>
30 #    define MultiLineEditParent KTextEdit
31 #else
32 #    include <QTextEdit>
33 #    define MultiLineEditParent QTextEdit
34 #endif
35
36 #if defined HAVE_SONNET && !defined HAVE_KDE
37 #    include <QContextMenuEvent>
38
39 #    include <Sonnet/Highlighter>
40 #    include <Sonnet/SpellCheckDecorator>
41 #endif
42
43 class UISUPPORT_EXPORT MultiLineEdit : public MultiLineEditParent
44 {
45     Q_OBJECT
46
47 public:
48     enum Mode
49     {
50         SingleLine,
51         MultiLine
52     };
53
54     MultiLineEdit(QWidget* parent = nullptr);
55
56     void setCustomFont(const QFont&);  // should be used instead setFont(), so we can set our size correctly
57
58     // Compatibility methods with the rest of the classes which still expect this to be a QLineEdit
59     inline QString text() const { return toPlainText(); }
60     inline QString html() const { return toHtml(); }
61     inline int cursorPosition() const { return textCursor().position(); }
62     inline void insert(const QString& newText) { insertPlainText(newText); }
63     inline void backspace() { keyPressEvent(new QKeyEvent(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier)); }
64     inline bool hasSelectedText() const { return textCursor().hasSelection(); }
65
66     inline bool isSingleLine() const { return _singleLine; }
67     inline bool pasteProtectionEnabled() const { return _pasteProtectionEnabled; }
68
69     QSize sizeHint() const override;
70     QSize minimumSizeHint() const override;
71
72     inline QString mircColorFromRGB(QString rgbColor) const { return _mircColorMap.key(rgbColor); }
73     inline QString rgbColorFromMirc(QString mircColor) const { return _mircColorMap[mircColor]; }
74     inline QMap<QString, QString> mircColorMap() const { return _mircColorMap; }
75
76     inline QStringList history() const { return _history; }
77     inline QHash<int, QString> tempHistory() const { return _tempHistory; }
78     inline qint32 idx() const { return _idx; }
79     inline bool emacsMode() const { return _emacsMode; }
80
81     void addCompletionSpace();
82
83 public slots:
84     void setMode(Mode mode);
85     void setMinHeight(int numLines);
86     void setMaxHeight(int numLines);
87     void setEmacsMode(bool enable = true);
88     void setScrollBarsEnabled(bool enable = true);
89     void setPasteProtectionEnabled(bool enable = true, QWidget* msgBoxParent = nullptr);
90     void setLineWrapEnabled(bool enable = false);
91
92     inline void setHistory(QStringList history) { _history = history; }
93     inline void setTempHistory(QHash<int, QString> tempHistory) { _tempHistory = tempHistory; }
94     inline void setIdx(qint32 idx) { _idx = idx; }
95
96 signals:
97     void textEntered(const QString& text);
98     void noTextEntered();
99
100 protected:
101     bool event(QEvent* e) override;
102     void keyPressEvent(QKeyEvent* event) override;
103     void resizeEvent(QResizeEvent* event) override;
104
105 #if defined HAVE_SONNET && !defined HAVE_KDE
106     void contextMenuEvent(QContextMenuEvent* event) override;
107 #endif
108
109 private slots:
110     void on_returnPressed();
111     void on_returnPressed(QString text);
112     void on_textChanged();
113     void on_documentHeightChanged(qreal height);
114
115     bool addToHistory(const QString& text, bool temporary = false);
116     void historyMoveForward();
117     void historyMoveBack();
118
119     QString convertRichtextToMircCodes();
120     QString convertMircCodesToHtml(const QString& text);
121     bool mircCodesChanged(QTextCursor& cursor, QTextCursor& peekcursor);
122
123 private:
124     void reset();
125     void showHistoryEntry();
126     void updateScrollBars();
127     void updateSizeHint();
128
129 private:
130     QStringList _history;
131     QHash<int, QString> _tempHistory;
132     qint32 _idx{0};
133     Mode _mode{SingleLine};
134     bool _singleLine{true};
135     int _minHeight{1};
136     int _maxHeight{5};
137     bool _scrollBarsEnabled{true};
138     bool _pasteProtectionEnabled{true};
139     bool _emacsMode{false};
140     int _completionSpace{0};
141
142     QSize _sizeHint;
143     qreal _lastDocumentHeight{-1};
144
145     QMap<QString, QString> _mircColorMap;
146
147 #if defined HAVE_SONNET && !defined HAVE_KDE
148     // This member function is provided by KTextEdit
149     Sonnet::Highlighter* highlighter() const;
150
151 private slots:
152     void setSpellCheckEnabled(bool enabled);
153
154 private:
155     Sonnet::SpellCheckDecorator* _spellCheckDecorator{nullptr};
156 #endif
157 };