1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "inputwidget.h"
24 #include "actioncollection.h"
25 #include "bufferview.h"
27 #include "iconloader.h"
29 #include "jumpkeyhandler.h"
30 #include "networkmodel.h"
32 #include "qtuisettings.h"
33 #include "tabcompleter.h"
36 InputWidget::InputWidget(QWidget *parent)
37 : AbstractItemView(parent),
41 connect(ui.ownNick, SIGNAL(activated(QString)), this, SLOT(changeNick(QString)));
43 layout()->setAlignment(ui.ownNick, Qt::AlignBottom);
44 layout()->setAlignment(ui.inputEdit, Qt::AlignBottom);
45 layout()->setAlignment(ui.showStyleButton, Qt::AlignBottom);
46 layout()->setAlignment(ui.styleFrame, Qt::AlignBottom);
48 ui.styleFrame->setVisible(false);
50 setFocusProxy(ui.inputEdit);
51 ui.ownNick->setFocusProxy(ui.inputEdit);
53 ui.ownNick->setSizeAdjustPolicy(QComboBox::AdjustToContents);
54 ui.ownNick->installEventFilter(new MouseWheelFilter(this));
55 ui.inputEdit->installEventFilter(new JumpKeyHandler(this));
56 ui.inputEdit->installEventFilter(this);
58 ui.inputEdit->setMinHeight(1);
59 ui.inputEdit->setMaxHeight(5);
60 ui.inputEdit->setMode(MultiLineEdit::MultiLine);
61 ui.inputEdit->setPasteProtectionEnabled(true);
63 ui.boldButton->setIcon(SmallIcon("format-text-bold"));
64 ui.italicButton->setIcon(SmallIcon("format-text-italic"));
65 ui.underlineButton->setIcon(SmallIcon("format-text-underline"));
66 ui.textcolorButton->setIcon(SmallIcon("format-text-color"));
67 ui.highlightcolorButton->setIcon(SmallIcon("format-fill-color"));
69 _colorMenu = new QMenu();
70 _colorFillMenu = new QMenu();
73 names << tr("White") << tr("Black") << tr("Dark blue") << tr("Dark green") << tr("Red") << tr("Dark red") << tr("Dark magenta") << tr("Orange")
74 << tr("Yellow") << tr("Green") << tr("Dark cyan") << tr("Cyan") << tr("Blue") << tr("Magenta") << tr("Dark gray") << tr("Light gray");
77 for (int i = 0; i < inputLine()->mircColorMap().count(); i++) {
78 pix.fill(inputLine()->mircColorMap().values()[i]);
79 _colorMenu->addAction(pix, names[i])->setData(inputLine()->mircColorMap().keys()[i]);
80 _colorFillMenu->addAction(pix, names[i])->setData(inputLine()->mircColorMap().keys()[i]);
83 pix.fill(Qt::transparent);
84 _colorMenu->addAction(pix, tr("Clear Color"))->setData("");
85 _colorFillMenu->addAction(pix, tr("Clear Color"))->setData("");
87 ui.textcolorButton->setMenu(_colorMenu);
88 connect(_colorMenu, SIGNAL(triggered(QAction*)), this, SLOT(colorChoosen(QAction*)));
89 ui.highlightcolorButton->setMenu(_colorFillMenu);
90 connect(_colorFillMenu, SIGNAL(triggered(QAction*)), this, SLOT(colorHighlightChoosen(QAction*)));
92 new TabCompleter(ui.inputEdit);
94 UiStyleSettings fs("Fonts");
95 fs.notify("UseCustomInputWidgetFont", this, SLOT(setUseCustomFont(QVariant)));
96 fs.notify("InputWidget", this, SLOT(setCustomFont(QVariant)));
97 if(fs.value("UseCustomInputWidgetFont", false).toBool())
98 setCustomFont(fs.value("InputWidget", QFont()));
100 UiSettings s("InputWidget");
103 s.notify("EnableSpellCheck", this, SLOT(setEnableSpellCheck(QVariant)));
104 setEnableSpellCheck(s.value("EnableSpellCheck", false));
107 s.notify("ShowNickSelector", this, SLOT(setShowNickSelector(QVariant)));
108 setShowNickSelector(s.value("ShowNickSelector", true));
110 s.notify("MaxNumLines", this, SLOT(setMaxLines(QVariant)));
111 setMaxLines(s.value("MaxNumLines", 5));
113 s.notify("EnableScrollBars", this, SLOT(setScrollBarsEnabled(QVariant)));
114 setScrollBarsEnabled(s.value("EnableScrollBars", true));
116 s.notify("EnableMultiLine", this, SLOT(setMultiLineEnabled(QVariant)));
117 setMultiLineEnabled(s.value("EnableMultiLine", true));
119 ActionCollection *coll = QtUi::actionCollection();
121 Action *activateInputline = coll->add<Action>("FocusInputLine");
122 connect(activateInputline, SIGNAL(triggered()), SLOT(setFocus()));
123 activateInputline->setText(tr("Focus Input Line"));
124 activateInputline->setShortcut(tr("Ctrl+L"));
126 connect(inputLine(), SIGNAL(currentCharFormatChanged(QTextCharFormat)), this, SLOT(currentCharFormatChanged(QTextCharFormat)));
129 InputWidget::~InputWidget() {
132 void InputWidget::setUseCustomFont(const QVariant &v) {
134 UiStyleSettings fs("Fonts");
135 setCustomFont(fs.value("InputWidget"));
137 setCustomFont(QFont());
140 void InputWidget::setCustomFont(const QVariant &v) {
141 QFont font = v.value<QFont>();
142 if(font.family().isEmpty())
143 font = QApplication::font();
144 ui.inputEdit->setCustomFont(font);
147 void InputWidget::setEnableSpellCheck(const QVariant &v) {
148 ui.inputEdit->setSpellCheckEnabled(v.toBool());
151 void InputWidget::setShowNickSelector(const QVariant &v) {
152 ui.ownNick->setVisible(v.toBool());
155 void InputWidget::setMaxLines(const QVariant &v) {
156 ui.inputEdit->setMaxHeight(v.toInt());
159 void InputWidget::setScrollBarsEnabled(const QVariant &v) {
160 ui.inputEdit->setScrollBarsEnabled(v.toBool());
163 void InputWidget::setMultiLineEnabled(const QVariant &v) {
164 ui.inputEdit->setMode(v.toBool()? MultiLineEdit::MultiLine : MultiLineEdit::SingleLine);
167 bool InputWidget::eventFilter(QObject *watched, QEvent *event) {
168 if(event->type() != QEvent::KeyPress)
171 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
173 // keys from BufferView should be sent to (and focus) the input line
174 BufferView *view = qobject_cast<BufferView *>(watched);
176 if(keyEvent->text().length() == 1 && !(keyEvent->modifiers() & (Qt::ControlModifier ^ Qt::AltModifier)) ) { // normal key press
177 QChar c = keyEvent->text().at(0);
178 if(c.isLetterOrNumber() || c.isSpace() || c.isPunct() || c.isSymbol()) {
180 QCoreApplication::sendEvent(inputLine(), keyEvent);
185 } else if(watched == ui.inputEdit) {
186 if(keyEvent->matches(QKeySequence::Find)) {
187 QAction *act = GraphicalUi::actionCollection()->action("ToggleSearchBar");
198 void InputWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
200 NetworkId networkId = current.data(NetworkModel::NetworkIdRole).value<NetworkId>();
201 if(networkId == _networkId)
204 setNetwork(networkId);
205 updateNickSelector();
206 updateEnabledState();
209 void InputWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
210 QItemSelectionRange changedArea(topLeft, bottomRight);
211 if(changedArea.contains(selectionModel()->currentIndex())) {
212 updateEnabledState();
216 void InputWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
219 for(int row = start; row <= end; row++) {
220 child = model()->index(row, 0, parent);
221 if(NetworkModel::NetworkItemType != child.data(NetworkModel::ItemTypeRole).toInt())
223 networkId = child.data(NetworkModel::NetworkIdRole).value<NetworkId>();
224 if(networkId == _networkId) {
226 updateNickSelector();
232 void InputWidget::updateEnabledState() {
233 QModelIndex currentIndex = selectionModel()->currentIndex();
235 const Network *net = Client::networkModel()->networkByIndex(currentIndex);
236 bool enabled = false;
238 // disable inputline if it's a channelbuffer we parted from or...
239 enabled = (currentIndex.data(NetworkModel::ItemActiveRole).value<bool>() || (currentIndex.data(NetworkModel::BufferTypeRole).toInt() != BufferInfo::ChannelBuffer));
240 // ... if we're not connected to the network at all
241 enabled &= net->isConnected();
243 ui.inputEdit->setEnabled(enabled);
246 const Network *InputWidget::currentNetwork() const {
247 return Client::network(_networkId);
250 BufferInfo InputWidget::currentBufferInfo() const {
251 return selectionModel()->currentIndex().data(NetworkModel::BufferInfoRole).value<BufferInfo>();
254 void InputWidget::setNetwork(NetworkId networkId) {
255 if(_networkId == networkId)
258 const Network *previousNet = Client::network(_networkId);
260 disconnect(previousNet, 0, this, 0);
261 if(previousNet->me())
262 disconnect(previousNet->me(), 0, this, 0);
265 _networkId = networkId;
267 const Network *network = Client::network(networkId);
269 connect(network, SIGNAL(identitySet(IdentityId)), this, SLOT(setIdentity(IdentityId)));
271 setIdentity(network->identity());
278 void InputWidget::connectMyIrcUser() {
279 const Network *network = currentNetwork();
281 connect(network->me(), SIGNAL(nickSet(const QString &)), this, SLOT(updateNickSelector()));
282 connect(network->me(), SIGNAL(userModesSet(QString)), this, SLOT(updateNickSelector()));
283 connect(network->me(), SIGNAL(userModesAdded(QString)), this, SLOT(updateNickSelector()));
284 connect(network->me(), SIGNAL(userModesRemoved(QString)), this, SLOT(updateNickSelector()));
285 connect(network->me(), SIGNAL(awaySet(bool)), this, SLOT(updateNickSelector()));
286 disconnect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
287 updateNickSelector();
289 connect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
293 void InputWidget::setIdentity(IdentityId identityId) {
294 if(_identityId == identityId)
297 const Identity *previousIdentity = Client::identity(_identityId);
299 disconnect(previousIdentity, 0, this, 0);
301 _identityId = identityId;
303 const Identity *identity = Client::identity(identityId);
305 connect(identity, SIGNAL(nicksSet(QStringList)), this, SLOT(updateNickSelector()));
309 updateNickSelector();
312 void InputWidget::updateNickSelector() const {
315 const Network *net = currentNetwork();
319 const Identity *identity = Client::identity(net->identity());
321 qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId() << "IdentityId:" << net->identity();
326 QStringList nicks = identity->nicks();
327 if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
328 nicks.prepend(net->myNick());
335 IrcUser *me = net->me();
337 nicks[nickIdx] = net->myNick();
338 if(!me->userModes().isEmpty())
339 nicks[nickIdx] += QString(" (+%1)").arg(me->userModes());
342 ui.ownNick->addItems(nicks);
344 if(me && me->isAway())
345 ui.ownNick->setItemData(nickIdx, SmallIcon("user-away"), Qt::DecorationRole);
347 ui.ownNick->setCurrentIndex(nickIdx);
350 void InputWidget::changeNick(const QString &newNick) const {
351 const Network *net = currentNetwork();
352 if(!net || net->isMyNick(newNick))
355 // we reset the nick selecter as we have no confirmation yet, that this will succeed.
356 // if the action succeeds it will be properly updated anyways.
357 updateNickSelector();
358 Client::userInput(BufferInfo::fakeStatusBuffer(net->networkId()), QString("/NICK %1").arg(newNick));
361 void InputWidget::on_inputEdit_textEntered(const QString &text) const {
362 Client::userInput(currentBufferInfo(), text);
363 ui.boldButton->setChecked(false);
364 ui.underlineButton->setChecked(false);
365 ui.italicButton->setChecked(false);
368 fmt.setFontWeight(QFont::Normal);
369 fmt.setFontUnderline(false);
370 fmt.setFontItalic(false);
371 fmt.clearForeground();
372 fmt.clearBackground();
373 inputLine()->setCurrentCharFormat(fmt);
376 void InputWidget::mergeFormatOnWordOrSelection(const QTextCharFormat &format) {
377 QTextCursor cursor = inputLine()->textCursor();
378 if (!cursor.hasSelection())
379 cursor.select(QTextCursor::WordUnderCursor);
380 cursor.mergeCharFormat(format);
381 inputLine()->mergeCurrentCharFormat(format);
384 void InputWidget::setFormatOnWordOrSelection(const QTextCharFormat &format) {
385 QTextCursor cursor = inputLine()->textCursor();
386 if (!cursor.hasSelection())
387 cursor.select(QTextCursor::WordUnderCursor);
388 cursor.setCharFormat(format);
389 inputLine()->setCurrentCharFormat(format);
392 QTextCharFormat InputWidget::getFormatOfWordOrSelection() {
393 QTextCursor cursor = inputLine()->textCursor();
394 return cursor.charFormat();
397 void InputWidget::currentCharFormatChanged(const QTextCharFormat &format) {
398 fontChanged(format.font());
399 if (format.foreground().isOpaque())
400 colorChanged(format.foreground().color());
402 colorChanged(Qt::transparent);
403 if (format.background().isOpaque())
404 colorHighlightChanged(format.background().color());
406 colorHighlightChanged(Qt::transparent);
409 void InputWidget::on_boldButton_toggled(bool checked) {
411 fmt.setFontWeight(checked ? QFont::Bold : QFont::Normal);
412 mergeFormatOnWordOrSelection(fmt);
415 void InputWidget::on_underlineButton_toggled(bool checked) {
417 fmt.setFontUnderline(checked);
418 mergeFormatOnWordOrSelection(fmt);
421 void InputWidget::on_italicButton_toggled(bool checked) {
423 fmt.setFontItalic(checked);
424 mergeFormatOnWordOrSelection(fmt);
427 void InputWidget::fontChanged(const QFont &f)
429 ui.boldButton->setChecked(f.bold());
430 ui.italicButton->setChecked(f.italic());
431 ui.underlineButton->setChecked(f.underline());
434 void InputWidget::colorChoosen(QAction * action) {
437 if (qVariantValue<QString>(action->data()) == "") {
438 color = Qt::transparent;
439 fmt = getFormatOfWordOrSelection();
440 fmt.clearForeground();
441 setFormatOnWordOrSelection(fmt);
444 color = QColor(inputLine()->rgbColorFromMirc(qVariantValue<QString>(action->data())));
445 fmt.setForeground(color);
446 mergeFormatOnWordOrSelection(fmt);
448 ui.textcolorButton->setDefaultAction(action);
449 ui.textcolorButton->setIcon(createColorToolButtonIcon(SmallIcon("format-text-color"), color));
452 void InputWidget::colorHighlightChoosen(QAction * action) {
455 if (qVariantValue<QString>(action->data()) == "") {
456 color = Qt::transparent;
457 fmt = getFormatOfWordOrSelection();
458 fmt.clearBackground();
459 setFormatOnWordOrSelection(fmt);
462 color = QColor(inputLine()->rgbColorFromMirc(qVariantValue<QString>(action->data())));
463 fmt.setBackground(color);
464 mergeFormatOnWordOrSelection(fmt);
466 ui.highlightcolorButton->setDefaultAction(action);
467 ui.highlightcolorButton->setIcon(createColorToolButtonIcon(SmallIcon("format-fill-color"), color));
470 void InputWidget::colorChanged(const QColor &fg) {
471 if (fg == Qt::transparent)
472 ui.textcolorButton->setDefaultAction(_colorMenu->actions().last());
474 ui.textcolorButton->setDefaultAction(_colorMenu->actions().value(inputLine()->mircColorFromRGB(fg.name()).toInt()));
476 ui.textcolorButton->setIcon(createColorToolButtonIcon(SmallIcon("format-text-color"), fg));
479 void InputWidget::colorHighlightChanged(const QColor &bg) {
480 if (bg == Qt::transparent)
481 ui.highlightcolorButton->setDefaultAction(_colorFillMenu->actions().last());
483 ui.highlightcolorButton->setDefaultAction(_colorFillMenu->actions().value(inputLine()->mircColorFromRGB(bg.name()).toInt()));
485 ui.highlightcolorButton->setIcon(createColorToolButtonIcon(SmallIcon("format-fill-color"), bg));
488 void InputWidget::on_showStyleButton_toggled(bool checked) {
489 ui.styleFrame->setVisible(checked);
491 ui.showStyleButton->setArrowType(Qt::LeftArrow);
494 ui.showStyleButton->setArrowType(Qt::RightArrow);
498 QIcon InputWidget::createColorToolButtonIcon(const QIcon &icon, QColor color) {
499 QPixmap pixmap(16, 16);
500 pixmap.fill(Qt::transparent);
501 QPainter painter(&pixmap);
502 QPixmap image = icon.pixmap(16,16);
503 QRect target(0, 0, 16, 14);
504 QRect source(0, 0, 16, 14);
505 painter.fillRect(QRect(0, 14, 16, 16), color);
506 painter.drawPixmap(target, image, source);
508 return QIcon(pixmap);
511 // MOUSE WHEEL FILTER
512 MouseWheelFilter::MouseWheelFilter(QObject *parent)
517 bool MouseWheelFilter::eventFilter(QObject *obj, QEvent *event) {
518 if(event->type() != QEvent::Wheel)
519 return QObject::eventFilter(obj, event);