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)
36 connect(ui.inputEdit, SIGNAL(sendText(QString)), this, SLOT(sendText(QString)));
37 connect(ui.ownNick, SIGNAL(activated(QString)), this, SLOT(changeNick(QString)));
38 connect(this, SIGNAL(userInput(BufferInfo, QString)), Client::instance(), SIGNAL(sendInput(BufferInfo, QString)));
39 setFocusProxy(ui.inputEdit);
41 ui.ownNick->installEventFilter(new MouseWheelFilter(this));
42 ui.inputEdit->installEventFilter(new JumpKeyHandler(this));
45 InputWidget::~InputWidget() {
48 void InputWidget::setModel(BufferModel *bufferModel) {
50 disconnect(_bufferModel, 0, this, 0);
52 _bufferModel = bufferModel;
53 connect(bufferModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
54 this, SLOT(dataChanged(QModelIndex, QModelIndex)));
57 void InputWidget::setSelectionModel(QItemSelectionModel *selectionModel) {
59 disconnect(_selectionModel, 0, this, 0);
61 _selectionModel = selectionModel;
62 connect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
63 this, SLOT(currentChanged(QModelIndex, QModelIndex)));
66 void InputWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
69 validBuffer = current.isValid();
75 variant = current.data(NetworkModel::BufferInfoRole);
76 if(!variant.isValid())
79 currentBufferInfo = current.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
80 setNetwork(Client::networkModel()->networkByIndex(current));
82 ui.inputEdit->setEnabled(current.data(NetworkModel::ItemActiveRole).value<bool>());
85 void InputWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
86 QItemSelectionRange changedArea(topLeft, bottomRight);
87 QModelIndex currentIndex = Client::bufferModel()->currentIndex();
88 if(changedArea.contains(currentIndex)) {
89 ui.inputEdit->setEnabled(currentIndex.data(NetworkModel::ItemActiveRole).value<bool>());
94 const Network *InputWidget::currentNetwork() const {
98 return Client::network(_networkId);
101 void InputWidget::setNetwork(const Network *network) {
102 if(!network || _networkId == network->networkId())
105 const Network *previousNet = Client::network(_networkId);
107 disconnect(previousNet, 0, this, 0);
108 if(previousNet->me())
109 disconnect(previousNet->me(), 0, this, 0);
113 _networkId = network->networkId();
114 connect(network, SIGNAL(identitySet(IdentityId)), this, SLOT(setIdentity(IdentityId)));
116 connect(network->me(), SIGNAL(nickSet(QString)), this, SLOT(updateNickSelector()));
117 connect(network->me(), SIGNAL(userModesSet(QString)), this, SLOT(updateNickSelector()));
118 connect(network->me(), SIGNAL(userModesAdded(QString)), this, SLOT(updateNickSelector()));
119 connect(network->me(), SIGNAL(userModesRemoved(QString)), this, SLOT(updateNickSelector()));
122 setIdentity(network->identity());
125 void InputWidget::setIdentity(const IdentityId &identityId) {
126 if(_identityId == identityId)
129 const Identity *previousIdentity = Client::identity(_identityId);
131 disconnect(previousIdentity, 0, this, 0);
133 const Identity *identity = Client::identity(identityId);
135 _identityId = identityId;
136 connect(identity, SIGNAL(nicksSet(QStringList)),
137 this, SLOT(updateNickSelector()));
139 updateNickSelector();
142 void InputWidget::updateNickSelector() const {
145 const Network *net = currentNetwork();
149 const Identity *identity = Client::identity(net->identity());
151 qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId();
156 QStringList nicks = identity->nicks();
157 if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
158 nicks.prepend(net->myNick());
162 if(net->me() && nickIdx < nicks.count())
163 nicks[nickIdx] = net->myNick() + QString(" (%1)").arg(net->me()->userModes());
165 ui.ownNick->addItems(nicks);
166 ui.ownNick->setCurrentIndex(nickIdx);
169 void InputWidget::changeNick(const QString &newNick) const {
170 const Network *net = currentNetwork();
171 if(!net || net->isMyNick(newNick))
173 emit userInput(currentBufferInfo, QString("/nick %1").arg(newNick));
176 void InputWidget::sendText(QString text) {
177 emit userInput(currentBufferInfo, text);
181 // MOUSE WHEEL FILTER
182 MouseWheelFilter::MouseWheelFilter(QObject *parent)
187 bool MouseWheelFilter::eventFilter(QObject *obj, QEvent *event) {
188 if(event->type() != QEvent::Wheel)
189 return QObject::eventFilter(obj, event);