From 385abb3041f8d71ef21fb7f68114b99f2d235203 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Sat, 31 Dec 2016 14:13:42 +0800 Subject: [PATCH] hide image preview block when selected We do not want user to copy the image preview block (a special char). Signed-off-by: Le Tan --- src/hgmarkdownhighlighter.cpp | 6 +++++ src/hgmarkdownhighlighter.h | 2 ++ src/vmdedit.cpp | 45 ++++++++++++++++++++++++++++++++++- src/vmdedit.h | 3 +++ 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/hgmarkdownhighlighter.cpp b/src/hgmarkdownhighlighter.cpp index 06dfcc49..a9443b1a 100644 --- a/src/hgmarkdownhighlighter.cpp +++ b/src/hgmarkdownhighlighter.cpp @@ -263,3 +263,9 @@ void HGMarkdownHighlighter::timerTimeout() rehighlight(); emit highlightCompleted(); } + +void HGMarkdownHighlighter::updateHighlight() +{ + timer->stop(); + timerTimeout(); +} diff --git a/src/hgmarkdownhighlighter.h b/src/hgmarkdownhighlighter.h index 6823b502..be4cb74d 100644 --- a/src/hgmarkdownhighlighter.h +++ b/src/hgmarkdownhighlighter.h @@ -56,6 +56,8 @@ public: QTextDocument *parent = 0); ~HGMarkdownHighlighter(); void setStyles(const QVector &styles); + // Request to update highlihgt (re-parse and re-highlight) + void updateHighlight(); signals: void highlightCompleted(); diff --git a/src/vmdedit.cpp b/src/vmdedit.cpp index 74c87973..40a8c4bc 100644 --- a/src/vmdedit.cpp +++ b/src/vmdedit.cpp @@ -16,7 +16,7 @@ const QString VMdEdit::c_cursorLineColor = "Indigo1"; const QString VMdEdit::c_cursorLineColorVim = "Green2"; VMdEdit::VMdEdit(VFile *p_file, QWidget *p_parent) - : VEdit(p_file, p_parent), m_mdHighlighter(NULL) + : VEdit(p_file, p_parent), m_mdHighlighter(NULL), m_previewImage(true) { Q_ASSERT(p_file->getDocType() == DocType::Markdown); @@ -40,6 +40,9 @@ VMdEdit::VMdEdit(VFile *p_file, QWidget *p_parent) this, &VMdEdit::highlightCurrentLine); } + connect(this, &VMdEdit::selectionChanged, + this, &VMdEdit::handleSelectionChanged); + m_editOps->updateTabSettings(); updateFontAndPalette(); } @@ -229,6 +232,9 @@ void VMdEdit::scrollToHeader(int p_headerIndex) void VMdEdit::updateImageBlocks(QSet p_imageBlocks) { + if (!m_previewImage) { + return; + } // We need to handle blocks backward to avoid shifting all the following blocks. // Inserting the preview image block may cause highlighter to emit signal again. QList blockList = p_imageBlocks.toList(); @@ -310,6 +316,25 @@ void VMdEdit::clearCorruptedImagePreviewBlock(QTextBlock p_block) } } +void VMdEdit::clearAllImagePreviewBlocks() +{ + QTextDocument *doc = document(); + QTextBlock block = doc->begin(); + bool modified = isModified(); + while (block.isValid()) { + if (isImagePreviewBlock(block)) { + QTextBlock nextBlock = block.next(); + removeBlock(block); + block = nextBlock; + } else { + clearCorruptedImagePreviewBlock(block); + block = block.next(); + } + } + setModified(modified); + emit statusChanged(); +} + QString VMdEdit::fetchImageToPreview(const QString &p_text) { QRegExp regExp("\\!\\[[^\\]]*\\]\\((images/[^/\\)]+)\\)"); @@ -491,3 +516,21 @@ void VMdEdit::handleEditStateChanged(KeyState p_state) } highlightCurrentLine(); } + +void VMdEdit::handleSelectionChanged() +{ + QString text = textCursor().selectedText(); + if (text.isEmpty() && !m_previewImage) { + m_previewImage = true; + m_mdHighlighter->updateHighlight(); + } else if (m_previewImage) { + if (text.trimmed() == QString(QChar::ObjectReplacementCharacter)) { + // Select the image and some whitespaces. + // We can let the user copy the image. + return; + } else if (text.contains(QChar::ObjectReplacementCharacter)) { + m_previewImage = false; + clearAllImagePreviewBlocks(); + } + } +} diff --git a/src/vmdedit.h b/src/vmdedit.h index 16e2e288..1957c7ee 100644 --- a/src/vmdedit.h +++ b/src/vmdedit.h @@ -40,6 +40,7 @@ private slots: void updateImageBlocks(QSet p_imageBlocks); void highlightCurrentLine(); void handleEditStateChanged(KeyState p_state); + void handleSelectionChanged(); protected: void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE; @@ -69,12 +70,14 @@ private: // Returns the image relative path (image/xxx.png) only when // there is one and only one image link. QString fetchImageToPreview(const QString &p_text); + void clearAllImagePreviewBlocks(); HGMarkdownHighlighter *m_mdHighlighter; QVector m_insertedImages; QVector m_initImages; QVector m_headers; QColor m_cursorLineColor; + bool m_previewImage; static const QString c_cursorLineColor; static const QString c_cursorLineColorVim;