mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
support copying image from image preview block
Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
parent
385abb3041
commit
7eadaed727
@ -42,6 +42,8 @@ VMdEdit::VMdEdit(VFile *p_file, QWidget *p_parent)
|
||||
|
||||
connect(this, &VMdEdit::selectionChanged,
|
||||
this, &VMdEdit::handleSelectionChanged);
|
||||
connect(QApplication::clipboard(), &QClipboard::changed,
|
||||
this, &VMdEdit::handleClipboardChanged);
|
||||
|
||||
m_editOps->updateTabSettings();
|
||||
updateFontAndPalette();
|
||||
@ -473,21 +475,20 @@ QString VMdEdit::toPlainTextWithoutImg() const
|
||||
break;
|
||||
}
|
||||
start = removeObjectReplacementLine(text, index);
|
||||
} while (start < text.size());
|
||||
} while (start > -1 && start < text.size());
|
||||
return text;
|
||||
}
|
||||
|
||||
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.at(p_index + 1) == '\n');
|
||||
int prevLineIdx = p_text.lastIndexOf('\n', p_index);
|
||||
if (prevLineIdx == -1) {
|
||||
prevLineIdx = 0;
|
||||
}
|
||||
// Remove \n[....?\n]
|
||||
p_text.remove(prevLineIdx + 1, p_index - prevLineIdx + 1);
|
||||
return prevLineIdx;
|
||||
// Remove [\n....?]
|
||||
p_text.remove(prevLineIdx, p_index - prevLineIdx + 1);
|
||||
return prevLineIdx - 1;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include <QVector>
|
||||
#include <QString>
|
||||
#include <QColor>
|
||||
#include <QClipboard>
|
||||
#include "vtoc.h"
|
||||
#include "veditoperations.h"
|
||||
|
||||
@ -41,6 +42,7 @@ private slots:
|
||||
void highlightCurrentLine();
|
||||
void handleEditStateChanged(KeyState p_state);
|
||||
void handleSelectionChanged();
|
||||
void handleClipboardChanged(QClipboard::Mode p_mode);
|
||||
|
||||
protected:
|
||||
void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
|
||||
@ -71,6 +73,8 @@ private:
|
||||
// there is one and only one image link.
|
||||
QString fetchImageToPreview(const QString &p_text);
|
||||
void clearAllImagePreviewBlocks();
|
||||
// There is a QChar::ObjectReplacementCharacter in the selection. Find out the image path.
|
||||
QString selectedImage();
|
||||
|
||||
HGMarkdownHighlighter *m_mdHighlighter;
|
||||
QVector<QString> m_insertedImages;
|
||||
|
Loading…
x
Reference in New Issue
Block a user