|
|
@@ -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)
|