refine image hosting

This commit is contained in:
Le Tan 2019-11-26 20:06:24 +08:00
parent b97b908e0b
commit 8660668a94
11 changed files with 214 additions and 133 deletions

View File

@ -135,6 +135,7 @@ add_executable(VNote main.cpp
dialog/vinserttabledialog.cpp dialog/vinserttabledialog.cpp
isearchengine.cpp isearchengine.cpp
iuniversalentry.cpp iuniversalentry.cpp
vimagehosting.cpp
vnote.qrc translations.qrc) vnote.qrc translations.qrc)
# Qt5 libraries # Qt5 libraries
@ -206,4 +207,4 @@ elseif(DARWIN)
set_source_files_properties(${MACOSX_BUNDLE_ICON_FILE} PROPERTIES MACOSX_PACKAGE_LOCATION "Resources") set_source_files_properties(${MACOSX_BUNDLE_ICON_FILE} PROPERTIES MACOSX_PACKAGE_LOCATION "Resources")
endif() endif()
include(${CMAKE_CURRENT_LIST_DIR}/Packaging.cmake) include(${CMAKE_CURRENT_LIST_DIR}/Packaging.cmake)

View File

@ -1601,7 +1601,7 @@ VImageHostingTab::VImageHostingTab(QWidget *p_parent)
imageHostingTabWeg->addTab(githubImageHostingTab, tr("GitHub")); imageHostingTabWeg->addTab(githubImageHostingTab, tr("GitHub"));
imageHostingTabWeg->addTab(giteeImageHostingTab, tr("Gitee")); imageHostingTabWeg->addTab(giteeImageHostingTab, tr("Gitee"));
imageHostingTabWeg->addTab(wechatImageHostingTab, tr("WeChat")); imageHostingTabWeg->addTab(wechatImageHostingTab, tr("WeChat"));
imageHostingTabWeg->addTab(tencentImageHostingTab, tr("Tencent")); imageHostingTabWeg->addTab(tencentImageHostingTab, tr("Tencent Cloud"));
imageHostingTabWeg->setCurrentIndex(0); imageHostingTabWeg->setCurrentIndex(0);
// Set the tab of GitHub image Hosting // Set the tab of GitHub image Hosting
@ -1615,7 +1615,7 @@ VImageHostingTab::VImageHostingTab(QWidget *p_parent)
m_githubUserNameEdit->setToolTip(tr("User name of GitHub")); m_githubUserNameEdit->setToolTip(tr("User name of GitHub"));
m_githubUserNameEdit->setMinimumWidth(250 * VUtils::calculateScaleFactor()); m_githubUserNameEdit->setMinimumWidth(250 * VUtils::calculateScaleFactor());
m_githubKeepImgScaleCB = new QCheckBox(tr("Keep image scale(such as '=100x')")); m_githubKeepImgScaleCB = new QCheckBox(tr("Keep image scale(such as '=100x')"));
m_githubDoNotReplaceLinkCB = new QCheckBox(tr("Don't replace new image link")); m_githubDoNotReplaceLinkCB = new QCheckBox(tr("Copy the new content instead of replacing"));
QFormLayout *githubLayout = new QFormLayout(); QFormLayout *githubLayout = new QFormLayout();
githubLayout->addRow(tr("Personal access token:"), m_githubPersonalAccessTokenEdit); githubLayout->addRow(tr("Personal access token:"), m_githubPersonalAccessTokenEdit);
@ -1637,7 +1637,7 @@ VImageHostingTab::VImageHostingTab(QWidget *p_parent)
m_giteeUserNameEdit->setToolTip(tr("User name of Gitee")); m_giteeUserNameEdit->setToolTip(tr("User name of Gitee"));
m_giteeUserNameEdit->setMinimumWidth(250 * VUtils::calculateScaleFactor()); m_giteeUserNameEdit->setMinimumWidth(250 * VUtils::calculateScaleFactor());
m_giteeKeepImgScaleCB = new QCheckBox(tr("Keep image scale(such as '=100x')")); m_giteeKeepImgScaleCB = new QCheckBox(tr("Keep image scale(such as '=100x')"));
m_giteeDoNotReplaceLinkCB = new QCheckBox(tr("Don't replace new image link")); m_giteeDoNotReplaceLinkCB = new QCheckBox(tr("Copy the new content instead of replacing"));
QFormLayout *giteeLayout = new QFormLayout(); QFormLayout *giteeLayout = new QFormLayout();
giteeLayout->addRow(tr("Personal access token:"), m_giteePersonalAccessTokenEdit); giteeLayout->addRow(tr("Personal access token:"), m_giteePersonalAccessTokenEdit);
@ -1650,21 +1650,18 @@ VImageHostingTab::VImageHostingTab(QWidget *p_parent)
// Set the tab of Wechat image Hosting // Set the tab of Wechat image Hosting
m_wechatAppidEdit = new VLineEdit(); m_wechatAppidEdit = new VLineEdit();
m_wechatAppidEdit->setToolTip(tr("WeChat appid"));
m_wechatAppidEdit->setMinimumWidth(250 * VUtils::calculateScaleFactor()); m_wechatAppidEdit->setMinimumWidth(250 * VUtils::calculateScaleFactor());
m_wechatSecretEdit = new VLineEdit(); m_wechatSecretEdit = new VLineEdit();
m_wechatSecretEdit->setToolTip(tr("Please input wechat secret"));
m_wechatSecretEdit->setMinimumWidth(250 * VUtils::calculateScaleFactor()); m_wechatSecretEdit->setMinimumWidth(250 * VUtils::calculateScaleFactor());
m_markdown2WechatToolUrlEdit = new VLineEdit(); m_markdown2WechatToolUrlEdit = new VLineEdit();
m_markdown2WechatToolUrlEdit->setToolTip(tr("Please input markdown to wechat tool's url"));
m_markdown2WechatToolUrlEdit->setMinimumWidth(250 * VUtils::calculateScaleFactor()); m_markdown2WechatToolUrlEdit->setMinimumWidth(250 * VUtils::calculateScaleFactor());
m_wechatKeepImgScaleCB = new QCheckBox(tr("Keep image scale(such as '=100x')")); m_wechatKeepImgScaleCB = new QCheckBox(tr("Keep image scale(such as '=100x')"));
m_wechatDoNotReplaceLinkCB = new QCheckBox(tr("Don't replace new image link")); m_wechatDoNotReplaceLinkCB = new QCheckBox(tr("Copy the new content instead of replacing"));
QFormLayout *wechatLayout = new QFormLayout(); QFormLayout *wechatLayout = new QFormLayout();
wechatLayout->addRow(tr("appid:"), m_wechatAppidEdit); wechatLayout->addRow(tr("AppId:"), m_wechatAppidEdit);
wechatLayout->addRow(tr("secret:"), m_wechatSecretEdit); wechatLayout->addRow(tr("AppSecret:"), m_wechatSecretEdit);
wechatLayout->addRow(tr("markdown2WechatToolUrl"), m_markdown2WechatToolUrlEdit); wechatLayout->addRow(tr("markdown2WechatToolUrl:"), m_markdown2WechatToolUrlEdit);
wechatLayout->addRow(m_wechatKeepImgScaleCB); wechatLayout->addRow(m_wechatKeepImgScaleCB);
wechatLayout->addRow(m_wechatDoNotReplaceLinkCB); wechatLayout->addRow(m_wechatDoNotReplaceLinkCB);
@ -1675,18 +1672,16 @@ VImageHostingTab::VImageHostingTab(QWidget *p_parent)
m_tencentAccessDomainNameEdit->setToolTip(tr("Tencent access domain name")); m_tencentAccessDomainNameEdit->setToolTip(tr("Tencent access domain name"));
m_tencentAccessDomainNameEdit->setMinimumWidth(250 * VUtils::calculateScaleFactor()); m_tencentAccessDomainNameEdit->setMinimumWidth(250 * VUtils::calculateScaleFactor());
m_tencentSecretIdEdit = new VLineEdit(); m_tencentSecretIdEdit = new VLineEdit();
m_tencentSecretIdEdit->setToolTip(tr("Tencent secret id"));
m_tencentSecretIdEdit->setMinimumWidth(250 * VUtils::calculateScaleFactor()); m_tencentSecretIdEdit->setMinimumWidth(250 * VUtils::calculateScaleFactor());
m_tencentSecretKeyEdit = new VLineEdit(); m_tencentSecretKeyEdit = new VLineEdit();
m_tencentSecretKeyEdit->setToolTip(tr("Tencent secret key"));
m_tencentSecretKeyEdit->setMinimumWidth(250 * VUtils::calculateScaleFactor()); m_tencentSecretKeyEdit->setMinimumWidth(250 * VUtils::calculateScaleFactor());
m_tencentKeepImgScaleCB = new QCheckBox(tr("Keep image scale(such as '=100x')")); m_tencentKeepImgScaleCB = new QCheckBox(tr("Keep image scale(such as '=100x')"));
m_tencentDoNotReplaceLinkCB = new QCheckBox(tr("Don't replace new image link")); m_tencentDoNotReplaceLinkCB = new QCheckBox(tr("Copy the new content instead of replacing"));
QFormLayout *tencentLayout = new QFormLayout(); QFormLayout *tencentLayout = new QFormLayout();
tencentLayout->addRow(tr("accessDomainName"), m_tencentAccessDomainNameEdit); tencentLayout->addRow(tr("Access domain name:"), m_tencentAccessDomainNameEdit);
tencentLayout->addRow(tr("secretId"), m_tencentSecretIdEdit); tencentLayout->addRow(tr("SecretId:"), m_tencentSecretIdEdit);
tencentLayout->addRow(tr("secretKey"), m_tencentSecretKeyEdit); tencentLayout->addRow(tr("SecretKey:"), m_tencentSecretKeyEdit);
tencentLayout->addRow(m_tencentKeepImgScaleCB); tencentLayout->addRow(m_tencentKeepImgScaleCB);
tencentLayout->addRow(m_tencentDoNotReplaceLinkCB); tencentLayout->addRow(m_tencentDoNotReplaceLinkCB);

