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"
27 #include "qtuisettings.h"
30 InputWidget::InputWidget(QWidget *parent)
31 : AbstractItemView(parent),
35 connect(ui.inputEdit, SIGNAL(sendText(QString)), this, SLOT(sendText(QString)));
36 connect(ui.ownNick, SIGNAL(activated(QString)), this, SLOT(changeNick(QString)));
37 connect(this, SIGNAL(userInput(BufferInfo, QString)), Client::instance(), SIGNAL(sendInput(BufferInfo, QString)));
38 setFocusProxy(ui.inputEdit);
40 ui.ownNick->setSizeAdjustPolicy(QComboBox::AdjustToContents);
41 ui.ownNick->installEventFilter(new MouseWheelFilter(this));
42 ui.inputEdit->installEventFilter(new JumpKeyHandler(this));
45 bool useInputLineFont = s.value("UseInputLineFont", QVariant(false)).toBool();
46 if(useInputLineFont) {
47 ui.inputEdit->setFont(s.value("InputLineFont").value<QFont>());
51 InputWidget::~InputWidget() {
54 void InputWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
55 if(current.data(NetworkModel::BufferInfoRole) == previous.data(NetworkModel::BufferInfoRole))
58 const Network *net = Client::networkModel()->networkByIndex(current);
64 void InputWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
65 QItemSelectionRange changedArea(topLeft, bottomRight);
66 if(changedArea.contains(selectionModel()->currentIndex())) {
71 void InputWidget::updateEnabledState() {
72 QModelIndex currentIndex = selectionModel()->currentIndex();
74 const Network *net = Client::networkModel()->networkByIndex(currentIndex);
77 // disable inputline if it's a channelbuffer we parted from or...
78 enabled = (currentIndex.data(NetworkModel::ItemActiveRole).value<bool>() || (currentIndex.data(NetworkModel::BufferTypeRole).toInt() != BufferInfo::ChannelBuffer));
79 // ... if we're not connected to the network at all
80 enabled &= net->isConnected();
82 ui.inputEdit->setEnabled(enabled);
85 const Network *InputWidget::currentNetwork() const {
86 return Client::network(_networkId);
89 BufferInfo InputWidget::currentBufferInfo() const {
90 return selectionModel()->currentIndex().data(NetworkModel::BufferInfoRole).value<BufferInfo>();
93 void InputWidget::setNetwork(const Network *network) {
94 if(!network || _networkId == network->networkId())
97 const Network *previousNet = Client::network(_networkId);
99 disconnect(previousNet, 0, this, 0);
100 if(previousNet->me())
101 disconnect(previousNet->me(), 0, this, 0);
105 _networkId = network->networkId();
106 connect(network, SIGNAL(identitySet(IdentityId)), this, SLOT(setIdentity(IdentityId)));
108 connect(network->me(), SIGNAL(nickSet(QString)), this, SLOT(updateNickSelector()));
109 connect(network->me(), SIGNAL(userModesSet(QString)), this, SLOT(updateNickSelector()));
110 connect(network->me(), SIGNAL(userModesAdded(QString)), this, SLOT(updateNickSelector()));
111 connect(network->me(), SIGNAL(userModesRemoved(QString)), this, SLOT(updateNickSelector()));
114 setIdentity(network->identity());
117 void InputWidget::setIdentity(const IdentityId &identityId) {
118 if(_identityId == identityId)
121 const Identity *previousIdentity = Client::identity(_identityId);
123 disconnect(previousIdentity, 0, this, 0);
125 const Identity *identity = Client::identity(identityId);
127 _identityId = identityId;
128 connect(identity, SIGNAL(nicksSet(QStringList)),
129 this, SLOT(updateNickSelector()));
131 updateNickSelector();
134 void InputWidget::updateNickSelector() const {
137 const Network *net = currentNetwork();
141 const Identity *identity = Client::identity(net->identity());
143 qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId();
148 QStringList nicks = identity->nicks();
149 if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
150 nicks.prepend(net->myNick());
154 if(net->me() && nickIdx < nicks.count())
155 nicks[nickIdx] = net->myNick() + QString(" (+%1)").arg(net->me()->userModes());
157 ui.ownNick->addItems(nicks);
158 ui.ownNick->setCurrentIndex(nickIdx);
161 void InputWidget::changeNick(const QString &newNick) const {
162 const Network *net = currentNetwork();
163 if(!net || net->isMyNick(newNick))
165 emit userInput(currentBufferInfo(), QString("/nick %1").arg(newNick));
168 void InputWidget::sendText(QString text) {
169 emit userInput(currentBufferInfo(), text);
173 // MOUSE WHEEL FILTER
174 MouseWheelFilter::MouseWheelFilter(QObject *parent)
179 bool MouseWheelFilter::eventFilter(QObject *obj, QEvent *event) {
180 if(event->type() != QEvent::Wheel)
181 return QObject::eventFilter(obj, event);