support copying image from image preview block

Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
Le Tan 2016-12-31 16:12:51 +08:00
parent 385abb3041
commit 7eadaed727
2 changed files with 58 additions and 5 deletions

View File

@ -42,6 +42,8 @@ VMdEdit::VMdEdit(VFile *p_file, QWidget *p_parent)
connect(this, &VMdEdit::selectionChanged, connect(this, &VMdEdit::selectionChanged,
this, &VMdEdit::handleSelectionChanged); this, &VMdEdit::handleSelectionChanged);
connect(QApplication::clipboard(), &QClipboard::changed,
this, &VMdEdit::handleClipboardChanged);
m_editOps->updateTabSettings(); m_editOps->updateTabSettings();
updateFontAndPalette(); updateFontAndPalette();
@ -473,21 +475,20 @@ QString VMdEdit::toPlainTextWithoutImg() const
break; break;
} }
start = removeObjectReplacementLine(text, index); start = removeObjectReplacementLine(text, index);
} while (start < text.size()); } while (start > -1 && start < text.size());
return text; return text;
} }
int VMdEdit::removeObjectReplacementLine(QString &p_text, int p_index) const int VMdEdit::removeObjectReplacementLine(QString &p_text, int p_index) const
{ {
Q_ASSERT(p_text.size() > p_index && p_text.at(p_index) == QChar::ObjectReplacementCharacter); Q_ASSERT(p_text.size() > p_index && p_text.at(p_index) == QChar::ObjectReplacementCharacter);
Q_ASSERT(p_text.at(p_index + 1) == '\n');
int prevLineIdx = p_text.lastIndexOf('\n', p_index); int prevLineIdx = p_text.lastIndexOf('\n', p_index);
if (prevLineIdx == -1) { if (prevLineIdx == -1) {
prevLineIdx = 0; prevLineIdx = 0;
} }
// Remove \n[....?\n] // Remove [\n....?]
p_text.remove(prevLineIdx + 1, p_index - prevLineIdx + 1); p_text.remove(prevLineIdx, p_index - prevLineIdx + 1);
return prevLineIdx; return prevLineIdx - 1;
} }
void VMdEdit::highlightCurrentLine() void VMdEdit::highlightCurrentLine()
@ -534,3 +535,51 @@ void VMdEdit::handleSelectionChanged()
} }
} }
} }
void VMdEdit::handleClipboardChanged(QClipboard::Mode p_mode)
{
if (p_mode == QClipboard::Clipboard) {
QClipboard *clipboard = QApplication::clipboard();
const QMimeData *mimeData = clipboard->mimeData();
if (mimeData->hasText()) {
QString text = mimeData->text();
if (clipboard->ownsClipboard() &&
(text.trimmed() == QString(QChar::ObjectReplacementCharacter))) {
QString imagePath = selectedImage();
qDebug() << "clipboard" << imagePath;
Q_ASSERT(!imagePath.isEmpty());
QImage image(imagePath);
Q_ASSERT(!image.isNull());
clipboard->clear(QClipboard::Clipboard);
clipboard->setImage(image, QClipboard::Clipboard);
}
}
}
}
QString VMdEdit::selectedImage()
{
QString imagePath;
QTextCursor cursor = textCursor();
if (!cursor.hasSelection()) {
return imagePath;
}
int start = cursor.selectionStart();
int end = cursor.selectionEnd();
QTextDocument *doc = document();
QTextBlock startBlock = doc->findBlock(start);
QTextBlock endBlock = doc->findBlock(end);
QTextBlock block = startBlock;
while (block.isValid()) {
if (isImagePreviewBlock(block)) {
QString image = fetchImageToPreview(block.previous().text());
imagePath = QDir(m_file->retriveBasePath()).filePath(image);
break;
}
if (block == endBlock) {
break;
}
block = block.next();
}
return imagePath;
}

View File

@ -5,6 +5,7 @@
#include <QVector> #include <QVector>
#include <QString> #include <QString>
#include <QColor> #include <QColor>
#include <QClipboard>
#include "vtoc.h" #include "vtoc.h"
#include "veditoperations.h" #include "veditoperations.h"
@ -41,6 +42,7 @@ private slots:
void highlightCurrentLine(); void highlightCurrentLine();
void handleEditStateChanged(KeyState p_state); void handleEditStateChanged(KeyState p_state);
void handleSelectionChanged(); void handleSelectionChanged();
void handleClipboardChanged(QClipboard::Mode p_mode);
protected: protected:
void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE; void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
@ -71,6 +73,8 @@ private:
// there is one and only one image link. // there is one and only one image link.
QString fetchImageToPreview(const QString &p_text); QString fetchImageToPreview(const QString &p_text);
void clearAllImagePreviewBlocks(); void clearAllImagePreviewBlocks();
// There is a QChar::ObjectReplacementCharacter in the selection. Find out the image path.
QString selectedImage();
HGMarkdownHighlighter *m_mdHighlighter; HGMarkdownHighlighter *m_mdHighlighter;
QVector<QString> m_insertedImages; QVector<QString> m_insertedImages;