d7b18f4716830fa0f926d5bec86fbfc9add1ab73
[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 #include "graphicalui.h"
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   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
40   setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
41   setAcceptRichText(false);
42   setLineWrapMode(NoWrap);
43   enableFindReplace(false);
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 void InputLine::setCustomFont(const QFont &font) {
55   setFont(font);
56 #ifdef HAVE_KDE
57   setMaximumHeight(document()->size().toSize().height());
58 #endif
59 }
60
61 bool InputLine::eventFilter(QObject *watched, QEvent *event) {
62   if(event->type() != QEvent::KeyPress)
63     return false;
64
65   // keys from BufferView should be sent to (and focus) the input line
66   BufferView *view = qobject_cast<BufferView *>(watched);
67   if(view) {
68     QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
69     if(keyEvent->text().length() == 1 && !(keyEvent->modifiers() & (Qt::ControlModifier ^ Qt::AltModifier)) ) { // normal key press
70       QChar c = keyEvent->text().at(0);
71       if(c.isLetterOrNumber() || c.isSpace() || c.isPunct() || c.isSymbol()) {
72         setFocus();
73         keyPressEvent(keyEvent);
74         return true;
75       } else
76         return false;
77     }
78   }
79   return false;
80 }
81
82 void InputLine::keyPressEvent(QKeyEvent * event) {
83
84 #ifdef HAVE_KDE
85   if(event->matches(QKeySequence::Find)) {
86     QAction *act = GraphicalUi::actionCollection()->action("ToggleSearchBar");
87     if(act) {
88       act->toggle();
89       event->accept();
90       return;
91     }
92   }
93 #endif
94
95   switch(event->key()) {
96   case Qt::Key_Up:
97     event->accept();
98
99     addToHistory(text(), true);
100
101     if(idx > 0) {
102       idx--;
103       showHistoryEntry();
104     }
105
106     break;
107
108   case Qt::Key_Down:
109     event->accept();
110
111     addToHistory(text(), true);
112
113     if(idx < history.count()) {
114       idx++;
115       if(idx < history.count() || tempHistory.contains(idx)) // tempHistory might have an entry for idx == history.count() + 1
116         showHistoryEntry();
117       else
118         resetLine();              // equals clear() in this case
119     } else {
120       addToHistory(text());
121       resetLine();
122     }
123
124     break;
125
126   case Qt::Key_Select:          // for Qtopia
127     emit returnPressed();
128     break;
129
130 #ifdef HAVE_KDE
131 //Since this is a ktextedit, we don't have this signal "natively"
132   case Qt::Key_Return:
133     event->accept();
134     emit returnPressed();
135     break;
136
137 #endif
138
139   default:
140 #ifdef HAVE_KDE
141     KTextEdit::keyPressEvent(event);
142 #else
143     QLineEdit::keyPressEvent(event);
144 #endif
145   }
146 }
147
148 bool InputLine::addToHistory(const QString &text, bool temporary) {
149   if(text.isEmpty())
150     return false;
151
152   Q_ASSERT(0 <= idx && idx <= history.count());
153
154   if(history.isEmpty() || text != history[idx - (int)(idx == history.count())]) {
155     // if an entry of the history is changed, we remember it and show it again at this
156     // position until a line was actually sent
157     // sent lines get appended to the history
158     if(temporary) {
159       tempHistory[idx] = text;
160     } else {
161       history << text;
162       tempHistory.clear();
163     }
164     return true;
165   } else {
166     return false;
167   }
168 }
169
170 void InputLine::on_returnPressed() {
171   addToHistory(text());
172   emit sendText(text());
173   resetLine();
174 }
175
176 void InputLine::on_textChanged(QString newText) {
177   QStringList lineSeparators;
178   lineSeparators << QString("\r\n")
179                  << QString('\n')
180                  << QString('\r');
181
182   QString lineSep;
183   foreach(QString separator, lineSeparators) {
184     if(newText.contains(separator)) {
185       lineSep = separator;
186       break;
187     }
188   }
189
190   if(lineSep.isEmpty())
191     return;
192
193   QStringList lines = newText.split(lineSep);
194   clear();
195
196   if(lines.count() >= 4) {
197     QString msg = tr("Do you really want to paste %1 lines?").arg(lines.count());
198     msg += "<p>";
199     for(int i = 0; i < 3; i++) {
200       msg += lines[i].left(40);
201       if(lines[i].count() > 40)
202         msg += "...";
203       msg += "<br />";
204     }
205     msg += "...</p>";
206     QMessageBox question(QMessageBox::NoIcon, tr("Paste Protection"), msg, QMessageBox::Yes|QMessageBox::No);
207     question.setDefaultButton(QMessageBox::No);
208 #ifdef Q_WS_MAC
209     question.setWindowFlags(question.windowFlags() | Qt::Sheet);
210 #endif
211     if(question.exec() == QMessageBox::No)
212       return;
213   }
214
215   foreach(QString line, lines) {
216     clear();
217     insert(line);
218     emit returnPressed();
219   }
220 //   if(newText.contains(lineSep)) {
221 //     clear();
222 //     QString line = newText.section(lineSep, 0, 0);
223 //     QString remainder = newText.section(lineSep, 1);
224 //     insert(line);
225 //     emit returnPressed();
226 //     insert(remainder);
227 //   }
228 }
229
230 void InputLine::resetLine() {
231   // every time the InputLine is cleared we also reset history index
232   idx = history.count();
233   clear();
234 }
235
236 void InputLine::showHistoryEntry() {
237   // if the user changed the history, display the changed line
238   tempHistory.contains(idx) ? setText(tempHistory[idx]) : setText(history[idx]);
239 }