Browse Source

AttachmentList: support copying file path of attachment

Le Tan 7 years ago
parent
commit
0484656c1f
4 changed files with 135 additions and 145 deletions
  1. 20 1
      src/vattachmentlist.cpp
  2. 2 0
      src/vattachmentlist.h
  3. 113 142
      src/vmdeditor.cpp
  4. 0 2
      src/vmdeditor.h

+ 20 - 1
src/vattachmentlist.cpp

@@ -287,6 +287,11 @@ void VAttachmentList::handleContextMenuRequested(QPoint p_pos)
     if (selectedSize == 1) {
         menu.addSeparator();
 
+        QAction *copyPathAct = new QAction(tr("Copy File Path"), &menu);
+        connect(copyPathAct, &QAction::triggered,
+                this, &VAttachmentList::copyAttachmentFilePath);
+        menu.addAction(copyPathAct);
+
         QAction *fileInfoAct = new QAction(VIconUtils::menuIcon(":/resources/icons/note_info.svg"),
                                            tr("&Info (Rename)\t%1").arg(VUtils::getShortcutText(c_infoShortcutSequence)),
                                            &menu);
@@ -306,7 +311,7 @@ void VAttachmentList::handleItemActivated(QListWidgetItem *p_item)
     if (p_item) {
         Q_ASSERT(m_file);
 
-        QString name = p_item->text();
+        QString name = p_item->data(Qt::UserRole).toString();
         QString folderPath = m_file->fetchAttachmentFolderPath();
         QUrl url = QUrl::fromLocalFile(QDir(folderPath).filePath(name));
         QDesktopServices::openUrl(url);
@@ -698,3 +703,17 @@ void VAttachmentList::attachmentInfo()
         item->setText(name);
     }
 }
+
+void VAttachmentList::copyAttachmentFilePath()
+{
+    QListWidgetItem *item = m_attachmentList->currentItem();
+    if (!item) {
+        return;
+    }
+
+    QString name = item->data(Qt::UserRole).toString();
+    QString filePath = QDir(m_file->fetchAttachmentFolderPath()).filePath(name);
+    QClipboard *clipboard = QApplication::clipboard();
+    clipboard->setText(filePath);
+    g_mainWin->showStatusMessage(tr("Attachment file path copied %1").arg(filePath));
+}

+ 2 - 0
src/vattachmentlist.h

@@ -52,6 +52,8 @@ private slots:
 
     void attachmentInfo();
 
+    void copyAttachmentFilePath();
+
 private:
     void setupUI();
 

+ 113 - 142
src/vmdeditor.cpp

@@ -873,10 +873,6 @@ void VMdEditor::insertFromMimeData(const QMimeData *p_source)
         return;
     }
 
-    if (processTextFromMimeData(p_source)) {
-        return;
-    }
-
     VTextEdit::insertFromMimeData(p_source);
 }
 
