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