Browse Source

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 <[email protected]>
Le Tan 9 years ago
parent
commit
385abb3041
4 changed files with 55 additions and 1 deletions
  1. 6 0
      src/hgmarkdownhighlighter.cpp
  2. 2 0
      src/hgmarkdownhighlighter.h
  3. 44 1
      src/vmdedit.cpp
  4. 3 0
      src/vmdedit.h

+ 6 - 0
src/hgmarkdownhighlighter.cpp

@@ -263,3 +263,9 @@ void HGMarkdownHighlighter::timerTimeout()
     rehighlight();
     emit highlightCompleted();
 }
+
+void HGMarkdownHighlighter::updateHighlight()
+{
+    timer->stop();
+    timerTimeout();
+}

+ 2 - 0
src/hgmarkdownhighlighter.h

@@ -56,6 +56,8 @@ public:
                           QTextDocument *parent = 0);
     ~HGMarkdownHighlighter();
     void setStyles(const QVector<HighlightingStyle> &styles);
+    // Request to update highlihgt (re-parse and re-highlight)
+    void updateHighlight();
 
 signals:
     void highlightCompleted();

+ 44 - 1
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<int> 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<int> 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();
+        }
+    }
+}

+ 3 - 0
src/vmdedit.h

@@ -40,6 +40,7 @@ private slots:
     void updateImageBlocks(QSet<int> 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<QString> m_insertedImages;
     QVector<QString> m_initImages;
     QVector<VHeader> m_headers;
     QColor m_cursorLineColor;
+    bool m_previewImage;
 
     static const QString c_cursorLineColor;
     static const QString c_cursorLineColorVim;