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