From 5a8f74ce99e184bf78b81b69cd3e35c6f269ac89 Mon Sep 17 00:00:00 2001 From: hunbernd Date: Mon, 7 Dec 2015 02:28:27 +0100 Subject: [PATCH] Spoiler feature Select text to hide, then right click --> Spoiler --- .../src/gui/common/MimeTextEdit.cpp | 7 ++++ retroshare-gui/src/gui/common/MimeTextEdit.h | 2 + retroshare-gui/src/util/HandleRichText.cpp | 40 +++++++++++++++++++ retroshare-gui/src/util/HandleRichText.h | 2 + 4 files changed, 51 insertions(+) diff --git a/retroshare-gui/src/gui/common/MimeTextEdit.cpp b/retroshare-gui/src/gui/common/MimeTextEdit.cpp index 9513703d6..78e19cbe1 100644 --- a/retroshare-gui/src/gui/common/MimeTextEdit.cpp +++ b/retroshare-gui/src/gui/common/MimeTextEdit.cpp @@ -231,6 +231,8 @@ void MimeTextEdit::contextMenuEvent(QContextMenuEvent *e) /* Add actions for pasting links */ contextMenu->addAction( tr("Paste as plain text"), this, SLOT(pastePlainText())); + QAction *spoilerAction = contextMenu->addAction(tr("Spoiler"), this, SLOT(spoiler())); + spoilerAction->setToolTip(tr("Select text to hide, then push this button")); contextMenu->addSeparator(); QAction *pasteLinkAction = contextMenu->addAction(QIcon(":/images/pasterslink.png"), tr("Paste RetroShare Link"), this, SLOT(pasteLink())); contextMenu->addAction(QIcon(":/images/pasterslink.png"), tr("Paste my certificate link"), this, SLOT(pasteOwnCertificateLink())); @@ -268,3 +270,8 @@ void MimeTextEdit::pastePlainText() { insertPlainText(QApplication::clipboard()->text()); } + +void MimeTextEdit::spoiler() +{ + RsHtml::insertSpoilerText(this->textCursor()); +} diff --git a/retroshare-gui/src/gui/common/MimeTextEdit.h b/retroshare-gui/src/gui/common/MimeTextEdit.h index d28bb2c6e..f327e4a6f 100644 --- a/retroshare-gui/src/gui/common/MimeTextEdit.h +++ b/retroshare-gui/src/gui/common/MimeTextEdit.h @@ -59,6 +59,8 @@ private slots: void pasteLink(); void pasteOwnCertificateLink(); void pastePlainText(); + void spoiler(); + private: QString textUnderCursor() const; diff --git a/retroshare-gui/src/util/HandleRichText.cpp b/retroshare-gui/src/util/HandleRichText.cpp index d88cc366c..bd4faba94 100644 --- a/retroshare-gui/src/util/HandleRichText.cpp +++ b/retroshare-gui/src/util/HandleRichText.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include "HandleRichText.h" #include "gui/RetroShareLink.h" @@ -643,6 +644,28 @@ static void optimizeHtml(QDomDocument& doc continue; } + //hidden text in a + if (element.tagName().toLower() == "a") { + if(element.hasAttribute("href")){ + QString href = element.attribute("href", ""); + if(href.startsWith("hidden:")){ + //this text should be hidden and appear in title + //we need this trick, because QTextEdit doesn't export the title attribute + QString title = href.remove(0, QString("hidden:").length()); + QString text = element.text(); + element.setTagName("span"); + element.removeAttribute("href"); + QDomNodeList c = element.childNodes(); + for(int i = 0; i < c.count(); i++){ + element.removeChild(c.at(i)); + }; + element.setAttribute(QString("title"), title); + QDomText textnode = doc.createTextNode(text); + element.appendChild(textnode); + } + } + } + // iterate children optimizeHtml(doc, element, stylesList, knownStyle); @@ -1004,3 +1027,20 @@ QString RsHtml::makeQuotedText(RSTextBrowser *browser) text = sl.join("\n>"); return QString(">") + text; } + +void RsHtml::insertSpoilerText(QTextCursor cursor) +{ + QString hiddentext = cursor.selection().toPlainText(); + if(hiddentext.isEmpty()) return; + QString publictext = "*SPOILER*"; + + QString encoded = hiddentext; + encoded = encoded.replace(QChar('\"'), QString(""")); + encoded = encoded.replace(QChar('\''), QString("'")); + encoded = encoded.replace(QChar('<'), QString("<")); + encoded = encoded.replace(QChar('>'), QString(">")); + encoded = encoded.replace(QChar('&'), QString("&")); + + QString html = QString("%2").arg(encoded, publictext); + cursor.insertHtml(html); +} diff --git a/retroshare-gui/src/util/HandleRichText.h b/retroshare-gui/src/util/HandleRichText.h index 4cea4a8d5..e832ee02a 100644 --- a/retroshare-gui/src/util/HandleRichText.h +++ b/retroshare-gui/src/util/HandleRichText.h @@ -53,6 +53,7 @@ class QDomDocument; class QDomElement; class EmbedInHtml; class RetroShareLink; +class QTextCursor; class RsHtml { @@ -75,6 +76,7 @@ public: static QString plainText(const std::string &text); static QString makeQuotedText(RSTextBrowser* browser); + static void insertSpoilerText(QTextCursor cursor); protected: void embedHtml(QTextDocument *textDocument, QDomDocument &doc, QDomElement ¤tElement, EmbedInHtml& embedInfos, ulong flag);