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(setCustomFont(QVariant)));
49 setCustomFont(s.value("InputLine", QFont()));
51 ActionCollection *coll = QtUi::actionCollection();
53 Action *activateInputline = coll->add<Action>("FocusInputLine");
54 connect(activateInputline, SIGNAL(triggered()), SLOT(setFocus()));
55 activateInputline->setText(tr("Focus Input Line"));
56 activateInputline->setShortcut(tr("Ctrl+L"));
59 InputWidget::~InputWidget() {
62 void InputWidget::setCustomFont(const QVariant &v) {
63 QFont font = v.value<QFont>();
64 if(font.family().isEmpty())
65 font = QApplication::font();
66 ui.inputEdit->setFont(font);
69 void InputWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
71 NetworkId networkId = current.data(NetworkModel::NetworkIdRole).value<NetworkId>();
72 if(networkId == _networkId)
75 setNetwork(networkId);
80 void InputWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
81 QItemSelectionRange changedArea(topLeft, bottomRight);
82 if(changedArea.contains(selectionModel()->currentIndex())) {
87 void InputWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
90 for(int row = start; row <= end; row++) {
91 child = model()->index(row, 0, parent);
92 if(NetworkModel::NetworkItemType != child.data(NetworkModel::ItemTypeRole).toInt())
94 networkId = child.data(NetworkModel::NetworkIdRole).value<NetworkId>();
95 if(networkId == _networkId) {
103 void InputWidget::updateEnabledState() {
104 QModelIndex currentIndex = selectionModel()->currentIndex();
106 const Network *net = Client::networkModel()->networkByIndex(currentIndex);
107 bool enabled = false;
109 // disable inputline if it's a channelbuffer we parted from or...
110 enabled = (currentIndex.data(NetworkModel::ItemActiveRole).value<bool>() || (currentIndex.data(NetworkModel::BufferTypeRole).toInt() != BufferInfo::ChannelBuffer));
111 // ... if we're not connected to the network at all
112 enabled &= net->isConnected();
114 ui.inputEdit->setEnabled(enabled);
117 const Network *InputWidget::currentNetwork() const {
118 return Client::network(_networkId);
121 BufferInfo InputWidget::currentBufferInfo() const {
122 return selectionModel()->currentIndex().data(NetworkModel::BufferInfoRole).value<BufferInfo>();
125 void InputWidget::setNetwork(NetworkId networkId) {
126 if(_networkId == networkId)
129 const Network *previousNet = Client::network(_networkId);
131 disconnect(previousNet, 0, this, 0);
132 if(previousNet->me())
133 disconnect(previousNet->me(), 0, this, 0);
136 _networkId = networkId;
138 const Network *network = Client::network(networkId);
140 connect(network, SIGNAL(identitySet(IdentityId)), this, SLOT(setIdentity(IdentityId)));
142 setIdentity(network->identity());
149 void InputWidget::connectMyIrcUser() {
150 const Network *network = currentNetwork();
152 connect(network->me(), SIGNAL(nickSet(const QString &)), this, SLOT(updateNickSelector()));
153 connect(network->me(), SIGNAL(userModesSet(QString)), this, SLOT(updateNickSelector()));
154 connect(network->me(), SIGNAL(userModesAdded(QString)), this, SLOT(updateNickSelector()));
155 connect(network->me(), SIGNAL(userModesRemoved(QString)), this, SLOT(updateNickSelector()));
156 connect(network->me(), SIGNAL(awaySet(bool)), this, SLOT(updateNickSelector()));
157 disconnect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
158 updateNickSelector();
160 connect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
164 void InputWidget::setIdentity(IdentityId identityId) {
165 if(_identityId == identityId)
168 const Identity *previousIdentity = Client::identity(_identityId);
170 disconnect(previousIdentity, 0, this, 0);
172 _identityId = identityId;
174 const Identity *identity = Client::identity(identityId);
176 connect(identity, SIGNAL(nicksSet(QStringList)), this, SLOT(updateNickSelector()));
180 updateNickSelector();
183 void InputWidget::updateNickSelector() const {
186 const Network *net = currentNetwork();
190 const Identity *identity = Client::identity(net->identity());
192 qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId() << "IdentityId:" << net->identity();
197 QStringList nicks = identity->nicks();
198 if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
199 nicks.prepend(net->myNick());
206 IrcUser *me = net->me();
208 nicks[nickIdx] = net->myNick();
209 if(!me->userModes().isEmpty())
210 nicks[nickIdx] += QString(" (+%1)").arg(me->userModes());
213 ui.ownNick->addItems(nicks);
215 if(me && me->isAway())
216 ui.ownNick->setItemData(nickIdx, SmallIcon("user-away"), Qt::DecorationRole);
218 ui.ownNick->setCurrentIndex(nickIdx);
221 void InputWidget::changeNick(const QString &newNick) const {
222 const Network *net = currentNetwork();
223 if(!net || net->isMyNick(newNick))
226 // we reset the nick selecter as we have no confirmation yet, that this will succeed.
227 // if the action succeeds it will be properly updated anyways.
228 updateNickSelector();
229 emit userInput(BufferInfo::fakeStatusBuffer(net->networkId()), QString("/nick %1").arg(newNick));
232 void InputWidget::sendText(QString text) {
233 emit userInput(currentBufferInfo(), text);
237 // MOUSE WHEEL FILTER
238 MouseWheelFilter::MouseWheelFilter(QObject *parent)
243 bool MouseWheelFilter::eventFilter(QObject *obj, QEvent *event) {
244 if(event->type() != QEvent::Wheel)
245 return QObject::eventFilter(obj, event);