1 /***************************************************************************
2 * Copyright (C) 2005-09 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"
24 #include "actioncollection.h"
26 #include "iconloader.h"
28 #include "jumpkeyhandler.h"
29 #include "networkmodel.h"
31 #include "qtuisettings.h"
33 InputWidget::InputWidget(QWidget *parent)
34 : AbstractItemView(parent),
38 connect(ui.inputEdit, SIGNAL(sendText(QString)), this, SLOT(sendText(QString)));
39 connect(ui.ownNick, SIGNAL(activated(QString)), this, SLOT(changeNick(QString)));
40 connect(this, SIGNAL(userInput(BufferInfo, QString)), Client::instance(), SIGNAL(sendInput(BufferInfo, QString)));
41 setFocusProxy(ui.inputEdit);
43 ui.ownNick->setSizeAdjustPolicy(QComboBox::AdjustToContents);
44 ui.ownNick->installEventFilter(new MouseWheelFilter(this));
45 ui.inputEdit->installEventFilter(new JumpKeyHandler(this));
48 bool useInputLineFont = s.value("UseInputLineFont", QVariant(false)).toBool();
49 if(useInputLineFont) {
50 ui.inputEdit->setFont(s.value("InputLineFont").value<QFont>());
53 ActionCollection *coll = QtUi::actionCollection();
55 Action *activateInputline = coll->add<Action>("FocusInputLine");
56 connect(activateInputline, SIGNAL(triggered()), SLOT(setFocus()));
57 activateInputline->setText(tr("Focus Input Line"));
58 activateInputline->setShortcut(tr("Ctrl+L"));
61 InputWidget::~InputWidget() {
64 void InputWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
66 NetworkId networkId = current.data(NetworkModel::NetworkIdRole).value<NetworkId>();
67 if(networkId == _networkId)
70 setNetwork(networkId);
75 void InputWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
76 QItemSelectionRange changedArea(topLeft, bottomRight);
77 if(changedArea.contains(selectionModel()->currentIndex())) {
82 void InputWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
85 for(int row = start; row <= end; row++) {
86 child = model()->index(row, 0, parent);
87 if(NetworkModel::NetworkItemType != child.data(NetworkModel::ItemTypeRole).toInt())
89 networkId = child.data(NetworkModel::NetworkIdRole).value<NetworkId>();
90 if(networkId == _networkId) {
98 void InputWidget::updateEnabledState() {
99 QModelIndex currentIndex = selectionModel()->currentIndex();
101 const Network *net = Client::networkModel()->networkByIndex(currentIndex);
102 bool enabled = false;
104 // disable inputline if it's a channelbuffer we parted from or...
105 enabled = (currentIndex.data(NetworkModel::ItemActiveRole).value<bool>() || (currentIndex.data(NetworkModel::BufferTypeRole).toInt() != BufferInfo::ChannelBuffer));
106 // ... if we're not connected to the network at all
107 enabled &= net->isConnected();
109 ui.inputEdit->setEnabled(enabled);
112 const Network *InputWidget::currentNetwork() const {
113 return Client::network(_networkId);
116 BufferInfo InputWidget::currentBufferInfo() const {
117 return selectionModel()->currentIndex().data(NetworkModel::BufferInfoRole).value<BufferInfo>();
120 void InputWidget::setNetwork(NetworkId networkId) {
121 if(_networkId == networkId)
124 const Network *previousNet = Client::network(_networkId);
126 disconnect(previousNet, 0, this, 0);
127 if(previousNet->me())
128 disconnect(previousNet->me(), 0, this, 0);
131 _networkId = networkId;
133 const Network *network = Client::network(networkId);
135 connect(network, SIGNAL(identitySet(IdentityId)), this, SLOT(setIdentity(IdentityId)));
137 setIdentity(network->identity());
144 void InputWidget::connectMyIrcUser() {
145 const Network *network = currentNetwork();
147 connect(network->me(), SIGNAL(nickSet(const QString &)), this, SLOT(updateNickSelector()));
148 connect(network->me(), SIGNAL(userModesSet(QString)), this, SLOT(updateNickSelector()));
149 connect(network->me(), SIGNAL(userModesAdded(QString)), this, SLOT(updateNickSelector()));
150 connect(network->me(), SIGNAL(userModesRemoved(QString)), this, SLOT(updateNickSelector()));
151 connect(network->me(), SIGNAL(awaySet(bool)), this, SLOT(updateNickSelector()));
152 disconnect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
154 connect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
158 void InputWidget::setIdentity(IdentityId identityId) {
159 if(_identityId == identityId)
162 const Identity *previousIdentity = Client::identity(_identityId);
164 disconnect(previousIdentity, 0, this, 0);
166 _identityId = identityId;
168 const Identity *identity = Client::identity(identityId);
170 connect(identity, SIGNAL(nicksSet(QStringList)),
171 this, SLOT(updateNickSelector()));
173 updateNickSelector();
176 void InputWidget::updateNickSelector() const {
179 const Network *net = currentNetwork();
183 const Identity *identity = Client::identity(net->identity());
185 qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId();
190 QStringList nicks = identity->nicks();
191 if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
192 nicks.prepend(net->myNick());
199 IrcUser *me = net->me();
201 nicks[nickIdx] = net->myNick() + QString(" (+%1)").arg(me->userModes());
203 ui.ownNick->addItems(nicks);
205 if(me && me->isAway())
206 ui.ownNick->setItemData(nickIdx, SmallIcon("user-away"), Qt::DecorationRole);
208 ui.ownNick->setCurrentIndex(nickIdx);
211 void InputWidget::changeNick(const QString &newNick) const {
212 const Network *net = currentNetwork();
213 if(!net || net->isMyNick(newNick))
215 emit userInput(currentBufferInfo(), QString("/nick %1").arg(newNick));
218 void InputWidget::sendText(QString text) {
219 emit userInput(currentBufferInfo(), text);
223 // MOUSE WHEEL FILTER
224 MouseWheelFilter::MouseWheelFilter(QObject *parent)
229 bool MouseWheelFilter::eventFilter(QObject *obj, QEvent *event) {
230 if(event->type() != QEvent::Wheel)
231 return QObject::eventFilter(obj, event);