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