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