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