cleanups (WiP)
[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 #include "qtuisettings.h"
28
29
30 InputWidget::InputWidget(QWidget *parent)
31   : AbstractItemView(parent),
32     _networkId(0)
33 {
34   ui.setupUi(this);
35   connect(ui.inputEdit, SIGNAL(sendText(QString)), this, SLOT(sendText(QString)));
36   connect(ui.ownNick, SIGNAL(activated(QString)), this, SLOT(changeNick(QString)));
37   connect(this, SIGNAL(userInput(BufferInfo, QString)), Client::instance(), SIGNAL(sendInput(BufferInfo, QString)));
38   setFocusProxy(ui.inputEdit);
39
40   ui.ownNick->setSizeAdjustPolicy(QComboBox::AdjustToContents);
41   ui.ownNick->installEventFilter(new MouseWheelFilter(this));
42   ui.inputEdit->installEventFilter(new JumpKeyHandler(this));
43
44   QtUiSettings s;
45   bool useInputLineFont = s.value("UseInputLineFont", QVariant(false)).toBool();
46   if(useInputLineFont) {
47     ui.inputEdit->setFont(s.value("InputLineFont").value<QFont>());
48   }
49 }
50
51 InputWidget::~InputWidget() {
52 }
53
54 void InputWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
55   if(current.data(NetworkModel::BufferInfoRole) == previous.data(NetworkModel::BufferInfoRole))
56     return;
57
58   const Network *net = Client::networkModel()->networkByIndex(current);
59   setNetwork(net);
60   updateNickSelector();
61   updateEnabledState();
62 }
63
64 void InputWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
65   QItemSelectionRange changedArea(topLeft, bottomRight);
66   if(changedArea.contains(selectionModel()->currentIndex())) {
67     updateEnabledState();
68   }
69 };
70
71 void InputWidget::updateEnabledState() {
72   QModelIndex currentIndex = selectionModel()->currentIndex();
73   
74   const Network *net = Client::networkModel()->networkByIndex(currentIndex);
75   bool enabled = false;
76   if(net) {
77     // disable inputline if it's a channelbuffer we parted from or...
78     enabled = (currentIndex.data(NetworkModel::ItemActiveRole).value<bool>() || (currentIndex.data(NetworkModel::BufferTypeRole).toInt() != BufferInfo::ChannelBuffer));
79     // ... if we're not connected to the network at all
80     enabled &= net->isConnected();
81   }
82   ui.inputEdit->setEnabled(enabled);
83 }
84
85 const Network *InputWidget::currentNetwork() const {
86   return Client::network(_networkId);
87 }
88
89 BufferInfo InputWidget::currentBufferInfo() const {
90   return selectionModel()->currentIndex().data(NetworkModel::BufferInfoRole).value<BufferInfo>();
91 };
92
93 void InputWidget::setNetwork(const Network *network) {
94   if(!network || _networkId == network->networkId())
95     return;
96
97   const Network *previousNet = Client::network(_networkId);
98   if(previousNet) {
99     disconnect(previousNet, 0, this, 0);
100     if(previousNet->me())
101       disconnect(previousNet->me(), 0, this, 0);
102   }
103
104   if(network) {
105     _networkId = network->networkId();
106     connect(network, SIGNAL(identitySet(IdentityId)), this, SLOT(setIdentity(IdentityId)));
107     if(network->me()) {
108       connect(network->me(), SIGNAL(nickSet(QString)), this, SLOT(updateNickSelector()));
109       connect(network->me(), SIGNAL(userModesSet(QString)), this, SLOT(updateNickSelector()));
110       connect(network->me(), SIGNAL(userModesAdded(QString)), this, SLOT(updateNickSelector()));
111       connect(network->me(), SIGNAL(userModesRemoved(QString)), this, SLOT(updateNickSelector()));
112     }
113   }
114   setIdentity(network->identity());
115 }
116
117 void InputWidget::setIdentity(const IdentityId &identityId) {
118   if(_identityId == identityId)
119     return;
120
121   const Identity *previousIdentity = Client::identity(_identityId);
122   if(previousIdentity)
123     disconnect(previousIdentity, 0, this, 0);
124
125   const Identity *identity = Client::identity(identityId);
126   if(identity) {
127     _identityId = identityId;
128     connect(identity, SIGNAL(nicksSet(QStringList)),
129             this, SLOT(updateNickSelector()));
130   }
131   updateNickSelector();
132 }
133
134 void InputWidget::updateNickSelector() const {
135   ui.ownNick->clear();
136
137   const Network *net = currentNetwork();
138   if(!net)
139     return;
140
141   const Identity *identity = Client::identity(net->identity());
142   if(!identity) {
143     qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId();
144     return;
145   }
146
147   int nickIdx;
148   QStringList nicks = identity->nicks();
149   if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
150     nicks.prepend(net->myNick());
151     nickIdx = 0;
152   }
153
154   if(net->me() && nickIdx < nicks.count())
155     nicks[nickIdx] = net->myNick() + QString(" (+%1)").arg(net->me()->userModes());
156       
157   ui.ownNick->addItems(nicks);
158   ui.ownNick->setCurrentIndex(nickIdx);
159 }
160
161 void InputWidget::changeNick(const QString &newNick) const {
162   const Network *net = currentNetwork();
163   if(!net || net->isMyNick(newNick))
164     return;
165   emit userInput(currentBufferInfo(), QString("/nick %1").arg(newNick));
166 }
167
168 void InputWidget::sendText(QString text) {
169   emit userInput(currentBufferInfo(), text);
170 }
171
172
173 // MOUSE WHEEL FILTER
174 MouseWheelFilter::MouseWheelFilter(QObject *parent)
175   : QObject(parent)
176 {
177 }
178
179 bool MouseWheelFilter::eventFilter(QObject *obj, QEvent *event) {
180   if(event->type() != QEvent::Wheel)
181     return QObject::eventFilter(obj, event);
182   else
183     return true;
184 }