uistyle: Add more type-safety to UiStyle, and clean up a bit
[quassel.git] / src / qtui / topicwidget.cpp
index 8b8e16d..712e74c 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-2013 by the Quassel Project                        *
+ *   Copyright (C) 2005-2016 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
 
 #include "topicwidget.h"
 
+#include <QIcon>
+
 #include "client.h"
-#include "iconloader.h"
 #include "networkmodel.h"
 #include "uisettings.h"
+#include "graphicalui.h"
+#include "uistyle.h"
 
 TopicWidget::TopicWidget(QWidget *parent)
     : AbstractItemView(parent)
 {
     ui.setupUi(this);
-    ui.topicEditButton->setIcon(SmallIcon("edit-rename"));
-    ui.topicLineEdit->setWordWrapEnabled(true);
+    ui.topicEditButton->setIcon(QIcon::fromTheme("edit-rename"));
+    ui.topicLineEdit->setLineWrapEnabled(true);
     ui.topicLineEdit->installEventFilter(this);
 
     connect(ui.topicLabel, SIGNAL(clickableActivated(Clickable)), SLOT(clickableActivated(Clickable)));
@@ -112,11 +115,19 @@ void TopicWidget::setTopic(const QModelIndex &index)
         switch (Client::networkModel()->bufferType(id)) {
         case BufferInfo::StatusBuffer:
             if (network) {
+#if QT_VERSION < 0x050000
                 newtopic = QString("%1 (%2) | %3 | %4")
                            .arg(Qt::escape(network->networkName()))
                            .arg(Qt::escape(network->currentServer()))
                            .arg(tr("Users: %1").arg(network->ircUsers().count()))
                            .arg(tr("Lag: %1 msecs").arg(network->latency()));
+#else
+                newtopic = QString("%1 (%2) | %3 | %4")
+                           .arg(network->networkName().toHtmlEscaped())
+                           .arg(network->currentServer().toHtmlEscaped())
+                           .arg(tr("Users: %1").arg(network->ircUsers().count()))
+                           .arg(tr("Lag: %1 msecs").arg(network->latency()));
+#endif
             }
             else {
                 newtopic = index0.data(Qt::DisplayRole).toString();
@@ -154,12 +165,12 @@ void TopicWidget::setTopic(const QModelIndex &index)
         }
     }
 
-    _topic = newtopic;
+    _topic = sanitizeTopic(newtopic);
     _readonly = readonly;
 
     ui.topicEditButton->setVisible(!_readonly);
-    ui.topicLabel->setText(newtopic);
-    ui.topicLineEdit->setPlainText(newtopic);
+    ui.topicLabel->setText(_topic);
+    ui.topicLineEdit->setPlainText(_topic);
     switchPlain();
 }
 
@@ -191,7 +202,8 @@ void TopicWidget::updateResizeMode()
 void TopicWidget::clickableActivated(const Clickable &click)
 {
     NetworkId networkId = selectionModel()->currentIndex().data(NetworkModel::NetworkIdRole).value<NetworkId>();
-    click.activate(networkId, _topic);
+    UiStyle::StyledString sstr = GraphicalUi::uiStyle()->styleString(GraphicalUi::uiStyle()->mircToInternal(_topic), UiStyle::FormatType::PlainMsg);
+    click.activate(networkId, sstr.plainText);
 }
 
 
@@ -261,3 +273,19 @@ bool TopicWidget::eventFilter(QObject *obj, QEvent *event)
 
     return false;
 }
+
+QString TopicWidget::sanitizeTopic(const QString& topic)
+{
+    // Normally, you don't have new lines in topic messages
+    // But the use of "plain text" functionnality from Qt replaces
+    // some unicode characters with a new line, which then triggers
+    // a stack overflow later
+    QString result(topic);
+#if QT_VERSION >= 0x050000
+    result.replace(QChar::CarriageReturn, " ");
+#endif
+    result.replace(QChar::ParagraphSeparator, " ");
+    result.replace(QChar::LineSeparator, " ");
+
+    return result;
+}