Description: provide a shortcut to reset formatting
Author: Alexey Rusakov <Kitsune-Ral@users.sf.net>
Origin: https://github.com/quotient-im/Quaternion/commit/376384b254b396233dc780511c1cd349078cdc10, https://github.com/quotient-im/Quaternion/commit/a8e2c7d40fbcb9cfa0db8a977cb2115df7ae2b33
Bug: https://github.com/quotient-im/Quaternion/issues/730
Last-Update: 2021-03-01

diff --git a/client/chatedit.cpp b/client/chatedit.cpp
index 65b8117..c6300fa 100644
--- a/client/chatedit.cpp
+++ b/client/chatedit.cpp
@@ -21,15 +21,22 @@
 
 #include "chatroomwidget.h"
 
-#include <util.h>
-
+#include <QtWidgets/QMenu>
+#include <QtWidgets/QShortcut>
 #include <QtGui/QKeyEvent>
 #include <QtCore/QMimeData>
 #include <QtCore/QStringBuilder>
 
+#include <util.h>
+
+static const QKeySequence ResetFormatShortcut("Ctrl+M");
+
 ChatEdit::ChatEdit(ChatRoomWidget* c)
     : KChatEdit(c), chatRoomWidget(c), matchesListPosition(0)
-{ }
+{
+    new QShortcut(ResetFormatShortcut, this,
+                  this, &KChatEdit::resetCurrentFormat);
+}
 
 void ChatEdit::keyPressEvent(QKeyEvent* event)
 {
@@ -43,6 +50,20 @@ void ChatEdit::keyPressEvent(QKeyEvent* event)
     KChatEdit::keyPressEvent(event);
 }
 
+void ChatEdit::contextMenuEvent(QContextMenuEvent *event)
+{
+    auto* menu = createStandardContextMenu();
+    // The shortcut here is in order to show it to the user; it's the QShortcut
+    // in the constructor that actually triggers on Ctrl+M (no idea
+    // why the QAction doesn't work - because it's not in the main menu?)
+    menu->addAction(tr("Reset formatting"),
+                    this, &KChatEdit::resetCurrentFormat, ResetFormatShortcut)
+        ->setStatusTip(
+            tr("Reset the current character formatting to the default"));
+    menu->setAttribute(Qt::WA_DeleteOnClose);
+    menu->popup(event->globalPos());
+}
+
 void ChatEdit::switchContext(QObject* contextKey)
 {
     cancelCompletion();
diff --git a/client/chatedit.h b/client/chatedit.h
index 650f18f..ad91f5e 100644
--- a/client/chatedit.h
+++ b/client/chatedit.h
@@ -69,6 +69,7 @@ class ChatEdit : public KChatEdit
         void appendMentionAt(QTextCursor& cursor, QString mention,
                              QUrl mentionUrl, bool select);
         void keyPressEvent(QKeyEvent* event) override;
+        void contextMenuEvent(QContextMenuEvent* event) override;
 };
 
 
diff --git a/client/kchatedit.cpp b/client/kchatedit.cpp
index a934123..794bc98 100644
--- a/client/kchatedit.cpp
+++ b/client/kchatedit.cpp
@@ -125,6 +125,7 @@ void KChatEdit::KChatEditPrivate::saveInput()
 
     index = history.size() - 1;
     q->clear();
+    q->resetCurrentFormat();
 }
 
 KChatEdit::KChatEdit(QWidget *parent)
@@ -193,6 +193,11 @@ void KChatEdit::switchContext(QObject* contextKey)
     emit contextSwitched();
 }
 
+void KChatEdit::resetCurrentFormat()
+{
+    setCurrentCharFormat({});
+}
+
 QSize KChatEdit::minimumSizeHint() const
 {
     QSize minimumSizeHint = QTextEdit::minimumSizeHint();
diff --git a/client/kchatedit.h b/client/kchatedit.h
index 102943d..8338c98 100644
--- a/client/kchatedit.h
+++ b/client/kchatedit.h
@@ -102,6 +102,13 @@ public Q_SLOTS:
      */
     virtual void switchContext(QObject* contextKey);
 
+    /**
+     * @brief Reset the current character(s) formatting
+     *
+     * This is equivalent to calling `setCurrentCharFormat({})`.
+     */
+    void resetCurrentFormat();
+
 Q_SIGNALS:
     /**
      * A new input has been saved in the history.
