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