mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
MdEditor: insert image from network via tempoary file
This commit is contained in:
parent
cda48a612a
commit
7c7b9e8a3d
@ -107,7 +107,11 @@ QString VInsertImageDialog::getImageTitleInput() const
|
||||
|
||||
QString VInsertImageDialog::getPathInput() const
|
||||
{
|
||||
return m_pathEdit->text();
|
||||
if (m_tempFile.isNull()) {
|
||||
return m_pathEdit->text();
|
||||
} else {
|
||||
return m_tempFile->fileName();
|
||||
}
|
||||
}
|
||||
|
||||
void VInsertImageDialog::handleBrowseBtnClicked()
|
||||
@ -163,8 +167,38 @@ void VInsertImageDialog::setImage(const QImage &image)
|
||||
|
||||
void VInsertImageDialog::imageDownloaded(const QByteArray &data)
|
||||
{
|
||||
m_imageType = ImageType::ImageData;
|
||||
setImage(QImage::fromData(data));
|
||||
|
||||
// Try to save it to a temp file.
|
||||
{
|
||||
if (data.isEmpty()) {
|
||||
goto image_data;
|
||||
}
|
||||
|
||||
QString format = QFileInfo(getPathInput()).suffix();
|
||||
if (format.isEmpty()) {
|
||||
goto image_data;
|
||||
}
|
||||
|
||||
m_tempFile.reset(new QTemporaryFile(QDir::tempPath()
|
||||
+ QDir::separator()
|
||||
+ "XXXXXX." + format));
|
||||
if (!m_tempFile->open()) {
|
||||
goto image_data;
|
||||
}
|
||||
|
||||
if (m_tempFile->write(data) == -1) {
|
||||
goto image_data;
|
||||
}
|
||||
|
||||
m_imageType = ImageType::LocalFile;
|
||||
m_tempFile->close();
|
||||
return;
|
||||
}
|
||||
|
||||
image_data:
|
||||
m_tempFile.clear();
|
||||
m_imageType = ImageType::ImageData;
|
||||
}
|
||||
|
||||
QImage VInsertImageDialog::getImage() const
|
||||
|
@ -5,6 +5,8 @@
|
||||
#include <QImage>
|
||||
#include <QString>
|
||||
#include <QByteArray>
|
||||
#include <QTemporaryFile>
|
||||
#include <QSharedPointer>
|
||||
|
||||
class QLabel;
|
||||
class VLineEdit;
|
||||
@ -71,6 +73,8 @@ private:
|
||||
bool m_browsable;
|
||||
|
||||
ImageType m_imageType;
|
||||
|
||||
QSharedPointer<QTemporaryFile> m_tempFile;
|
||||
};
|
||||
|
||||
inline VInsertImageDialog::ImageType VInsertImageDialog::getImageType() const
|
||||
|
@ -97,7 +97,8 @@ public:
|
||||
|
||||
static QJsonObject readJsonFromDisk(const QString &p_filePath);
|
||||
|
||||
static QString generateImageFileName(const QString &path, const QString &title,
|
||||
static QString generateImageFileName(const QString &path,
|
||||
const QString &title,
|
||||
const QString &format = "png");
|
||||
|
||||
// Given the file name @p_fileName and directory path @p_dirPath, generate
|
||||
|
@ -58,8 +58,10 @@ bool VMdEditOperations::insertImageFromMimeData(const QMimeData *source)
|
||||
return true;
|
||||
}
|
||||
|
||||
void VMdEditOperations::insertImageFromQImage(const QString &title, const QString &path,
|
||||
const QString &folderInLink, const QImage &image)
|
||||
void VMdEditOperations::insertImageFromQImage(const QString &title,
|
||||
const QString &path,
|
||||
const QString &folderInLink,
|
||||
const QImage &image)
|
||||
{
|
||||
QString fileName = VUtils::generateImageFileName(path, title);
|
||||
QString filePath = QDir(path).filePath(fileName);
|
||||
@ -184,10 +186,17 @@ bool VMdEditOperations::insertImageFromURL(const QUrl &imageUrl)
|
||||
m_file->getImageFolderInLink(),
|
||||
imagePath);
|
||||
} else {
|
||||
insertImageFromQImage(dialog.getImageTitleInput(),
|
||||
m_file->fetchImageFolderPath(),
|
||||
m_file->getImageFolderInLink(),
|
||||
dialog.getImage());
|
||||
if (dialog.getImageType() == VInsertImageDialog::ImageType::LocalFile) {
|
||||
insertImageFromPath(dialog.getImageTitleInput(),
|
||||
m_file->fetchImageFolderPath(),
|
||||
m_file->getImageFolderInLink(),
|
||||
dialog.getPathInput());
|
||||
} else {
|
||||
insertImageFromQImage(dialog.getImageTitleInput(),
|
||||
m_file->fetchImageFolderPath(),
|
||||
m_file->getImageFolderInLink(),
|
||||
dialog.getImage());
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
@ -1822,5 +1822,4 @@ void VMdEditor::exportGraphAndCopy(const QString &p_lang,
|
||||
}
|
||||
|
||||
m_exportTempFile->close();
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user