1 /***************************************************************************
2 * Copyright (C) 2005/06 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "bufferview.h"
23 #include "inputline.h"
24 #include "tabcompleter.h"
26 InputLine::InputLine(QWidget *parent)
29 tabCompleter(new TabCompleter(this))
31 connect(this, SIGNAL(returnPressed()), this, SLOT(on_returnPressed()));
32 connect(this, SIGNAL(textChanged(QString)), this, SLOT(on_textChanged(QString)));
35 InputLine::~InputLine() {
38 bool InputLine::eventFilter(QObject *watched, QEvent *event) {
39 if(event->type() != QEvent::KeyPress)
42 // keys from BufferView should be sent to (and focus) the input line
43 BufferView *view = qobject_cast<BufferView *>(watched);
45 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
46 if(keyEvent->text().length() == 1 && !(keyEvent->modifiers() & (Qt::ControlModifier ^ Qt::AltModifier)) ) { // normal key press
47 QChar c = keyEvent->text().at(0);
48 if(c.isLetterOrNumber() || c.isSpace() || c.isPunct() || c.isSymbol()) {
50 keyPressEvent(keyEvent);
59 void InputLine::keyPressEvent(QKeyEvent * event) {
60 switch(event->key()) {
64 addToHistory(text(), true);
76 addToHistory(text(), true);
78 if(idx < history.count()) {
80 if(idx < history.count() || tempHistory.contains(idx)) // tempHistory might have an entry for idx == history.count() + 1
83 resetLine(); // equals clear() in this case
91 case Qt::Key_Select: // for Qtopia
95 QLineEdit::keyPressEvent(event);
99 bool InputLine::addToHistory(const QString &text, bool temporary) {
103 Q_ASSERT(0 <= idx && idx <= history.count());
105 if(history.isEmpty() || text != history[idx - (int)(idx == history.count())]) {
106 // if an entry of the history is changed, we remember it and show it again at this
107 // position until a line was actually sent
108 // sent lines get appended to the history
110 tempHistory[idx] = text;
121 void InputLine::on_returnPressed() {
122 addToHistory(text());
123 emit sendText(text());
127 void InputLine::on_textChanged(QString newText) {
128 QStringList lineSeparators;
129 lineSeparators << QString("\r\n")
134 foreach(QString separator, lineSeparators) {
135 if(newText.contains(separator)) {
141 if(lineSep.isEmpty())
144 QStringList lines = newText.split(lineSep);
147 if(lines.count() > 4) {
148 QString msg = tr("Do you really want to paste %1 lines?").arg(lines.count());
150 for(int i = 0; i < 3; i++) {
151 msg += lines[i].left(40);
152 if(lines[i].count() > 40)
157 QMessageBox question(QMessageBox::NoIcon, tr("Paste Protection"), msg, QMessageBox::Yes|QMessageBox::No);
158 question.setDefaultButton(QMessageBox::No);
159 question.setWindowFlags(question.windowFlags() | Qt::Sheet);
160 if(question.exec() == QMessageBox::No)
164 foreach(QString line, lines) {
167 emit returnPressed();
169 // if(newText.contains(lineSep)) {
171 // QString line = newText.section(lineSep, 0, 0);
172 // QString remainder = newText.section(lineSep, 1);
174 // emit returnPressed();
175 // insert(remainder);
179 void InputLine::resetLine() {
180 // every time the InputLine is cleared we also reset history index
181 idx = history.count();
185 void InputLine::showHistoryEntry() {
186 // if the user changed the history, display the changed line
187 tempHistory.contains(idx) ? setText(tempHistory[idx]) : setText(history[idx]);