Closing BR #126
[quassel.git] / src / qtui / inputwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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 "ircuser.h"
24 #include "client.h"
25 #include "networkmodel.h"
26 #include "jumpkeyhandler.h"
27
28
29 InputWidget::InputWidget(QWidget *parent)
30   : QWidget(parent),
31     validBuffer(false),
32     _bufferModel(0),
33     _selectionModel(0)
34 {
35   ui.setupUi(this);
36   connect(ui.inputEdit, SIGNAL(sendText(QString)), this, SLOT(sendText(QString)));
37   connect(ui.ownNick, SIGNAL(activated(QString)), this, SLOT(changeNick(QString)));
38   connect(this, SIGNAL(userInput(BufferInfo, QString)), Client::instance(), SIGNAL(sendInput(BufferInfo, QString)));
39   setFocusProxy(ui.inputEdit);
40
41   ui.ownNick->installEventFilter(new MouseWheelFilter(this));
42   ui.inputEdit->installEventFilter(new JumpKeyHandler(this));  
43 }
44
45 InputWidget::~InputWidget() {
46 }
47
48 void InputWidget::setModel(BufferModel *bufferModel) {
49   if(_bufferModel) {
50     disconnect(_bufferModel, 0, this, 0);
51   }
52   _bufferModel = bufferModel;
53   connect(bufferModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
54           this, SLOT(dataChanged(QModelIndex, QModelIndex)));
55 }
56
57 void InputWidget::setSelectionModel(QItemSelectionModel *selectionModel) {
58   if(_selectionModel) {
59     disconnect(_selectionModel, 0, this, 0);
60   }
61   _selectionModel = selectionModel;
62   connect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
63           this, SLOT(currentChanged(QModelIndex, QModelIndex)));
64 }
65
66 void InputWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
67   Q_UNUSED(previous);
68
69   validBuffer = current.isValid();
70
71   if(!validBuffer)
72     return;
73   
74   QVariant variant;
75   variant = current.data(NetworkModel::BufferInfoRole);
76   if(!variant.isValid())
77     return;
78
79   currentBufferInfo  = current.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
80   setNetwork(Client::networkModel()->networkByIndex(current));
81   updateNickSelector();
82   ui.inputEdit->setEnabled(current.data(NetworkModel::ItemActiveRole).value<bool>());
83 }
84
85 void InputWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
86   QItemSelectionRange changedArea(topLeft, bottomRight);
87   QModelIndex currentIndex = Client::bufferModel()->currentIndex();
88   if(changedArea.contains(currentIndex)) {
89     ui.inputEdit->setEnabled(currentIndex.data(NetworkModel::ItemActiveRole).value<bool>());
90   }
91 };
92
93
94 const Network *InputWidget::currentNetwork() const {
95   if(!validBuffer)
96     return 0;
97
98   return Client::network(_networkId);
99 }
100
101 void InputWidget::setNetwork(const Network *network) {
102   if(!network || _networkId == network->networkId())
103     return;
104
105   const Network *previousNet = Client::network(_networkId);
106   if(previousNet) {
107     disconnect(previousNet, 0, this, 0);
108     if(previousNet->me())
109       disconnect(previousNet->me(), 0, this, 0);
110   }
111
112   if(network) {
113     _networkId = network->networkId();
114     connect(network, SIGNAL(identitySet(IdentityId)), this, SLOT(setIdentity(IdentityId)));
115     if(network->me()) {
116       connect(network->me(), SIGNAL(nickSet(QString)), this, SLOT(updateNickSelector()));
117       connect(network->me(), SIGNAL(userModesSet(QString)), this, SLOT(updateNickSelector()));
118       connect(network->me(), SIGNAL(userModesAdded(QString)), this, SLOT(updateNickSelector()));
119       connect(network->me(), SIGNAL(userModesRemoved(QString)), this, SLOT(updateNickSelector()));
120     }
121   }
122   setIdentity(network->identity());
123 }
124
125 void InputWidget::setIdentity(const IdentityId &identityId) {
126   if(_identityId == identityId)
127     return;
128
129   const Identity *previousIdentity = Client::identity(_identityId);
130   if(previousIdentity)
131     disconnect(previousIdentity, 0, this, 0);
132
133   const Identity *identity = Client::identity(identityId);
134   if(identity) {
135     _identityId = identityId;
136     connect(identity, SIGNAL(nicksSet(QStringList)),
137             this, SLOT(updateNickSelector()));
138   }
139   updateNickSelector();
140 }
141
142 void InputWidget::updateNickSelector() const {
143   ui.ownNick->clear();
144
145   const Network *net = currentNetwork();
146   if(!net)
147     return;
148
149   const Identity *identity = Client::identity(net->identity());
150   if(!identity) {
151     qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId();
152     return;
153   }
154
155   int nickIdx;
156   QStringList nicks = identity->nicks();
157   if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
158     nicks.prepend(net->myNick());
159     nickIdx = 0;
160   }
161
162   if(net->me() && nickIdx < nicks.count())
163     nicks[nickIdx] = net->myNick() + QString(" (%1)").arg(net->me()->userModes());
164       
165   ui.ownNick->addItems(nicks);
166   ui.ownNick->setCurrentIndex(nickIdx);
167 }
168
169 void InputWidget::changeNick(const QString &newNick) const {
170   const Network *net = currentNetwork();
171   if(!net || net->isMyNick(newNick))
172     return;
173   emit userInput(currentBufferInfo, QString("/nick %1").arg(newNick));
174 }
175
176 void InputWidget::sendText(QString text) {
177   emit userInput(currentBufferInfo, text);
178 }
179
180
181 // MOUSE WHEEL FILTER
182 MouseWheelFilter::MouseWheelFilter(QObject *parent)
183   : QObject(parent)
184 {
185 }
186
187 bool MouseWheelFilter::eventFilter(QObject *obj, QEvent *event) {
188   if(event->type() != QEvent::Wheel)
189     return QObject::eventFilter(obj, event);
190   else
191     return true;
192 }