Browse Source

refine image insert selection logics

Le Tan 7 years ago
parent
commit
69c14394ac

+ 5 - 2
src/dialog/vinsertlinkdialog.cpp

@@ -9,8 +9,10 @@ VInsertLinkDialog::VInsertLinkDialog(const QString &p_title,
                                      const QString &p_info,
                                      const QString &p_linkText,
                                      const QString &p_linkUrl,
+                                     bool p_linkTextEmptyAllowed,
                                      QWidget *p_parent)
-    : QDialog(p_parent)
+    : QDialog(p_parent),
+      m_linkTextEmptyAllowed(p_linkTextEmptyAllowed)
 {
     setupUI(p_title, p_text, p_info, p_linkText, p_linkUrl);
 
@@ -80,7 +82,8 @@ void VInsertLinkDialog::setupUI(const QString &p_title,
 void VInsertLinkDialog::handleInputChanged()
 {
     bool textOk = true;
-    if (m_linkTextEdit->getEvaluatedText().isEmpty()) {
+    if (m_linkTextEdit->getEvaluatedText().isEmpty()
+        && !m_linkTextEmptyAllowed) {
         textOk = false;
     }
 

+ 3 - 0
src/dialog/vinsertlinkdialog.h

@@ -18,6 +18,7 @@ public:
                       const QString &p_info,
                       const QString &p_linkText,
                       const QString &p_linkUrl,
+                      bool p_linkTextEmptyAllowed,
                       QWidget *p_parent = nullptr);
 
     QString getLinkText() const;
@@ -45,6 +46,8 @@ private:
     VLineEdit *m_linkUrlEdit;
 
     QDialogButtonBox *m_btnBox;
+
+    bool m_linkTextEmptyAllowed;
 };
 
 #endif // VINSERTLINKDIALOG_H

+ 1 - 0
src/veditor.cpp

@@ -394,6 +394,7 @@ void VEditor::insertLink()
                              "",
                              linkText,
                              linkUrl,
+                             false,
                              m_editor);
     if (dialog.exec() == QDialog::Accepted) {
         linkText = dialog.getLinkText();

+ 13 - 0
src/vmdeditoperations.cpp

@@ -1050,3 +1050,16 @@ bool VMdEditOperations::insertLink(const QString &p_linkText,
 
     return true;
 }
+
+bool VMdEditOperations::insertImageLink(const QString &p_linkText,
+                                        const QString &p_linkUrl)
+{
+    QString link = QString("![%1](%2)").arg(p_linkText).arg(p_linkUrl);
+    QTextCursor cursor = m_editor->textCursorW();
+    cursor.insertText(link);
+    m_editor->setTextCursorW(cursor);
+
+    setVimMode(VimMode::Insert);
+
+    return true;
+}

+ 2 - 0
src/vmdeditoperations.h

@@ -32,6 +32,8 @@ public:
     // If it is Vim Normal mode, change to Insert mode first.
     void decorateText(TextDecoration p_decoration, int p_level = -1) Q_DECL_OVERRIDE;
 
+    bool insertImageLink(const QString &p_linkText, const QString &p_linkUrl);
+
 private:
     // Insert image from @oriImagePath as @path.
     // @folderInLink: the folder part in the image link.

+ 65 - 7
src/vmdeditor.cpp

@@ -21,6 +21,7 @@
 #include "utils/viconutils.h"
 #include "dialog/vcopytextashtmldialog.h"
 #include "utils/vwebutils.h"
+#include "dialog/vinsertlinkdialog.h"
 
 extern VWebUtils *g_webUtils;
 
@@ -728,28 +729,57 @@ bool VMdEditor::canInsertFromMimeData(const QMimeData *p_source) const
 
 void VMdEditor::insertFromMimeData(const QMimeData *p_source)
 {
-    VSelectDialog dialog(tr("Insert From Clipboard"), this);
-    dialog.addSelection(tr("Insert As Image"), 0);
-    dialog.addSelection(tr("Insert As Text"), 1);
-
     if (p_source->hasHtml()) {
         // Handle <img>.
         QRegExp reg("<img ([^>]*)src=\"([^\"]+)\"([^>]*)>");
         if (reg.indexIn(p_source->html()) != -1) {
+            if (p_source->hasImage()) {
+                // Both image data and URL are embedded.
+                VSelectDialog dialog(tr("Insert From Clipboard"), this);
+                dialog.addSelection(tr("Insert From URL"), 0);
+                dialog.addSelection(tr("Insert From Image Data"), 1);
+                dialog.addSelection(tr("Insert As Image Link"), 2);
+
+                if (dialog.exec() == QDialog::Accepted) {
+                    int selection = dialog.getSelection();
+                    if (selection == 1) {
+                        // Insert from image data.
+                        m_editOps->insertImageFromMimeData(p_source);
+                        return;
+                    } else if (selection == 2) {
+                        // Insert as link.
+                        insertImageLink("", reg.cap(2));
+                        return;
+                    }
+                } else {
+                    return;
+                }
+            }
+
             m_editOps->insertImageFromURL(QUrl(reg.cap(2)));
             return;
         }
     }
 
+    VSelectDialog dialog(tr("Insert From Clipboard"), this);
+    dialog.addSelection(tr("Insert As Image"), 0);
+    dialog.addSelection(tr("Insert As Text"), 1);
+    dialog.addSelection(tr("Insert As Image Link"), 2);
+
     if (p_source->hasImage()) {
         // Image data in the clipboard
         if (p_source->hasText()) {
             if (dialog.exec() == QDialog::Accepted) {
-                if (dialog.getSelection() == 1) {
+                int selection = dialog.getSelection();
+                if (selection == 1) {
                     // Insert as text.
                     Q_ASSERT(p_source->hasText() && p_source->hasImage());
                     VTextEdit::insertFromMimeData(p_source);
                     return;
+                } else if (selection == 2) {
+                    // Insert as link.
+                    insertImageLink("", p_source->text());
+                    return;
                 }
             } else {
                 return;
@@ -765,10 +795,15 @@ void VMdEditor::insertFromMimeData(const QMimeData *p_source)
         if (urls.size() == 1 && VUtils::isImageURL(urls[0])) {
             if (dialog.exec() == QDialog::Accepted) {
                 // FIXME: After calling dialog.exec(), p_source->hasUrl() returns false.
-                if (dialog.getSelection() == 0) {
+                int selection = dialog.getSelection();
+                if (selection == 0) {
                     // Insert as image.
                     m_editOps->insertImageFromURL(urls[0]);
                     return;
+                } else if (selection == 2) {
+                    // Insert as link.
+                    insertImageLink("", urls[0].toString(QUrl::FullyEncoded));
+                    return;
                 }
 
                 QMimeData newSource;
@@ -786,12 +821,18 @@ void VMdEditor::insertFromMimeData(const QMimeData *p_source)
         if (VUtils::isImageURLText(text)) {
             // The text is a URL to an image.
             if (dialog.exec() == QDialog::Accepted) {
-                if (dialog.getSelection() == 0) {
+                int selection = dialog.getSelection();
+                if (selection == 0) {
                     // Insert as image.
                     QUrl url(text);
                     if (url.isValid()) {
                         m_editOps->insertImageFromURL(QUrl(text));
                     }
+
+                    return;
+                } else if (selection == 2) {
+                    // Insert as link.
+                    insertImageLink("", text);
                     return;
                 }
             } else {
@@ -1175,3 +1216,20 @@ void VMdEditor::initCopyAsMenu(QAction *p_before, QMenu *p_menu)
         p_menu->insertSeparator(menuAct);
     }
 }
+
+void VMdEditor::insertImageLink(const QString &p_text, const QString &p_url)
+{
+    VInsertLinkDialog dialog(tr("Insert Image Link"),
+                             "",
+                             "",
+                             p_text,
+                             p_url,
+                             true,
+                             this);
+    if (dialog.exec() == QDialog::Accepted) {
+        QString linkText = dialog.getLinkText();
+        QString linkUrl = dialog.getLinkUrl();
+        static_cast<VMdEditOperations *>(m_editOps)->insertImageLink(linkText, linkUrl);
+    }
+}
+

+ 2 - 0
src/vmdeditor.h

@@ -234,6 +234,8 @@ private:
 
     void initCopyAsMenu(QAction *p_before, QMenu *p_menu);
 
+    void insertImageLink(const QString &p_text, const QString &p_url);
+
     HGMarkdownHighlighter *m_mdHighlighter;
 
     VCodeBlockHighlightHelper *m_cbHighlighter;