diff --git a/src/vattachmentlist.cpp b/src/vattachmentlist.cpp index 3f9d0696..1d3c82fd 100644 --- a/src/vattachmentlist.cpp +++ b/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)); +} diff --git a/src/vattachmentlist.h b/src/vattachmentlist.h index 647f14d1..21a46385 100644 --- a/src/vattachmentlist.h +++ b/src/vattachmentlist.h @@ -52,6 +52,8 @@ private slots: void attachmentInfo(); + void copyAttachmentFilePath(); + private: void setupUI(); diff --git a/src/vmdeditor.cpp b/src/vmdeditor.cpp index f5c41c1d..a1dabcd3 100644 --- a/src/vmdeditor.cpp +++ b/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()) { - return false; - } - - QList urls = p_source->urls(); - if (urls.size() == 1) { - bool isImage = VUtils::isImageURL(urls[0]); - bool isLocalFile = urls[0].isLocalFile() - && QFileInfo::exists(urls[0].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(); - } + QUrl url; + if (p_source->hasUrls()) { + QList urls = p_source->urls(); + if (urls.size() == 1) { + url = urls[0]; } - - VSelectDialog dialog(tr("Insert From Clipboard"), this); - if (isImage) { - dialog.addSelection(tr("Insert As Image"), 0); - dialog.addSelection(tr("Insert As Image Link"), 1); - } - - 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); - } - - // 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; - } - - case 1: - { - // Insert As Image Link. - insertImageLink("", urls[0].isLocalFile() ? urls[0].toString(QUrl::EncodeSpaces) - : urls[0].toString()); - 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(); - } - - 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); - } - - return true; - } - - case 4: - { - // Insert As Text. - if (p_source->hasText()) { - m_editOps->insertText(p_source->text()); - } else { - m_editOps->insertText(urls[0].toString()); - } - - return true; - } - - case 5: - { - // Insert File Content. - Q_ASSERT(!localTextFilePath.isEmpty()); - m_editOps->insertText(VUtils::readFileFromDisk(localTextFilePath)); - return true; - } - - default: - Q_ASSERT(false); - break; - } - } - - 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); - } - - if (url.isValid()) { - m_editOps->insertImageFromURL(url); - } - - return true; - } else if (selection == 2) { - // Insert as link. - insertImageLink("", text); - return true; - } + } 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; + } + + bool isImage = VUtils::isImageURL(url); + bool isLocalFile = url.isLocalFile() + && QFileInfo::exists(url.toLocalFile()); + + 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(); + } + } + + VSelectDialog dialog(tr("Insert From Clipboard"), this); + if (isImage) { + dialog.addSelection(tr("Insert As Image"), 0); + dialog.addSelection(tr("Insert As Image Link"), 1); + } + + 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); + } + + // 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 1: + { + // Insert As Image Link. + insertImageLink("", url.isLocalFile() ? url.toString(QUrl::EncodeSpaces) + : url.toString()); + return true; + } + + case 3: + // Insert As Relative link. + relativeLink = true; + V_FALLTHROUGH; + + case 2: + { + // Insert As Link. + QString linkText; + if (isLocalFile) { + linkText = QFileInfo(url.toLocalFile()).fileName(); + } + + 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(); + } + + 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); + } + + return true; + } + + case 4: + { + // Insert As Text. + if (p_source->hasText()) { + m_editOps->insertText(p_source->text()); + } else { + m_editOps->insertText(url.toString()); + } + + return true; + } + + 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) diff --git a/src/vmdeditor.h b/src/vmdeditor.h index cce3897d..d1d4db05 100644 --- a/src/vmdeditor.h +++ b/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;