1 /***************************************************************************
2 * Copyright (C) 2005-2014 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "inputwidget.h"
24 #include "actioncollection.h"
25 #include "bufferview.h"
27 #include "iconloader.h"
29 #include "networkmodel.h"
31 #include "qtuisettings.h"
32 #include "tabcompleter.h"
35 const int leftMargin = 3;
37 InputWidget::InputWidget(QWidget *parent)
38 : AbstractItemView(parent),
42 connect(ui.ownNick, SIGNAL(activated(QString)), this, SLOT(changeNick(QString)));
44 layout()->setAlignment(ui.ownNick, Qt::AlignBottom);
45 layout()->setAlignment(ui.inputEdit, Qt::AlignBottom);
46 layout()->setAlignment(ui.showStyleButton, Qt::AlignBottom);
47 layout()->setAlignment(ui.styleFrame, Qt::AlignBottom);
49 ui.styleFrame->setVisible(false);
51 setFocusProxy(ui.inputEdit);
52 ui.ownNick->setFocusProxy(ui.inputEdit);
54 ui.ownNick->setSizeAdjustPolicy(QComboBox::AdjustToContents);
55 ui.ownNick->installEventFilter(new MouseWheelFilter(this));
56 ui.inputEdit->installEventFilter(this);
58 ui.inputEdit->setMinHeight(1);
59 ui.inputEdit->setMaxHeight(5);
60 ui.inputEdit->setMode(MultiLineEdit::MultiLine);
61 ui.inputEdit->setPasteProtectionEnabled(true);
63 ui.boldButton->setIcon(SmallIcon("format-text-bold"));
64 ui.italicButton->setIcon(SmallIcon("format-text-italic"));
65 ui.underlineButton->setIcon(SmallIcon("format-text-underline"));
66 ui.textcolorButton->setIcon(SmallIcon("format-text-color"));
67 ui.highlightcolorButton->setIcon(SmallIcon("format-fill-color"));
68 ui.encryptionIconLabel->hide();
70 _colorMenu = new QMenu();
71 _colorFillMenu = new QMenu();
74 names << tr("White") << tr("Black") << tr("Dark blue") << tr("Dark green") << tr("Red") << tr("Dark red") << tr("Dark magenta") << tr("Orange")
75 << tr("Yellow") << tr("Green") << tr("Dark cyan") << tr("Cyan") << tr("Blue") << tr("Magenta") << tr("Dark gray") << tr("Light gray");
78 for (int i = 0; i < inputLine()->mircColorMap().count(); i++) {
79 pix.fill(inputLine()->mircColorMap().values()[i]);
80 _colorMenu->addAction(pix, names[i])->setData(inputLine()->mircColorMap().keys()[i]);
81 _colorFillMenu->addAction(pix, names[i])->setData(inputLine()->mircColorMap().keys()[i]);
84 pix.fill(Qt::transparent);
85 _colorMenu->addAction(pix, tr("Clear Color"))->setData("");
86 _colorFillMenu->addAction(pix, tr("Clear Color"))->setData("");
88 ui.textcolorButton->setMenu(_colorMenu);
89 connect(_colorMenu, SIGNAL(triggered(QAction *)), this, SLOT(colorChosen(QAction *)));
90 ui.highlightcolorButton->setMenu(_colorFillMenu);
91 connect(_colorFillMenu, SIGNAL(triggered(QAction *)), this, SLOT(colorHighlightChosen(QAction *)));
93 new TabCompleter(ui.inputEdit);
95 UiStyleSettings fs("Fonts");
96 fs.notify("UseCustomInputWidgetFont", this, SLOT(setUseCustomFont(QVariant)));
97 fs.notify("InputWidget", this, SLOT(setCustomFont(QVariant)));
98 if (fs.value("UseCustomInputWidgetFont", false).toBool())
99 setCustomFont(fs.value("InputWidget", QFont()));
101 UiSettings s("InputWidget");
104 s.notify("EnableSpellCheck", this, SLOT(setEnableSpellCheck(QVariant)));
105 setEnableSpellCheck(s.value("EnableSpellCheck", false));
108 s.notify("EnableEmacsMode", this, SLOT(setEnableEmacsMode(QVariant)));
109 setEnableEmacsMode(s.value("EnableEmacsMode", false));
111 s.notify("ShowNickSelector", this, SLOT(setShowNickSelector(QVariant)));
112 setShowNickSelector(s.value("ShowNickSelector", true));
114 s.notify("ShowStyleButtons", this, SLOT(setShowStyleButtons(QVariant)));
115 setShowStyleButtons(s.value("ShowStyleButtons", true));
117 s.notify("EnablePerChatHistory", this, SLOT(setEnablePerChatHistory(QVariant)));
118 setEnablePerChatHistory(s.value("EnablePerChatHistory", false));
120 s.notify("MaxNumLines", this, SLOT(setMaxLines(QVariant)));
121 setMaxLines(s.value("MaxNumLines", 5));
123 s.notify("EnableScrollBars", this, SLOT(setScrollBarsEnabled(QVariant)));
124 setScrollBarsEnabled(s.value("EnableScrollBars", true));
126 s.notify("EnableLineWrap", this, SLOT(setLineWrapEnabled(QVariant)));
127 setLineWrapEnabled(s.value("EnableLineWrap", false));
129 s.notify("EnableMultiLine", this, SLOT(setMultiLineEnabled(QVariant)));
130 setMultiLineEnabled(s.value("EnableMultiLine", true));
132 ActionCollection *coll = QtUi::actionCollection();
134 Action *activateInputline = coll->add<Action>("FocusInputLine");
135 connect(activateInputline, SIGNAL(triggered()), SLOT(setFocus()));
136 activateInputline->setText(tr("Focus Input Line"));
137 activateInputline->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_L));
139 connect(inputLine(), SIGNAL(textEntered(QString)), SLOT(onTextEntered(QString)), Qt::QueuedConnection); // make sure the line is already reset, bug #984
140 connect(inputLine(), SIGNAL(currentCharFormatChanged(QTextCharFormat)), this, SLOT(currentCharFormatChanged(QTextCharFormat)));
144 InputWidget::~InputWidget()
149 void InputWidget::setUseCustomFont(const QVariant &v)
152 UiStyleSettings fs("Fonts");
153 setCustomFont(fs.value("InputWidget"));
156 setCustomFont(QFont());
160 void InputWidget::setCustomFont(const QVariant &v)
162 QFont font = v.value<QFont>();
163 if (font.family().isEmpty())
164 font = QApplication::font();
165 // we don't want font styles as this conflics with mirc code richtext editing
167 font.setItalic(false);
168 font.setUnderline(false);
169 font.setStrikeOut(false);
170 ui.inputEdit->setCustomFont(font);
174 void InputWidget::setEnableSpellCheck(const QVariant &v)
176 ui.inputEdit->setSpellCheckEnabled(v.toBool());
180 void InputWidget::setEnableEmacsMode(const QVariant &v)
182 ui.inputEdit->setEmacsMode(v.toBool());
186 void InputWidget::setShowNickSelector(const QVariant &v)
188 ui.ownNick->setVisible(v.toBool());
192 void InputWidget::setShowStyleButtons(const QVariant &v)
194 ui.showStyleButton->setVisible(v.toBool());
198 void InputWidget::setEnablePerChatHistory(const QVariant &v)
200 _perChatHistory = v.toBool();
204 void InputWidget::setMaxLines(const QVariant &v)
206 ui.inputEdit->setMaxHeight(v.toInt());
210 void InputWidget::setScrollBarsEnabled(const QVariant &v)
212 ui.inputEdit->setScrollBarsEnabled(v.toBool());
216 void InputWidget::setLineWrapEnabled(const QVariant &v)
218 ui.inputEdit->setLineWrapEnabled(v.toBool());
222 void InputWidget::setMultiLineEnabled(const QVariant &v)
224 ui.inputEdit->setMode(v.toBool() ? MultiLineEdit::MultiLine : MultiLineEdit::SingleLine);
228 bool InputWidget::eventFilter(QObject *watched, QEvent *event)
230 if (event->type() != QEvent::KeyPress)
233 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
235 // keys from BufferView should be sent to (and focus) the input line
236 BufferView *view = qobject_cast<BufferView *>(watched);
238 if (keyEvent->text().length() == 1 && !(keyEvent->modifiers() & (Qt::ControlModifier ^ Qt::AltModifier))) { // normal key press
239 QChar c = keyEvent->text().at(0);
240 if (c.isLetterOrNumber() || c.isSpace() || c.isPunct() || c.isSymbol()) {
242 QCoreApplication::sendEvent(inputLine(), keyEvent);
248 else if (watched == ui.inputEdit) {
249 if (keyEvent->matches(QKeySequence::Find)) {
250 QAction *act = GraphicalUi::actionCollection()->action("ToggleSearchBar");
262 void InputWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
264 BufferId currentBufferId = current.data(NetworkModel::BufferIdRole).value<BufferId>();
265 BufferId previousBufferId = previous.data(NetworkModel::BufferIdRole).value<BufferId>();
267 if (_perChatHistory) {
269 historyMap[previousBufferId].history = inputLine()->history();
270 historyMap[previousBufferId].tempHistory = inputLine()->tempHistory();
271 historyMap[previousBufferId].idx = inputLine()->idx();
272 historyMap[previousBufferId].inputLine = inputLine()->html();
275 inputLine()->setHistory(historyMap[currentBufferId].history);
276 inputLine()->setTempHistory(historyMap[currentBufferId].tempHistory);
277 inputLine()->setIdx(historyMap[currentBufferId].idx);
278 inputLine()->setHtml(historyMap[currentBufferId].inputLine);
279 inputLine()->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
281 // FIXME this really should be in MultiLineEdit (and the const int on top removed)
282 QTextBlockFormat format = inputLine()->textCursor().blockFormat();
283 format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
284 inputLine()->textCursor().setBlockFormat(format);
287 NetworkId networkId = current.data(NetworkModel::NetworkIdRole).value<NetworkId>();
288 if (networkId == _networkId)
291 setNetwork(networkId);
292 updateNickSelector();
293 updateEnabledState();
297 void InputWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
299 QItemSelectionRange changedArea(topLeft, bottomRight);
300 if (changedArea.contains(selectionModel()->currentIndex())) {
301 updateEnabledState();
303 bool encrypted = false;
305 IrcChannel *chan = qobject_cast<IrcChannel *>(Client::bufferModel()->data(selectionModel()->currentIndex(), NetworkModel::IrcChannelRole).value<QObject *>());
307 encrypted = chan->encrypted();
309 IrcUser *user = qobject_cast<IrcUser *>(Client::bufferModel()->data(selectionModel()->currentIndex(), NetworkModel::IrcUserRole).value<QObject *>());
311 encrypted = user->encrypted();
314 ui.encryptionIconLabel->show();
316 ui.encryptionIconLabel->hide();
321 void InputWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
325 for (int row = start; row <= end; row++) {
326 child = model()->index(row, 0, parent);
327 if (NetworkModel::NetworkItemType != child.data(NetworkModel::ItemTypeRole).toInt())
329 networkId = child.data(NetworkModel::NetworkIdRole).value<NetworkId>();
330 if (networkId == _networkId) {
332 updateNickSelector();
339 void InputWidget::updateEnabledState()
341 // FIXME: Find a visualization for this that does not disable the widget!
342 // Disabling kills global action shortcuts, plus users sometimes need/want to enter text
343 // even in inactive channels.
345 QModelIndex currentIndex = selectionModel()->currentIndex();
347 const Network *net = Client::networkModel()->networkByIndex(currentIndex);
348 bool enabled = false;
350 // disable inputline if it's a channelbuffer we parted from or...
351 enabled = (currentIndex.data(NetworkModel::ItemActiveRole).value<bool>() || (currentIndex.data(NetworkModel::BufferTypeRole).toInt() != BufferInfo::ChannelBuffer));
352 // ... if we're not connected to the network at all
353 enabled &= net->isConnected();
356 ui.inputEdit->setEnabled(enabled);
361 const Network *InputWidget::currentNetwork() const
363 return Client::network(_networkId);
367 BufferInfo InputWidget::currentBufferInfo() const
369 return selectionModel()->currentIndex().data(NetworkModel::BufferInfoRole).value<BufferInfo>();
372 void InputWidget::setNetwork(NetworkId networkId)
374 if (_networkId == networkId)
377 const Network *previousNet = Client::network(_networkId);
379 disconnect(previousNet, 0, this, 0);
380 if (previousNet->me())
381 disconnect(previousNet->me(), 0, this, 0);
384 _networkId = networkId;
386 const Network *network = Client::network(networkId);
388 connect(network, SIGNAL(identitySet(IdentityId)), this, SLOT(setIdentity(IdentityId)));
390 setIdentity(network->identity());
399 void InputWidget::connectMyIrcUser()
401 const Network *network = currentNetwork();
403 connect(network->me(), SIGNAL(nickSet(const QString &)), this, SLOT(updateNickSelector()));
404 connect(network->me(), SIGNAL(userModesSet(QString)), this, SLOT(updateNickSelector()));
405 connect(network->me(), SIGNAL(userModesAdded(QString)), this, SLOT(updateNickSelector()));
406 connect(network->me(), SIGNAL(userModesRemoved(QString)), this, SLOT(updateNickSelector()));
407 connect(network->me(), SIGNAL(awaySet(bool)), this, SLOT(updateNickSelector()));
408 disconnect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
409 updateNickSelector();
412 connect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
417 void InputWidget::setIdentity(IdentityId identityId)
419 if (_identityId == identityId)
422 const Identity *previousIdentity = Client::identity(_identityId);
423 if (previousIdentity)
424 disconnect(previousIdentity, 0, this, 0);
426 _identityId = identityId;
428 const Identity *identity = Client::identity(identityId);
430 connect(identity, SIGNAL(nicksSet(QStringList)), this, SLOT(updateNickSelector()));
435 updateNickSelector();
439 void InputWidget::updateNickSelector() const
443 const Network *net = currentNetwork();
447 const Identity *identity = Client::identity(net->identity());
449 qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId() << "IdentityId:" << net->identity();
454 QStringList nicks = identity->nicks();
455 if ((nickIdx = nicks.indexOf(net->myNick())) == -1) {
456 nicks.prepend(net->myNick());
463 IrcUser *me = net->me();
465 nicks[nickIdx] = net->myNick();
466 if (!me->userModes().isEmpty())
467 nicks[nickIdx] += QString(" (+%1)").arg(me->userModes());
470 ui.ownNick->addItems(nicks);
472 if (me && me->isAway())
473 ui.ownNick->setItemData(nickIdx, SmallIcon("user-away"), Qt::DecorationRole);
475 ui.ownNick->setCurrentIndex(nickIdx);
479 void InputWidget::changeNick(const QString &newNick) const
481 const Network *net = currentNetwork();
482 if (!net || net->isMyNick(newNick))
485 // we reset the nick selecter as we have no confirmation yet, that this will succeed.
486 // if the action succeeds it will be properly updated anyways.
487 updateNickSelector();
488 Client::userInput(BufferInfo::fakeStatusBuffer(net->networkId()), QString("/NICK %1").arg(newNick));
492 void InputWidget::onTextEntered(const QString &text)
494 Client::userInput(currentBufferInfo(), text);
495 ui.boldButton->setChecked(false);
496 ui.underlineButton->setChecked(false);
497 ui.italicButton->setChecked(false);
500 fmt.setFontWeight(QFont::Normal);
501 fmt.setFontUnderline(false);
502 fmt.setFontItalic(false);
503 fmt.clearForeground();
504 fmt.clearBackground();
505 inputLine()->setCurrentCharFormat(fmt);
508 // Set highlighter back to active in case it was deactivated by too many errors.
509 if (ui.inputEdit->highlighter())
510 ui.inputEdit->highlighter()->setActive(true);
515 void InputWidget::mergeFormatOnSelection(const QTextCharFormat &format)
517 QTextCursor cursor = inputLine()->textCursor();
518 cursor.mergeCharFormat(format);
519 inputLine()->mergeCurrentCharFormat(format);
523 void InputWidget::setFormatOnSelection(const QTextCharFormat &format)
525 QTextCursor cursor = inputLine()->textCursor();
526 cursor.setCharFormat(format);
527 inputLine()->setCurrentCharFormat(format);
531 QTextCharFormat InputWidget::getFormatOfWordOrSelection()
533 QTextCursor cursor = inputLine()->textCursor();
534 return cursor.charFormat();
538 void InputWidget::currentCharFormatChanged(const QTextCharFormat &format)
540 fontChanged(format.font());
544 void InputWidget::on_boldButton_clicked(bool checked)
547 fmt.setFontWeight(checked ? QFont::Bold : QFont::Normal);
548 mergeFormatOnSelection(fmt);
552 void InputWidget::on_underlineButton_clicked(bool checked)
555 fmt.setFontUnderline(checked);
556 mergeFormatOnSelection(fmt);
560 void InputWidget::on_italicButton_clicked(bool checked)
563 fmt.setFontItalic(checked);
564 mergeFormatOnSelection(fmt);
568 void InputWidget::fontChanged(const QFont &f)
570 ui.boldButton->setChecked(f.bold());
571 ui.italicButton->setChecked(f.italic());
572 ui.underlineButton->setChecked(f.underline());
576 void InputWidget::colorChosen(QAction *action)
580 if (action->data().value<QString>() == "") {
581 color = Qt::transparent;
582 fmt = getFormatOfWordOrSelection();
583 fmt.clearForeground();
584 setFormatOnSelection(fmt);
587 color = QColor(inputLine()->rgbColorFromMirc(action->data().value<QString>()));
588 fmt.setForeground(color);
589 mergeFormatOnSelection(fmt);
591 ui.textcolorButton->setDefaultAction(action);
592 ui.textcolorButton->setIcon(createColorToolButtonIcon(SmallIcon("format-text-color"), color));
596 void InputWidget::colorHighlightChosen(QAction *action)
600 if (action->data().value<QString>() == "") {
601 color = Qt::transparent;
602 fmt = getFormatOfWordOrSelection();
603 fmt.clearBackground();
604 setFormatOnSelection(fmt);
607 color = QColor(inputLine()->rgbColorFromMirc(action->data().value<QString>()));
608 fmt.setBackground(color);
609 mergeFormatOnSelection(fmt);
611 ui.highlightcolorButton->setDefaultAction(action);
612 ui.highlightcolorButton->setIcon(createColorToolButtonIcon(SmallIcon("format-fill-color"), color));
616 void InputWidget::on_showStyleButton_toggled(bool checked)
618 ui.styleFrame->setVisible(checked);
620 ui.showStyleButton->setArrowType(Qt::LeftArrow);
623 ui.showStyleButton->setArrowType(Qt::RightArrow);
628 QIcon InputWidget::createColorToolButtonIcon(const QIcon &icon, const QColor &color)
630 QPixmap pixmap(16, 16);
631 pixmap.fill(Qt::transparent);
632 QPainter painter(&pixmap);
633 QPixmap image = icon.pixmap(16, 16);
634 QRect target(0, 0, 16, 14);
635 QRect source(0, 0, 16, 14);
636 painter.fillRect(QRect(0, 14, 16, 16), color);
637 painter.drawPixmap(target, image, source);
639 return QIcon(pixmap);
643 // MOUSE WHEEL FILTER
644 MouseWheelFilter::MouseWheelFilter(QObject *parent)
650 bool MouseWheelFilter::eventFilter(QObject *obj, QEvent *event)
652 if (event->type() != QEvent::Wheel)
653 return QObject::eventFilter(obj, event);