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));
47 QtUiStyleSettings s("Fonts");
48 s.notify("InputLine", this, SLOT(setFont(QVariant)));
49 QFont font = s.value("InputLine", QFont()).value<QFont>();
50 if(font.family().isEmpty())
51 font = QApplication::font();
54 ActionCollection *coll = QtUi::actionCollection();
56 Action *activateInputline = coll->add<Action>("FocusInputLine");
57 connect(activateInputline, SIGNAL(triggered()), SLOT(setFocus()));
58 activateInputline->setText(tr("Focus Input Line"));
59 activateInputline->setShortcut(tr("Ctrl+L"));
62 InputWidget::~InputWidget() {
65 void InputWidget::setCustomFont(const QVariant &v) {
66 QFont font = v.value<QFont>();
67 if(font.family().isEmpty())
68 font = QApplication::font();
69 ui.inputEdit->setFont(font);
72 void InputWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
74 NetworkId networkId = current.data(NetworkModel::NetworkIdRole).value<NetworkId>();
75 if(networkId == _networkId)
78 setNetwork(networkId);
83 void InputWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
84 QItemSelectionRange changedArea(topLeft, bottomRight);
85 if(changedArea.contains(selectionModel()->currentIndex())) {
90 void InputWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
93 for(int row = start; row <= end; row++) {
94 child = model()->index(row, 0, parent);
95 if(NetworkModel::NetworkItemType != child.data(NetworkModel::ItemTypeRole).toInt())
97 networkId = child.data(NetworkModel::NetworkIdRole).value<NetworkId>();
98 if(networkId == _networkId) {
100 updateNickSelector();
106 void InputWidget::updateEnabledState() {
107 QModelIndex currentIndex = selectionModel()->currentIndex();
109 const Network *net = Client::networkModel()->networkByIndex(currentIndex);
110 bool enabled = false;
112 // disable inputline if it's a channelbuffer we parted from or...
113 enabled = (currentIndex.data(NetworkModel::ItemActiveRole).value<bool>() || (currentIndex.data(NetworkModel::BufferTypeRole).toInt() != BufferInfo::ChannelBuffer));
114 // ... if we're not connected to the network at all
115 enabled &= net->isConnected();
117 ui.inputEdit->setEnabled(enabled);
120 const Network *InputWidget::currentNetwork() const {
121 return Client::network(_networkId);
124 BufferInfo InputWidget::currentBufferInfo() const {
125 return selectionModel()->currentIndex().data(NetworkModel::BufferInfoRole).value<BufferInfo>();
128 void InputWidget::setNetwork(NetworkId networkId) {
129 if(_networkId == networkId)
132 const Network *previousNet = Client::network(_networkId);
134 disconnect(previousNet, 0, this, 0);
135 if(previousNet->me())
136 disconnect(previousNet->me(), 0, this, 0);
139 _networkId = networkId;
141 const Network *network = Client::network(networkId);
143 connect(network, SIGNAL(identitySet(IdentityId)), this, SLOT(setIdentity(IdentityId)));
145 setIdentity(network->identity());
152 void InputWidget::connectMyIrcUser() {
153 const Network *network = currentNetwork();
155 connect(network->me(), SIGNAL(nickSet(const QString &)), this, SLOT(updateNickSelector()));
156 connect(network->me(), SIGNAL(userModesSet(QString)), this, SLOT(updateNickSelector()));
157 connect(network->me(), SIGNAL(userModesAdded(QString)), this, SLOT(updateNickSelector()));
158 connect(network->me(), SIGNAL(userModesRemoved(QString)), this, SLOT(updateNickSelector()));
159 connect(network->me(), SIGNAL(awaySet(bool)), this, SLOT(updateNickSelector()));
160 disconnect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
162 connect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
166 void InputWidget::setIdentity(IdentityId identityId) {
167 if(_identityId == identityId)
170 const Identity *previousIdentity = Client::identity(_identityId);
172 disconnect(previousIdentity, 0, this, 0);
174 _identityId = identityId;
176 const Identity *identity = Client::identity(identityId);
178 connect(identity, SIGNAL(nicksSet(QStringList)),
179 this, SLOT(updateNickSelector()));
181 updateNickSelector();
184 void InputWidget::updateNickSelector() const {
187 const Network *net = currentNetwork();
191 const Identity *identity = Client::identity(net->identity());
193 qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId();
198 QStringList nicks = identity->nicks();
199 if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
200 nicks.prepend(net->myNick());
207 IrcUser *me = net->me();
209 nicks[nickIdx] = net->myNick() + QString(" (+%1)").arg(me->userModes());
211 ui.ownNick->addItems(nicks);
213 if(me && me->isAway())
214 ui.ownNick->setItemData(nickIdx, SmallIcon("user-away"), Qt::DecorationRole);
216 ui.ownNick->setCurrentIndex(nickIdx);
219 void InputWidget::changeNick(const QString &newNick) const {
220 const Network *net = currentNetwork();
221 if(!net || net->isMyNick(newNick))
223 emit userInput(currentBufferInfo(), QString("/nick %1").arg(newNick));
226 void InputWidget::sendText(QString text) {
227 emit userInput(currentBufferInfo(), text);
231 // MOUSE WHEEL FILTER
232 MouseWheelFilter::MouseWheelFilter(QObject *parent)
237 bool MouseWheelFilter::eventFilter(QObject *obj, QEvent *event) {
238 if(event->type() != QEvent::Wheel)
239 return QObject::eventFilter(obj, event);