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