Merge pull request #97 from Bombe/focus-host-input
[quassel.git] / src / uisupport / multilineedit.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2015 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 class QKeyEvent;
39 class TabCompleter;
40
41 class MultiLineEdit : public MultiLineEditParent
42 {
43     Q_OBJECT
44
45 public:
46     enum Mode {
47         SingleLine,
48         MultiLine
49     };
50
51     MultiLineEdit(QWidget *parent = 0);
52     ~MultiLineEdit();
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     virtual QSize sizeHint() const;
68     virtual QSize minimumSizeHint() const;
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 public slots:
80     void setMode(Mode mode);
81     void setMinHeight(int numLines);
82     void setMaxHeight(int numLines);
83     void setEmacsMode(bool enable = true);
84     void setScrollBarsEnabled(bool enable = true);
85     void setSpellCheckEnabled(bool enable = true);
86     void setPasteProtectionEnabled(bool enable = true, QWidget *msgBoxParent = 0);
87     void setLineWrapEnabled(bool enable = false);
88
89     inline void setHistory(QStringList history) { _history = history; }
90     inline void setTempHistory(QHash<int, QString> tempHistory) { _tempHistory = tempHistory; }
91     inline void setIdx(qint32 idx) { _idx = idx; }
92
93 signals:
94     void textEntered(const QString &text);
95     void noTextEntered();
96
97 protected:
98     virtual bool event(QEvent *e);
99     virtual void keyPressEvent(QKeyEvent *event);
100     virtual void resizeEvent(QResizeEvent *event);
101
102 private slots:
103     void on_returnPressed();
104     void on_returnPressed(const QString &text);
105     void on_textChanged();
106     void on_documentHeightChanged(qreal height);
107
108     bool addToHistory(const QString &text, bool temporary = false);
109     void historyMoveForward();
110     void historyMoveBack();
111
112     QString convertRichtextToMircCodes();
113     QString convertMircCodesToHtml(const QString &text);
114     bool mircCodesChanged(QTextCursor &cursor, QTextCursor &peekcursor);
115
116 private:
117     QStringList _history;
118     QHash<int, QString> _tempHistory;
119     qint32 _idx;
120     Mode _mode;
121     bool _singleLine;
122     int _minHeight;
123     int _maxHeight;
124     bool _scrollBarsEnabled;
125     bool _pasteProtectionEnabled;
126     bool _emacsMode;
127
128     QSize _sizeHint;
129     qreal _lastDocumentHeight;
130
131     QMap<QString, QString> _mircColorMap;
132
133     void reset();
134     void showHistoryEntry();
135     void updateScrollBars();
136     void updateSizeHint();
137 };
138
139
140 #endif