View File

@ -219,3 +219,10 @@ void VClipboardUtils::setImageAndLinkToClipboard(QClipboard *p_clipboard,
data, data,
p_mode); p_mode);
} }
void VClipboardUtils::setTextToClipboard(QClipboard *p_clipboard,
const QString &p_text,
QClipboard::Mode p_mode)
{
p_clipboard->setText(p_text, p_mode);
}

View File

@ -34,6 +34,10 @@ public:
const QString &p_link, const QString &p_link,
QClipboard::Mode p_mode = QClipboard::Clipboard); QClipboard::Mode p_mode = QClipboard::Clipboard);
static void setTextToClipboard(QClipboard *p_clipboard,
const QString &p_text,
QClipboard::Mode p_mode = QClipboard::Clipboard);
private: private:
VClipboardUtils() VClipboardUtils()
{ {

View File

@ -1,10 +1,12 @@
#include "vimagehosting.h" #include "vimagehosting.h"
#include "utils/vutils.h" #include "utils/vutils.h"
#include "vedittab.h" #include "veditor.h"
#include "vfile.h"
#include "utils/vclipboardutils.h"
extern VConfigManager *g_config; extern VConfigManager *g_config;
VGithubImageHosting::VGithubImageHosting(VFile *p_file, QWidget *p_parent) VGithubImageHosting::VGithubImageHosting(VFile *p_file, QObject *p_parent)
:QObject(p_parent), :QObject(p_parent),
m_file(p_file) m_file(p_file)
{ {
@ -109,8 +111,8 @@ void VGithubImageHosting::githubImageBedAuthFinished()
} }
else else
{ {
qDebug() << m_file->getName() << " No images to upload"; qDebug() << m_file->getName() << " No local images to upload";
QString info = m_file->getName() + " No pictures to upload"; QString info = tr("No local images to upload: %1").arg(m_file->getName());
QMessageBox::information(nullptr, tr("Github Image Hosting"), info); QMessageBox::information(nullptr, tr("Github Image Hosting"), info);
} }
} }
@ -139,7 +141,7 @@ void VGithubImageHosting::githubImageBedUploadManager()
{ {
imageToUpload = it.key(); imageToUpload = it.key();
proDlg->setValue(uploadImageCount - 1 - uploadImageCountIndex); proDlg->setValue(uploadImageCount - 1 - uploadImageCountIndex);
proDlg->setLabelText(tr("Uploaading image: %1").arg(imageToUpload)); proDlg->setLabelText(tr("Uploading image: %1").arg(imageToUpload));
break; break;
} }
} }
@ -372,6 +374,8 @@ void VGithubImageHosting::githubImageBedUploadFinished()
void VGithubImageHosting::githubImageBedReplaceLink(QString p_fileContent, const QString p_filePath) void VGithubImageHosting::githubImageBedReplaceLink(QString p_fileContent, const QString p_filePath)
{ {
Q_UNUSED(p_filePath);
// This function must be executed when the upload is completed or fails in the middle. // This function must be executed when the upload is completed or fails in the middle.
if(!g_config->getGithubKeepImgScale()) if(!g_config->getGithubKeepImgScale())
{ {
@ -379,22 +383,14 @@ void VGithubImageHosting::githubImageBedReplaceLink(QString p_fileContent, const
p_fileContent.replace(QRegExp("\\s+=\\d+x"),""); p_fileContent.replace(QRegExp("\\s+=\\d+x"),"");
} }
if(!g_config->getGithubDoNotReplaceLink()) if(!g_config->getGithubDoNotReplaceLink()) {
{
// Write content to file. // Write content to file.
QFile file(p_filePath); m_editor->setContent(p_fileContent, true);
file.open(QIODevice::WriteOnly | QIODevice::Text); } else {
file.write(p_fileContent.toUtf8()); VClipboardUtils::setTextToClipboard(QApplication::clipboard(), p_fileContent);
file.close(); emit m_editor->object()->statusMessage(tr("Copied contents with new image links"));
} }
// Write content to clipboard.
QClipboard *board = QApplication::clipboard();
board->setText(p_fileContent);
QMessageBox::warning(nullptr,
tr("Github Image Hosting"),
tr("The article has been copied to the clipboard!"));
// Reset. // Reset.
imageUrlMap.clear(); imageUrlMap.clear();
imageUploaded = false; imageUploaded = false;
@ -421,7 +417,12 @@ QString VGithubImageHosting::githubImageBedGenerateParam(const QString p_imagePa
return jsonStr; return jsonStr;
} }
VGiteeImageHosting::VGiteeImageHosting(VFile *p_file, QWidget *p_parent) void VGithubImageHosting::setEditor(VEditor *p_editor)
{
m_editor = p_editor;
}
VGiteeImageHosting::VGiteeImageHosting(VFile *p_file, QObject *p_parent)
:QObject(p_parent), :QObject(p_parent),
m_file(p_file) m_file(p_file)
{ {
@ -525,7 +526,7 @@ void VGiteeImageHosting::giteeImageBedAuthFinished()
else else
{ {
qDebug() << m_file->getName() << " No images to upload"; qDebug() << m_file->getName() << " No images to upload";
QString info = m_file->getName() + " No pictures to upload"; QString info = tr("No local images to upload: %1").arg(m_file->getName());
QMessageBox::information(nullptr, tr("Gitee Image Hosting"), info); QMessageBox::information(nullptr, tr("Gitee Image Hosting"), info);
} }
break; break;
@ -565,7 +566,7 @@ void VGiteeImageHosting::giteeImageBedUploadManager()
{ {
imageToUpload = it.key(); imageToUpload = it.key();
proDlg->setValue(uploadImageCount - 1 - uploadImageCountIndex); proDlg->setValue(uploadImageCount - 1 - uploadImageCountIndex);
proDlg->setLabelText(tr("Uploaading image: %1").arg(imageToUpload)); proDlg->setLabelText(tr("Uploading image: %1").arg(imageToUpload));
break; break;
} }
} }
@ -794,8 +795,10 @@ void VGiteeImageHosting::giteeImageBedUploadFinished()
} }
} }
void VGiteeImageHosting::giteeImageBedReplaceLink(QString p_fileContent, const QString p_file_path) void VGiteeImageHosting::giteeImageBedReplaceLink(QString p_fileContent, const QString p_filePath)
{ {
Q_UNUSED(p_filePath);
// This function must be executed when the upload is completed or fails in the middle. // This function must be executed when the upload is completed or fails in the middle.
if(!g_config->getGiteeKeepImgScale()) if(!g_config->getGiteeKeepImgScale())
{ {
@ -806,19 +809,12 @@ void VGiteeImageHosting::giteeImageBedReplaceLink(QString p_fileContent, const Q
if(!g_config->getGiteeDoNotReplaceLink()) if(!g_config->getGiteeDoNotReplaceLink())
{ {
// Write content to file. // Write content to file.
QFile file(p_file_path); m_editor->setContent(p_fileContent, true);
file.open(QIODevice::WriteOnly | QIODevice::Text); } else {
file.write(p_fileContent.toUtf8()); VClipboardUtils::setTextToClipboard(QApplication::clipboard(), p_fileContent);
file.close(); emit m_editor->object()->statusMessage(tr("Copied contents with new image links"));
} }
// Write content to clipboard.
QClipboard *board = QApplication::clipboard();
board->setText(p_fileContent);
QMessageBox::warning(nullptr,
tr("Gitee Image Hosting"),
tr("The article has been copied to the clipboard!"));
// Reset. // Reset.
imageUrlMap.clear(); imageUrlMap.clear();
imageUploaded = false; imageUploaded = false;
@ -846,7 +842,12 @@ QString VGiteeImageHosting::giteeImageBedGenerateParam(const QString &p_imagePat
return jsonStr; return jsonStr;
} }
VWechatImageHosting::VWechatImageHosting(VFile *p_file, QWidget *p_parent) void VGiteeImageHosting::setEditor(VEditor *p_editor)
{
m_editor = p_editor;
}
VWechatImageHosting::VWechatImageHosting(VFile *p_file, QObject *p_parent)
:QObject(p_parent), :QObject(p_parent),
m_file(p_file) m_file(p_file)
{ {
@ -953,7 +954,7 @@ void VWechatImageHosting::wechatImageBedAuthFinished()
else else
{ {
qDebug() << m_file->getName() << " No pictures to upload"; qDebug() << m_file->getName() << " No pictures to upload";
QString info = m_file->getName() + tr(" No pictures to upload"); QString info = tr("No local images to upload: %1").arg(m_file->getName());
QMessageBox::information(nullptr, tr("Wechat Image Hosting"), info); QMessageBox::information(nullptr, tr("Wechat Image Hosting"), info);
} }
} }
@ -1023,7 +1024,7 @@ void VWechatImageHosting::wechatImageBedUploadManager()
{ {
image_to_upload = it.key(); image_to_upload = it.key();
proDlg->setValue(uploadImageCount - 1 - uploadImageCountIndex); proDlg->setValue(uploadImageCount - 1 - uploadImageCountIndex);
proDlg->setLabelText(tr("Uploaading image: %1").arg(image_to_upload)); proDlg->setLabelText(tr("Uploading image: %1").arg(image_to_upload));
break; break;
} }
} }
@ -1200,6 +1201,8 @@ void VWechatImageHosting::wechatImageBedUploadFinished()
void VWechatImageHosting::wechatImageBedReplaceLink(QString &p_fileContent, const QString &p_filePath) void VWechatImageHosting::wechatImageBedReplaceLink(QString &p_fileContent, const QString &p_filePath)
{ {
Q_UNUSED(p_filePath);
if(!g_config->getWechatKeepImgScale()) if(!g_config->getWechatKeepImgScale())
{ {
// delete image scale // delete image scale
@ -1209,30 +1212,21 @@ void VWechatImageHosting::wechatImageBedReplaceLink(QString &p_fileContent, cons
if(!g_config->getWechatDoNotReplaceLink()) if(!g_config->getWechatDoNotReplaceLink())
{ {
// Write content to file. // Write content to file.
QFile file(p_filePath); m_editor->setContent(p_fileContent, true);
file.open(QIODevice::WriteOnly | QIODevice::Text);
file.write(p_fileContent.toUtf8());
file.close();
} }
// Write content to clipboard. VClipboardUtils::setTextToClipboard(QApplication::clipboard(), p_fileContent);
QClipboard *board = QApplication::clipboard(); emit m_editor->object()->statusMessage(tr("Copied contents with new image links"));
board->setText(p_fileContent);
QString url = g_config->getMarkdown2WechatToolUrl(); QString url = g_config->getMarkdown2WechatToolUrl();
if(url.isEmpty()) if(!url.isEmpty()) {
{
QMessageBox::warning(nullptr,
tr("Wechat Image Hosting"),
tr("The article has been copied to the clipboard. Please find a text file and save it!!"));
}
else
{
QMessageBox::StandardButton result; QMessageBox::StandardButton result;
result = QMessageBox::question(nullptr, result = QMessageBox::question(nullptr,
tr("Wechat Image Hosting"), tr("Wechat Image Hosting"),
tr("The article has been copied to the clipboard.") + tr("Contents with new image links are copied. ") +
tr("Do you want to open the tool link of mark down to wechat?"), tr("Do you want to open the tool link of mark down to wechat?"),
QMessageBox::Yes|QMessageBox::No,QMessageBox::Yes); QMessageBox::Yes | QMessageBox::No,
QMessageBox::Yes);
if(result == QMessageBox::Yes) if(result == QMessageBox::Yes)
{ {
QDesktopServices::openUrl(QUrl(url)); QDesktopServices::openUrl(QUrl(url));
@ -1244,7 +1238,12 @@ void VWechatImageHosting::wechatImageBedReplaceLink(QString &p_fileContent, cons
imageUploaded = false; imageUploaded = false;
} }
VTencentImageHosting::VTencentImageHosting(VFile *p_file, QWidget *p_parent) void VWechatImageHosting::setEditor(VEditor *p_editor)
{
m_editor = p_editor;
}
VTencentImageHosting::VTencentImageHosting(VFile *p_file, QObject *p_parent)
:QObject(p_parent), :QObject(p_parent),
m_file(p_file) m_file(p_file)
{ {
@ -1313,7 +1312,7 @@ void VTencentImageHosting::findAndStartUploadImage()
else else
{ {
qDebug() << m_file->getName() << " No images to upload"; qDebug() << m_file->getName() << " No images to upload";
QString info = m_file->getName() + " No pictures to upload"; QString info = tr("No local images to upload: %1").arg(m_file->getName());
QMessageBox::information(nullptr, tr("Tencent Image Hosting"), info); QMessageBox::information(nullptr, tr("Tencent Image Hosting"), info);
} }
} }
@ -1331,7 +1330,7 @@ void VTencentImageHosting::tencentImageBedUploadManager()
{ {
image_to_upload = it.key(); image_to_upload = it.key();
proDlg->setValue(uploadImageCount - 1 - uploadImageCountIndex); proDlg->setValue(uploadImageCount - 1 - uploadImageCountIndex);
proDlg->setLabelText(tr("Uploaading image: %1").arg(image_to_upload)); proDlg->setLabelText(tr("Uploading image: %1").arg(image_to_upload));
break; break;
} }
} }
@ -1525,6 +1524,8 @@ void VTencentImageHosting::tencentImageBedUploadFinished()
void VTencentImageHosting::tencentImageBedReplaceLink(QString &p_fileContent, const QString &p_filePath) void VTencentImageHosting::tencentImageBedReplaceLink(QString &p_fileContent, const QString &p_filePath)
{ {
Q_UNUSED(p_filePath);
if(!g_config->getTencentKeepImgScale()) if(!g_config->getTencentKeepImgScale())
{ {
// delete image scale // delete image scale
@ -1534,19 +1535,12 @@ void VTencentImageHosting::tencentImageBedReplaceLink(QString &p_fileContent, co
if(!g_config->getTencentDoNotReplaceLink()) if(!g_config->getTencentDoNotReplaceLink())
{ {
// Write content to file. // Write content to file.
QFile file(p_filePath); m_editor->setContent(p_fileContent, true);
file.open(QIODevice::WriteOnly | QIODevice::Text); } else {
file.write(p_fileContent.toUtf8()); VClipboardUtils::setTextToClipboard(QApplication::clipboard(), p_fileContent);
file.close(); emit m_editor->object()->statusMessage(tr("Copied contents with new image links"));
} }
// Write content to clipboard.
QClipboard *board = QApplication::clipboard();
board->setText(p_fileContent);
QMessageBox::warning(nullptr,
tr("Tencent Image Hosting"),
tr("The article has been copied to the clipboard!!"));
// Reset. // Reset.
imageUrlMap.clear(); imageUrlMap.clear();
imageUploaded = false; imageUploaded = false;
@ -1615,3 +1609,8 @@ QByteArray VTencentImageHosting::getImgContent(const QString &p_imagePath)
file.close(); file.close();
return fdata; return fdata;
} }
void VTencentImageHosting::setEditor(VEditor *p_editor)
{
m_editor = p_editor;
}

View File

@ -11,16 +11,18 @@
#include <QNetworkRequest> #include <QNetworkRequest>
#include <QNetworkReply> #include <QNetworkReply>
#include <QApplication> #include <QApplication>
#include <vfile.h>
#include <QClipboard> #include <QClipboard>
#include <QCryptographicHash> #include <QCryptographicHash>
#include <QRegExp> #include <QRegExp>
class VEditor;
class VFile;
class VGithubImageHosting : public QObject class VGithubImageHosting : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit VGithubImageHosting(VFile *p_file, QWidget *p_parent = nullptr); explicit VGithubImageHosting(VFile *p_file, QObject *p_parent = nullptr);
// GitHub identity authentication. // GitHub identity authentication.
void authenticateGithubImageHosting(QString p_token); void authenticateGithubImageHosting(QString p_token);
@ -43,6 +45,8 @@ public:
// Process the image upload request to GitHub. // Process the image upload request to GitHub.
void handleUploadImageToGithubRequested(); void handleUploadImageToGithubRequested();
void setEditor(VEditor *p_editor);
public slots: public slots:
// GitHub image hosting identity authentication completed. // GitHub image hosting identity authentication completed.
void githubImageBedAuthFinished(); void githubImageBedAuthFinished();
@ -70,13 +74,15 @@ private:
// Image upload status. // Image upload status.
bool uploadImageStatus; bool uploadImageStatus;
VFile *m_file; VFile *m_file;
VEditor *m_editor = nullptr;
}; };
class VGiteeImageHosting : public QObject class VGiteeImageHosting : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit VGiteeImageHosting(VFile *p_file, QWidget *p_parent = nullptr); explicit VGiteeImageHosting(VFile *p_file, QObject *p_parent = nullptr);
// GitHub identity authentication. // GitHub identity authentication.
void authenticateGiteeImageHosting(const QString &p_username, void authenticateGiteeImageHosting(const QString &p_username,
@ -101,6 +107,8 @@ public:
// Process the image upload request to Gitee. // Process the image upload request to Gitee.
void handleUploadImageToGiteeRequested(); void handleUploadImageToGiteeRequested();
void setEditor(VEditor *p_editor);
public slots: public slots:
// Gitee image hosting identity authentication completed. // Gitee image hosting identity authentication completed.
void giteeImageBedAuthFinished(); void giteeImageBedAuthFinished();
@ -128,13 +136,15 @@ private:
// Image upload status. // Image upload status.
bool uploadImageStatus; bool uploadImageStatus;
VFile *m_file; VFile *m_file;
VEditor *m_editor = nullptr;
}; };
class VWechatImageHosting : public QObject class VWechatImageHosting : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit VWechatImageHosting(VFile *p_file, QWidget *p_parent = nullptr); explicit VWechatImageHosting(VFile *p_file, QObject *p_parent = nullptr);
// Wechat identity authentication. // Wechat identity authentication.
void authenticateWechatImageHosting(const QString p_appid, const QString p_secret); void authenticateWechatImageHosting(const QString p_appid, const QString p_secret);
@ -151,6 +161,8 @@ public:
// Process image upload request to wechat. // Process image upload request to wechat.
void handleUploadImageToWechatRequested(); void handleUploadImageToWechatRequested();
void setEditor(VEditor *p_editor);
public slots: public slots:
// Wechat mage hosting identity authentication completed. // Wechat mage hosting identity authentication completed.
void wechatImageBedAuthFinished(); void wechatImageBedAuthFinished();
@ -182,13 +194,15 @@ private:
// Relative image path currently Uploaded. // Relative image path currently Uploaded.
QString currentUploadRelativeImagePah; QString currentUploadRelativeImagePah;
VFile *m_file; VFile *m_file;
VEditor *m_editor = nullptr;
}; };
class VTencentImageHosting : public QObject class VTencentImageHosting : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit VTencentImageHosting(VFile *p_file, QWidget *p_parent = nullptr); explicit VTencentImageHosting(VFile *p_file, QObject *p_parent = nullptr);
QByteArray hmacSha1(const QByteArray &p_key, const QByteArray &p_baseString); QByteArray hmacSha1(const QByteArray &p_key, const QByteArray &p_baseString);
@ -215,6 +229,8 @@ public:
// Process image upload request to tencent. // Process image upload request to tencent.
void handleUploadImageToTencentRequested(); void handleUploadImageToTencentRequested();
void setEditor(VEditor *p_editor);
public slots: public slots:
// Tencent image hosting upload completed. // Tencent image hosting upload completed.
void tencentImageBedUploadFinished(); void tencentImageBedUploadFinished();
@ -243,6 +259,9 @@ private:
// Relative image path currently Uploaded. // Relative image path currently Uploaded.
QString currentUploadRelativeImagePah; QString currentUploadRelativeImagePah;
QString new_file_name; QString new_file_name;
VFile *m_file; VFile *m_file;
VEditor *m_editor = nullptr;
}; };
#endif // VGITHUBIMAGEHOSTING_H #endif // VGITHUBIMAGEHOSTING_H

View File

@ -430,6 +430,8 @@ void VMdEditor::contextMenuEvent(QContextMenuEvent *p_event)
} }
initAttachmentMenu(menu.data()); initAttachmentMenu(menu.data());
initImageHostingMenu(menu.data());
} }
menu->exec(p_event->globalPos()); menu->exec(p_event->globalPos());
@ -2277,3 +2279,31 @@ void VMdEditor::insertTable()
// Insert table right at cursor. // Insert table right at cursor.
m_tableHelper->insertTable(rowCount, colCount, alignment); m_tableHelper->insertTable(rowCount, colCount, alignment);
} }
void VMdEditor::initImageHostingMenu(QMenu *p_menu)
{
QMenu *uploadImageMenu = new QMenu(tr("&Upload Image To"), p_menu);
// Upload the image to GitHub image hosting.
QAction *uploadImageToGithub = new QAction(tr("&GitHub"), uploadImageMenu);
connect(uploadImageToGithub, &QAction::triggered, this, &VMdEditor::requestUploadImageToGithub);
uploadImageMenu->addAction(uploadImageToGithub);
// Upload the image to Gitee image hosting.
QAction *uploadImageToGitee = new QAction(tr("&Gitee"), uploadImageMenu);
connect(uploadImageToGitee, &QAction::triggered, this, &VMdEditor::requestUploadImageToGitee);
uploadImageMenu->addAction(uploadImageToGitee);
// Upload the image to Wechat image hosting
QAction *uploadImageToWechat = new QAction(tr("&Wechat"), uploadImageMenu);
connect(uploadImageToWechat, &QAction::triggered, this, &VMdEditor::requestUploadImageToWechat);
uploadImageMenu->addAction(uploadImageToWechat);
// Upload the image to Tencent image hosting.
QAction *uploadImageToTencent = new QAction(tr("&Tencent"), uploadImageMenu);
connect(uploadImageToTencent, &QAction::triggered, this, &VMdEditor::requestUploadImageToTencent);
uploadImageMenu->addAction(uploadImageToTencent);
p_menu->addSeparator();
p_menu->addMenu(uploadImageMenu);
}

