+
+void InputWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
+{
+ QItemSelectionRange changedArea(topLeft, bottomRight);
+ if (changedArea.contains(selectionModel()->currentIndex())) {
+ updateEnabledState();
+
+ bool encrypted = false;
+
+ IrcChannel *chan = qobject_cast<IrcChannel *>(Client::bufferModel()->data(selectionModel()->currentIndex(), NetworkModel::IrcChannelRole).value<QObject *>());
+ if (chan)
+ encrypted = chan->encrypted();
+
+ IrcUser *user = qobject_cast<IrcUser *>(Client::bufferModel()->data(selectionModel()->currentIndex(), NetworkModel::IrcUserRole).value<QObject *>());
+ if (user)
+ encrypted = user->encrypted();
+
+ if (encrypted)
+ ui.encryptionIconLabel->show();
+ else
+ ui.encryptionIconLabel->hide();
+ }
+}
+
+
+void InputWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
+{
+ NetworkId networkId;
+ QModelIndex child;
+ for (int row = start; row <= end; row++) {
+ child = model()->index(row, 0, parent);
+ if (NetworkModel::NetworkItemType != child.data(NetworkModel::ItemTypeRole).toInt())
+ continue;
+ networkId = child.data(NetworkModel::NetworkIdRole).value<NetworkId>();
+ if (networkId == _networkId) {
+ setNetwork(0);
+ updateNickSelector();
+ return;
+ }
+ }
+}
+
+
+void InputWidget::updateEnabledState()
+{
+// FIXME: Find a visualization for this that does not disable the widget!
+// Disabling kills global action shortcuts, plus users sometimes need/want to enter text
+// even in inactive channels.
+#if 0
+ QModelIndex currentIndex = selectionModel()->currentIndex();
+
+ const Network *net = Client::networkModel()->networkByIndex(currentIndex);
+ bool enabled = false;
+ if (net) {
+ // disable inputline if it's a channelbuffer we parted from or...
+ enabled = (currentIndex.data(NetworkModel::ItemActiveRole).value<bool>() || (currentIndex.data(NetworkModel::BufferTypeRole).toInt() != BufferInfo::ChannelBuffer));
+ // ... if we're not connected to the network at all
+ enabled &= net->isConnected();
+ }
+
+ ui.inputEdit->setEnabled(enabled);
+#endif
+}
+
+
+const Network *InputWidget::currentNetwork() const
+{
+ return Client::network(_networkId);
+}
+
+
+BufferInfo InputWidget::currentBufferInfo() const
+{
+ return selectionModel()->currentIndex().data(NetworkModel::BufferInfoRole).value<BufferInfo>();
+};
+
+void InputWidget::setNetwork(NetworkId networkId)
+{
+ if (_networkId == networkId)
+ return;
+
+ const Network *previousNet = Client::network(_networkId);
+ if (previousNet) {
+ disconnect(previousNet, 0, this, 0);
+ if (previousNet->me())
+ disconnect(previousNet->me(), 0, this, 0);
+ }
+
+ _networkId = networkId;
+
+ const Network *network = Client::network(networkId);
+ if (network) {
+ connect(network, SIGNAL(identitySet(IdentityId)), this, SLOT(setIdentity(IdentityId)));
+ connectMyIrcUser();
+ setIdentity(network->identity());
+ }
+ else {
+ setIdentity(0);
+ _networkId = 0;
+ }
+}
+
+
+void InputWidget::connectMyIrcUser()
+{
+ const Network *network = currentNetwork();
+ if (network->me()) {
+ connect(network->me(), SIGNAL(nickSet(const QString &)), this, SLOT(updateNickSelector()));
+ connect(network->me(), SIGNAL(userModesSet(QString)), this, SLOT(updateNickSelector()));
+ connect(network->me(), SIGNAL(userModesAdded(QString)), this, SLOT(updateNickSelector()));
+ connect(network->me(), SIGNAL(userModesRemoved(QString)), this, SLOT(updateNickSelector()));
+ connect(network->me(), SIGNAL(awaySet(bool)), this, SLOT(updateNickSelector()));
+ disconnect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
+ updateNickSelector();
+ }
+ else {
+ connect(network, SIGNAL(myNickSet(const QString &)), this, SLOT(connectMyIrcUser()));
+ }
+}
+
+
+void InputWidget::setIdentity(IdentityId identityId)
+{
+ if (_identityId == identityId)
+ return;
+
+ const Identity *previousIdentity = Client::identity(_identityId);
+ if (previousIdentity)
+ disconnect(previousIdentity, 0, this, 0);
+
+ _identityId = identityId;
+
+ const Identity *identity = Client::identity(identityId);
+ if (identity) {
+ connect(identity, SIGNAL(nicksSet(QStringList)), this, SLOT(updateNickSelector()));
+ }
+ else {
+ _identityId = 0;
+ }
+ updateNickSelector();
+}
+
+
+void InputWidget::updateNickSelector() const
+{
+ ui.ownNick->clear();
+
+ const Network *net = currentNetwork();
+ if (!net)
+ return;
+
+ const Identity *identity = Client::identity(net->identity());
+ if (!identity) {
+ qWarning() << "InputWidget::updateNickSelector(): can't find Identity for Network" << net->networkId() << "IdentityId:" << net->identity();
+ return;
+ }
+
+ int nickIdx;
+ QStringList nicks = identity->nicks();
+ if ((nickIdx = nicks.indexOf(net->myNick())) == -1) {
+ nicks.prepend(net->myNick());
+ nickIdx = 0;
+ }
+
+ if (nicks.isEmpty())
+ return;
+
+ IrcUser *me = net->me();
+ if (me) {
+ nicks[nickIdx] = net->myNick();
+ if (!me->userModes().isEmpty())
+ nicks[nickIdx] += QString(" (+%1)").arg(me->userModes());
+ }
+
+ ui.ownNick->addItems(nicks);
+
+ if (me && me->isAway())
+ ui.ownNick->setItemData(nickIdx, QIcon::fromTheme("user-away"), Qt::DecorationRole);
+
+ ui.ownNick->setCurrentIndex(nickIdx);
+}
+
+
+void InputWidget::changeNick(const QString &newNick) const
+{
+ const Network *net = currentNetwork();
+ if (!net || net->isMyNick(newNick))
+ return;
+
+ // we reset the nick selecter as we have no confirmation yet, that this will succeed.
+ // if the action succeeds it will be properly updated anyways.
+ updateNickSelector();
+ Client::userInput(BufferInfo::fakeStatusBuffer(net->networkId()), QString("/NICK %1").arg(newNick));
+}
+
+
+void InputWidget::onTextEntered(const QString &text)
+{
+ Client::userInput(currentBufferInfo(), text);
+ ui.boldButton->setChecked(false);
+ ui.underlineButton->setChecked(false);
+ ui.italicButton->setChecked(false);
+
+ QTextCharFormat fmt;
+ fmt.setFontWeight(QFont::Normal);
+ fmt.setFontUnderline(false);
+ fmt.setFontItalic(false);
+ fmt.clearForeground();
+ fmt.clearBackground();
+ inputLine()->setCurrentCharFormat(fmt);
+
+#ifdef HAVE_KDE
+ // Set highlighter back to active in case it was deactivated by too many errors.
+ if (ui.inputEdit->highlighter())
+ ui.inputEdit->highlighter()->setActive(true);
+#endif
+}
+
+
+void InputWidget::mergeFormatOnSelection(const QTextCharFormat &format)
+{
+ QTextCursor cursor = inputLine()->textCursor();
+ cursor.mergeCharFormat(format);
+ inputLine()->mergeCurrentCharFormat(format);
+}
+
+
+void InputWidget::setFormatOnSelection(const QTextCharFormat &format)
+{
+ QTextCursor cursor = inputLine()->textCursor();
+ cursor.setCharFormat(format);
+ inputLine()->setCurrentCharFormat(format);
+}
+
+
+QTextCharFormat InputWidget::getFormatOfWordOrSelection()
+{
+ QTextCursor cursor = inputLine()->textCursor();
+ return cursor.charFormat();
+}
+
+
+void InputWidget::currentCharFormatChanged(const QTextCharFormat &format)
+{
+ fontChanged(format.font());
+}
+
+
+void InputWidget::on_boldButton_clicked(bool checked)
+{
+ QTextCharFormat fmt;
+ fmt.setFontWeight(checked ? QFont::Bold : QFont::Normal);
+ mergeFormatOnSelection(fmt);
+}
+
+
+void InputWidget::on_underlineButton_clicked(bool checked)
+{
+ QTextCharFormat fmt;
+ fmt.setFontUnderline(checked);
+ mergeFormatOnSelection(fmt);
+}
+
+
+void InputWidget::on_italicButton_clicked(bool checked)
+{
+ QTextCharFormat fmt;
+ fmt.setFontItalic(checked);
+ mergeFormatOnSelection(fmt);
+}
+
+
+void InputWidget::fontChanged(const QFont &f)
+{
+ ui.boldButton->setChecked(f.bold());
+ ui.italicButton->setChecked(f.italic());
+ ui.underlineButton->setChecked(f.underline());
+}
+
+
+void InputWidget::colorChosen(QAction *action)
+{
+ QTextCharFormat fmt;
+ QColor color;
+ if (action->data().value<QString>() == "") {
+ color = Qt::transparent;
+ fmt = getFormatOfWordOrSelection();
+ fmt.clearForeground();
+ setFormatOnSelection(fmt);
+ }
+ else {
+ color = QColor(inputLine()->rgbColorFromMirc(action->data().value<QString>()));
+ fmt.setForeground(color);
+ mergeFormatOnSelection(fmt);
+ }
+ ui.textcolorButton->setDefaultAction(action);
+ ui.textcolorButton->setIcon(createColorToolButtonIcon(QIcon::fromTheme("format-text-color"), color));
+}
+
+
+void InputWidget::colorHighlightChosen(QAction *action)
+{
+ QTextCharFormat fmt;
+ QColor color;
+ if (action->data().value<QString>() == "") {
+ color = Qt::transparent;
+ fmt = getFormatOfWordOrSelection();
+ fmt.clearBackground();
+ setFormatOnSelection(fmt);
+ }
+ else {
+ color = QColor(inputLine()->rgbColorFromMirc(action->data().value<QString>()));
+ fmt.setBackground(color);
+ mergeFormatOnSelection(fmt);
+ }
+ ui.highlightcolorButton->setDefaultAction(action);
+ ui.highlightcolorButton->setIcon(createColorToolButtonIcon(QIcon::fromTheme("format-fill-color"), color));
+}
+
+
+void InputWidget::on_showStyleButton_toggled(bool checked)
+{
+ ui.styleFrame->setVisible(checked);
+ if (checked) {
+ ui.showStyleButton->setArrowType(Qt::LeftArrow);
+ }
+ else {
+ ui.showStyleButton->setArrowType(Qt::RightArrow);
+ }
+}
+
+
+QIcon InputWidget::createColorToolButtonIcon(const QIcon &icon, const QColor &color)
+{
+ QPixmap pixmap(16, 16);
+ pixmap.fill(Qt::transparent);
+ QPainter painter(&pixmap);
+ QPixmap image = icon.pixmap(16, 16);
+ QRect target(0, 0, 16, 14);
+ QRect source(0, 0, 16, 14);
+ painter.fillRect(QRect(0, 14, 16, 16), color);
+ painter.drawPixmap(target, image, source);
+
+ return QIcon(pixmap);
+}
+
+
+// MOUSE WHEEL FILTER
+MouseWheelFilter::MouseWheelFilter(QObject *parent)
+ : QObject(parent)
+{
+}
+
+
+bool MouseWheelFilter::eventFilter(QObject *obj, QEvent *event)
+{
+ if (event->type() != QEvent::Wheel)
+ return QObject::eventFilter(obj, event);
+ else
+ return true;
+}