modernize: Use std::make_unique
[quassel.git] / src / qtui / inputwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "inputwidget.h"
22
23 #include <QIcon>
24 #include <QPainter>
25 #include <QPixmap>
26 #include <QRect>
27
28 #include "action.h"
29 #include "actioncollection.h"
30 #include "bufferview.h"
31 #include "client.h"
32 #include "icon.h"
33 #include "ircuser.h"
34 #include "networkmodel.h"
35 #include "qtui.h"
36 #include "qtuisettings.h"
37 #include "tabcompleter.h"
38
39 const int leftMargin = 3;
40
41 InputWidget::InputWidget(QWidget *parent)
42     : AbstractItemView(parent),
43     _networkId(0)
44 {
45     ui.setupUi(this);
46     connect(ui.ownNick, SIGNAL(activated(QString)), this, SLOT(changeNick(QString)));
47
48     layout()->setAlignment(ui.ownNick, Qt::AlignBottom);
49     layout()->setAlignment(ui.inputEdit, Qt::AlignBottom);
50     layout()->setAlignment(ui.showStyleButton, Qt::AlignBottom);
51     layout()->setAlignment(ui.styleFrame, Qt::AlignBottom);
52
53     setStyleOptionsExpanded(false);
54
55     setFocusProxy(ui.inputEdit);
56     ui.ownNick->setFocusProxy(ui.inputEdit);
57
58     ui.ownNick->setSizeAdjustPolicy(QComboBox::AdjustToContents);
59     ui.ownNick->installEventFilter(new MouseWheelFilter(this));
60     ui.inputEdit->installEventFilter(this);
61
62     ui.inputEdit->setMinHeight(1);
63     ui.inputEdit->setMaxHeight(5);
64     ui.inputEdit->setMode(MultiLineEdit::MultiLine);
65     ui.inputEdit->setPasteProtectionEnabled(true);
66
67     ui.boldButton->setIcon(icon::get("format-text-bold"));
68     ui.italicButton->setIcon(icon::get("format-text-italic"));
69     ui.underlineButton->setIcon(icon::get("format-text-underline"));
70     ui.clearButton->setIcon(icon::get("edit-clear"));
71     ui.encryptionIconLabel->hide();
72
73     _colorMenu = new QMenu();
74     _colorFillMenu = new QMenu();
75
76     QStringList names;
77     names << tr("White") << tr("Black") << tr("Dark blue") << tr("Dark green") << tr("Red") << tr("Dark red") << tr("Dark magenta")  << tr("Orange")
78           << tr("Yellow") << tr("Green") << tr("Dark cyan") << tr("Cyan") << tr("Blue") << tr("Magenta") << tr("Dark gray") << tr("Light gray");
79
80     QPixmap pix(16, 16);
81     for (int i = 0; i < inputLine()->mircColorMap().count(); i++) {
82         pix.fill(inputLine()->mircColorMap().values()[i]);
83         _colorMenu->addAction(pix, names[i])->setData(inputLine()->mircColorMap().keys()[i]);
84         _colorFillMenu->addAction(pix, names[i])->setData(inputLine()->mircColorMap().keys()[i]);
85     }
86
87     pix.fill(Qt::transparent);
88     _colorMenu->addAction(pix, tr("Clear Color"))->setData("");
89     _colorFillMenu->addAction(pix, tr("Clear Color"))->setData("");
90
91     ui.textcolorButton->setMenu(_colorMenu);
92     // Set the default action to clear color (last added action)
93     ui.textcolorButton->setDefaultAction(_colorMenu->actions().last());
94     connect(_colorMenu, SIGNAL(triggered(QAction *)), this, SLOT(colorChosen(QAction *)));
95
96     ui.highlightcolorButton->setMenu(_colorFillMenu);
97     // Set the default action to clear fill color (last added action)
98     ui.highlightcolorButton->setDefaultAction(_colorFillMenu->actions().last());
99     connect(_colorFillMenu, SIGNAL(triggered(QAction *)), this, SLOT(colorHighlightChosen(QAction *)));
100
101     // Needs to be done after adding the menu, otherwise the icon mysteriously vanishes until clicked
102     ui.textcolorButton->setIcon(icon::get("format-text-color"));
103     ui.highlightcolorButton->setIcon(icon::get("format-fill-color"));
104
105     // Show/hide style button
106     connect(ui.showStyleButton, SIGNAL(toggled(bool)), this, SLOT(setStyleOptionsExpanded(bool)));
107
108     // Clear formatting button
109     connect(ui.clearButton, SIGNAL(clicked()), this, SLOT(clearFormat()));
110
111     new TabCompleter(ui.inputEdit);
112
113     UiStyleSettings fs("Fonts");
114     fs.notify("UseCustomInputWidgetFont", this, SLOT(setUseCustomFont(QVariant)));
115     fs.notify("InputWidget", this, SLOT(setCustomFont(QVariant)));
116     if (fs.value("UseCustomInputWidgetFont", false).toBool())
117         setCustomFont(fs.value("InputWidget", QFont()));
118
119     UiSettings s("InputWidget");
120
121     s.notify("EnableEmacsMode", this, SLOT(setEnableEmacsMode(QVariant)));
122     setEnableEmacsMode(s.value("EnableEmacsMode", false));
123
124     s.notify("ShowNickSelector", this, SLOT(setShowNickSelector(QVariant)));
125     setShowNickSelector(s.value("ShowNickSelector", true));
126
127     s.notify("ShowStyleButtons", this, SLOT(setShowStyleButtons(QVariant)));
128     setShowStyleButtons(s.value("ShowStyleButtons", true));
129
130     s.notify("EnablePerChatHistory", this, SLOT(setEnablePerChatHistory(QVariant)));
131     setEnablePerChatHistory(s.value("EnablePerChatHistory", true));
132
133     s.notify("MaxNumLines", this, SLOT(setMaxLines(QVariant)));
134     setMaxLines(s.value("MaxNumLines", 5));
135
136     s.notify("EnableScrollBars", this, SLOT(setScrollBarsEnabled(QVariant)));
137     setScrollBarsEnabled(s.value("EnableScrollBars", true));
138
139     s.notify("EnableLineWrap", this, SLOT(setLineWrapEnabled(QVariant)));
140     setLineWrapEnabled(s.value("EnableLineWrap", true));
141
142     s.notify("EnableMultiLine", this, SLOT(setMultiLineEnabled(QVariant)));
143     setMultiLineEnabled(s.value("EnableMultiLine", true));
144
145     ActionCollection *coll = QtUi::actionCollection();
146
147     auto *activateInputline = coll->add<Action>("FocusInputLine");
148     connect(activateInputline, SIGNAL(triggered()), SLOT(setFocus()));
149     activateInputline->setText(tr("Focus Input Line"));
150     activateInputline->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_L));
151
152     connect(inputLine(), SIGNAL(textEntered(QString)), SLOT(onTextEntered(QString)), Qt::QueuedConnection); // make sure the line is already reset, bug #984
153     connect(inputLine(), SIGNAL(currentCharFormatChanged(QTextCharFormat)), this, SLOT(currentCharFormatChanged(QTextCharFormat)));
154 }
155
156
157 void InputWidget::setUseCustomFont(const QVariant &v)
158 {
159     if (v.toBool()) {
160         UiStyleSettings fs("Fonts");
161         setCustomFont(fs.value("InputWidget"));
162     }
163     else
164         setCustomFont(QFont());
165 }
166
167
168 void InputWidget::setCustomFont(const QVariant &v)
169 {
170     QFont font = v.value<QFont>();
171     if (font.family().isEmpty())
172         font = QApplication::font();
173     // we don't want font styles as this conflics with mirc code richtext editing
174     font.setBold(false);
175     font.setItalic(false);
176     font.setUnderline(false);
177     font.setStrikeOut(false);
178     ui.inputEdit->setCustomFont(font);
179 }
180
181
182 void InputWidget::setEnableEmacsMode(const QVariant &v)
183 {
184     ui.inputEdit->setEmacsMode(v.toBool());
185 }
186
187
188 void InputWidget::setShowNickSelector(const QVariant &v)
189 {
190     ui.ownNick->setVisible(v.toBool());
191 }
192
193
194 void InputWidget::setShowStyleButtons(const QVariant &v)
195 {
196     ui.showStyleButton->setVisible(v.toBool());
197 }
198
199
200 void InputWidget::setEnablePerChatHistory(const QVariant &v)
201 {
202     _perChatHistory = v.toBool();
203 }
204
205
206 void InputWidget::setMaxLines(const QVariant &v)
207 {
208     ui.inputEdit->setMaxHeight(v.toInt());
209 }
210
211
212 void InputWidget::setScrollBarsEnabled(const QVariant &v)
213 {
214     ui.inputEdit->setScrollBarsEnabled(v.toBool());
215 }
216
217
218 void InputWidget::setLineWrapEnabled(const QVariant &v)
219 {
220     ui.inputEdit->setLineWrapEnabled(v.toBool());
221 }
222
223
224 void InputWidget::setMultiLineEnabled(const QVariant &v)
225 {
226     ui.inputEdit->setMode(v.toBool() ? MultiLineEdit::MultiLine : MultiLineEdit::SingleLine);
227 }
228
229
230 bool InputWidget::eventFilter(QObject *watched, QEvent *event)
231 {
232     if (event->type() != QEvent::KeyPress)
233         return false;
234
235     auto *keyEvent = static_cast<QKeyEvent *>(event);
236
237     // keys from BufferView should be sent to (and focus) the input line
238     auto *view = qobject_cast<BufferView *>(watched);
239     if (view) {
240         if (keyEvent->text().length() == 1 && !(keyEvent->modifiers() & (Qt::ControlModifier ^ Qt::AltModifier))) { // normal key press
241             QChar c = keyEvent->text().at(0);
242             if (c.isLetterOrNumber() || c.isSpace() || c.isPunct() || c.isSymbol()) {
243                 setFocus();
244                 QCoreApplication::sendEvent(inputLine(), keyEvent);
245                 return true;
246             }
247         }
248         return false;
249     }
250     else if (watched == ui.inputEdit) {
251         if (keyEvent->matches(QKeySequence::Find)) {
252             QAction *act = GraphicalUi::actionCollection()->action("ToggleSearchBar");
253             if (act) {
254                 act->toggle();
255                 return true;
256             }
257         }
258         return false;
259     }
260     return false;
261 }
262
263
264 void InputWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous)
265 {
266     BufferId currentBufferId = current.data(NetworkModel::BufferIdRole).value<BufferId>();
267     BufferId previousBufferId = previous.data(NetworkModel::BufferIdRole).value<BufferId>();
268
269     if (_perChatHistory) {
270         //backup
271         historyMap[previousBufferId].history = inputLine()->history();
272         historyMap[previousBufferId].tempHistory = inputLine()->tempHistory();
273         historyMap[previousBufferId].idx = inputLine()->idx();
274         historyMap[previousBufferId].inputLine = inputLine()->html();
275
276         //restore
277         inputLine()->setHistory(historyMap[currentBufferId].history);
278         inputLine()->setTempHistory(historyMap[currentBufferId].tempHistory);
279         inputLine()->setIdx(historyMap[currentBufferId].idx);
280         inputLine()->setHtml(historyMap[currentBufferId].inputLine);
281         inputLine()->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
282
283         // FIXME this really should be in MultiLineEdit (and the const int on top removed)
284         QTextBlockFormat format = inputLine()->textCursor().blockFormat();
285         format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
286         inputLine()->textCursor().setBlockFormat(format);
287     }
288
289     NetworkId networkId = current.data(NetworkModel::NetworkIdRole).value<NetworkId>();
290     if (networkId == _networkId)
291         return;
292
293     setNetwork(networkId);
294     updateNickSelector();
295     updateEnabledState();
296 }
297
298
299 void InputWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
300 {
301     QItemSelectionRange changedArea(topLeft, bottomRight);
302     if (changedArea.contains(selectionModel()->currentIndex())) {
303         updateEnabledState();
304
305         bool encrypted = false;
306
307         auto *chan = qobject_cast<IrcChannel *>(Client::bufferModel()->data(selectionModel()->currentIndex(), NetworkModel::IrcChannelRole).value<QObject *>());
308         if (chan)
309             encrypted = chan->encrypted();
310
311         auto *user = qobject_cast<IrcUser *>(Client::bufferModel()->data(selectionModel()->currentIndex(), NetworkModel::IrcUserRole).value<QObject *>());
312         if (user)
313             encrypted = user->encrypted();
314
315         if (encrypted)
316             ui.encryptionIconLabel->show();
317         else
318             ui.encryptionIconLabel->hide();
319     }
320 }
321
322
323 void InputWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
324 {
325     NetworkId networkId;
326     QModelIndex child;
327     for (int row = start; row <= end; row++) {
328         child = model()->index(row, 0, parent);
329         if (NetworkModel::NetworkItemType != child.data(NetworkModel::ItemTypeRole).toInt())
330             continue;
331         networkId = child.data(NetworkModel::NetworkIdRole).value<NetworkId>();
332         if (networkId == _networkId) {
333             setNetwork(0);
334             updateNickSelector();
335             return;
336         }
337     }
338 }
339
340
341 void InputWidget::updateEnabledState()
342 {
343 // FIXME: Find a visualization for this that does not disable the widget!
344 //        Disabling kills global action shortcuts, plus users sometimes need/want to enter text
345 //        even in inactive channels.
346 #if 0
347     QModelIndex currentIndex = selectionModel()->currentIndex();
348
349     const Network *net = Client::networkModel()->networkByIndex(currentIndex);
350     bool enabled = false;
351     if (net) {
352         // disable inputline if it's a channelbuffer we parted from or...
353         enabled = (currentIndex.data(NetworkModel::ItemActiveRole).value<bool>() || (currentIndex.data(NetworkModel::BufferTypeRole).toInt() != BufferInfo::ChannelBuffer));
354         // ... if we're not connected to the network at all
355         enabled &= net->isConnected();
356     }
357
358     ui.inputEdit->setEnabled(enabled);
359 #endif
360 }
361
362
363 const Network *InputWidget::currentNetwork() const
364 {
365     return Client::network(_networkId);
366 }
367
368
369 BufferInfo InputWidget::currentBufferInfo() const
370 {
371     return selectionModel()->currentIndex().data(NetworkModel::BufferInfoRole).value<BufferInfo>();
372 }
373
374
375 void InputWidget::applyFormatActiveColor()
376 {
377     if (!ui.textcolorButton->defaultAction()) {
378         return;
379     }
380     colorChosen(ui.textcolorButton->defaultAction());
381 }
382
383
384 void InputWidget::applyFormatActiveColorFill()
385 {
386     if (!ui.highlightcolorButton->defaultAction()) {
387         return;
388     }
389     colorHighlightChosen(ui.highlightcolorButton->defaultAction());
390 }
391
392
393 void InputWidget::toggleFormatBold()
394 {
395     setFormatBold(!ui.boldButton->isChecked());
396 }
397
398
399 void InputWidget::toggleFormatItalic()
400 {
401     setFormatItalic(!ui.italicButton->isChecked());
402 }
403
404
405 void InputWidget::toggleFormatUnderline()
406 {
407     setFormatUnderline(!ui.underlineButton->isChecked());
408 }
409
410
411 void InputWidget::clearFormat()
412 {
413     // Clear all formatting for selection (not global)
414     setFormatClear(false);
415 }
416
417
418 void InputWidget::setNetwork(NetworkId networkId)
419 {
420     if (_networkId == networkId)
421         return;
422
423     const Network *previousNet = Client::network(_networkId);
424     if (previousNet) {
425         disconnect(previousNet, nullptr, this, nullptr);
426         if (previousNet->me())
427             disconnect(previousNet->me(), nullptr, this, nullptr);
428     }
429
430     _networkId = networkId;
431
432     const Network *network = Client::network(networkId);
433     if (network) {
434         connect(network, SIGNAL(identitySet(IdentityId)), this, SLOT(setIdentity(IdentityId)));
435         connectMyIrcUser();
436         setIdentity(network->identity());
437     }
438     else {
439         setIdentity(0);
440         _networkId = 0;
441     }
442 }
443
444
445 void InputWidget::connectMyIrcUser()
446 {
447     const Network *network = currentNetwork();
448     if (network->me()) {
449         connect(network->me(), SIGNAL(nickSet(const QString &)), this, SLOT(updateNickSelector()));
450         connect(network->me(), SIGNAL(userModesSet(QString)), this, SLOT(updateNickSelector()));
451         connect(network->me(), SIGNAL(userModesAdded(QString)), this, SLOT(updateNickSelector()));
452         connect(network->me(), SIGNAL(userModesRemoved(QString)), this, SLOT(updateNickSelector()));
453         connect(network->me(), SIGNAL(awaySet(bool)), this, SLOT(updateNickSelector()));
454         disconnect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
455         updateNickSelector();
456     }
457     else {
458         connect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
459     }
460 }
461
462
463 void InputWidget::setIdentity(IdentityId identityId)
464 {
465     if (_identityId == identityId)
466         return;
467
468     const Identity *previousIdentity = Client::identity(_identityId);
469     if (previousIdentity)
470         disconnect(previousIdentity, nullptr, this, nullptr);
471
472     _identityId = identityId;
473
474     const Identity *identity = Client::identity(identityId);
475     if (identity) {
476         connect(identity, SIGNAL(nicksSet(QStringList)), this, SLOT(updateNickSelector()));
477     }
478     else {
479         _identityId = 0;
480     }
481     updateNickSelector();
482 }
483
484
485 void InputWidget::updateNickSelector() const
486 {
487     ui.ownNick->clear();
488
489     const Network *net = currentNetwork();
490     if (!net)
491         return;
492
493     const Identity *identity = Client::identity(net->identity());
494     if (!identity) {
495         qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId() << "IdentityId:" << net->identity();
496         return;
497     }
498
499     int nickIdx;
500     QStringList nicks = identity->nicks();
501     if ((nickIdx = nicks.indexOf(net->myNick())) == -1) {
502         nicks.prepend(net->myNick());
503         nickIdx = 0;
504     }
505
506     if (nicks.isEmpty())
507         return;
508
509     IrcUser *me = net->me();
510     if (me) {
511         nicks[nickIdx] = net->myNick();
512         if (!me->userModes().isEmpty())
513             nicks[nickIdx] += QString(" (+%1)").arg(me->userModes());
514     }
515
516     ui.ownNick->addItems(nicks);
517
518     if (me && me->isAway())
519         ui.ownNick->setItemData(nickIdx, icon::get({"im-user-away", "user-away"}), Qt::DecorationRole);
520
521     ui.ownNick->setCurrentIndex(nickIdx);
522 }
523
524
525 void InputWidget::changeNick(const QString &newNick) const
526 {
527     const Network *net = currentNetwork();
528     if (!net || net->isMyNick(newNick))
529         return;
530
531     // we reset the nick selecter as we have no confirmation yet, that this will succeed.
532     // if the action succeeds it will be properly updated anyways.
533     updateNickSelector();
534     Client::userInput(BufferInfo::fakeStatusBuffer(net->networkId()), QString("/NICK %1").arg(newNick));
535 }
536
537
538 void InputWidget::onTextEntered(const QString &text)
539 {
540     Client::userInput(currentBufferInfo(), text);
541
542     // Remove formatting from entered text
543     // TODO: Offer a way to convert pasted text to mIRC formatting codes
544     setFormatClear(true);
545 }
546
547
548 void InputWidget::setFormatClear(const bool global)
549 {
550     // Apply formatting
551     QTextCharFormat fmt;
552     fmt.setFontWeight(QFont::Normal);
553     fmt.setFontUnderline(false);
554     fmt.setFontItalic(false);
555     fmt.clearForeground();
556     fmt.clearBackground();
557     if (global) {
558         inputLine()->setCurrentCharFormat(fmt);
559     } else {
560         setFormatOnSelection(fmt);
561     }
562
563     // Make sure UI state follows
564     ui.boldButton->setChecked(false);
565     ui.italicButton->setChecked(false);
566     ui.underlineButton->setChecked(false);
567 }
568
569
570 void InputWidget::setFormatBold(const bool bold)
571 {
572     // Apply formatting
573     QTextCharFormat fmt;
574     fmt.setFontWeight(bold ? QFont::Bold : QFont::Normal);
575     mergeFormatOnSelection(fmt);
576     // Make sure UI state follows
577     ui.boldButton->setChecked(bold);
578 }
579
580
581 void InputWidget::setFormatItalic(const bool italic)
582 {
583     // Apply formatting
584     QTextCharFormat fmt;
585     fmt.setFontItalic(italic);
586     mergeFormatOnSelection(fmt);
587     // Make sure UI state follows
588     ui.italicButton->setChecked(italic);
589 }
590
591
592 void InputWidget::setFormatUnderline(const bool underline)
593 {
594     // Apply formatting
595     QTextCharFormat fmt;
596     fmt.setFontUnderline(underline);
597     mergeFormatOnSelection(fmt);
598     // Make sure UI state follows
599     ui.underlineButton->setChecked(underline);
600 }
601
602
603 void InputWidget::mergeFormatOnSelection(const QTextCharFormat &format)
604 {
605     QTextCursor cursor = inputLine()->textCursor();
606     cursor.mergeCharFormat(format);
607     inputLine()->mergeCurrentCharFormat(format);
608 }
609
610
611 void InputWidget::setFormatOnSelection(const QTextCharFormat &format)
612 {
613     QTextCursor cursor = inputLine()->textCursor();
614     cursor.setCharFormat(format);
615     inputLine()->setCurrentCharFormat(format);
616 }
617
618
619 QTextCharFormat InputWidget::getFormatOfWordOrSelection()
620 {
621     QTextCursor cursor = inputLine()->textCursor();
622     return cursor.charFormat();
623 }
624
625
626 void InputWidget::setStyleOptionsExpanded(bool expanded)
627 {
628     ui.styleFrame->setVisible(expanded);
629     if (expanded) {
630         ui.showStyleButton->setArrowType(Qt::LeftArrow);
631         ui.showStyleButton->setToolTip(tr("Hide formatting options"));
632     } else {
633         ui.showStyleButton->setArrowType(Qt::RightArrow);
634         ui.showStyleButton->setToolTip(tr("Show formatting options"));
635     }
636 }
637
638
639 void InputWidget::currentCharFormatChanged(const QTextCharFormat &format)
640 {
641     fontChanged(format.font());
642 }
643
644
645 void InputWidget::on_boldButton_clicked(bool checked)
646 {
647     setFormatBold(checked);
648 }
649
650
651 void InputWidget::on_underlineButton_clicked(bool checked)
652 {
653     setFormatUnderline(checked);
654 }
655
656
657 void InputWidget::on_italicButton_clicked(bool checked)
658 {
659     setFormatItalic(checked);
660 }
661
662
663 void InputWidget::fontChanged(const QFont &f)
664 {
665     ui.boldButton->setChecked(f.bold());
666     ui.italicButton->setChecked(f.italic());
667     ui.underlineButton->setChecked(f.underline());
668 }
669
670
671 void InputWidget::colorChosen(QAction *action)
672 {
673     QTextCharFormat fmt;
674     QColor color;
675     if (action->data().value<QString>() == "") {
676         color = Qt::transparent;
677         fmt = getFormatOfWordOrSelection();
678         fmt.clearForeground();
679         setFormatOnSelection(fmt);
680     }
681     else {
682         color = QColor(inputLine()->rgbColorFromMirc(action->data().value<QString>()));
683         fmt.setForeground(color);
684         mergeFormatOnSelection(fmt);
685     }
686     ui.textcolorButton->setDefaultAction(action);
687     ui.textcolorButton->setIcon(createColorToolButtonIcon(icon::get("format-text-color"), color));
688 }
689
690
691 void InputWidget::colorHighlightChosen(QAction *action)
692 {
693     QTextCharFormat fmt;
694     QColor color;
695     if (action->data().value<QString>() == "") {
696         color = Qt::transparent;
697         fmt = getFormatOfWordOrSelection();
698         fmt.clearBackground();
699         setFormatOnSelection(fmt);
700     }
701     else {
702         color = QColor(inputLine()->rgbColorFromMirc(action->data().value<QString>()));
703         fmt.setBackground(color);
704         mergeFormatOnSelection(fmt);
705     }
706     ui.highlightcolorButton->setDefaultAction(action);
707     ui.highlightcolorButton->setIcon(createColorToolButtonIcon(icon::get("format-fill-color"), color));
708 }
709
710
711 void InputWidget::on_showStyleButton_toggled(bool checked)
712 {
713     setStyleOptionsExpanded(checked);
714 }
715
716
717 QIcon InputWidget::createColorToolButtonIcon(const QIcon &icon, const QColor &color)
718 {
719     QPixmap pixmap(16, 16);
720     pixmap.fill(Qt::transparent);
721     QPainter painter(&pixmap);
722     QPixmap image = icon.pixmap(16, 16);
723     QRect target(0, 0, 16, 14);
724     QRect source(0, 0, 16, 14);
725     painter.fillRect(QRect(0, 14, 16, 16), color);
726     painter.drawPixmap(target, image, source);
727
728     return QIcon(pixmap);
729 }
730
731
732 // MOUSE WHEEL FILTER
733 MouseWheelFilter::MouseWheelFilter(QObject *parent)
734     : QObject(parent)
735 {
736 }
737
738
739 bool MouseWheelFilter::eventFilter(QObject *obj, QEvent *event)
740 {
741     if (event->type() != QEvent::Wheel)
742         return QObject::eventFilter(obj, event);
743     else
744         return true;
745 }