diff options
| author | Mike Skec <skec@protonmail.ch> | 2021-02-21 13:55:22 +1100 |
|---|---|---|
| committer | Felix Queißner <felix@ib-queissner.de> | 2021-02-23 11:31:33 +0100 |
| commit | 195b5560ab1b39eb9d99815814e49f376bb4c571 (patch) | |
| tree | 35a2c61ad2287151359e4b3509ecad421b0ca966 /src/widgets/kristalltextbrowser.cpp | |
| parent | 0f23a86b9d351d04769e3c60e65d55858cdcea6f (diff) | |
| download | kristall-195b5560ab1b39eb9d99815814e49f376bb4c571.tar.gz | |
KristallTextBrowser: replace typographer quotes with ASCII when copying text
Text which is selected in a kristalltextbrowser now has it's quotes replaced with ASCII quotes. This was done as copying fancy quotes is seen as an annoyance to most people. It is still possible to copy these quotes, if they are the only thing in the selection (whitespace also allowed, and commas, and fullstops)
Diffstat (limited to 'src/widgets/kristalltextbrowser.cpp')
| -rw-r--r-- | src/widgets/kristalltextbrowser.cpp | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/widgets/kristalltextbrowser.cpp b/src/widgets/kristalltextbrowser.cpp index 5ff476d..91b5846 100644 --- a/src/widgets/kristalltextbrowser.cpp +++ b/src/widgets/kristalltextbrowser.cpp @@ -1,8 +1,12 @@ #include "kristalltextbrowser.hpp" +#include "kristall.hpp" + #include <QMouseEvent> #include <QScroller> #include <QTouchDevice> +#include <QRegularExpression> +#include <QLineEdit> const Qt::CursorShape KristallTextBrowser::NORMAL_CURSOR = Qt::IBeamCursor; @@ -76,3 +80,62 @@ void KristallTextBrowser::updateCursor() this->viewport()->setCursor(wanted_cursor); } } + +void KristallTextBrowser::keyPressEvent(QKeyEvent *event) +{ + if(event->modifiers() == Qt::ControlModifier && + event->key() == Qt::Key_C) { + this->betterCopy(); + } else { + QTextBrowser::keyPressEvent(event); + } +} + +void KristallTextBrowser::keyReleaseEvent(QKeyEvent *event) +{ + if(event->modifiers() == Qt::ControlModifier && + event->key() == Qt::Key_C) { + // Eat the event + } else { + QTextBrowser::keyReleaseEvent(event); + } +} + + +void KristallTextBrowser::betterCopy() +{ + // Our own implementation of a copy. All we need to do is get the + // selected text, adjust it if needed, and copy it. + // + // The main adjustment we make here is stripping "fancy" quotes from the text, + // as these quotes are usually an annoyance when copying text. + // + // There is a little trick here though: if a user selects a piece of text + // which consists only of whitespace and fancy quotes (or some punctuation: ,.), + // the fancy quotes will be included in the copy. + // + // In all other cases the quotes will be replaced. This can be said to be + // a usability feature for the few people that may want to actually copy + // the quotes themselves. + + QTextCursor cursor = QTextBrowser::textCursor(); + QString text = cursor.selectedText(); + + if (text.isEmpty()) return; + + // Check if text only consists of fancy quotes: + static const QRegularExpression REGEX_ONLY_QUOTES(R"(^[\s“”‘’,.]+$)", + QRegularExpression::CaseInsensitiveOption); + if (text.contains(REGEX_ONLY_QUOTES)) + { + // Copy the original text. + kristall::clipboard->setText(text); + return; + } + + // Replace fancy quotes + static const QRegularExpression REGEX_QUOTES_D("(“|”)"), REGEX_QUOTES_S("(‘|’)"); + text.replace(REGEX_QUOTES_D, "\"").replace(REGEX_QUOTES_S, "'"); + + kristall::clipboard->setText(text); +} |