@@ -1840,169 +1836,144 @@ bool VMdEditor::processImageFromMimeData(const QMimeData *p_source)
 
 bool VMdEditor::processUrlFromMimeData(const QMimeData *p_source)
 {
-    if (!p_source->hasUrls()) {
+    QUrl url;
+    if (p_source->hasUrls()) {
+        QList<QUrl> urls = p_source->urls();
+        if (urls.size() == 1) {
+            url = urls[0];
+        }
+    } else if (p_source->hasText()) {
+        // Try to get URL from text.
+        QString text = p_source->text();
+        if (QFileInfo::exists(text)) {
+            url = QUrl::fromLocalFile(text);
+        } else {
+            url = QUrl(text);
+            if (url.scheme() != "https" && url.scheme() != "http") {
+                url.clear();
+            }
+        }
+    }
+
+    if (!url.isValid()) {
         return false;
     }
 
-    QList<QUrl> urls = p_source->urls();
-    if (urls.size() == 1) {
-        bool isImage = VUtils::isImageURL(urls[0]);
-        bool isLocalFile = urls[0].isLocalFile()
-                           && QFileInfo::exists(urls[0].toLocalFile());
+    bool isImage = VUtils::isImageURL(url);
+    bool isLocalFile = url.isLocalFile()
+                       && QFileInfo::exists(url.toLocalFile());
 
-        QString localTextFilePath;
-        if (!isImage && isLocalFile) {
-            localTextFilePath = urls[0].toLocalFile();
-            QMimeDatabase mimeDatabase;
-            const QMimeType mimeType = mimeDatabase.mimeTypeForFile(localTextFilePath);
-            if (mimeType.isValid() && !mimeType.inherits(QStringLiteral("text/plain"))) {
-                localTextFilePath.clear();
-            }
-        }
-
-        VSelectDialog dialog(tr("Insert From Clipboard"), this);
-        if (isImage) {
-            dialog.addSelection(tr("Insert As Image"), 0);
-            dialog.addSelection(tr("Insert As Image Link"), 1);
+    QString localTextFilePath;
+    if (!isImage && isLocalFile) {
+        localTextFilePath = url.toLocalFile();
+        QMimeDatabase mimeDatabase;
+        const QMimeType mimeType = mimeDatabase.mimeTypeForFile(localTextFilePath);
+        if (mimeType.isValid() && !mimeType.inherits(QStringLiteral("text/plain"))) {
+            localTextFilePath.clear();
         }
+    }
 
-        dialog.addSelection(tr("Insert As Link"), 2);
-        if (isLocalFile) {
-            dialog.addSelection(tr("Insert As Relative Link"), 3);
-        }
-        dialog.addSelection(tr("Insert As Text"), 4);
-        if (!localTextFilePath.isEmpty()) {
-            dialog.addSelection(tr("Insert File Content"), 5);
-        }
+    VSelectDialog dialog(tr("Insert From Clipboard"), this);
+    if (isImage) {
+        dialog.addSelection(tr("Insert As Image"), 0);
+        dialog.addSelection(tr("Insert As Image Link"), 1);
+    }
 
-        // FIXME: After calling dialog.exec(), p_source->hasUrl() returns false.
-        if (dialog.exec() == QDialog::Accepted) {
-            bool relativeLink = false;
-            switch (dialog.getSelection()) {
-            case 0:
-            {
-                // Insert As Image.
-                m_editOps->insertImageFromURL(urls[0]);
-                return true;
-            }
+    dialog.addSelection(tr("Insert As Link"), 2);
+    if (isLocalFile) {
+        dialog.addSelection(tr("Insert As Relative Link"), 3);
+    }
+    dialog.addSelection(tr("Insert As Text"), 4);
+    if (!localTextFilePath.isEmpty()) {
+        dialog.addSelection(tr("Insert File Content"), 5);
+    }
 
-            case 1:
-            {
-                // Insert As Image Link.
-                insertImageLink("", urls[0].isLocalFile() ? urls[0].toString(QUrl::EncodeSpaces)
-                                                          : urls[0].toString());
-                return true;
-            }
+    // FIXME: After calling dialog.exec(), p_source->hasUrl() returns false.
+    if (dialog.exec() == QDialog::Accepted) {
+        bool relativeLink = false;
+        switch (dialog.getSelection()) {
+        case 0:
+        {
+            // Insert As Image.
+            m_editOps->insertImageFromURL(url);
+            return true;
+        }
 
-            case 3:
-                // Insert As Relative link.
-                relativeLink = true;
-                V_FALLTHROUGH;
-
-            case 2:
-            {
-                // Insert As Link.
-                QString ut;
-                if (relativeLink) {
-                    QDir dir(m_file->fetchBasePath());
-                    ut = dir.relativeFilePath(urls[0].toLocalFile());
-                    ut = QUrl(ut).toString(QUrl::EncodeSpaces);
-                } else {
-                    ut = urls[0].isLocalFile() ? urls[0].toString(QUrl::EncodeSpaces)
-                                               : urls[0].toString();
-                }
+        case 1:
+        {
+            // Insert As Image Link.
+            insertImageLink("", url.isLocalFile() ? url.toString(QUrl::EncodeSpaces)
+                                                  : url.toString());
+            return true;
+        }
 
-                VInsertLinkDialog ld(QObject::tr("Insert Link"),
-                                     "",
-                                     "",
-                                     "",
-                                     ut,
-                                     false,
-                                     this);
-                if (ld.exec() == QDialog::Accepted) {
-                    QString linkText = ld.getLinkText();
-                    QString linkUrl = ld.getLinkUrl();
-                    Q_ASSERT(!linkText.isEmpty() && !linkUrl.isEmpty());
-                    m_editOps->insertLink(linkText, linkUrl);
-                }
+        case 3:
+            // Insert As Relative link.
+            relativeLink = true;
+            V_FALLTHROUGH;
 
-                return true;
+        case 2:
+        {
+            // Insert As Link.
+            QString linkText;
+            if (isLocalFile) {
+                linkText = QFileInfo(url.toLocalFile()).fileName();
             }
 
-            case 4:
-            {
-                // Insert As Text.
-                if (p_source->hasText()) {
-                    m_editOps->insertText(p_source->text());
-                } else {
-                    m_editOps->insertText(urls[0].toString());
-                }
-
-                return true;
+            QString ut;
+            if (relativeLink) {
+                QDir dir(m_file->fetchBasePath());
+                ut = dir.relativeFilePath(url.toLocalFile());
+                ut = QUrl(ut).toString(QUrl::EncodeSpaces);
+            } else {
+                ut = url.isLocalFile() ? url.toString(QUrl::EncodeSpaces)
+                                       : url.toString();
             }
 
-            case 5:
-            {
-                // Insert File Content.
-                Q_ASSERT(!localTextFilePath.isEmpty());
-                m_editOps->insertText(VUtils::readFileFromDisk(localTextFilePath));
-                return true;
+            VInsertLinkDialog ld(QObject::tr("Insert Link"),
+                                 "",
+                                 "",
+                                 linkText,
+                                 ut,
+                                 false,
+                                 this);
+            if (ld.exec() == QDialog::Accepted) {
+                QString linkText = ld.getLinkText();
+                QString linkUrl = ld.getLinkUrl();
+                Q_ASSERT(!linkText.isEmpty() && !linkUrl.isEmpty());
+                m_editOps->insertLink(linkText, linkUrl);
             }
 
-            default:
-                Q_ASSERT(false);
-                break;
-            }
+            return true;
         }
 
-        return true;
-    }
-
-    return false;
-}
-
-bool VMdEditor::processTextFromMimeData(const QMimeData *p_source)
-{
-    if (!p_source->hasText()) {
-        return false;
-    }
-
-    QString text = p_source->text();
-    if (VUtils::isImageURLText(text)) {
-        // The text is a URL to an image.
-        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 (dialog.exec() == QDialog::Accepted) {
-            int selection = dialog.getSelection();
-            if (selection == 0) {
-                // Insert as image.
-                QUrl url;
-                if (QFileInfo::exists(text)) {
-                    url = QUrl::fromLocalFile(text);
-                } else {
-                    url = QUrl(text);
-                }
+        case 4:
+        {
+            // Insert As Text.
+            if (p_source->hasText()) {
+                m_editOps->insertText(p_source->text());
+            } else {
+                m_editOps->insertText(url.toString());
+            }
 
-                if (url.isValid()) {
-                    m_editOps->insertImageFromURL(url);
-                }
+            return true;
+        }
 
-                return true;
-            } else if (selection == 2) {
-                // Insert as link.
-                insertImageLink("", text);
-                return true;
-            }
-        } else {
+        case 5:
+        {
+            // Insert File Content.
+            Q_ASSERT(!localTextFilePath.isEmpty());
+            m_editOps->insertText(VUtils::readFileFromDisk(localTextFilePath));
             return true;
         }
+
+        default:
+            Q_ASSERT(false);
+            break;
+        }
     }
 
-    Q_ASSERT(p_source->hasText());
-    return false;
+    return true;
 }
 
 void VMdEditor::replaceTextWithLocalImages(QString &p_text)

+ 0 - 2
src/vmdeditor.h

@@ -313,8 +313,6 @@ private:
 
     bool processUrlFromMimeData(const QMimeData *p_source);
 
-    bool processTextFromMimeData(const QMimeData *p_source);
-
     void replaceTextWithLocalImages(QString &p_text);
 
     PegMarkdownHighlighter *m_pegHighlighter;