made the check for lineseperators OS independent
[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 "inputline.h"
22
23 #include "tabcompleter.h"
24
25 InputLine::InputLine(QWidget *parent)
26   : QLineEdit(parent),
27     idx(0),
28     tabCompleter(new TabCompleter(this))
29 {
30   
31 #ifdef Q_WS_MAC
32   bindModifier = Qt::ControlModifier | Qt::AltModifier;
33   jumpModifier = Qt::ControlModifier;
34 #else
35   bindModifier = Qt::ControlModifier;
36   jumpModifier = Qt::AltModifier;
37 #endif
38
39   connect(this, SIGNAL(returnPressed()), this, SLOT(on_returnPressed()));
40   connect(this, SIGNAL(textChanged(QString)), this, SLOT(on_textChanged(QString)));
41
42 }
43
44 InputLine::~InputLine() {
45 }
46
47 void InputLine::keyPressEvent(QKeyEvent * event) {
48   if((event->modifiers() == jumpModifier || event->modifiers() == bindModifier) &&
49      (Qt::Key_0 <= event->key() && event->key() <= Qt::Key_9)) {
50     event->ignore();
51     return;
52   }
53   
54   if(event->key() == Qt::Key_Tab) { // Tabcomplete
55     tabCompleter->complete();
56     event->accept();
57   } else {
58     tabCompleter->reset();
59     if(event->key() == Qt::Key_Up) {
60       if(idx > 0) { idx--; setText(history[idx]); }
61       event->accept();
62     } else if(event->key() == Qt::Key_Down) {
63       if(idx < history.count()) idx++;
64       if(idx < history.count()) setText(history[idx]);
65       else setText("");
66       event->accept();
67     } else if(event->key() == Qt::Key_Select) {  // for Qtopia
68       emit returnPressed();
69       QLineEdit::keyPressEvent(event);
70     } else {
71       QLineEdit::keyPressEvent(event);
72     }
73   }
74 }
75
76 void InputLine::on_returnPressed() {
77   history << text();
78   idx = history.count();
79   emit sendText(text());
80   clear();
81 }
82
83 void InputLine::on_textChanged(QString newText) {
84   QStringList lineSeperators;
85   lineSeperators << QString("\r\n")
86                  << QString('\n')
87                  << QString('\r');
88   
89   QString lineSep;
90   foreach(QString seperator, lineSeperators) {
91     if(newText.contains(seperator)) {
92       lineSep = seperator;
93       break;
94     }
95   }
96
97   if(lineSep.isEmpty())
98     return;
99   
100   if(newText.contains(lineSep)) {
101     clear();
102     QString line = newText.section(lineSep, 0, 0);
103     QString remainder = newText.section(lineSep, 1);
104     insert(line);
105     emit returnPressed();
106     insert(remainder);
107   }
108   
109 }
110