View File

@ -233,6 +233,14 @@ signals:
// Request to convert @p_html to Markdown text. // Request to convert @p_html to Markdown text.
void requestHtmlToText(const QString &p_html, int p_id, int p_timeStamp); void requestHtmlToText(const QString &p_html, int p_id, int p_timeStamp);
void requestUploadImageToGithub();
void requestUploadImageToGitee();
void requestUploadImageToWechat();
void requestUploadImageToTencent();
protected: protected:
void contextMenuEvent(QContextMenuEvent *p_event) Q_DECL_OVERRIDE; void contextMenuEvent(QContextMenuEvent *p_event) Q_DECL_OVERRIDE;
@ -309,6 +317,8 @@ private:
void initAttachmentMenu(QMenu *p_menu); void initAttachmentMenu(QMenu *p_menu);
void initImageHostingMenu(QMenu *p_menu);
void insertImageLink(const QString &p_text, const QString &p_url); void insertImageLink(const QString &p_text, const QString &p_url);
void setFontPointSizeByStyleSheet(int p_ptSize); void setFontPointSizeByStyleSheet(int p_ptSize);

View File

@ -450,15 +450,6 @@ void VMdTab::setupMarkdownViewer()
connect(m_webViewer, &VWebView::requestExpandRestorePreviewArea, connect(m_webViewer, &VWebView::requestExpandRestorePreviewArea,
this, &VMdTab::expandRestorePreviewArea); this, &VMdTab::expandRestorePreviewArea);
connect(m_webViewer, &VWebView::requestUploadImageToGithub,
this, &VMdTab::handleUploadImageToGithubRequested);
connect(m_webViewer, &VWebView::requestUploadImageToGitee,
this, &VMdTab::handleUploadImageToGiteeRequested);
connect(m_webViewer, &VWebView::requestUploadImageToWechat,
this, &VMdTab::handleUploadImageToWechatRequested);
connect(m_webViewer, &VWebView::requestUploadImageToTencent,
this, &VMdTab::handleUploadImageToTencentRequested);
VPreviewPage *page = new VPreviewPage(m_webViewer); VPreviewPage *page = new VPreviewPage(m_webViewer);
m_webViewer->setPage(page); m_webViewer->setPage(page);
m_webViewer->setZoomFactor(g_config->getWebZoomFactor()); m_webViewer->setZoomFactor(g_config->getWebZoomFactor());
@ -591,6 +582,15 @@ void VMdTab::setupMarkdownEditor()
connect(m_editor, &VMdEditor::requestHtmlToText, connect(m_editor, &VMdEditor::requestHtmlToText,
this, &VMdTab::htmlToTextViaWebView); this, &VMdTab::htmlToTextViaWebView);
connect(m_editor, &VMdEditor::requestUploadImageToGithub,
this, &VMdTab::handleUploadImageToGithubRequested);
connect(m_editor, &VMdEditor::requestUploadImageToGitee,
this, &VMdTab::handleUploadImageToGiteeRequested);
connect(m_editor, &VMdEditor::requestUploadImageToWechat,
this, &VMdTab::handleUploadImageToWechatRequested);
connect(m_editor, &VMdEditor::requestUploadImageToTencent,
this, &VMdTab::handleUploadImageToTencentRequested);
if (m_editor->getVim()) { if (m_editor->getVim()) {
connect(m_editor->getVim(), &VVim::commandLineTriggered, connect(m_editor->getVim(), &VVim::commandLineTriggered,
this, [this](VVim::CommandLineType p_type) { this, [this](VVim::CommandLineType p_type) {
@ -625,6 +625,11 @@ void VMdTab::setupMarkdownEditor()
connect(m_mathjaxPreviewHelper, &VMathJaxInplacePreviewHelper::checkBlocksForObsoletePreview, connect(m_mathjaxPreviewHelper, &VMathJaxInplacePreviewHelper::checkBlocksForObsoletePreview,
m_editor->getPreviewManager(), &VPreviewManager::checkBlocksForObsoletePreview); m_editor->getPreviewManager(), &VPreviewManager::checkBlocksForObsoletePreview);
m_mathjaxPreviewHelper->setEnabled(m_editor->getPreviewManager()->isPreviewEnabled()); m_mathjaxPreviewHelper->setEnabled(m_editor->getPreviewManager()->isPreviewEnabled());
vGithubImageHosting->setEditor(m_editor);
vGiteeImageHosting->setEditor(m_editor);
vWechatImageHosting->setEditor(m_editor);
vTencentImageHosting->setEditor(m_editor);
} }
void VMdTab::updateOutlineFromHtml(const QString &p_tocHtml) void VMdTab::updateOutlineFromHtml(const QString &p_tocHtml)
@ -1520,21 +1525,65 @@ void VMdTab::handleSavePageRequested()
void VMdTab::handleUploadImageToGithubRequested() void VMdTab::handleUploadImageToGithubRequested()
{ {
vGithubImageHosting->handleUploadImageToGithubRequested(); if (isModified()) {
VUtils::showMessage(QMessageBox::Information,
tr("Information"),
tr("Please save changes to file before uploading images."),
"",
QMessageBox::Ok,
QMessageBox::Ok,
this);
return;
}
vGithubImageHosting->handleUploadImageToGithubRequested();
} }
void VMdTab::handleUploadImageToGiteeRequested() void VMdTab::handleUploadImageToGiteeRequested()
{ {
vGiteeImageHosting->handleUploadImageToGiteeRequested(); if (isModified()) {
VUtils::showMessage(QMessageBox::Information,
tr("Information"),
tr("Please save changes to file before uploading images."),
"",
QMessageBox::Ok,
QMessageBox::Ok,
this);
return;
}
vGiteeImageHosting->handleUploadImageToGiteeRequested();
} }
void VMdTab::handleUploadImageToWechatRequested() void VMdTab::handleUploadImageToWechatRequested()
{ {
if (isModified()) {
VUtils::showMessage(QMessageBox::Information,
tr("Information"),
tr("Please save changes to file before uploading images."),
"",
QMessageBox::Ok,
QMessageBox::Ok,
this);
return;
}
vWechatImageHosting->handleUploadImageToWechatRequested(); vWechatImageHosting->handleUploadImageToWechatRequested();
} }
void VMdTab::handleUploadImageToTencentRequested() void VMdTab::handleUploadImageToTencentRequested()
{ {
if (isModified()) {
VUtils::showMessage(QMessageBox::Information,
tr("Information"),
tr("Please save changes to file before uploading images."),
"",
QMessageBox::Ok,
QMessageBox::Ok,
this);
return;
}
vTencentImageHosting->handleUploadImageToTencentRequested(); vTencentImageHosting->handleUploadImageToTencentRequested();
} }

View File

@ -98,31 +98,6 @@ void VWebView::contextMenuEvent(QContextMenuEvent *p_event)
connect(savePageAct, &QAction::triggered, connect(savePageAct, &QAction::triggered,
this, &VWebView::requestSavePage); this, &VWebView::requestSavePage);
menu->addAction(savePageAct); menu->addAction(savePageAct);
// In preview mode, add the right-click upload menu.
QMenu *uploadImageMenu = new QMenu(tr("&Upload Image To"), menu);
// Upload the image to GitHub image hosting.
QAction *uploadImageToGithub = new QAction(tr("&GitHub"), uploadImageMenu);
connect(uploadImageToGithub, &QAction::triggered, this, &VWebView::requestUploadImageToGithub);
uploadImageMenu->addAction(uploadImageToGithub);
// Upload the image to Gitee image hosting.
QAction *uploadImageToGitee = new QAction(tr("&Gitee"), uploadImageMenu);
connect(uploadImageToGitee, &QAction::triggered, this, &VWebView::requestUploadImageToGitee);
uploadImageMenu->addAction(uploadImageToGitee);
// Upload the image to Wechat image hosting
QAction *uploadImageToWechat = new QAction(tr("&Wechat"), uploadImageMenu);
connect(uploadImageToWechat, &QAction::triggered, this, &VWebView::requestUploadImageToWechat);
uploadImageMenu->addAction(uploadImageToWechat);
// Upload the image to Tencent image hosting.
QAction *uploadImageToTencent = new QAction(tr("&Tencent"), uploadImageMenu);
connect(uploadImageToTencent, &QAction::triggered, this, &VWebView::requestUploadImageToTencent);
uploadImageMenu->addAction(uploadImageToTencent);
menu->addMenu(uploadImageMenu);
} }
} }

View File

@ -24,14 +24,6 @@ signals:
void requestExpandRestorePreviewArea(); void requestExpandRestorePreviewArea();
void requestUploadImageToGithub();
void requestUploadImageToGitee();
void requestUploadImageToWechat();
void requestUploadImageToTencent();
protected: protected:
void contextMenuEvent(QContextMenuEvent *p_event); void contextMenuEvent(QContextMenuEvent *p_event);