Revert "Reset the BufferViewFilter after setting a new config"
[quassel.git] / src / uisupport / multilineedit.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #ifndef MULTILINEEDIT_H_
22 #define MULTILINEEDIT_H_
23
24 #include <QKeyEvent>
25 #include <QHash>
26 #include <QTextEdit>
27
28 #ifdef HAVE_KDE
29 #  include <KDE/KTextEdit>
30 #endif
31
32 class QKeyEvent;
33 class TabCompleter;
34
35 class MultiLineEdit : public
36 #ifdef HAVE_KDE
37                   KTextEdit
38 #else
39                   QTextEdit
40 #endif
41 {
42   Q_OBJECT
43
44 public:
45   enum Mode {
46     SingleLine,
47     MultiLine
48   };
49
50   MultiLineEdit(QWidget *parent = 0);
51   ~MultiLineEdit();
52
53   void setCustomFont(const QFont &); // should be used instead setFont(), so we can set our size correctly
54
55   // Compatibility methods with the rest of the classes which still expect this to be a QLineEdit
56   inline QString text() const { return toPlainText(); }
57   inline QString html() const { return toHtml(); }
58   inline int cursorPosition() const { return textCursor().position(); }
59   inline void insert(const QString &newText) { insertPlainText(newText); }
60   inline void backspace() { keyPressEvent(new QKeyEvent(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier)); }
61   inline bool hasSelectedText() const { return textCursor().hasSelection(); }
62
63   inline bool isSingleLine() const { return _singleLine; }
64   inline bool pasteProtectionEnabled() const { return _pasteProtectionEnabled; }
65
66   virtual QSize sizeHint() const;
67   virtual QSize minimumSizeHint() const;
68
69   inline QString mircColorFromRGB(QString rgbColor) const { return _mircColorMap.key(rgbColor); }
70   inline QString rgbColorFromMirc(QString mircColor) const { return _mircColorMap[mircColor]; }
71   inline QMap<QString, QString>  mircColorMap() const { return _mircColorMap; }
72
73   inline QStringList history() const { return _history; }
74   inline QHash<int, QString> tempHistory() const { return _tempHistory; }
75   inline qint32 idx() const { return _idx; }
76
77 public slots:
78   void setMode(Mode mode);
79   void setMinHeight(int numLines);
80   void setMaxHeight(int numLines);
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 void keyPressEvent(QKeyEvent * event);
98   virtual void resizeEvent(QResizeEvent *event);
99
100 private slots:
101   void on_returnPressed();
102   void on_returnPressed(const QString &text);
103   void on_textChanged();
104   void on_documentHeightChanged(qreal height);
105
106   bool addToHistory(const QString &text, bool temporary = false);
107   void historyMoveForward();
108   void historyMoveBack();
109
110   QString convertRichtextToMircCodes();
111   QString convertMircCodesToHtml(const QString &text);
112   bool mircCodesChanged(QTextCursor &cursor, QTextCursor &peekcursor);
113
114 private:
115   QStringList _history;
116   QHash<int, QString> _tempHistory;
117   qint32 _idx;
118   Mode _mode;
119   bool _singleLine;
120   int _minHeight;
121   int _maxHeight;
122   bool _scrollBarsEnabled;
123   bool _pasteProtectionEnabled;
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 #endif