Rename setters to be more Qt-like
[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
53   ui.inputEdit->setMinHeight(1);
54   ui.inputEdit->setMaxHeight(5);
55   ui.inputEdit->setMode(MultiLineEdit::MultiLine);
56
57   new TabCompleter(ui.inputEdit);
58
59   UiStyleSettings fs("Fonts");
60   fs.notify("InputWidget", this, SLOT(setCustomFont(QVariant)));
61   setCustomFont(fs.value("InputWidget", QFont()));
62
63   UiSettings s("InputWidget");
64
65 #ifdef HAVE_KDE
66   s.notify("EnableSpellCheck", this, SLOT(setEnableSpellCheck(QVariant)));
67   setEnableSpellCheck(s.value("EnableSpellCheck", false));
68 #endif
69
70   s.notify("ShowNickSelector", this, SLOT(setShowNickSelector(QVariant)));
71   setShowNickSelector(s.value("ShowNickSelector", true));
72
73   s.notify("MaxNumLines", this, SLOT(setMaxLines(QVariant)));
74   setMaxLines(s.value("MaxNumLines", 5));
75
76   s.notify("EnableScrollBars", this, SLOT(setEnableScrollBars(QVariant)));
77   setEnableScrollBars(s.value("EnableScrollBars", true));
78
79   ActionCollection *coll = QtUi::actionCollection();
80
81   Action *activateInputline = coll->add<Action>("FocusInputLine");
82   connect(activateInputline, SIGNAL(triggered()), SLOT(setFocus()));
83   activateInputline->setText(tr("Focus Input Line"));
84   activateInputline->setShortcut(tr("Ctrl+L"));
85 }
86
87 InputWidget::~InputWidget() {
88 }
89
90 void InputWidget::setCustomFont(const QVariant &v) {
91   QFont font = v.value<QFont>();
92   if(font.family().isEmpty())
93     font = QApplication::font();
94   ui.inputEdit->setCustomFont(font);
95 }
96
97 void InputWidget::setEnableSpellCheck(const QVariant &v) {
98   ui.inputEdit->setSpellCheckEnabled(v.toBool());
99 }
100
101 void InputWidget::setShowNickSelector(const QVariant &v) {
102   ui.ownNick->setVisible(v.toBool());
103 }
104
105 void InputWidget::setMaxLines(const QVariant &v) {
106   ui.inputEdit->setMaxHeight(v.toInt());
107 }
108
109 void InputWidget::setEnableScrollBars(const QVariant &v) {
110   ui.inputEdit->setScrollBarsEnabled(v.toBool());
111 }
112
113 bool InputWidget::eventFilter(QObject *watched, QEvent *event) {
114   if(event->type() != QEvent::KeyPress)
115     return false;
116
117   // keys from BufferView should be sent to (and focus) the input line
118   BufferView *view = qobject_cast<BufferView *>(watched);
119   if(view) {
120     QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
121     if(keyEvent->text().length() == 1 && !(keyEvent->modifiers() & (Qt::ControlModifier ^ Qt::AltModifier)) ) { // normal key press
122       QChar c = keyEvent->text().at(0);
123       if(c.isLetterOrNumber() || c.isSpace() || c.isPunct() || c.isSymbol()) {
124         setFocus();
125         QCoreApplication::sendEvent(inputLine(), keyEvent);
126         return true;
127       } else
128         return false;
129     }
130   }
131   return false;
132 }
133
134 void InputWidget::keyPressEvent(QKeyEvent * event) {
135   if(event->matches(QKeySequence::Find)) {
136     QAction *act = GraphicalUi::actionCollection()->action("ToggleSearchBar");
137     if(act) {
138       act->toggle();
139       return;
140     }
141   }
142   AbstractItemView::keyPressEvent(event);
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 }