Don't let the inputline eat application shortcuts
[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
74 public slots:
75   void setMode(Mode mode);
76   void setMinHeight(int numLines);
77   void setMaxHeight(int numLines);
78   void setScrollBarsEnabled(bool enable = true);
79   void setSpellCheckEnabled(bool enable = true);
80   void setPasteProtectionEnabled(bool enable = true, QWidget *msgBoxParent = 0);
81
82   // Note: Enabling wrap will make isSingleLine() not work correctly, so only use this if minHeight() > 1!
83   void setWordWrapEnabled(bool enable = true);
84
85   inline void setHistory(QStringList history) { _history = history; }
86   inline void setTempHistory(QHash<int, QString> tempHistory) { _tempHistory = tempHistory; }
87   inline void setIdx(qint32 idx) { _idx = idx; }
88
89 signals:
90   void textEntered(const QString &text);
91   void noTextEntered();
92
93 protected:
94   virtual bool event(QEvent *e);
95   virtual void keyPressEvent(QKeyEvent * event);
96   virtual void resizeEvent(QResizeEvent *event);
97
98 private slots:
99   void on_returnPressed();
100   void on_returnPressed(const QString &text);
101   void on_textChanged();
102   void on_documentHeightChanged(qreal height);
103
104   bool addToHistory(const QString &text, bool temporary = false);
105   void historyMoveForward();
106   void historyMoveBack();
107
108   QString convertRichtextToMircCodes();
109   QString convertMircCodesToHtml(const QString &text);
110   bool mircCodesChanged(QTextCursor &cursor, QTextCursor &peekcursor);
111
112 private:
113   QStringList _history;
114   QHash<int, QString> _tempHistory;
115   qint32 _idx;
116   Mode _mode;
117   bool _singleLine;
118   int _minHeight;
119   int _maxHeight;
120   bool _scrollBarsEnabled;
121   bool _pasteProtectionEnabled;
122
123   QSize _sizeHint;
124   qreal _lastDocumentHeight;
125
126   QMap<QString, QString> _mircColorMap;
127
128   void reset();
129   void showHistoryEntry();
130   void updateScrollBars();
131   void updateSizeHint();
132 };
133
134 #endif