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