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 #if QT_VERSION >= 0x040500
39 document()->setDocumentMargin(0);
41 setMaximumHeight(document()->size().toSize().height());
42 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
43 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
44 setAcceptRichText(false);
45 setLineWrapMode(NoWrap);
46 enableFindReplace(false);
47 connect(this, SIGNAL(textChanged()), this, SLOT(on_textChanged()));
50 connect(this, SIGNAL(returnPressed()), this, SLOT(on_returnPressed()));
51 connect(this, SIGNAL(textChanged(QString)), this, SLOT(on_textChanged(QString)));
54 InputLine::~InputLine() {
57 void InputLine::setCustomFont(const QFont &font) {
60 setMaximumHeight(document()->size().toSize().height() + 2*frameWidth());
64 bool InputLine::eventFilter(QObject *watched, QEvent *event) {
65 if(event->type() != QEvent::KeyPress)
68 // keys from BufferView should be sent to (and focus) the input line
69 BufferView *view = qobject_cast<BufferView *>(watched);
71 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
72 if(keyEvent->text().length() == 1 && !(keyEvent->modifiers() & (Qt::ControlModifier ^ Qt::AltModifier)) ) { // normal key press
73 QChar c = keyEvent->text().at(0);
74 if(c.isLetterOrNumber() || c.isSpace() || c.isPunct() || c.isSymbol()) {
76 keyPressEvent(keyEvent);
85 void InputLine::keyPressEvent(QKeyEvent * event) {
88 if(event->matches(QKeySequence::Find)) {
89 QAction *act = GraphicalUi::actionCollection()->action("ToggleSearchBar");
98 switch(event->key()) {
102 addToHistory(text(), true);
114 addToHistory(text(), true);
116 if(idx < history.count()) {
118 if(idx < history.count() || tempHistory.contains(idx)) // tempHistory might have an entry for idx == history.count() + 1
121 resetLine(); // equals clear() in this case
123 addToHistory(text());
129 case Qt::Key_Select: // for Qtopia
130 emit returnPressed();
134 //Since this is a ktextedit, we don't have this signal "natively"
138 emit returnPressed();
145 KTextEdit::keyPressEvent(event);
147 QLineEdit::keyPressEvent(event);
152 bool InputLine::addToHistory(const QString &text, bool temporary) {
156 Q_ASSERT(0 <= idx && idx <= history.count());
158 if(history.isEmpty() || text != history[idx - (int)(idx == history.count())]) {
159 // if an entry of the history is changed, we remember it and show it again at this
160 // position until a line was actually sent
161 // sent lines get appended to the history
163 tempHistory[idx] = text;
174 void InputLine::on_returnPressed() {
175 if(!text().isEmpty()) {
176 addToHistory(text());
177 emit sendText(text());
182 void InputLine::on_textChanged(QString newText) {
183 QStringList lineSeparators;
184 lineSeparators << QString("\r\n")
189 foreach(QString separator, lineSeparators) {
190 if(newText.contains(separator)) {
196 if(lineSep.isEmpty())
199 QStringList lines = newText.split(lineSep);
202 if(lines.count() >= 4) {
203 QString msg = tr("Do you really want to paste %n lines?", "", lines.count());
205 for(int i = 0; i < 3; i++) {
206 msg += lines[i].left(40);
207 if(lines[i].count() > 40)
212 QMessageBox question(QMessageBox::NoIcon, tr("Paste Protection"), msg, QMessageBox::Yes|QMessageBox::No);
213 question.setDefaultButton(QMessageBox::No);
215 question.setWindowFlags(question.windowFlags() | Qt::Sheet);
217 if(question.exec() == QMessageBox::No)
221 foreach(QString line, lines) {
222 if(!line.isEmpty()) {
225 emit returnPressed();
228 // if(newText.contains(lineSep)) {
230 // QString line = newText.section(lineSep, 0, 0);
231 // QString remainder = newText.section(lineSep, 1);
233 // emit returnPressed();
234 // insert(remainder);
238 void InputLine::resetLine() {
239 // every time the InputLine is cleared we also reset history index
240 idx = history.count();
244 void InputLine::showHistoryEntry() {
245 // if the user changed the history, display the changed line
246 tempHistory.contains(idx) ? setText(tempHistory[idx]) : setText(history[idx]);
248 QTextCursor cursor = textCursor();
249 cursor.movePosition(QTextCursor::End);
250 setTextCursor(cursor);