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