Fix fontchange notification in InputWidget
[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 "client.h"
26 #include "iconloader.h"
27 #include "ircuser.h"
28 #include "jumpkeyhandler.h"
29 #include "networkmodel.h"
30 #include "qtui.h"
31 #include "qtuisettings.h"
32
33 InputWidget::InputWidget(QWidget *parent)
34   : AbstractItemView(parent),
35     _networkId(0)
36 {
37   ui.setupUi(this);
38   connect(ui.inputEdit, SIGNAL(sendText(QString)), this, SLOT(sendText(QString)));
39   connect(ui.ownNick, SIGNAL(activated(QString)), this, SLOT(changeNick(QString)));
40   connect(this, SIGNAL(userInput(BufferInfo, QString)), Client::instance(), SIGNAL(sendInput(BufferInfo, QString)));
41   setFocusProxy(ui.inputEdit);
42
43   ui.ownNick->setSizeAdjustPolicy(QComboBox::AdjustToContents);
44   ui.ownNick->installEventFilter(new MouseWheelFilter(this));
45   ui.inputEdit->installEventFilter(new JumpKeyHandler(this));
46
47   QtUiStyleSettings s("Fonts");
48   s.notify("InputLine", this, SLOT(setCustomFont(QVariant)));
49   setCustomFont(s.value("InputLine", QFont()));
50
51   ActionCollection *coll = QtUi::actionCollection();
52
53   Action *activateInputline = coll->add<Action>("FocusInputLine");
54   connect(activateInputline, SIGNAL(triggered()), SLOT(setFocus()));
55   activateInputline->setText(tr("Focus Input Line"));
56   activateInputline->setShortcut(tr("Ctrl+L"));
57 }
58
59 InputWidget::~InputWidget() {
60 }
61
62 void InputWidget::setCustomFont(const QVariant &v) {
63   QFont font = v.value<QFont>();
64   if(font.family().isEmpty())
65     font = QApplication::font();
66   ui.inputEdit->setFont(font);
67 }
68
69 void InputWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
70   Q_UNUSED(previous)
71   NetworkId networkId = current.data(NetworkModel::NetworkIdRole).value<NetworkId>();
72   if(networkId == _networkId)
73     return;
74
75   setNetwork(networkId);
76   updateNickSelector();
77   updateEnabledState();
78 }
79
80 void InputWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
81   QItemSelectionRange changedArea(topLeft, bottomRight);
82   if(changedArea.contains(selectionModel()->currentIndex())) {
83     updateEnabledState();
84   }
85 };
86
87 void InputWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
88   NetworkId networkId;
89   QModelIndex child;
90   for(int row = start; row <= end; row++) {
91     child = model()->index(row, 0, parent);
92     if(NetworkModel::NetworkItemType != child.data(NetworkModel::ItemTypeRole).toInt())
93       continue;
94     networkId = child.data(NetworkModel::NetworkIdRole).value<NetworkId>();
95     if(networkId == _networkId) {
96       setNetwork(0);
97       updateNickSelector();
98       return;
99     }
100   }
101 }
102
103 void InputWidget::updateEnabledState() {
104   QModelIndex currentIndex = selectionModel()->currentIndex();
105   
106   const Network *net = Client::networkModel()->networkByIndex(currentIndex);
107   bool enabled = false;
108   if(net) {
109     // disable inputline if it's a channelbuffer we parted from or...
110     enabled = (currentIndex.data(NetworkModel::ItemActiveRole).value<bool>() || (currentIndex.data(NetworkModel::BufferTypeRole).toInt() != BufferInfo::ChannelBuffer));
111     // ... if we're not connected to the network at all
112     enabled &= net->isConnected();
113   }
114   ui.inputEdit->setEnabled(enabled);
115 }
116
117 const Network *InputWidget::currentNetwork() const {
118   return Client::network(_networkId);
119 }
120
121 BufferInfo InputWidget::currentBufferInfo() const {
122   return selectionModel()->currentIndex().data(NetworkModel::BufferInfoRole).value<BufferInfo>();
123 };
124
125 void InputWidget::setNetwork(NetworkId networkId) {
126   if(_networkId == networkId)
127     return;
128
129   const Network *previousNet = Client::network(_networkId);
130   if(previousNet) {
131     disconnect(previousNet, 0, this, 0);
132     if(previousNet->me())
133       disconnect(previousNet->me(), 0, this, 0);
134   }
135
136   _networkId = networkId;
137
138   const Network *network = Client::network(networkId);
139   if(network) {
140     connect(network, SIGNAL(identitySet(IdentityId)), this, SLOT(setIdentity(IdentityId)));
141     connectMyIrcUser();
142     setIdentity(network->identity());
143   } else {
144     setIdentity(0);
145     _networkId = 0;
146   }
147 }
148
149 void InputWidget::connectMyIrcUser() {
150   const Network *network = currentNetwork();
151   if(network->me()) {
152     connect(network->me(), SIGNAL(nickSet(const QString &)), this, SLOT(updateNickSelector()));
153     connect(network->me(), SIGNAL(userModesSet(QString)), this, SLOT(updateNickSelector()));
154     connect(network->me(), SIGNAL(userModesAdded(QString)), this, SLOT(updateNickSelector()));
155     connect(network->me(), SIGNAL(userModesRemoved(QString)), this, SLOT(updateNickSelector()));
156     connect(network->me(), SIGNAL(awaySet(bool)), this, SLOT(updateNickSelector()));
157     disconnect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
158   } else {
159     connect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
160   }
161 }
162
163 void InputWidget::setIdentity(IdentityId identityId) {
164   if(_identityId == identityId)
165     return;
166
167   const Identity *previousIdentity = Client::identity(_identityId);
168   if(previousIdentity)
169     disconnect(previousIdentity, 0, this, 0);
170
171   _identityId = identityId;
172
173   const Identity *identity = Client::identity(identityId);
174   if(identity) {
175     connect(identity, SIGNAL(nicksSet(QStringList)),
176             this, SLOT(updateNickSelector()));
177   }
178   updateNickSelector();
179 }
180
181 void InputWidget::updateNickSelector() const {
182   ui.ownNick->clear();
183
184   const Network *net = currentNetwork();
185   if(!net)
186     return;
187
188   const Identity *identity = Client::identity(net->identity());
189   if(!identity) {
190     qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId();
191     return;
192   }
193
194   int nickIdx;
195   QStringList nicks = identity->nicks();
196   if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
197     nicks.prepend(net->myNick());
198     nickIdx = 0;
199   }
200
201   if(nicks.isEmpty())
202     return;
203
204   IrcUser *me = net->me();
205   if(me)
206     nicks[nickIdx] = net->myNick() + QString(" (+%1)").arg(me->userModes());
207       
208   ui.ownNick->addItems(nicks);
209
210   if(me && me->isAway())
211     ui.ownNick->setItemData(nickIdx, SmallIcon("user-away"), Qt::DecorationRole);
212
213   ui.ownNick->setCurrentIndex(nickIdx);
214 }
215
216 void InputWidget::changeNick(const QString &newNick) const {
217   const Network *net = currentNetwork();
218   if(!net || net->isMyNick(newNick))
219     return;
220   emit userInput(currentBufferInfo(), QString("/nick %1").arg(newNick));
221 }
222
223 void InputWidget::sendText(QString text) {
224   emit userInput(currentBufferInfo(), text);
225 }
226
227
228 // MOUSE WHEEL FILTER
229 MouseWheelFilter::MouseWheelFilter(QObject *parent)
230   : QObject(parent)
231 {
232 }
233
234 bool MouseWheelFilter::eventFilter(QObject *obj, QEvent *event) {
235   if(event->type() != QEvent::Wheel)
236     return QObject::eventFilter(obj, event);
237   else
238     return true;
239 }