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
|
QString VInsertImageDialog::getPathInput() const
|
||||||
{
|
{
|
||||||
return m_pathEdit->text();
|
if (m_tempFile.isNull()) {
|
||||||
|
return m_pathEdit->text();
|
||||||
|
} else {
|
||||||
|
return m_tempFile->fileName();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void VInsertImageDialog::handleBrowseBtnClicked()
|
void VInsertImageDialog::handleBrowseBtnClicked()
|
||||||
@ -163,8 +167,38 @@ void VInsertImageDialog::setImage(const QImage &image)
|
|||||||
|
|
||||||
void VInsertImageDialog::imageDownloaded(const QByteArray &data)
|
void VInsertImageDialog::imageDownloaded(const QByteArray &data)
|
||||||
{
|
{
|
||||||
m_imageType = ImageType::ImageData;
|
|
||||||
setImage(QImage::fromData(data));
|
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
|
QImage VInsertImageDialog::getImage() const
|
||||||
|
@ -5,6 +5,8 @@
|
|||||||
#include <QImage>
|
#include <QImage>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QByteArray>
|
#include <QByteArray>
|
||||||
|
#include <QTemporaryFile>
|
||||||
|
#include <QSharedPointer>
|
||||||
|
|
||||||
class QLabel;
|
class QLabel;
|
||||||
class VLineEdit;
|
class VLineEdit;
|
||||||
@ -71,6 +73,8 @@ private:
|
|||||||
bool m_browsable;
|
bool m_browsable;
|
||||||
|
|
||||||
ImageType m_imageType;
|
ImageType m_imageType;
|
||||||
|
|
||||||
|
QSharedPointer<QTemporaryFile> m_tempFile;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline VInsertImageDialog::ImageType VInsertImageDialog::getImageType() const
|
inline VInsertImageDialog::ImageType VInsertImageDialog::getImageType() const
|
||||||
|
@ -97,7 +97,8 @@ public:
|
|||||||
|
|
||||||
static QJsonObject readJsonFromDisk(const QString &p_filePath);
|
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");
|
const QString &format = "png");
|
||||||
|
|
||||||
// Given the file name @p_fileName and directory path @p_dirPath, generate
|
// Given the file name @p_fileName and directory path @p_dirPath, generate
|
||||||
|
@ -58,8 +58,10 @@ bool VMdEditOperations::insertImageFromMimeData(const QMimeData *source)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void VMdEditOperations::insertImageFromQImage(const QString &title, const QString &path,
|
void VMdEditOperations::insertImageFromQImage(const QString &title,
|
||||||
const QString &folderInLink, const QImage &image)
|
const QString &path,
|
||||||
|
const QString &folderInLink,
|
||||||
|
const QImage &image)
|
||||||
{
|
{
|
||||||
QString fileName = VUtils::generateImageFileName(path, title);
|
QString fileName = VUtils::generateImageFileName(path, title);
|
||||||
QString filePath = QDir(path).filePath(fileName);
|
QString filePath = QDir(path).filePath(fileName);
|
||||||
@ -184,10 +186,17 @@ bool VMdEditOperations::insertImageFromURL(const QUrl &imageUrl)
|
|||||||
m_file->getImageFolderInLink(),
|
m_file->getImageFolderInLink(),
|
||||||
imagePath);
|
imagePath);
|
||||||
} else {
|
} else {
|
||||||
insertImageFromQImage(dialog.getImageTitleInput(),
|
if (dialog.getImageType() == VInsertImageDialog::ImageType::LocalFile) {
|
||||||
m_file->fetchImageFolderPath(),
|
insertImageFromPath(dialog.getImageTitleInput(),
|
||||||
m_file->getImageFolderInLink(),
|
m_file->fetchImageFolderPath(),
|
||||||
dialog.getImage());
|
m_file->getImageFolderInLink(),
|
||||||
|
dialog.getPathInput());
|
||||||
|
} else {
|
||||||
|
insertImageFromQImage(dialog.getImageTitleInput(),
|
||||||
|
m_file->fetchImageFolderPath(),
|
||||||
|
m_file->getImageFolderInLink(),
|
||||||
|
dialog.getImage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -1822,5 +1822,4 @@ void VMdEditor::exportGraphAndCopy(const QString &p_lang,
|
|||||||
}
|
}
|
||||||
|
|
||||||
m_exportTempFile->close();
|
m_exportTempFile->close();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user