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