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 <QApplication>
23 #include <QMessageBox>
25 #include "bufferview.h"
26 #include "graphicalui.h"
27 #include "inputline.h"
28 #include "tabcompleter.h"
30 const int leftMargin = 3;
32 InputLine::InputLine(QWidget *parent)
40 tabCompleter(new TabCompleter(this))
42 // Make the QTextEdit look like a QLineEdit
43 #if QT_VERSION >= 0x040500
44 document()->setDocumentMargin(0); // new in Qt 4.5 and we really don't want it here
46 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
47 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
48 setAcceptRichText(false);
49 setLineWrapMode(NoWrap);
51 enableFindReplace(false);
55 connect(this, SIGNAL(textChanged()), this, SLOT(on_textChanged()));
56 connect(this, SIGNAL(returnPressed()), this, SLOT(on_returnPressed()));
57 connect(this, SIGNAL(textChanged(QString)), this, SLOT(on_textChanged(QString)));
60 InputLine::~InputLine() {
63 void InputLine::setCustomFont(const QFont &font) {
67 QSize InputLine::sizeHint() const {
68 // use the style to determine a decent size
69 QFontMetrics fm(font());
70 int h = fm.lineSpacing() + 2 * frameWidth();
71 QStyleOptionFrameV2 opt;
73 opt.rect = QRect(0, 0, 100, h);
74 opt.lineWidth = lineWidth();
75 opt.midLineWidth = midLineWidth();
76 opt.state |= QStyle::State_Sunken;
77 QSize s = style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(100, h).expandedTo(QApplication::globalStrut()), this);
81 QSize InputLine::minimumSizeHint() const {
85 bool InputLine::eventFilter(QObject *watched, QEvent *event) {
86 if(event->type() != QEvent::KeyPress)
89 // keys from BufferView should be sent to (and focus) the input line
90 BufferView *view = qobject_cast<BufferView *>(watched);
92 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
93 if(keyEvent->text().length() == 1 && !(keyEvent->modifiers() & (Qt::ControlModifier ^ Qt::AltModifier)) ) { // normal key press
94 QChar c = keyEvent->text().at(0);
95 if(c.isLetterOrNumber() || c.isSpace() || c.isPunct() || c.isSymbol()) {
97 keyPressEvent(keyEvent);
106 void InputLine::keyPressEvent(QKeyEvent * event) {
107 if(event->matches(QKeySequence::Find)) {
108 QAction *act = GraphicalUi::actionCollection()->action("ToggleSearchBar");
116 switch(event->key()) {
120 addToHistory(text(), true);
132 addToHistory(text(), true);
134 if(idx < history.count()) {
136 if(idx < history.count() || tempHistory.contains(idx)) // tempHistory might have an entry for idx == history.count() + 1
139 resetLine(); // equals clear() in this case
141 addToHistory(text());
151 emit returnPressed();
155 QTextEdit::keyPressEvent(event);
159 bool InputLine::addToHistory(const QString &text, bool temporary) {
163 Q_ASSERT(0 <= idx && idx <= history.count());
166 // if an entry of the history is changed, we remember it and show it again at this
167 // position until a line was actually sent
168 // sent lines get appended to the history
169 if(history.isEmpty() || text != history[idx - (int)(idx == history.count())]) {
170 tempHistory[idx] = text;
174 if(history.isEmpty() || text != history.last()) {
183 void InputLine::on_returnPressed() {
184 if(!text().isEmpty()) {
185 addToHistory(text());
186 emit sendText(text());
191 void InputLine::on_textChanged(QString newText) {
192 QStringList lineSeparators;
193 lineSeparators << QString("\r\n")
198 foreach(QString separator, lineSeparators) {
199 if(newText.contains(separator)) {
205 if(lineSep.isEmpty())
208 QStringList lines = newText.split(lineSep, QString::SkipEmptyParts);
210 if(lines.count() >= 4) {
211 QString msg = tr("Do you really want to paste %n lines?", "", lines.count());
213 for(int i = 0; i < 3; i++) {
214 msg += lines[i].left(40);
215 if(lines[i].count() > 40)
220 QMessageBox question(QMessageBox::NoIcon, tr("Paste Protection"), msg, QMessageBox::Yes|QMessageBox::No);
221 question.setDefaultButton(QMessageBox::No);
223 question.setWindowFlags(question.windowFlags() | Qt::Sheet); // Qt::Sheet is not ignored on other platforms as it should :/
225 if(question.exec() == QMessageBox::No) {
231 foreach(QString line, lines) {
232 if(!line.isEmpty()) {
235 emit returnPressed();
239 // if(newText.contains(lineSep)) {
241 // QString line = newText.section(lineSep, 0, 0);
242 // QString remainder = newText.section(lineSep, 1);
244 // emit returnPressed();
245 // insert(remainder);
249 void InputLine::resetLine() {
250 // every time the InputLine is cleared we also reset history index
251 idx = history.count();
253 QTextBlockFormat format = textCursor().blockFormat();
254 format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
255 textCursor().setBlockFormat(format);
258 void InputLine::showHistoryEntry() {
259 // if the user changed the history, display the changed line
260 setPlainText(tempHistory.contains(idx) ? tempHistory[idx] : history[idx]);
261 QTextCursor cursor = textCursor();
262 QTextBlockFormat format = cursor.blockFormat();
263 format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
264 cursor.setBlockFormat(format);
265 cursor.movePosition(QTextCursor::End);
266 setTextCursor(cursor);