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