Fix ALL the license headers!
[quassel.git] / src / uisupport / multilineedit.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2012 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_KDE
28 #  include <KDE/KTextEdit>
29 #  define MultiLineEditParent KTextEdit
30 #else
31 #  include <QTextEdit>
32 #  define MultiLineEditParent QTextEdit
33 #endif
34
35 class QKeyEvent;
36 class TabCompleter;
37
38 class MultiLineEdit : public MultiLineEditParent
39 {
40     Q_OBJECT
41
42 public:
43     enum Mode {
44         SingleLine,
45         MultiLine
46     };
47
48     MultiLineEdit(QWidget *parent = 0);
49     ~MultiLineEdit();
50
51     void setCustomFont(const QFont &); // should be used instead setFont(), so we can set our size correctly
52
53     // Compatibility methods with the rest of the classes which still expect this to be a QLineEdit
54     inline QString text() const { return toPlainText(); }
55     inline QString html() const { return toHtml(); }
56     inline int cursorPosition() const { return textCursor().position(); }
57     inline void insert(const QString &newText) { insertPlainText(newText); }
58     inline void backspace() { keyPressEvent(new QKeyEvent(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier)); }
59     inline bool hasSelectedText() const { return textCursor().hasSelection(); }
60
61     inline bool isSingleLine() const { return _singleLine; }
62     inline bool pasteProtectionEnabled() const { return _pasteProtectionEnabled; }
63
64     virtual QSize sizeHint() const;
65     virtual QSize minimumSizeHint() const;
66
67     inline QString mircColorFromRGB(QString rgbColor) const { return _mircColorMap.key(rgbColor); }
68     inline QString rgbColorFromMirc(QString mircColor) const { return _mircColorMap[mircColor]; }
69     inline QMap<QString, QString>  mircColorMap() const { return _mircColorMap; }
70
71     inline QStringList history() const { return _history; }
72     inline QHash<int, QString> tempHistory() const { return _tempHistory; }
73     inline qint32 idx() const { return _idx; }
74     inline bool emacsMode() const { return _emacsMode; }
75
76 public slots:
77     void setMode(Mode mode);
78     void setMinHeight(int numLines);
79     void setMaxHeight(int numLines);
80     void setEmacsMode(bool enable = true);
81     void setScrollBarsEnabled(bool enable = true);
82     void setSpellCheckEnabled(bool enable = true);
83     void setPasteProtectionEnabled(bool enable = true, QWidget *msgBoxParent = 0);
84
85     // Note: Enabling wrap will make isSingleLine() not work correctly, so only use this if minHeight() > 1!
86     void setWordWrapEnabled(bool enable = true);
87
88     inline void setHistory(QStringList history) { _history = history; }
89     inline void setTempHistory(QHash<int, QString> tempHistory) { _tempHistory = tempHistory; }
90     inline void setIdx(qint32 idx) { _idx = idx; }
91
92 signals:
93     void textEntered(const QString &text);
94     void noTextEntered();
95
96 protected:
97     virtual bool event(QEvent *e);
98     virtual void keyPressEvent(QKeyEvent *event);
99     virtual void resizeEvent(QResizeEvent *event);
100
101 private slots:
102     void on_returnPressed();
103     void on_returnPressed(const QString &text);
104     void on_textChanged();
105     void on_documentHeightChanged(qreal height);
106
107     bool addToHistory(const QString &text, bool temporary = false);
108     void historyMoveForward();
109     void historyMoveBack();
110
111     QString convertRichtextToMircCodes();
112     QString convertMircCodesToHtml(const QString &text);
113     bool mircCodesChanged(QTextCursor &cursor, QTextCursor &peekcursor);
114
115 private:
116     QStringList _history;
117     QHash<int, QString> _tempHistory;
118     qint32 _idx;
119     Mode _mode;
120     bool _singleLine;
121     int _minHeight;
122     int _maxHeight;
123     bool _scrollBarsEnabled;
124     bool _pasteProtectionEnabled;
125     bool _emacsMode;
126
127     QSize _sizeHint;
128     qreal _lastDocumentHeight;
129
130     QMap<QString, QString> _mircColorMap;
131
132     void reset();
133     void showHistoryEntry();
134     void updateScrollBars();
135     void updateSizeHint();
136 };
137
138
139 #endif