1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "inputwidget.h"
25 #include "networkmodel.h"
26 #include "jumpkeyhandler.h"
29 InputWidget::InputWidget(QWidget *parent)
30 : AbstractItemView(parent),
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);
39 ui.ownNick->installEventFilter(new MouseWheelFilter(this));
40 ui.inputEdit->installEventFilter(new JumpKeyHandler(this));
43 InputWidget::~InputWidget() {
46 void InputWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
47 if(current.data(NetworkModel::BufferInfoRole) == previous.data(NetworkModel::BufferInfoRole))
50 setNetwork(Client::networkModel()->networkByIndex(current));
52 ui.inputEdit->setEnabled(current.data(NetworkModel::ItemActiveRole).value<bool>());
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>());
64 const Network *InputWidget::currentNetwork() const {
65 return Client::network(_networkId);
68 BufferInfo InputWidget::currentBufferInfo() const {
69 return selectionModel()->currentIndex().data(NetworkModel::BufferInfoRole).value<BufferInfo>();
72 void InputWidget::setNetwork(const Network *network) {
73 if(!network || _networkId == network->networkId())
76 const Network *previousNet = Client::network(_networkId);
78 disconnect(previousNet, 0, this, 0);
80 disconnect(previousNet->me(), 0, this, 0);
84 _networkId = network->networkId();
85 connect(network, SIGNAL(identitySet(IdentityId)), this, SLOT(setIdentity(IdentityId)));
87 connect(network->me(), SIGNAL(nickSet(QString)), this, SLOT(updateNickSelector()));
88 connect(network->me(), SIGNAL(userModesSet(QString)), this, SLOT(updateNickSelector()));
89 connect(network->me(), SIGNAL(userModesAdded(QString)), this, SLOT(updateNickSelector()));
90 connect(network->me(), SIGNAL(userModesRemoved(QString)), this, SLOT(updateNickSelector()));
93 setIdentity(network->identity());
96 void InputWidget::setIdentity(const IdentityId &identityId) {
97 if(_identityId == identityId)
100 const Identity *previousIdentity = Client::identity(_identityId);
102 disconnect(previousIdentity, 0, this, 0);
104 const Identity *identity = Client::identity(identityId);
106 _identityId = identityId;
107 connect(identity, SIGNAL(nicksSet(QStringList)),
108 this, SLOT(updateNickSelector()));
110 updateNickSelector();
113 void InputWidget::updateNickSelector() const {
116 const Network *net = currentNetwork();
120 const Identity *identity = Client::identity(net->identity());
122 qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId();
127 QStringList nicks = identity->nicks();
128 if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
129 nicks.prepend(net->myNick());
133 if(net->me() && nickIdx < nicks.count())
134 nicks[nickIdx] = net->myNick() + QString(" (%1)").arg(net->me()->userModes());
136 ui.ownNick->addItems(nicks);
137 ui.ownNick->setCurrentIndex(nickIdx);
140 void InputWidget::changeNick(const QString &newNick) const {
141 const Network *net = currentNetwork();
142 if(!net || net->isMyNick(newNick))
144 emit userInput(currentBufferInfo(), QString("/nick %1").arg(newNick));
147 void InputWidget::sendText(QString text) {
148 emit userInput(currentBufferInfo(), text);
152 // MOUSE WHEEL FILTER
153 MouseWheelFilter::MouseWheelFilter(QObject *parent)
158 bool MouseWheelFilter::eventFilter(QObject *obj, QEvent *event) {
159 if(event->type() != QEvent::Wheel)
160 return QObject::eventFilter(obj, event);