11e3ec35a4ffa5152b317a0741b7b35d0643c61b
[quassel.git] / src / qtui / inputwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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 "inputwidget.h"
22
23 #include "action.h"
24 #include "actioncollection.h"
25 #include "bufferview.h"
26 #include "client.h"
27 #include "iconloader.h"
28 #include "ircuser.h"
29 #include "jumpkeyhandler.h"
30 #include "networkmodel.h"
31 #include "qtui.h"
32 #include "qtuisettings.h"
33 #include "tabcompleter.h"
34
35 InputWidget::InputWidget(QWidget *parent)
36   : AbstractItemView(parent),
37     _networkId(0)
38 {
39   ui.setupUi(this);
40   connect(ui.inputEdit, SIGNAL(textEntered(QString)), this, SLOT(sendText(QString)));
41   connect(ui.ownNick, SIGNAL(activated(QString)), this, SLOT(changeNick(QString)));
42
43   layout()->setAlignment(ui.ownNick, Qt::AlignBottom);
44   layout()->setAlignment(ui.inputEdit, Qt::AlignBottom);
45
46   setFocusProxy(ui.inputEdit);
47   ui.ownNick->setFocusProxy(ui.inputEdit);
48
49   ui.ownNick->setSizeAdjustPolicy(QComboBox::AdjustToContents);
50   ui.ownNick->installEventFilter(new MouseWheelFilter(this));
51   ui.inputEdit->installEventFilter(new JumpKeyHandler(this));
52   ui.inputEdit->installEventFilter(this);
53
54   ui.inputEdit->setMinHeight(1);
55   ui.inputEdit->setMaxHeight(5);
56   ui.inputEdit->setMode(MultiLineEdit::MultiLine);
57
58   new TabCompleter(ui.inputEdit);
59
60   UiStyleSettings fs("Fonts");
61   fs.notify("InputWidget", this, SLOT(setCustomFont(QVariant)));
62   setCustomFont(fs.value("InputWidget", QFont()));
63
64   UiSettings s("InputWidget");
65
66 #ifdef HAVE_KDE
67   s.notify("EnableSpellCheck", this, SLOT(setEnableSpellCheck(QVariant)));
68   setEnableSpellCheck(s.value("EnableSpellCheck", false));
69 #endif
70
71   s.notify("ShowNickSelector", this, SLOT(setShowNickSelector(QVariant)));
72   setShowNickSelector(s.value("ShowNickSelector", true));
73
74   s.notify("MaxNumLines", this, SLOT(setMaxLines(QVariant)));
75   setMaxLines(s.value("MaxNumLines", 5));
76
77   s.notify("EnableScrollBars", this, SLOT(setEnableScrollBars(QVariant)));
78   setEnableScrollBars(s.value("EnableScrollBars", true));
79
80   ActionCollection *coll = QtUi::actionCollection();
81
82   Action *activateInputline = coll->add<Action>("FocusInputLine");
83   connect(activateInputline, SIGNAL(triggered()), SLOT(setFocus()));
84   activateInputline->setText(tr("Focus Input Line"));
85   activateInputline->setShortcut(tr("Ctrl+L"));
86 }
87
88 InputWidget::~InputWidget() {
89 }
90
91 void InputWidget::setCustomFont(const QVariant &v) {
92   QFont font = v.value<QFont>();
93   if(font.family().isEmpty())
94     font = QApplication::font();
95   ui.inputEdit->setCustomFont(font);
96 }
97
98 void InputWidget::setEnableSpellCheck(const QVariant &v) {
99   ui.inputEdit->setSpellCheckEnabled(v.toBool());
100 }
101
102 void InputWidget::setShowNickSelector(const QVariant &v) {
103   ui.ownNick->setVisible(v.toBool());
104 }
105
106 void InputWidget::setMaxLines(const QVariant &v) {
107   ui.inputEdit->setMaxHeight(v.toInt());
108 }
109
110 void InputWidget::setEnableScrollBars(const QVariant &v) {
111   ui.inputEdit->setScrollBarsEnabled(v.toBool());
112 }
113
114 bool InputWidget::eventFilter(QObject *watched, QEvent *event) {
115   if(event->type() != QEvent::KeyPress)
116     return false;
117
118   QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
119
120   // keys from BufferView should be sent to (and focus) the input line
121   BufferView *view = qobject_cast<BufferView *>(watched);
122   if(view) {
123     if(keyEvent->text().length() == 1 && !(keyEvent->modifiers() & (Qt::ControlModifier ^ Qt::AltModifier)) ) { // normal key press
124       QChar c = keyEvent->text().at(0);
125       if(c.isLetterOrNumber() || c.isSpace() || c.isPunct() || c.isSymbol()) {
126         setFocus();
127         QCoreApplication::sendEvent(inputLine(), keyEvent);
128         return true;
129       }
130     }
131     return false;
132   } else if(watched == ui.inputEdit) {
133     if(keyEvent->matches(QKeySequence::Find)) {
134       QAction *act = GraphicalUi::actionCollection()->action("ToggleSearchBar");
135       if(act) {
136         act->toggle();
137         return true;
138       }
139     }
140     return false;
141   }
142   return false;
143 }
144
145 void InputWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
146   Q_UNUSED(previous)
147   NetworkId networkId = current.data(NetworkModel::NetworkIdRole).value<NetworkId>();
148   if(networkId == _networkId)
149     return;
150
151   setNetwork(networkId);
152   updateNickSelector();
153   updateEnabledState();
154 }
155
156 void InputWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
157   QItemSelectionRange changedArea(topLeft, bottomRight);
158   if(changedArea.contains(selectionModel()->currentIndex())) {
159     updateEnabledState();
160   }
161 };
162
163 void InputWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
164   NetworkId networkId;
165   QModelIndex child;
166   for(int row = start; row <= end; row++) {
167     child = model()->index(row, 0, parent);
168     if(NetworkModel::NetworkItemType != child.data(NetworkModel::ItemTypeRole).toInt())
169       continue;
170     networkId = child.data(NetworkModel::NetworkIdRole).value<NetworkId>();
171     if(networkId == _networkId) {
172       setNetwork(0);
173       updateNickSelector();
174       return;
175     }
176   }
177 }
178
179 void InputWidget::updateEnabledState() {
180   QModelIndex currentIndex = selectionModel()->currentIndex();
181
182   const Network *net = Client::networkModel()->networkByIndex(currentIndex);
183   bool enabled = false;
184   if(net) {
185     // disable inputline if it's a channelbuffer we parted from or...
186     enabled = (currentIndex.data(NetworkModel::ItemActiveRole).value<bool>() || (currentIndex.data(NetworkModel::BufferTypeRole).toInt() != BufferInfo::ChannelBuffer));
187     // ... if we're not connected to the network at all
188     enabled &= net->isConnected();
189   }
190   ui.inputEdit->setEnabled(enabled);
191 }
192
193 const Network *InputWidget::currentNetwork() const {
194   return Client::network(_networkId);
195 }
196
197 BufferInfo InputWidget::currentBufferInfo() const {
198   return selectionModel()->currentIndex().data(NetworkModel::BufferInfoRole).value<BufferInfo>();
199 };
200
201 void InputWidget::setNetwork(NetworkId networkId) {
202   if(_networkId == networkId)
203     return;
204
205   const Network *previousNet = Client::network(_networkId);
206   if(previousNet) {
207     disconnect(previousNet, 0, this, 0);
208     if(previousNet->me())
209       disconnect(previousNet->me(), 0, this, 0);
210   }
211
212   _networkId = networkId;
213
214   const Network *network = Client::network(networkId);
215   if(network) {
216     connect(network, SIGNAL(identitySet(IdentityId)), this, SLOT(setIdentity(IdentityId)));
217     connectMyIrcUser();
218     setIdentity(network->identity());
219   } else {
220     setIdentity(0);
221     _networkId = 0;
222   }
223 }
224
225 void InputWidget::connectMyIrcUser() {
226   const Network *network = currentNetwork();
227   if(network->me()) {
228     connect(network->me(), SIGNAL(nickSet(const QString &)), this, SLOT(updateNickSelector()));
229     connect(network->me(), SIGNAL(userModesSet(QString)), this, SLOT(updateNickSelector()));
230     connect(network->me(), SIGNAL(userModesAdded(QString)), this, SLOT(updateNickSelector()));
231     connect(network->me(), SIGNAL(userModesRemoved(QString)), this, SLOT(updateNickSelector()));
232     connect(network->me(), SIGNAL(awaySet(bool)), this, SLOT(updateNickSelector()));
233     disconnect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
234     updateNickSelector();
235   } else {
236     connect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
237   }
238 }
239
240 void InputWidget::setIdentity(IdentityId identityId) {
241   if(_identityId == identityId)
242     return;
243
244   const Identity *previousIdentity = Client::identity(_identityId);
245   if(previousIdentity)
246     disconnect(previousIdentity, 0, this, 0);
247
248   _identityId = identityId;
249
250   const Identity *identity = Client::identity(identityId);
251   if(identity) {
252     connect(identity, SIGNAL(nicksSet(QStringList)), this, SLOT(updateNickSelector()));
253   } else {
254     _identityId = 0;
255   }
256   updateNickSelector();
257 }
258
259 void InputWidget::updateNickSelector() const {
260   ui.ownNick->clear();
261
262   const Network *net = currentNetwork();
263   if(!net)
264     return;
265
266   const Identity *identity = Client::identity(net->identity());
267   if(!identity) {
268     qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId() << "IdentityId:" << net->identity();
269     return;
270   }
271
272   int nickIdx;
273   QStringList nicks = identity->nicks();
274   if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
275     nicks.prepend(net->myNick());
276     nickIdx = 0;
277   }
278
279   if(nicks.isEmpty())
280     return;
281
282   IrcUser *me = net->me();
283   if(me) {
284     nicks[nickIdx] = net->myNick();
285     if(!me->userModes().isEmpty())
286       nicks[nickIdx] += QString(" (+%1)").arg(me->userModes());
287   }
288
289   ui.ownNick->addItems(nicks);
290
291   if(me && me->isAway())
292     ui.ownNick->setItemData(nickIdx, SmallIcon("user-away"), Qt::DecorationRole);
293
294   ui.ownNick->setCurrentIndex(nickIdx);
295 }
296
297 void InputWidget::changeNick(const QString &newNick) const {
298   const Network *net = currentNetwork();
299   if(!net || net->isMyNick(newNick))
300     return;
301
302   // we reset the nick selecter as we have no confirmation yet, that this will succeed.
303   // if the action succeeds it will be properly updated anyways.
304   updateNickSelector();
305   Client::userInput(BufferInfo::fakeStatusBuffer(net->networkId()), QString("/NICK %1").arg(newNick));
306 }
307
308 void InputWidget::sendText(const QString &text) const {
309   Client::userInput(currentBufferInfo(), text);
310 }
311
312
313 // MOUSE WHEEL FILTER
314 MouseWheelFilter::MouseWheelFilter(QObject *parent)
315   : QObject(parent)
316 {
317 }
318
319 bool MouseWheelFilter::eventFilter(QObject *obj, QEvent *event) {
320   if(event->type() != QEvent::Wheel)
321     return QObject::eventFilter(obj, event);
322   else
323     return true;
324 }