1 /***************************************************************************
2 * Copyright (C) 2005-2010 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(colorChosen(QAction*)));
89 ui.highlightcolorButton->setMenu(_colorFillMenu);
90 connect(_colorFillMenu, SIGNAL(triggered(QAction*)), this, SLOT(colorHighlightChosen(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("ShowStyleButtons", this, SLOT(setShowStyleButtons(QVariant)));
111 setShowStyleButtons(s.value("ShowStyleButtons", true));
113 s.notify("MaxNumLines", this, SLOT(setMaxLines(QVariant)));
114 setMaxLines(s.value("MaxNumLines", 5));
116 s.notify("EnableScrollBars", this, SLOT(setScrollBarsEnabled(QVariant)));
117 setScrollBarsEnabled(s.value("EnableScrollBars", true));
119 s.notify("EnableMultiLine", this, SLOT(setMultiLineEnabled(QVariant)));
120 setMultiLineEnabled(s.value("EnableMultiLine", true));
122 ActionCollection *coll = QtUi::actionCollection();
124 Action *activateInputline = coll->add<Action>("FocusInputLine");
125 connect(activateInputline, SIGNAL(triggered()), SLOT(setFocus()));
126 activateInputline->setText(tr("Focus Input Line"));
127 activateInputline->setShortcut(tr("Ctrl+L"));
129 connect(inputLine(), SIGNAL(currentCharFormatChanged(QTextCharFormat)), this, SLOT(currentCharFormatChanged(QTextCharFormat)));
132 InputWidget::~InputWidget() {
135 void InputWidget::setUseCustomFont(const QVariant &v) {
137 UiStyleSettings fs("Fonts");
138 setCustomFont(fs.value("InputWidget"));
140 setCustomFont(QFont());
143 void InputWidget::setCustomFont(const QVariant &v) {
144 QFont font = v.value<QFont>();
145 if(font.family().isEmpty())
146 font = QApplication::font();
147 ui.inputEdit->setCustomFont(font);
150 void InputWidget::setEnableSpellCheck(const QVariant &v) {
151 ui.inputEdit->setSpellCheckEnabled(v.toBool());
154 void InputWidget::setShowNickSelector(const QVariant &v) {
155 ui.ownNick->setVisible(v.toBool());
158 void InputWidget::setShowStyleButtons(const QVariant &v) {
159 ui.showStyleButton->setVisible(v.toBool());
162 void InputWidget::setMaxLines(const QVariant &v) {
163 ui.inputEdit->setMaxHeight(v.toInt());
166 void InputWidget::setScrollBarsEnabled(const QVariant &v) {
167 ui.inputEdit->setScrollBarsEnabled(v.toBool());
170 void InputWidget::setMultiLineEnabled(const QVariant &v) {
171 ui.inputEdit->setMode(v.toBool()? MultiLineEdit::MultiLine : MultiLineEdit::SingleLine);
174 bool InputWidget::eventFilter(QObject *watched, QEvent *event) {
175 if(event->type() != QEvent::KeyPress)
178 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
180 // keys from BufferView should be sent to (and focus) the input line
181 BufferView *view = qobject_cast<BufferView *>(watched);
183 if(keyEvent->text().length() == 1 && !(keyEvent->modifiers() & (Qt::ControlModifier ^ Qt::AltModifier)) ) { // normal key press
184 QChar c = keyEvent->text().at(0);
185 if(c.isLetterOrNumber() || c.isSpace() || c.isPunct() || c.isSymbol()) {
187 QCoreApplication::sendEvent(inputLine(), keyEvent);
192 } else if(watched == ui.inputEdit) {
193 if(keyEvent->matches(QKeySequence::Find)) {
194 QAction *act = GraphicalUi::actionCollection()->action("ToggleSearchBar");
205 void InputWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
207 NetworkId networkId = current.data(NetworkModel::NetworkIdRole).value<NetworkId>();
208 if(networkId == _networkId)
211 setNetwork(networkId);
212 updateNickSelector();
213 updateEnabledState();
216 void InputWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
217 QItemSelectionRange changedArea(topLeft, bottomRight);
218 if(changedArea.contains(selectionModel()->currentIndex())) {
219 updateEnabledState();
223 void InputWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
226 for(int row = start; row <= end; row++) {
227 child = model()->index(row, 0, parent);
228 if(NetworkModel::NetworkItemType != child.data(NetworkModel::ItemTypeRole).toInt())
230 networkId = child.data(NetworkModel::NetworkIdRole).value<NetworkId>();
231 if(networkId == _networkId) {
233 updateNickSelector();
239 void InputWidget::updateEnabledState() {
240 QModelIndex currentIndex = selectionModel()->currentIndex();
242 const Network *net = Client::networkModel()->networkByIndex(currentIndex);
243 bool enabled = false;
245 // disable inputline if it's a channelbuffer we parted from or...
246 enabled = (currentIndex.data(NetworkModel::ItemActiveRole).value<bool>() || (currentIndex.data(NetworkModel::BufferTypeRole).toInt() != BufferInfo::ChannelBuffer));
247 // ... if we're not connected to the network at all
248 enabled &= net->isConnected();
250 ui.inputEdit->setEnabled(enabled);
253 const Network *InputWidget::currentNetwork() const {
254 return Client::network(_networkId);
257 BufferInfo InputWidget::currentBufferInfo() const {
258 return selectionModel()->currentIndex().data(NetworkModel::BufferInfoRole).value<BufferInfo>();
261 void InputWidget::setNetwork(NetworkId networkId) {
262 if(_networkId == networkId)
265 const Network *previousNet = Client::network(_networkId);
267 disconnect(previousNet, 0, this, 0);
268 if(previousNet->me())
269 disconnect(previousNet->me(), 0, this, 0);
272 _networkId = networkId;
274 const Network *network = Client::network(networkId);
276 connect(network, SIGNAL(identitySet(IdentityId)), this, SLOT(setIdentity(IdentityId)));
278 setIdentity(network->identity());
285 void InputWidget::connectMyIrcUser() {
286 const Network *network = currentNetwork();
288 connect(network->me(), SIGNAL(nickSet(const QString &)), this, SLOT(updateNickSelector()));
289 connect(network->me(), SIGNAL(userModesSet(QString)), this, SLOT(updateNickSelector()));
290 connect(network->me(), SIGNAL(userModesAdded(QString)), this, SLOT(updateNickSelector()));
291 connect(network->me(), SIGNAL(userModesRemoved(QString)), this, SLOT(updateNickSelector()));
292 connect(network->me(), SIGNAL(awaySet(bool)), this, SLOT(updateNickSelector()));
293 disconnect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
294 updateNickSelector();
296 connect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
300 void InputWidget::setIdentity(IdentityId identityId) {
301 if(_identityId == identityId)
304 const Identity *previousIdentity = Client::identity(_identityId);
306 disconnect(previousIdentity, 0, this, 0);
308 _identityId = identityId;
310 const Identity *identity = Client::identity(identityId);
312 connect(identity, SIGNAL(nicksSet(QStringList)), this, SLOT(updateNickSelector()));
316 updateNickSelector();
319 void InputWidget::updateNickSelector() const {
322 const Network *net = currentNetwork();
326 const Identity *identity = Client::identity(net->identity());
328 qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId() << "IdentityId:" << net->identity();
333 QStringList nicks = identity->nicks();
334 if((nickIdx = nicks.indexOf(net->myNick())) == -1) {
335 nicks.prepend(net->myNick());
342 IrcUser *me = net->me();
344 nicks[nickIdx] = net->myNick();
345 if(!me->userModes().isEmpty())
346 nicks[nickIdx] += QString(" (+%1)").arg(me->userModes());
349 ui.ownNick->addItems(nicks);
351 if(me && me->isAway())
352 ui.ownNick->setItemData(nickIdx, SmallIcon("user-away"), Qt::DecorationRole);
354 ui.ownNick->setCurrentIndex(nickIdx);
357 void InputWidget::changeNick(const QString &newNick) const {
358 const Network *net = currentNetwork();
359 if(!net || net->isMyNick(newNick))
362 // we reset the nick selecter as we have no confirmation yet, that this will succeed.
363 // if the action succeeds it will be properly updated anyways.
364 updateNickSelector();
365 Client::userInput(BufferInfo::fakeStatusBuffer(net->networkId()), QString("/NICK %1").arg(newNick));
368 void InputWidget::on_inputEdit_textEntered(const QString &text) const {
369 Client::userInput(currentBufferInfo(), text);
370 ui.boldButton->setChecked(false);
371 ui.underlineButton->setChecked(false);
372 ui.italicButton->setChecked(false);
375 fmt.setFontWeight(QFont::Normal);
376 fmt.setFontUnderline(false);
377 fmt.setFontItalic(false);
378 fmt.clearForeground();
379 fmt.clearBackground();
380 inputLine()->setCurrentCharFormat(fmt);
383 void InputWidget::mergeFormatOnWordOrSelection(const QTextCharFormat &format) {
384 QTextCursor cursor = inputLine()->textCursor();
385 if (!cursor.hasSelection())
386 cursor.select(QTextCursor::WordUnderCursor);
387 cursor.mergeCharFormat(format);
388 inputLine()->mergeCurrentCharFormat(format);
391 void InputWidget::setFormatOnWordOrSelection(const QTextCharFormat &format) {
392 QTextCursor cursor = inputLine()->textCursor();
393 if (!cursor.hasSelection())
394 cursor.select(QTextCursor::WordUnderCursor);
395 cursor.setCharFormat(format);
396 inputLine()->setCurrentCharFormat(format);
399 QTextCharFormat InputWidget::getFormatOfWordOrSelection() {
400 QTextCursor cursor = inputLine()->textCursor();
401 return cursor.charFormat();
404 void InputWidget::currentCharFormatChanged(const QTextCharFormat &format) {
405 fontChanged(format.font());
407 if (format.foreground().isOpaque())
408 colorChanged(format.foreground().color());
410 colorChanged(Qt::transparent);
411 if (format.background().isOpaque())
412 colorHighlightChanged(format.background().color());
414 colorHighlightChanged(Qt::transparent);
417 void InputWidget::on_boldButton_clicked(bool checked) {
419 fmt.setFontWeight(checked ? QFont::Bold : QFont::Normal);
420 mergeFormatOnWordOrSelection(fmt);
423 void InputWidget::on_underlineButton_clicked(bool checked) {
425 fmt.setFontUnderline(checked);
426 mergeFormatOnWordOrSelection(fmt);
429 void InputWidget::on_italicButton_clicked(bool checked) {
431 fmt.setFontItalic(checked);
432 mergeFormatOnWordOrSelection(fmt);
435 void InputWidget::fontChanged(const QFont &f)
437 ui.boldButton->setChecked(f.bold());
438 ui.italicButton->setChecked(f.italic());
439 ui.underlineButton->setChecked(f.underline());
442 void InputWidget::colorChosen(QAction *action) {
445 if (qVariantValue<QString>(action->data()) == "") {
446 color = Qt::transparent;
447 fmt = getFormatOfWordOrSelection();
448 fmt.clearForeground();
449 setFormatOnWordOrSelection(fmt);
452 color = QColor(inputLine()->rgbColorFromMirc(qVariantValue<QString>(action->data())));
453 fmt.setForeground(color);
454 mergeFormatOnWordOrSelection(fmt);
456 ui.textcolorButton->setDefaultAction(action);
457 ui.textcolorButton->setIcon(createColorToolButtonIcon(SmallIcon("format-text-color"), color));
460 void InputWidget::colorHighlightChosen(QAction *action) {
463 if (qVariantValue<QString>(action->data()) == "") {
464 color = Qt::transparent;
465 fmt = getFormatOfWordOrSelection();
466 fmt.clearBackground();
467 setFormatOnWordOrSelection(fmt);
470 color = QColor(inputLine()->rgbColorFromMirc(qVariantValue<QString>(action->data())));
471 fmt.setBackground(color);
472 mergeFormatOnWordOrSelection(fmt);
474 ui.highlightcolorButton->setDefaultAction(action);
475 ui.highlightcolorButton->setIcon(createColorToolButtonIcon(SmallIcon("format-fill-color"), color));
478 void InputWidget::colorChanged(const QColor &fg) {
479 if (fg == Qt::transparent)
480 ui.textcolorButton->setDefaultAction(_colorMenu->actions().last());
482 ui.textcolorButton->setDefaultAction(_colorMenu->actions().value(inputLine()->mircColorFromRGB(fg.name()).toInt()));
484 ui.textcolorButton->setIcon(createColorToolButtonIcon(SmallIcon("format-text-color"), fg));
487 void InputWidget::colorHighlightChanged(const QColor &bg) {
488 if (bg == Qt::transparent)
489 ui.highlightcolorButton->setDefaultAction(_colorFillMenu->actions().last());
491 ui.highlightcolorButton->setDefaultAction(_colorFillMenu->actions().value(inputLine()->mircColorFromRGB(bg.name()).toInt()));
493 ui.highlightcolorButton->setIcon(createColorToolButtonIcon(SmallIcon("format-fill-color"), bg));
496 void InputWidget::on_showStyleButton_toggled(bool checked) {
497 ui.styleFrame->setVisible(checked);
499 ui.showStyleButton->setArrowType(Qt::LeftArrow);
502 ui.showStyleButton->setArrowType(Qt::RightArrow);
506 QIcon InputWidget::createColorToolButtonIcon(const QIcon &icon, const QColor &color) {
507 QPixmap pixmap(16, 16);
508 pixmap.fill(Qt::transparent);
509 QPainter painter(&pixmap);
510 QPixmap image = icon.pixmap(16,16);
511 QRect target(0, 0, 16, 14);
512 QRect source(0, 0, 16, 14);
513 painter.fillRect(QRect(0, 14, 16, 16), color);
514 painter.drawPixmap(target, image, source);
516 return QIcon(pixmap);
519 // MOUSE WHEEL FILTER
520 MouseWheelFilter::MouseWheelFilter(QObject *parent)
525 bool MouseWheelFilter::eventFilter(QObject *obj, QEvent *event) {
526 if(event->type() != QEvent::Wheel)
527 return QObject::eventFilter(obj, event);