Add support for adding a space when tab-completing mid-sentence
[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
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   Q_OBJECT
40
41 public:
42   enum Mode {
43     SingleLine,
44     MultiLine
45   };
46
47   MultiLineEdit(QWidget *parent = 0);
48   ~MultiLineEdit();
49
50   void setCustomFont(const QFont &); // should be used instead setFont(), so we can set our size correctly
51
52   // Compatibility methods with the rest of the classes which still expect this to be a QLineEdit
53   inline QString text() const { return toPlainText(); }
54   inline QString html() const { return toHtml(); }
55   inline int cursorPosition() const { return textCursor().position(); }
56   inline void insert(const QString &newText) { insertPlainText(newText); }
57   inline void backspace() { keyPressEvent(new QKeyEvent(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier)); }
58   inline bool hasSelectedText() const { return textCursor().hasSelection(); }
59
60   inline bool isSingleLine() const { return _singleLine; }
61   inline bool pasteProtectionEnabled() const { return _pasteProtectionEnabled; }
62
63   virtual QSize sizeHint() const;
64   virtual QSize minimumSizeHint() const;
65
66   inline QString mircColorFromRGB(QString rgbColor) const { return _mircColorMap.key(rgbColor); }
67   inline QString rgbColorFromMirc(QString mircColor) const { return _mircColorMap[mircColor]; }
68   inline QMap<QString, QString>  mircColorMap() const { return _mircColorMap; }
69
70   inline QStringList history() const { return _history; }
71   inline QHash<int, QString> tempHistory() const { return _tempHistory; }
72   inline qint32 idx() const { return _idx; }
73   inline bool emacsMode() const { return _emacsMode; }
74
75 public slots:
76   void setMode(Mode mode);
77   void setMinHeight(int numLines);
78   void setMaxHeight(int numLines);
79   void setEmacsMode(bool enable = true);
80   void setScrollBarsEnabled(bool enable = true);
81   void setSpellCheckEnabled(bool enable = true);
82   void setPasteProtectionEnabled(bool enable = true, QWidget *msgBoxParent = 0);
83
84   // Note: Enabling wrap will make isSingleLine() not work correctly, so only use this if minHeight() > 1!
85   void setWordWrapEnabled(bool enable = true);
86
87   inline void setHistory(QStringList history) { _history = history; }
88   inline void setTempHistory(QHash<int, QString> tempHistory) { _tempHistory = tempHistory; }
89   inline void setIdx(qint32 idx) { _idx = idx; }
90
91 signals:
92   void textEntered(const QString &text);
93   void noTextEntered();
94
95 protected:
96   virtual bool event(QEvent *e);
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   bool _emacsMode;
125
126   QSize _sizeHint;
127   qreal _lastDocumentHeight;
128
129   QMap<QString, QString> _mircColorMap;
130
131   void reset();
132   void showHistoryEntry();
133   void updateScrollBars();
134   void updateSizeHint();
135 };
136
137 #endif