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);
57 ui.inputEdit->setPasteProtectionEnabled(true);
59 new TabCompleter(ui.inputEdit);
61 UiStyleSettings fs("Fonts");
62 fs.notify("InputWidget", this, SLOT(setCustomFont(QVariant)));
63 setCustomFont(fs.value("InputWidget", QFont()));
65 UiSettings s("InputWidget");
68 s.notify("EnableSpellCheck", this, SLOT(setEnableSpellCheck(QVariant)));
69 setEnableSpellCheck(s.value("EnableSpellCheck", false));
72 s.notify("ShowNickSelector", this, SLOT(setShowNickSelector(QVariant)));
73 setShowNickSelector(s.value("ShowNickSelector", true));
75 s.notify("MaxNumLines", this, SLOT(setMaxLines(QVariant)));
76 setMaxLines(s.value("MaxNumLines", 5));
78 s.notify("EnableScrollBars", this, SLOT(setScrollBarsEnabled(QVariant)));
79 setScrollBarsEnabled(s.value("EnableScrollBars", true));
81 s.notify("EnableMultiLine", this, SLOT(setMultiLineEnabled(QVariant)));
82 setMultiLineEnabled(s.value("EnableMultiLine", true));
84 ActionCollection *coll = QtUi::actionCollection();
86 Action *activateInputline = coll->add<Action>("FocusInputLine");
87 connect(activateInputline, SIGNAL(triggered()), SLOT(setFocus()));
88 activateInputline->setText(tr("Focus Input Line"));
89 activateInputline->setShortcut(tr("Ctrl+L"));
92 InputWidget::~InputWidget() {
95 void InputWidget::setCustomFont(const QVariant &v) {
96 QFont font = v.value<QFont>();
97 if(font.family().isEmpty())
98 font = QApplication::font();
99 ui.inputEdit->setCustomFont(font);
102 void InputWidget::setEnableSpellCheck(const QVariant &v) {
103 ui.inputEdit->setSpellCheckEnabled(v.toBool());
106 void InputWidget::setShowNickSelector(const QVariant &v) {
107 ui.ownNick->setVisible(v.toBool());
110 void InputWidget::setMaxLines(const QVariant &v) {
111 ui.inputEdit->setMaxHeight(v.toInt());
114 void InputWidget::setScrollBarsEnabled(const QVariant &v) {
115 ui.inputEdit->setScrollBarsEnabled(v.toBool());
118 void InputWidget::setMultiLineEnabled(const QVariant &v) {
119 ui.inputEdit->setMode(v.toBool()? MultiLineEdit::MultiLine : MultiLineEdit::SingleLine);
122 bool InputWidget::eventFilter(QObject *watched, QEvent *event) {
123 if(event->type() != QEvent::KeyPress)
126 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
128 // keys from BufferView should be sent to (and focus) the input line
129 BufferView *view = qobject_cast<BufferView *>(watched);
131 if(keyEvent->text().length() == 1 && !(keyEvent->modifiers() & (Qt::ControlModifier ^ Qt::AltModifier)) ) { // normal key press
132 QChar c = keyEvent->text().at(0);
133 if(c.isLetterOrNumber() || c.isSpace() || c.isPunct() || c.isSymbol()) {
135 QCoreApplication::sendEvent(inputLine(), keyEvent);
140 } else if(watched == ui.inputEdit) {
141 if(keyEvent->matches(QKeySequence::Find)) {
142 QAction *act = GraphicalUi::actionCollection()->action("ToggleSearchBar");
153 void InputWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
155 NetworkId networkId = current.data(NetworkModel::NetworkIdRole).value<NetworkId>();
156 if(networkId == _networkId)
159 setNetwork(networkId);
160 updateNickSelector();
161 updateEnabledState();
164 void InputWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
165 QItemSelectionRange changedArea(topLeft, bottomRight);
166 if(changedArea.contains(selectionModel()->currentIndex())) {
167 updateEnabledState();
171 void InputWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
174 for(int row = start; row <= end; row++) {
175 child = model()->index(row, 0, parent);
176 if(NetworkModel::NetworkItemType != child.data(NetworkModel::ItemTypeRole).toInt())
178 networkId = child.data(NetworkModel::NetworkIdRole).value<NetworkId>();
179 if(networkId == _networkId) {
181 updateNickSelector();
187 void InputWidget::updateEnabledState() {
188 QModelIndex currentIndex = selectionModel()->currentIndex();
190 const Network *net = Client::networkModel()->networkByIndex(currentIndex);
191 bool enabled = false;
193 // disable inputline if it's a channelbuffer we parted from or...
194 enabled = (currentIndex.data(NetworkModel::ItemActiveRole).value<bool>() || (currentIndex.data(NetworkModel::BufferTypeRole).toInt() != BufferInfo::ChannelBuffer));
195 // ... if we're not connected to the network at all
196 enabled &= net->isConnected();
198 ui.inputEdit->setEnabled(enabled);
201 const Network *InputWidget::currentNetwork() const {
202 return Client::network(_networkId);
205 BufferInfo InputWidget::currentBufferInfo() const {
206 return selectionModel()->currentIndex().data(NetworkModel::BufferInfoRole).value<BufferInfo>();
209 void InputWidget::setNetwork(NetworkId networkId) {
210 if(_networkId == networkId)
213 const Network *previousNet = Client::network(_networkId);
215 disconnect(previousNet, 0, this, 0);
216 if(previousNet->me())
217 disconnect(previousNet->me(), 0, this, 0);
220 _networkId = networkId;
222 const Network *network = Client::network(networkId);
224 connect(network, SIGNAL(identitySet(IdentityId)), this, SLOT(setIdentity(IdentityId)));
226 setIdentity(network->identity());
233 void InputWidget::connectMyIrcUser() {
234 const Network *network = currentNetwork();
236 connect(network->me(), SIGNAL(nickSet(const QString &)), this, SLOT(updateNickSelector()));
237 connect(network->me(), SIGNAL(userModesSet(QString)), this, SLOT(updateNickSelector()));
238 connect(network->me(), SIGNAL(userModesAdded(QString)), this, SLOT(updateNickSelector()));
239 connect(network->me(), SIGNAL(userModesRemoved(QString)), this, SLOT(updateNickSelector()));
240 connect(network->me(), SIGNAL(awaySet(bool)), this, SLOT(updateNickSelector()));
241 disconnect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
242 updateNickSelector();
244 connect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
248 void InputWidget::setIdentity(IdentityId identityId) {
249 if(_identityId == identityId)
252 const Identity *previousIdentity = Client::identity(_identityId);
254 disconnect(previousIdentity, 0, this, 0);
256 _identityId = identityId;
258 const Identity *identity = Client::identity(identityId);
260 connect(identity, SIGNAL(nicksSet(QStringList)), this, SLOT(updateNickSelector()));
264 updateNickSelector();
267 void InputWidget::updateNickSelector() const {
270 const Network *net = currentNetwork();
274 const Identity *identity = Client::identity(net->identity());
276 qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId() << "IdentityId:" << net->identity();
281 QStringList nicks = identity->nicks();
282 if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
283 nicks.prepend(net->myNick());
290 IrcUser *me = net->me();
292 nicks[nickIdx] = net->myNick();
293 if(!me->userModes().isEmpty())
294 nicks[nickIdx] += QString(" (+%1)").arg(me->userModes());
297 ui.ownNick->addItems(nicks);
299 if(me && me->isAway())
300 ui.ownNick->setItemData(nickIdx, SmallIcon("user-away"), Qt::DecorationRole);
302 ui.ownNick->setCurrentIndex(nickIdx);
305 void InputWidget::changeNick(const QString &newNick) const {
306 const Network *net = currentNetwork();
307 if(!net || net->isMyNick(newNick))
310 // we reset the nick selecter as we have no confirmation yet, that this will succeed.
311 // if the action succeeds it will be properly updated anyways.
312 updateNickSelector();
313 Client::userInput(BufferInfo::fakeStatusBuffer(net->networkId()), QString("/NICK %1").arg(newNick));
316 void InputWidget::sendText(const QString &text) const {
317 Client::userInput(currentBufferInfo(), text);
321 // MOUSE WHEEL FILTER
322 MouseWheelFilter::MouseWheelFilter(QObject *parent)
327 bool MouseWheelFilter::eventFilter(QObject *obj, QEvent *event) {
328 if(event->type() != QEvent::Wheel)
329 return QObject::eventFilter(obj, event);