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