1/***************************************************************************
2 * Copyright (C) 2005-2014 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
35class QKeyEvent;
36class TabCompleter;
37
38class MultiLineEdit : public MultiLineEditParent
39{
40 Q_OBJECT
41
42public:
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
76public 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 void setLineWrapEnabled(bool enable = false);
85
86 inline void setHistory(QStringList history) { _history = history; }
87 inline void setTempHistory(QHash<int, QString> tempHistory) { _tempHistory = tempHistory; }
88 inline void setIdx(qint32 idx) { _idx = idx; }
89
90signals:
91 void textEntered(const QString &text);
92 void noTextEntered();
93
94protected:
95 virtual bool event(QEvent *e);
96 virtual void keyPressEvent(QKeyEvent *event);
97 virtual void resizeEvent(QResizeEvent *event);
98
99private slots:
100 void on_returnPressed();
101 void on_returnPressed(const QString &text);
102 void on_textChanged();
103 void on_documentHeightChanged(qreal height);
104
105 bool addToHistory(const QString &text, bool temporary = false);
106 void historyMoveForward();
107 void historyMoveBack();
108
109 QString convertRichtextToMircCodes();
110 QString convertMircCodesToHtml(const QString &text);
111 bool mircCodesChanged(QTextCursor &cursor, QTextCursor &peekcursor);
112
113private:
114 QStringList _history;
115 QHash<int, QString> _tempHistory;
116 qint32 _idx;
117 Mode _mode;
118 bool _singleLine;
119 int _minHeight;
120 int _maxHeight;
121 bool _scrollBarsEnabled;
122 bool _pasteProtectionEnabled;
123 bool _emacsMode;
124
125 QSize _sizeHint;
126 qreal _lastDocumentHeight;
127
128 QMap<QString, QString> _mircColorMap;
129
130 void reset();
131 void showHistoryEntry();
132 void updateScrollBars();
133 void updateSizeHint();
134};
135
136
137#endif
138