Some more tweaks to the KDEified input line
[quassel.git] / src / uisupport / inputline.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005/06 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 #include "bufferview.h"
22
23 #include "inputline.h"
24 #include "tabcompleter.h"
25
26 InputLine::InputLine(QWidget *parent)
27   :
28 #ifdef HAVE_KDE
29     KTextEdit(parent),
30 #else
31     QLineEdit(parent),
32 #endif
33     idx(0),
34     tabCompleter(new TabCompleter(this))
35 {
36 #ifdef HAVE_KDE
37 //This is done to make the KTextEdit look like a lineedit
38   setMaximumHeight(document()->size().toSize().height());
39   setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
40   setAcceptRichText(false);
41   setLineWrapMode(NoWrap);
42   enableFindReplace(false);
43   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
44   connect(this, SIGNAL(textChanged()), this, SLOT(on_textChanged()));
45 #endif
46
47   connect(this, SIGNAL(returnPressed()), this, SLOT(on_returnPressed()));
48   connect(this, SIGNAL(textChanged(QString)), this, SLOT(on_textChanged(QString)));
49 }
50
51 InputLine::~InputLine() {
52 }
53
54 bool InputLine::eventFilter(QObject *watched, QEvent *event) {
55   if(event->type() != QEvent::KeyPress)
56     return false;
57
58   // keys from BufferView should be sent to (and focus) the input line
59   BufferView *view = qobject_cast<BufferView *>(watched);
60   if(view) {
61     QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
62     if(keyEvent->text().length() == 1 && !(keyEvent->modifiers() & (Qt::ControlModifier ^ Qt::AltModifier)) ) { // normal key press
63       QChar c = keyEvent->text().at(0);
64       if(c.isLetterOrNumber() || c.isSpace() || c.isPunct() || c.isSymbol()) {
65         setFocus();
66         keyPressEvent(keyEvent);
67         return true;
68       } else
69         return false;
70     }
71   }
72   return false;
73 }
74
75 void InputLine::keyPressEvent(QKeyEvent * event) {
76   switch(event->key()) {
77   case Qt::Key_Up:
78     event->accept();
79
80     addToHistory(text(), true);
81
82     if(idx > 0) {
83       idx--;
84       showHistoryEntry();
85     }
86
87     break;
88
89   case Qt::Key_Down:
90     event->accept();
91
92     addToHistory(text(), true);
93
94     if(idx < history.count()) {
95       idx++;
96       if(idx < history.count() || tempHistory.contains(idx)) // tempHistory might have an entry for idx == history.count() + 1
97         showHistoryEntry();
98       else
99         resetLine();              // equals clear() in this case
100     } else {
101       addToHistory(text());
102       resetLine();
103     }
104
105     break;
106
107   case Qt::Key_Select:          // for Qtopia
108     emit returnPressed();
109     break;
110
111 #ifdef HAVE_KDE
112 //Since this is a ktextedit, we don't have this signal "natively"
113   case Qt::Key_Return:
114     event->accept();
115     emit returnPressed();
116     break;
117
118 #endif
119
120   default:
121 #ifdef HAVE_KDE
122     KTextEdit::keyPressEvent(event);
123 #else
124     QLineEdit::keyPressEvent(event);
125 #endif
126   }
127 }
128
129 bool InputLine::addToHistory(const QString &text, bool temporary) {
130   if(text.isEmpty())
131     return false;
132
133   Q_ASSERT(0 <= idx && idx <= history.count());
134
135   if(history.isEmpty() || text != history[idx - (int)(idx == history.count())]) {
136     // if an entry of the history is changed, we remember it and show it again at this
137     // position until a line was actually sent
138     // sent lines get appended to the history
139     if(temporary) {
140       tempHistory[idx] = text;
141     } else {
142       history << text;
143       tempHistory.clear();
144     }
145     return true;
146   } else {
147     return false;
148   }
149 }
150
151 void InputLine::on_returnPressed() {
152   addToHistory(text());
153   emit sendText(text());
154   resetLine();
155 }
156
157 void InputLine::on_textChanged(QString newText) {
158   QStringList lineSeparators;
159   lineSeparators << QString("\r\n")
160                  << QString('\n')
161                  << QString('\r');
162
163   QString lineSep;
164   foreach(QString separator, lineSeparators) {
165     if(newText.contains(separator)) {
166       lineSep = separator;
167       break;
168     }
169   }
170
171   if(lineSep.isEmpty())
172     return;
173
174   QStringList lines = newText.split(lineSep);
175   clear();
176
177   if(lines.count() >= 4) {
178     QString msg = tr("Do you really want to paste %1 lines?").arg(lines.count());
179     msg += "<p>";
180     for(int i = 0; i < 3; i++) {
181       msg += lines[i].left(40);
182       if(lines[i].count() > 40)
183         msg += "...";
184       msg += "<br />";
185     }
186     msg += "...</p>";
187     QMessageBox question(QMessageBox::NoIcon, tr("Paste Protection"), msg, QMessageBox::Yes|QMessageBox::No);
188     question.setDefaultButton(QMessageBox::No);
189 #ifdef Q_WS_MAC
190     question.setWindowFlags(question.windowFlags() | Qt::Sheet);
191 #endif
192     if(question.exec() == QMessageBox::No)
193       return;
194   }
195
196   foreach(QString line, lines) {
197     clear();
198     insert(line);
199     emit returnPressed();
200   }
201 //   if(newText.contains(lineSep)) {
202 //     clear();
203 //     QString line = newText.section(lineSep, 0, 0);
204 //     QString remainder = newText.section(lineSep, 1);
205 //     insert(line);
206 //     emit returnPressed();
207 //     insert(remainder);
208 //   }
209 }
210
211 void InputLine::resetLine() {
212   // every time the InputLine is cleared we also reset history index
213   idx = history.count();
214   clear();
215 }
216
217 void InputLine::showHistoryEntry() {
218   // if the user changed the history, display the changed line
219   tempHistory.contains(idx) ? setText(tempHistory[idx]) : setText(history[idx]);
220 }