Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. diff --git a/src/markdownentry.cpp b/src/markdownentry.cpp
  2. index f0ba5afb..bf9b0fe1 100644
  3. --- a/src/markdownentry.cpp
  4. +++ b/src/markdownentry.cpp
  5. @@ -30,6 +30,8 @@
  6. #include <QDebug>
  7. #include <QStandardPaths>
  8. #include <QDir>
  9. +#include <QFileDialog>
  10. +#include <KMessageBox>
  11.  
  12. #include "jupyterutils.h"
  13. #include "mathrender.h"
  14. @@ -78,6 +80,8 @@ void MarkdownEntry::populateMenu(QMenu* menu, QPointF pos)
  15. menu->addAction(i18n("Show LaTeX code"), this, &MarkdownEntry::resolveImagesAtCursor);
  16. menu->addSeparator();
  17. }
  18. + menu->addAction(i18n("Add attachment"), this, &MarkdownEntry::addAttachment);
  19. + menu->addAction(i18n("Clear attachments"), this, &MarkdownEntry::clearAttachments);
  20. WorksheetEntry::populateMenu(menu, pos);
  21. }
  22.  
  23. @@ -626,3 +630,35 @@ void MarkdownEntry::markUpMath()
  24. cursor.setCharFormat(format);
  25. }
  26. }
  27. +
  28. +void MarkdownEntry::addAttachment()
  29. +{
  30. + const QString& filename = QFileDialog::getOpenFileName(worksheet()->worksheetView(), i18n("Choose Image"), QString(), i18n("Images (*.png *.bmp *.jpg *.svg)"));
  31. + if (!filename.isEmpty())
  32. + {
  33. + QImageReader reader(filename);
  34. + const QImage img = reader.read();
  35. + if (!img.isNull())
  36. + {
  37. + QFileInfo info(filename);
  38. +
  39. + QUrl url;
  40. + url.setScheme(QLatin1String("internal"));
  41. + url.setPath(info.fileName());
  42. +
  43. + m_textItem->document()->addResource(QTextDocument::ImageResource, url, QVariant(img));
  44. + }
  45. + else
  46. + KMessageBox::error(worksheetView(), i18n("Cantor failed to read image with error \"%1\"", reader.errorString()), i18n("Cantor"));
  47. + }
  48. +}
  49. +
  50. +void MarkdownEntry::clearAttachments()
  51. +{
  52. + for (auto& attachment: attachedImages)
  53. + {
  54. + const QUrl& url = attachment.first;
  55. + m_textItem->document()->addResource(QTextDocument::ImageResource, url, QVariant());
  56. + }
  57. + attachedImages.clear();
  58. +}
  59. diff --git a/src/markdownentry.h b/src/markdownentry.h
  60. index c121e3b3..b2421a36 100644
  61. --- a/src/markdownentry.h
  62. +++ b/src/markdownentry.h
  63. @@ -87,6 +87,8 @@ class MarkdownEntry : public WorksheetEntry
  64.  
  65. protected Q_SLOTS:
  66. void handleMathRender(QSharedPointer<MathRenderResult> result);
  67. + void addAttachment();
  68. + void clearAttachments();
  69.  
  70. protected:
  71. WorksheetTextItem* m_textItem;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement