368852be93c583323381d87c7f10be381980d2bc
[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
23 #include "inputline.h"
24 #include "tabcompleter.h"
25
26 InputLine::InputLine(QWidget *parent)
27   : QLineEdit(parent),
28     idx(0),
29     tabCompleter(new TabCompleter(this))
30 {
31   connect(this, SIGNAL(returnPressed()), this, SLOT(on_returnPressed()));
32   connect(this, SIGNAL(textChanged(QString)), this, SLOT(on_textChanged(QString)));
33 }
34
35 InputLine::~InputLine() {
36 }
37
38 bool InputLine::eventFilter(QObject *watched, QEvent *event) {
39   if(event->type() != QEvent::KeyPress)
40     return false;
41
42   // keys from BufferView should be sent to (and focus) the input line
43   BufferView *view = qobject_cast<BufferView *>(watched);
44   if(view) {
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()) {
49         setFocus();
50         keyPressEvent(keyEvent);
51         return true;
52       } else
53         return false;
54     }
55   }
56   return false;
57 }
58
59 void InputLine::keyPressEvent(QKeyEvent * event) {
60   switch(event->key()) {
61   case Qt::Key_Up:
62     event->accept();
63
64     addToHistory(text(), true);
65
66     if(idx > 0) {
67       idx--;
68       showHistoryEntry();
69     }
70
71     break;
72
73   case Qt::Key_Down:
74     event->accept();
75
76     addToHistory(text(), true);
77
78     if(idx < history.count()) {
79       idx++;
80       if(idx < history.count() || tempHistory.contains(idx)) // tempHistory might have an entry for idx == history.count() + 1
81         showHistoryEntry();
82       else
83         resetLine();              // equals clear() in this case
84     } else {
85       addToHistory(text());
86       resetLine();
87     }
88
89     break;
90
91   case Qt::Key_Select:          // for Qtopia
92     emit returnPressed();
93
94   default:
95     QLineEdit::keyPressEvent(event);
96   }
97 }
98
99 bool InputLine::addToHistory(const QString &text, bool temporary) {
100   if(text.isEmpty())
101     return false;
102
103   Q_ASSERT(0 <= idx && idx <= history.count());
104
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
109     if(temporary) {
110       tempHistory[idx] = text;
111     } else {
112       history << text;
113       tempHistory.clear();
114     }
115     return true;
116   } else {
117     return false;
118   }
119 }
120
121 void InputLine::on_returnPressed() {
122   addToHistory(text());
123   emit sendText(text());
124   resetLine();
125 }
126
127 void InputLine::on_textChanged(QString newText) {
128   QStringList lineSeparators;
129   lineSeparators << QString("\r\n")
130                  << QString('\n')
131                  << QString('\r');
132
133   QString lineSep;
134   foreach(QString separator, lineSeparators) {
135     if(newText.contains(separator)) {
136       lineSep = separator;
137       break;
138     }
139   }
140
141   if(lineSep.isEmpty())
142     return;
143
144   QStringList lines = newText.split(lineSep);
145   clear();
146
147   if(lines.count() >= 4) {
148     QString msg = tr("Do you really want to paste %1 lines?", "", lines.count()).arg(lines.count());
149     msg += "<p>";
150     for(int i = 0; i < 3; i++) {
151       msg += lines[i].left(40);
152       if(lines[i].count() > 40)
153         msg += "...";
154       msg += "<br />";
155     }
156     msg += "...</p>";
157     QMessageBox question(QMessageBox::NoIcon, tr("Paste Protection"), msg, QMessageBox::Yes|QMessageBox::No);
158     question.setDefaultButton(QMessageBox::No);
159 #ifdef Q_WS_MAC
160     question.setWindowFlags(question.windowFlags() | Qt::Sheet);
161 #endif
162     if(question.exec() == QMessageBox::No)
163       return;
164   }
165
166   foreach(QString line, lines) {
167     clear();
168     insert(line);
169     emit returnPressed();
170   }
171 //   if(newText.contains(lineSep)) {
172 //     clear();
173 //     QString line = newText.section(lineSep, 0, 0);
174 //     QString remainder = newText.section(lineSep, 1);
175 //     insert(line);
176 //     emit returnPressed();
177 //     insert(remainder);
178 //   }
179 }
180
181 void InputLine::resetLine() {
182   // every time the InputLine is cleared we also reset history index
183   idx = history.count();
184   clear();
185 }
186
187 void InputLine::showHistoryEntry() {
188   // if the user changed the history, display the changed line
189   tempHistory.contains(idx) ? setText(tempHistory[idx]) : setText(history[idx]);
190 }