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 setFocusProxy(ui.inputEdit);
42 ui.ownNick->setSizeAdjustPolicy(QComboBox::AdjustToContents);
43 ui.ownNick->installEventFilter(new MouseWheelFilter(this));
44 ui.inputEdit->installEventFilter(new JumpKeyHandler(this));
46 QtUiStyleSettings s("Fonts");
47 s.notify("InputLine", this, SLOT(setCustomFont(QVariant)));
48 setCustomFont(s.value("InputLine", QFont()));
50 ActionCollection *coll = QtUi::actionCollection();
52 Action *activateInputline = coll->add<Action>("FocusInputLine");
53 connect(activateInputline, SIGNAL(triggered()), SLOT(setFocus()));
54 activateInputline->setText(tr("Focus Input Line"));
55 activateInputline->setShortcut(tr("Ctrl+L"));
58 InputWidget::~InputWidget() {
61 void InputWidget::setCustomFont(const QVariant &v) {
62 QFont font = v.value<QFont>();
63 if(font.family().isEmpty())
64 font = QApplication::font();
65 ui.inputEdit->setCustomFont(font);
68 void InputWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
70 NetworkId networkId = current.data(NetworkModel::NetworkIdRole).value<NetworkId>();
71 if(networkId == _networkId)
74 setNetwork(networkId);
79 void InputWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
80 QItemSelectionRange changedArea(topLeft, bottomRight);
81 if(changedArea.contains(selectionModel()->currentIndex())) {
86 void InputWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
89 for(int row = start; row <= end; row++) {
90 child = model()->index(row, 0, parent);
91 if(NetworkModel::NetworkItemType != child.data(NetworkModel::ItemTypeRole).toInt())
93 networkId = child.data(NetworkModel::NetworkIdRole).value<NetworkId>();
94 if(networkId == _networkId) {
102 void InputWidget::updateEnabledState() {
103 QModelIndex currentIndex = selectionModel()->currentIndex();
105 const Network *net = Client::networkModel()->networkByIndex(currentIndex);
106 bool enabled = false;
108 // disable inputline if it's a channelbuffer we parted from or...
109 enabled = (currentIndex.data(NetworkModel::ItemActiveRole).value<bool>() || (currentIndex.data(NetworkModel::BufferTypeRole).toInt() != BufferInfo::ChannelBuffer));
110 // ... if we're not connected to the network at all
111 enabled &= net->isConnected();
113 ui.inputEdit->setEnabled(enabled);
116 const Network *InputWidget::currentNetwork() const {
117 return Client::network(_networkId);
120 BufferInfo InputWidget::currentBufferInfo() const {
121 return selectionModel()->currentIndex().data(NetworkModel::BufferInfoRole).value<BufferInfo>();
124 void InputWidget::setNetwork(NetworkId networkId) {
125 if(_networkId == networkId)
128 const Network *previousNet = Client::network(_networkId);
130 disconnect(previousNet, 0, this, 0);
131 if(previousNet->me())
132 disconnect(previousNet->me(), 0, this, 0);
135 _networkId = networkId;
137 const Network *network = Client::network(networkId);
139 connect(network, SIGNAL(identitySet(IdentityId)), this, SLOT(setIdentity(IdentityId)));
141 setIdentity(network->identity());
148 void InputWidget::connectMyIrcUser() {
149 const Network *network = currentNetwork();
151 connect(network->me(), SIGNAL(nickSet(const QString &)), this, SLOT(updateNickSelector()));
152 connect(network->me(), SIGNAL(userModesSet(QString)), this, SLOT(updateNickSelector()));
153 connect(network->me(), SIGNAL(userModesAdded(QString)), this, SLOT(updateNickSelector()));
154 connect(network->me(), SIGNAL(userModesRemoved(QString)), this, SLOT(updateNickSelector()));
155 connect(network->me(), SIGNAL(awaySet(bool)), this, SLOT(updateNickSelector()));
156 disconnect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
157 updateNickSelector();
159 connect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
163 void InputWidget::setIdentity(IdentityId identityId) {
164 if(_identityId == identityId)
167 const Identity *previousIdentity = Client::identity(_identityId);
169 disconnect(previousIdentity, 0, this, 0);
171 _identityId = identityId;
173 const Identity *identity = Client::identity(identityId);
175 connect(identity, SIGNAL(nicksSet(QStringList)), this, SLOT(updateNickSelector()));
179 updateNickSelector();
182 void InputWidget::updateNickSelector() const {
185 const Network *net = currentNetwork();
189 const Identity *identity = Client::identity(net->identity());
191 qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId() << "IdentityId:" << net->identity();
196 QStringList nicks = identity->nicks();
197 if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
198 nicks.prepend(net->myNick());
205 IrcUser *me = net->me();
207 nicks[nickIdx] = net->myNick();
208 if(!me->userModes().isEmpty())
209 nicks[nickIdx] += QString(" (+%1)").arg(me->userModes());
212 ui.ownNick->addItems(nicks);
214 if(me && me->isAway())
215 ui.ownNick->setItemData(nickIdx, SmallIcon("user-away"), Qt::DecorationRole);
217 ui.ownNick->setCurrentIndex(nickIdx);
220 void InputWidget::changeNick(const QString &newNick) const {
221 const Network *net = currentNetwork();
222 if(!net || net->isMyNick(newNick))
225 // we reset the nick selecter as we have no confirmation yet, that this will succeed.
226 // if the action succeeds it will be properly updated anyways.
227 updateNickSelector();
228 Client::userInput(BufferInfo::fakeStatusBuffer(net->networkId()), QString("/NICK %1").arg(newNick));
231 void InputWidget::sendText(const QString &text) const {
232 Client::userInput(currentBufferInfo(), text);
236 // MOUSE WHEEL FILTER
237 MouseWheelFilter::MouseWheelFilter(QObject *parent)
242 bool MouseWheelFilter::eventFilter(QObject *obj, QEvent *event) {
243 if(event->type() != QEvent::Wheel)
244 return QObject::eventFilter(obj, event);