+}
+
+void TopicWidget::on_topicEditButton_clicked()
+{
+ switchEditable();
+}
+
+void TopicWidget::switchEditable()
+{
+ ui.stackedWidget->setCurrentIndex(1);
+ ui.topicLineEdit->setFocus();
+ ui.topicLineEdit->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
+ updateGeometry();
+}
+
+void TopicWidget::switchPlain()
+{
+ ui.stackedWidget->setCurrentIndex(0);
+ ui.topicLineEdit->setPlainText(_topic);
+ updateGeometry();
+ emit switchedPlain();
+}
+
+// filter for the input widget to switch back to normal mode
+bool TopicWidget::eventFilter(QObject* obj, QEvent* event)
+{
+ if (event->type() == QEvent::FocusOut && !_mouseEntered) {
+ switchPlain();
+ return true;
+ }
+
+ if (event->type() == QEvent::Enter) {
+ _mouseEntered = true;
+ }
+
+ if (event->type() == QEvent::Leave) {
+ _mouseEntered = false;
+ }
+
+ if (event->type() != QEvent::KeyRelease)
+ return QObject::eventFilter(obj, event);
+
+ auto* keyEvent = static_cast<QKeyEvent*>(event);
+
+ if (keyEvent->key() == Qt::Key_Escape) {
+ switchPlain();
+ return true;
+ }
+
+ 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);
+ result.replace(QChar::CarriageReturn, " ");
+ result.replace(QChar::ParagraphSeparator, " ");
+ result.replace(QChar::LineSeparator, " ");
+
+ return result;