recreating some indexes that were lost in an upgrade
[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 "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   connect(Client::instance(), SIGNAL(disconnected()), this, SLOT(updateNickSelector()));
42   setFocusProxy(ui.inputEdit);
43
44   ui.ownNick->setSizeAdjustPolicy(QComboBox::AdjustToContents);
45   ui.ownNick->installEventFilter(new MouseWheelFilter(this));
46   ui.inputEdit->installEventFilter(new JumpKeyHandler(this));
47
48   QtUiSettings s;
49   bool useInputLineFont = s.value("UseInputLineFont", QVariant(false)).toBool();
50   if(useInputLineFont) {
51     ui.inputEdit->setFont(s.value("InputLineFont").value<QFont>());
52   }
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::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
66   if(current.data(NetworkModel::BufferInfoRole) == previous.data(NetworkModel::BufferInfoRole))
67     return;
68
69   const Network *net = Client::networkModel()->networkByIndex(current);
70   setNetwork(net);
71   updateNickSelector();
72   updateEnabledState();
73 }
74
75 void InputWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
76   QItemSelectionRange changedArea(topLeft, bottomRight);
77   if(changedArea.contains(selectionModel()->currentIndex())) {
78     updateEnabledState();
79   }
80 };
81
82 void InputWidget::updateEnabledState() {
83   QModelIndex currentIndex = selectionModel()->currentIndex();
84   
85   const Network *net = Client::networkModel()->networkByIndex(currentIndex);
86   bool enabled = false;
87   if(net) {
88     // disable inputline if it's a channelbuffer we parted from or...
89     enabled = (currentIndex.data(NetworkModel::ItemActiveRole).value<bool>() || (currentIndex.data(NetworkModel::BufferTypeRole).toInt() != BufferInfo::ChannelBuffer));
90     // ... if we're not connected to the network at all
91     enabled &= net->isConnected();
92   }
93   ui.inputEdit->setEnabled(enabled);
94 }
95
96 const Network *InputWidget::currentNetwork() const {
97   return Client::network(_networkId);
98 }
99
100 BufferInfo InputWidget::currentBufferInfo() const {
101   return selectionModel()->currentIndex().data(NetworkModel::BufferInfoRole).value<BufferInfo>();
102 };
103
104 void InputWidget::setNetwork(const Network *network) {
105   if(!network || _networkId == network->networkId())
106     return;
107
108   const Network *previousNet = Client::network(_networkId);
109   if(previousNet) {
110     disconnect(previousNet, 0, this, 0);
111     if(previousNet->me())
112       disconnect(previousNet->me(), 0, this, 0);
113   }
114
115   if(network) {
116     _networkId = network->networkId();
117     connect(network, SIGNAL(identitySet(IdentityId)), this, SLOT(setIdentity(IdentityId)));
118     if(network->me()) {
119       connect(network->me(), SIGNAL(nickSet(QString)), this, SLOT(updateNickSelector()));
120       connect(network->me(), SIGNAL(userModesSet(QString)), this, SLOT(updateNickSelector()));
121       connect(network->me(), SIGNAL(userModesAdded(QString)), this, SLOT(updateNickSelector()));
122       connect(network->me(), SIGNAL(userModesRemoved(QString)), this, SLOT(updateNickSelector()));
123       connect(network->me(), SIGNAL(awaySet(bool)), this, SLOT(updateNickSelector()));
124     }
125   }
126   setIdentity(network->identity());
127 }
128
129 void InputWidget::setIdentity(const IdentityId &identityId) {
130   if(_identityId == identityId)
131     return;
132
133   const Identity *previousIdentity = Client::identity(_identityId);
134   if(previousIdentity)
135     disconnect(previousIdentity, 0, this, 0);
136
137   const Identity *identity = Client::identity(identityId);
138   if(identity) {
139     _identityId = identityId;
140     connect(identity, SIGNAL(nicksSet(QStringList)),
141             this, SLOT(updateNickSelector()));
142   }
143   updateNickSelector();
144 }
145
146 void InputWidget::updateNickSelector() const {
147   ui.ownNick->clear();
148
149   const Network *net = currentNetwork();
150   if(!net)
151     return;
152
153   const Identity *identity = Client::identity(net->identity());
154   if(!identity) {
155     qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId();
156     return;
157   }
158
159   int nickIdx;
160   QStringList nicks = identity->nicks();
161   if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
162     nicks.prepend(net->myNick());
163     nickIdx = 0;
164   }
165
166   if(nicks.isEmpty())
167     return;
168
169   IrcUser *me = net->me();
170   if(me)
171     nicks[nickIdx] = net->myNick() + QString(" (+%1)").arg(me->userModes());
172       
173   ui.ownNick->addItems(nicks);
174
175   if(me && me->isAway())
176     ui.ownNick->setItemData(nickIdx, SmallIcon("user-away"), Qt::DecorationRole);
177
178   ui.ownNick->setCurrentIndex(nickIdx);
179 }
180
181 void InputWidget::changeNick(const QString &newNick) const {
182   const Network *net = currentNetwork();
183   if(!net || net->isMyNick(newNick))
184     return;
185   emit userInput(currentBufferInfo(), QString("/nick %1").arg(newNick));
186 }
187
188 void InputWidget::sendText(QString text) {
189   emit userInput(currentBufferInfo(), text);
190 }
191
192
193 // MOUSE WHEEL FILTER
194 MouseWheelFilter::MouseWheelFilter(QObject *parent)
195   : QObject(parent)
196 {
197 }
198
199 bool MouseWheelFilter::eventFilter(QObject *obj, QEvent *event) {
200   if(event->type() != QEvent::Wheel)
201     return QObject::eventFilter(obj, event);
202   else
203     return true;
204 }