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