Browse Source

refine image hosting

Le Tan 6 years ago
parent
commit
8660668a94

+ 2 - 1
src/CMakeLists.txt

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

+ 11 - 16
src/dialog/vsettingsdialog.cpp

@@ -1601,7 +1601,7 @@ VImageHostingTab::VImageHostingTab(QWidget *p_parent)
     imageHostingTabWeg->addTab(githubImageHostingTab, tr("GitHub"));
     imageHostingTabWeg->addTab(giteeImageHostingTab, tr("Gitee"));
     imageHostingTabWeg->addTab(wechatImageHostingTab, tr("WeChat"));
-    imageHostingTabWeg->addTab(tencentImageHostingTab, tr("Tencent"));
+    imageHostingTabWeg->addTab(tencentImageHostingTab, tr("Tencent Cloud"));
     imageHostingTabWeg->setCurrentIndex(0);
 
     // 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->setMinimumWidth(250 * VUtils::calculateScaleFactor());
     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();
     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->setMinimumWidth(250 * VUtils::calculateScaleFactor());
     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();
     giteeLayout->addRow(tr("Personal access token:"), m_giteePersonalAccessTokenEdit);
@@ -1650,21 +1650,18 @@ VImageHostingTab::VImageHostingTab(QWidget *p_parent)
 
     // Set the tab of Wechat image Hosting
     m_wechatAppidEdit = new VLineEdit();
-    m_wechatAppidEdit->setToolTip(tr("WeChat appid"));
     m_wechatAppidEdit->setMinimumWidth(250 * VUtils::calculateScaleFactor());
     m_wechatSecretEdit = new VLineEdit();
-    m_wechatSecretEdit->setToolTip(tr("Please input wechat secret"));
     m_wechatSecretEdit->setMinimumWidth(250 * VUtils::calculateScaleFactor());
     m_markdown2WechatToolUrlEdit = new VLineEdit();
-    m_markdown2WechatToolUrlEdit->setToolTip(tr("Please input markdown to wechat tool's url"));
     m_markdown2WechatToolUrlEdit->setMinimumWidth(250 * VUtils::calculateScaleFactor());
     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();
-    wechatLayout->addRow(tr("appid:"), m_wechatAppidEdit);
-    wechatLayout->addRow(tr("secret:"), m_wechatSecretEdit);
-    wechatLayout->addRow(tr("markdown2WechatToolUrl"), m_markdown2WechatToolUrlEdit);
+    wechatLayout->addRow(tr("AppId:"), m_wechatAppidEdit);
+    wechatLayout->addRow(tr("AppSecret:"), m_wechatSecretEdit);
+    wechatLayout->addRow(tr("markdown2WechatToolUrl:"), m_markdown2WechatToolUrlEdit);
     wechatLayout->addRow(m_wechatKeepImgScaleCB);
     wechatLayout->addRow(m_wechatDoNotReplaceLinkCB);
 
@@ -1675,18 +1672,16 @@ VImageHostingTab::VImageHostingTab(QWidget *p_parent)
     m_tencentAccessDomainNameEdit->setToolTip(tr("Tencent access domain name"));
     m_tencentAccessDomainNameEdit->setMinimumWidth(250 * VUtils::calculateScaleFactor());
     m_tencentSecretIdEdit = new VLineEdit();
-    m_tencentSecretIdEdit->setToolTip(tr("Tencent secret id"));
     m_tencentSecretIdEdit->setMinimumWidth(250 * VUtils::calculateScaleFactor());
     m_tencentSecretKeyEdit = new VLineEdit();
-    m_tencentSecretKeyEdit->setToolTip(tr("Tencent secret key"));
     m_tencentSecretKeyEdit->setMinimumWidth(250 * VUtils::calculateScaleFactor());
     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();
-    tencentLayout->addRow(tr("accessDomainName"), m_tencentAccessDomainNameEdit);
-    tencentLayout->addRow(tr("secretId"), m_tencentSecretIdEdit);
-    tencentLayout->addRow(tr("secretKey"), m_tencentSecretKeyEdit);
+    tencentLayout->addRow(tr("Access domain name:"), m_tencentAccessDomainNameEdit);
+    tencentLayout->addRow(tr("SecretId:"), m_tencentSecretIdEdit);
+    tencentLayout->addRow(tr("SecretKey:"), m_tencentSecretKeyEdit);
     tencentLayout->addRow(m_tencentKeepImgScaleCB);
     tencentLayout->addRow(m_tencentDoNotReplaceLinkCB);
 

+ 7 - 0
src/utils/vclipboardutils.cpp

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

+ 4 - 0
src/utils/vclipboardutils.h

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

+ 66 - 67
src/vimagehosting.cpp

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

+ 24 - 5
src/vimagehosting.h

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

+ 30 - 0
src/vmdeditor.cpp

@@ -430,6 +430,8 @@ void VMdEditor::contextMenuEvent(QContextMenuEvent *p_event)
         }
 
         initAttachmentMenu(menu.data());
+
+        initImageHostingMenu(menu.data());
     }
 
     menu->exec(p_event->globalPos());
@@ -2277,3 +2279,31 @@ void VMdEditor::insertTable()
     // Insert table right at cursor.
     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);
+}

+ 10 - 0
src/vmdeditor.h

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

+ 60 - 11
src/vmdtab.cpp

@@ -450,15 +450,6 @@ void VMdTab::setupMarkdownViewer()
     connect(m_webViewer, &VWebView::requestExpandRestorePreviewArea,
             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);
     m_webViewer->setPage(page);
     m_webViewer->setZoomFactor(g_config->getWebZoomFactor());
@@ -591,6 +582,15 @@ void VMdTab::setupMarkdownEditor()
     connect(m_editor, &VMdEditor::requestHtmlToText,
             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()) {
         connect(m_editor->getVim(), &VVim::commandLineTriggered,
                 this, [this](VVim::CommandLineType p_type) {
@@ -625,6 +625,11 @@ void VMdTab::setupMarkdownEditor()
     connect(m_mathjaxPreviewHelper, &VMathJaxInplacePreviewHelper::checkBlocksForObsoletePreview,
             m_editor->getPreviewManager(), &VPreviewManager::checkBlocksForObsoletePreview);
     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)
@@ -1520,21 +1525,65 @@ void VMdTab::handleSavePageRequested()
 
 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()
 {
-      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()
 {
+    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();
 }
 
 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();
 }
 

+ 0 - 25
src/vwebview.cpp

@@ -98,31 +98,6 @@ void VWebView::contextMenuEvent(QContextMenuEvent *p_event)
             connect(savePageAct, &QAction::triggered,
                     this, &VWebView::requestSavePage);
             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);
         }
     }
 

+ 0 - 8
src/vwebview.h

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