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