From 30a3a6e74a5d1228ed115f855eadd3e592e143ea Mon Sep 17 00:00:00 2001 From: Le Tan Date: Fri, 14 Sep 2018 19:48:17 +0800 Subject: [PATCH] MdEditor: support inserting file as relative link --- src/vmdeditor.cpp | 200 +++++++++++++++++++++++++--------------------- 1 file changed, 107 insertions(+), 93 deletions(-) diff --git a/src/vmdeditor.cpp b/src/vmdeditor.cpp index 0b81145f..18bc14c4 100644 --- a/src/vmdeditor.cpp +++ b/src/vmdeditor.cpp @@ -1826,102 +1826,116 @@ bool VMdEditor::processUrlFromMimeData(const QMimeData *p_source) QList urls = p_source->urls(); if (urls.size() == 1) { - if (VUtils::isImageURL(urls[0])) { - 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); + bool isImage = VUtils::isImageURL(urls[0]); + bool isLocalFile = urls[0].isLocalFile() + && QFileInfo::exists(urls[0].toLocalFile()); - if (dialog.exec() == QDialog::Accepted) { - // FIXME: After calling dialog.exec(), p_source->hasUrl() returns false. - int selection = dialog.getSelection(); - if (selection == 0) { - // Insert as image. - m_editOps->insertImageFromURL(urls[0]); - return true; - } else if (selection == 2) { - // Insert as link. - insertImageLink("", urls[0].toString(QUrl::FullyEncoded)); - return true; - } - - QMimeData newSource; - newSource.setUrls(urls); - VTextEdit::insertFromMimeData(&newSource); - } - - return true; - } else { - QString localTextFilePath; - if (urls[0].isLocalFile()) { - localTextFilePath = urls[0].toLocalFile(); - if (!QFileInfo::exists(localTextFilePath)) { - localTextFilePath.clear(); - } else { - 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); - dialog.addSelection(tr("Insert As Link"), 0); - dialog.addSelection(tr("Insert As Text"), 1); - if (!localTextFilePath.isEmpty()) { - dialog.addSelection(tr("Insert File Content"), 2); - } - - if (dialog.exec() == QDialog::Accepted) { - switch (dialog.getSelection()) { - case 0: - { - QString 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); - } - - break; - } - - case 1: - if (p_source->hasText()) { - m_editOps->insertText(p_source->text()); - } else { - m_editOps->insertText(urls[0].toString()); - } - - break; - - case 2: - { - Q_ASSERT(!localTextFilePath.isEmpty()); - m_editOps->insertText(VUtils::readFileFromDisk(localTextFilePath)); - break; - } - - default: - Q_ASSERT(false); - break; - } - - return true; - } else { - return true; + 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); + } + + 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;