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