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"
22 #include "graphicalui.h"
23 #include "inputline.h"
24 #include "tabcompleter.h"
26 InputLine::InputLine(QWidget *parent)
34 tabCompleter(new TabCompleter(this))
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()));
47 connect(this, SIGNAL(returnPressed()), this, SLOT(on_returnPressed()));
48 connect(this, SIGNAL(textChanged(QString)), this, SLOT(on_textChanged(QString)));
51 InputLine::~InputLine() {
54 void InputLine::setCustomFont(const QFont &font) {
57 setMaximumHeight(document()->size().toSize().height());
61 bool InputLine::eventFilter(QObject *watched, QEvent *event) {
62 if(event->type() != QEvent::KeyPress)
65 // keys from BufferView should be sent to (and focus) the input line
66 BufferView *view = qobject_cast<BufferView *>(watched);
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()) {
73 keyPressEvent(keyEvent);
82 void InputLine::keyPressEvent(QKeyEvent * event) {
85 if(event->matches(QKeySequence::Find)) {
86 QAction *act = GraphicalUi::actionCollection()->action("ToggleSearchBar");
95 switch(event->key()) {
99 addToHistory(text(), true);
111 addToHistory(text(), true);
113 if(idx < history.count()) {
115 if(idx < history.count() || tempHistory.contains(idx)) // tempHistory might have an entry for idx == history.count() + 1
118 resetLine(); // equals clear() in this case
120 addToHistory(text());
126 case Qt::Key_Select: // for Qtopia
127 emit returnPressed();
131 //Since this is a ktextedit, we don't have this signal "natively"
135 emit returnPressed();
142 KTextEdit::keyPressEvent(event);
144 QLineEdit::keyPressEvent(event);
149 bool InputLine::addToHistory(const QString &text, bool temporary) {
153 Q_ASSERT(0 <= idx && idx <= history.count());
155 if(history.isEmpty() || text != history[idx - (int)(idx == history.count())]) {
156 // if an entry of the history is changed, we remember it and show it again at this
157 // position until a line was actually sent
158 // sent lines get appended to the history
160 tempHistory[idx] = text;
171 void InputLine::on_returnPressed() {
172 if(!text().isEmpty()) {
173 addToHistory(text());
174 emit sendText(text());
179 void InputLine::on_textChanged(QString newText) {
180 QStringList lineSeparators;
181 lineSeparators << QString("\r\n")
186 foreach(QString separator, lineSeparators) {
187 if(newText.contains(separator)) {
193 if(lineSep.isEmpty())
196 QStringList lines = newText.split(lineSep);
199 if(lines.count() >= 4) {
200 QString msg = tr("Do you really want to paste %n lines?", "", lines.count());
202 for(int i = 0; i < 3; i++) {
203 msg += lines[i].left(40);
204 if(lines[i].count() > 40)
209 QMessageBox question(QMessageBox::NoIcon, tr("Paste Protection"), msg, QMessageBox::Yes|QMessageBox::No);
210 question.setDefaultButton(QMessageBox::No);
212 question.setWindowFlags(question.windowFlags() | Qt::Sheet);
214 if(question.exec() == QMessageBox::No)
218 foreach(QString line, lines) {
219 if(!line.isEmpty()) {
222 emit returnPressed();
225 // if(newText.contains(lineSep)) {
227 // QString line = newText.section(lineSep, 0, 0);
228 // QString remainder = newText.section(lineSep, 1);
230 // emit returnPressed();
231 // insert(remainder);
235 void InputLine::resetLine() {
236 // every time the InputLine is cleared we also reset history index
237 idx = history.count();
241 void InputLine::showHistoryEntry() {
242 // if the user changed the history, display the changed line
243 tempHistory.contains(idx) ? setText(tempHistory[idx]) : setText(history[idx]);
245 QTextCursor cursor = textCursor();
246 cursor.movePosition(QTextCursor::End);
247 setTextCursor(cursor);