ce0fa373fb0826a8a188c3d0819d0fdf649e7ae9
[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 #include "graphicalui.h"
23 #include "inputline.h"
24 #include "tabcompleter.h"
25
26 InputLine::InputLine(QWidget *parent)
27   :
28 #ifdef HAVE_KDE
29     KTextEdit(parent),
30 #else
31     QLineEdit(parent),
32 #endif
33     idx(0),
34     tabCompleter(new TabCompleter(this))
35 {
36 #ifdef HAVE_KDE
37 //This is done to make the KTextEdit look like a lineedit
38 #if QT_VERSION >= 0x040500
39   document()->setDocumentMargin(0);
40 #endif
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()));
48 #endif
49
50   connect(this, SIGNAL(returnPressed()), this, SLOT(on_returnPressed()));
51   connect(this, SIGNAL(textChanged(QString)), this, SLOT(on_textChanged(QString)));
52 }
53
54 InputLine::~InputLine() {
55 }
56
57 void InputLine::setCustomFont(const QFont &font) {
58   setFont(font);
59 #ifdef HAVE_KDE
60   setMaximumHeight(document()->size().toSize().height() + 2*frameWidth());
61 #endif
62 }
63
64 bool InputLine::eventFilter(QObject *watched, QEvent *event) {
65   if(event->type() != QEvent::KeyPress)
66     return false;
67
68   // keys from BufferView should be sent to (and focus) the input line
69   BufferView *view = qobject_cast<BufferView *>(watched);
70   if(view) {
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()) {
75         setFocus();
76         keyPressEvent(keyEvent);
77         return true;
78       } else
79         return false;
80     }
81   }
82   return false;
83 }
84
85 void InputLine::keyPressEvent(QKeyEvent * event) {
86
87 #ifdef HAVE_KDE
88   if(event->matches(QKeySequence::Find)) {
89     QAction *act = GraphicalUi::actionCollection()->action("ToggleSearchBar");
90     if(act) {
91       act->toggle();
92       event->accept();
93       return;
94     }
95   }
96 #endif
97
98   switch(event->key()) {
99   case Qt::Key_Up:
100     event->accept();
101
102     addToHistory(text(), true);
103
104     if(idx > 0) {
105       idx--;
106       showHistoryEntry();
107     }
108
109     break;
110
111   case Qt::Key_Down:
112     event->accept();
113
114     addToHistory(text(), true);
115
116     if(idx < history.count()) {
117       idx++;
118       if(idx < history.count() || tempHistory.contains(idx)) // tempHistory might have an entry for idx == history.count() + 1
119         showHistoryEntry();
120       else
121         resetLine();              // equals clear() in this case
122     } else {
123       addToHistory(text());
124       resetLine();
125     }
126
127     break;
128
129   case Qt::Key_Select:          // for Qtopia
130     emit returnPressed();
131     break;
132
133 #ifdef HAVE_KDE
134 //Since this is a ktextedit, we don't have this signal "natively"
135   case Qt::Key_Return:
136   case Qt::Key_Enter:
137     event->accept();
138     emit returnPressed();
139     break;
140
141 #endif
142
143   default:
144 #ifdef HAVE_KDE
145     KTextEdit::keyPressEvent(event);
146 #else
147     QLineEdit::keyPressEvent(event);
148 #endif
149   }
150 }
151
152 bool InputLine::addToHistory(const QString &text, bool temporary) {
153   if(text.isEmpty())
154     return false;
155
156   Q_ASSERT(0 <= idx && idx <= history.count());
157
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
162     if(temporary) {
163       tempHistory[idx] = text;
164     } else {
165       history << text;
166       tempHistory.clear();
167     }
168     return true;
169   } else {
170     return false;
171   }
172 }
173
174 void InputLine::on_returnPressed() {
175   if(!text().isEmpty()) {
176     addToHistory(text());
177     emit sendText(text());
178     resetLine();
179   }
180 }
181
182 void InputLine::on_textChanged(QString newText) {
183   QStringList lineSeparators;
184   lineSeparators << QString("\r\n")
185                  << QString('\n')
186                  << QString('\r');
187
188   QString lineSep;
189   foreach(QString separator, lineSeparators) {
190     if(newText.contains(separator)) {
191       lineSep = separator;
192       break;
193     }
194   }
195
196   if(lineSep.isEmpty())
197     return;
198
199   QStringList lines = newText.split(lineSep);
200   clear();
201
202   if(lines.count() >= 4) {
203     QString msg = tr("Do you really want to paste %n lines?", "", lines.count());
204     msg += "<p>";
205     for(int i = 0; i < 3; i++) {
206       msg += lines[i].left(40);
207       if(lines[i].count() > 40)
208         msg += "...";
209       msg += "<br />";
210     }
211     msg += "...</p>";
212     QMessageBox question(QMessageBox::NoIcon, tr("Paste Protection"), msg, QMessageBox::Yes|QMessageBox::No);
213     question.setDefaultButton(QMessageBox::No);
214 #ifdef Q_WS_MAC
215     question.setWindowFlags(question.windowFlags() | Qt::Sheet);
216 #endif
217     if(question.exec() == QMessageBox::No)
218       return;
219   }
220
221   foreach(QString line, lines) {
222     if(!line.isEmpty()) {
223       clear();
224       insert(line);
225       emit returnPressed();
226     }
227   }
228 //   if(newText.contains(lineSep)) {
229 //     clear();
230 //     QString line = newText.section(lineSep, 0, 0);
231 //     QString remainder = newText.section(lineSep, 1);
232 //     insert(line);
233 //     emit returnPressed();
234 //     insert(remainder);
235 //   }
236 }
237
238 void InputLine::resetLine() {
239   // every time the InputLine is cleared we also reset history index
240   idx = history.count();
241   clear();
242 }
243
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]);
247 #ifdef HAVE_KDE
248   QTextCursor cursor = textCursor();
249   cursor.movePosition(QTextCursor::End);
250   setTextCursor(cursor);
251 #endif
252 }