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"
25 #include "bufferview.h"
27 #include "iconloader.h"
29 #include "jumpkeyhandler.h"
30 #include "networkmodel.h"
32 #include "qtuisettings.h"
33 #include "tabcompleter.h"
35 InputWidget::InputWidget(QWidget *parent)
36 : AbstractItemView(parent),
40 connect(ui.inputEdit, SIGNAL(textEntered(QString)), this, SLOT(sendText(QString)));
41 connect(ui.ownNick, SIGNAL(activated(QString)), this, SLOT(changeNick(QString)));
43 layout()->setAlignment(ui.ownNick, Qt::AlignBottom);
44 layout()->setAlignment(ui.inputEdit, Qt::AlignBottom);
46 setFocusProxy(ui.inputEdit);
47 ui.ownNick->setFocusProxy(ui.inputEdit);
49 ui.ownNick->setSizeAdjustPolicy(QComboBox::AdjustToContents);
50 ui.ownNick->installEventFilter(new MouseWheelFilter(this));
51 ui.inputEdit->installEventFilter(new JumpKeyHandler(this));
52 ui.inputEdit->installEventFilter(this);
54 ui.inputEdit->setMinHeight(1);
55 ui.inputEdit->setMaxHeight(5);
56 ui.inputEdit->setMode(MultiLineEdit::MultiLine);
58 new TabCompleter(ui.inputEdit);
60 UiStyleSettings fs("Fonts");
61 fs.notify("InputWidget", this, SLOT(setCustomFont(QVariant)));
62 setCustomFont(fs.value("InputWidget", QFont()));
64 UiSettings s("InputWidget");
67 s.notify("EnableSpellCheck", this, SLOT(setEnableSpellCheck(QVariant)));
68 setEnableSpellCheck(s.value("EnableSpellCheck", false));
71 s.notify("ShowNickSelector", this, SLOT(setShowNickSelector(QVariant)));
72 setShowNickSelector(s.value("ShowNickSelector", true));
74 s.notify("MaxNumLines", this, SLOT(setMaxLines(QVariant)));
75 setMaxLines(s.value("MaxNumLines", 5));
77 s.notify("EnableScrollBars", this, SLOT(setEnableScrollBars(QVariant)));
78 setEnableScrollBars(s.value("EnableScrollBars", true));
80 ActionCollection *coll = QtUi::actionCollection();
82 Action *activateInputline = coll->add<Action>("FocusInputLine");
83 connect(activateInputline, SIGNAL(triggered()), SLOT(setFocus()));
84 activateInputline->setText(tr("Focus Input Line"));
85 activateInputline->setShortcut(tr("Ctrl+L"));
88 InputWidget::~InputWidget() {
91 void InputWidget::setCustomFont(const QVariant &v) {
92 QFont font = v.value<QFont>();
93 if(font.family().isEmpty())
94 font = QApplication::font();
95 ui.inputEdit->setCustomFont(font);
98 void InputWidget::setEnableSpellCheck(const QVariant &v) {
99 ui.inputEdit->setSpellCheckEnabled(v.toBool());
102 void InputWidget::setShowNickSelector(const QVariant &v) {
103 ui.ownNick->setVisible(v.toBool());
106 void InputWidget::setMaxLines(const QVariant &v) {
107 ui.inputEdit->setMaxHeight(v.toInt());
110 void InputWidget::setEnableScrollBars(const QVariant &v) {
111 ui.inputEdit->setScrollBarsEnabled(v.toBool());
114 bool InputWidget::eventFilter(QObject *watched, QEvent *event) {
115 if(event->type() != QEvent::KeyPress)
118 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
120 // keys from BufferView should be sent to (and focus) the input line
121 BufferView *view = qobject_cast<BufferView *>(watched);
123 if(keyEvent->text().length() == 1 && !(keyEvent->modifiers() & (Qt::ControlModifier ^ Qt::AltModifier)) ) { // normal key press
124 QChar c = keyEvent->text().at(0);
125 if(c.isLetterOrNumber() || c.isSpace() || c.isPunct() || c.isSymbol()) {
127 QCoreApplication::sendEvent(inputLine(), keyEvent);
132 } else if(watched == ui.inputEdit) {
133 if(keyEvent->matches(QKeySequence::Find)) {
134 QAction *act = GraphicalUi::actionCollection()->action("ToggleSearchBar");
145 void InputWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
147 NetworkId networkId = current.data(NetworkModel::NetworkIdRole).value<NetworkId>();
148 if(networkId == _networkId)
151 setNetwork(networkId);
152 updateNickSelector();
153 updateEnabledState();
156 void InputWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
157 QItemSelectionRange changedArea(topLeft, bottomRight);
158 if(changedArea.contains(selectionModel()->currentIndex())) {
159 updateEnabledState();
163 void InputWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
166 for(int row = start; row <= end; row++) {
167 child = model()->index(row, 0, parent);
168 if(NetworkModel::NetworkItemType != child.data(NetworkModel::ItemTypeRole).toInt())
170 networkId = child.data(NetworkModel::NetworkIdRole).value<NetworkId>();
171 if(networkId == _networkId) {
173 updateNickSelector();
179 void InputWidget::updateEnabledState() {
180 QModelIndex currentIndex = selectionModel()->currentIndex();
182 const Network *net = Client::networkModel()->networkByIndex(currentIndex);
183 bool enabled = false;
185 // disable inputline if it's a channelbuffer we parted from or...
186 enabled = (currentIndex.data(NetworkModel::ItemActiveRole).value<bool>() || (currentIndex.data(NetworkModel::BufferTypeRole).toInt() != BufferInfo::ChannelBuffer));
187 // ... if we're not connected to the network at all
188 enabled &= net->isConnected();
190 ui.inputEdit->setEnabled(enabled);
193 const Network *InputWidget::currentNetwork() const {
194 return Client::network(_networkId);
197 BufferInfo InputWidget::currentBufferInfo() const {
198 return selectionModel()->currentIndex().data(NetworkModel::BufferInfoRole).value<BufferInfo>();
201 void InputWidget::setNetwork(NetworkId networkId) {
202 if(_networkId == networkId)
205 const Network *previousNet = Client::network(_networkId);
207 disconnect(previousNet, 0, this, 0);
208 if(previousNet->me())
209 disconnect(previousNet->me(), 0, this, 0);
212 _networkId = networkId;
214 const Network *network = Client::network(networkId);
216 connect(network, SIGNAL(identitySet(IdentityId)), this, SLOT(setIdentity(IdentityId)));
218 setIdentity(network->identity());
225 void InputWidget::connectMyIrcUser() {
226 const Network *network = currentNetwork();
228 connect(network->me(), SIGNAL(nickSet(const QString &)), this, SLOT(updateNickSelector()));
229 connect(network->me(), SIGNAL(userModesSet(QString)), this, SLOT(updateNickSelector()));
230 connect(network->me(), SIGNAL(userModesAdded(QString)), this, SLOT(updateNickSelector()));
231 connect(network->me(), SIGNAL(userModesRemoved(QString)), this, SLOT(updateNickSelector()));
232 connect(network->me(), SIGNAL(awaySet(bool)), this, SLOT(updateNickSelector()));
233 disconnect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
234 updateNickSelector();
236 connect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
240 void InputWidget::setIdentity(IdentityId identityId) {
241 if(_identityId == identityId)
244 const Identity *previousIdentity = Client::identity(_identityId);
246 disconnect(previousIdentity, 0, this, 0);
248 _identityId = identityId;
250 const Identity *identity = Client::identity(identityId);
252 connect(identity, SIGNAL(nicksSet(QStringList)), this, SLOT(updateNickSelector()));
256 updateNickSelector();
259 void InputWidget::updateNickSelector() const {
262 const Network *net = currentNetwork();
266 const Identity *identity = Client::identity(net->identity());
268 qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId() << "IdentityId:" << net->identity();
273 QStringList nicks = identity->nicks();
274 if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
275 nicks.prepend(net->myNick());
282 IrcUser *me = net->me();
284 nicks[nickIdx] = net->myNick();
285 if(!me->userModes().isEmpty())
286 nicks[nickIdx] += QString(" (+%1)").arg(me->userModes());
289 ui.ownNick->addItems(nicks);
291 if(me && me->isAway())
292 ui.ownNick->setItemData(nickIdx, SmallIcon("user-away"), Qt::DecorationRole);
294 ui.ownNick->setCurrentIndex(nickIdx);
297 void InputWidget::changeNick(const QString &newNick) const {
298 const Network *net = currentNetwork();
299 if(!net || net->isMyNick(newNick))
302 // we reset the nick selecter as we have no confirmation yet, that this will succeed.
303 // if the action succeeds it will be properly updated anyways.
304 updateNickSelector();
305 Client::userInput(BufferInfo::fakeStatusBuffer(net->networkId()), QString("/NICK %1").arg(newNick));
308 void InputWidget::sendText(const QString &text) const {
309 Client::userInput(currentBufferInfo(), text);
313 // MOUSE WHEEL FILTER
314 MouseWheelFilter::MouseWheelFilter(QObject *parent)
319 bool MouseWheelFilter::eventFilter(QObject *obj, QEvent *event) {
320 if(event->type() != QEvent::Wheel)
321 return QObject::eventFilter(obj, event);