File enable-image-insert.patch of Package kjots
From 362b0cb67711ab4a16247138a04165a8e90fa8e8 Mon Sep 17 00:00:00 2001
From: Spencer Brown <spencerbrown991@gmail.com>
Date: Tue, 2 Aug 2016 16:52:30 +0200
Subject: Image insert for KJots
Add image insert feature to KJots.
REVIEW: 128568
diff --git a/src/kjotsedit.cpp b/src/kjotsedit.cpp
index 3e63d91..2b9dddf 100644
--- a/src/kjotsedit.cpp
+++ b/src/kjotsedit.cpp
@@ -114,6 +114,7 @@ void KJotsEdit::delayedInitialization(KActionCollection *collection)
connect(actionCollection->action(QLatin1String("insert_checkmark")), SIGNAL(triggered()), SLOT(addCheckmark()));
connect(actionCollection->action(QLatin1String("manual_save")), SIGNAL(triggered()), SLOT(savePage()));
connect(actionCollection->action(QLatin1String("insert_date")), SIGNAL(triggered()), SLOT(insertDate()));
+ connect(actionCollection->action(QLatin1String("insert_image")), SIGNAL(triggered()), SLOT(insertImage()));
}
void KJotsEdit::insertDate()
@@ -121,6 +122,12 @@ void KJotsEdit::insertDate()
NoteShared::NoteEditorUtils().insertDate(this);
}
+void KJotsEdit::insertImage()
+{
+ QTextCursor cursor = textCursor();
+ NoteShared::NoteEditorUtils().insertImage(document(), cursor, this);
+}
+
void KJotsEdit::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
{
Q_UNUSED(selected)
diff --git a/src/kjotsedit.h b/src/kjotsedit.h
index 0273283..39b5128 100644
--- a/src/kjotsedit.h
+++ b/src/kjotsedit.h
@@ -65,6 +65,7 @@ public slots:
void savePage();
void insertDate();
+ void insertImage();
private:
void createAutoDecimalList();
diff --git a/src/kjotspartui.rc b/src/kjotspartui.rc
index e565732..e732c33 100644
--- a/src/kjotspartui.rc
+++ b/src/kjotspartui.rc
@@ -1,5 +1,5 @@
<!DOCTYPE kpartgui>
-<kpartgui version="11" name="kjots" >
+<kpartgui version="12" name="kjots" >
<MenuBar>
<Menu name="file">
@@ -52,6 +52,7 @@
<Menu name="tools">
<text>&Tools</text>
<Action name="insert_date"/>
+ <Action name="insert_image"/>
<Action name="copyIntoTitle"/>
<Action name="linkify"/>
</Menu>
diff --git a/src/kjotsui.rc b/src/kjotsui.rc
index e6eea8d..298a5fb 100644
--- a/src/kjotsui.rc
+++ b/src/kjotsui.rc
@@ -1,5 +1,5 @@
<!DOCTYPE kpartgui>
-<kpartgui version="11" name="kjots" >
+<kpartgui version="12" name="kjots" >
<MenuBar>
<Menu name="file">
@@ -50,6 +50,7 @@
<Menu name="tools">
<Action name="insert_date"/>
+ <Action name="insert_image"/>
<Action name="copyIntoTitle"/>
</Menu>
diff --git a/src/kjotswidget.cpp b/src/kjotswidget.cpp
index 0d0dad8..865129b 100644
--- a/src/kjotswidget.cpp
+++ b/src/kjotswidget.cpp
@@ -277,6 +277,10 @@ KJotsWidget::KJotsWidget(QWidget *parent, KXMLGUIClient *xmlGuiClient, Qt::Windo
action->setText(i18n("Link"));
action->setIcon(QIcon::fromTheme(QLatin1String("insert-link")));
+ action = actionCollection->addAction(QLatin1String("insert_image"));
+ action->setText(i18n("Insert Image"));
+ action->setIcon(QIcon::fromTheme(QLatin1String("insert-image")));
+
action = actionCollection->addAction(QLatin1String("insert_checkmark"));
action->setText(i18n("Insert Checkmark"));
action->setIcon(QIcon::fromTheme(QLatin1String("checkmark")));
diff --git a/src/noteshared/noteeditorutils.cpp b/src/noteshared/noteeditorutils.cpp
index b30a495..661bfa5 100644
--- a/src/noteshared/noteeditorutils.cpp
+++ b/src/noteshared/noteeditorutils.cpp
@@ -18,11 +18,15 @@
#include "noteeditorutils.h"
#include <QLocale>
-
+#include <QMimeDatabase>
+#include <QFileDialog>
+#include <KLocalizedString>
+#include <QBuffer>
#include <QChar>
#include <QTextCursor>
#include <QTextEdit>
#include <QDateTime>
+#include <QFileInfo>
using namespace NoteShared;
NoteEditorUtils::NoteEditorUtils()
@@ -45,3 +49,26 @@ void NoteEditorUtils::insertDate(QTextEdit *editor)
{
editor->insertPlainText(QLocale().toString(QDateTime::currentDateTime(), QLocale::ShortFormat) + QLatin1Char(' '));
}
+
+void NoteEditorUtils::insertImage(QTextDocument *doc, QTextCursor &cursor, QTextEdit *par)
+{
+ QString imageFileName = QFileDialog::getOpenFileName(par, i18n("Select image file"), QLatin1String("."), QLatin1String("Images (*.png *.bmp *.jpg *.jpeg *.jpe)"));
+ if (!imageFileName.isEmpty()) {
+ QFileInfo qfio = QFileInfo(imageFileName);
+ QImage imgRes(imageFileName);
+ if (!imgRes.isNull()) {
+ QMimeDatabase mimedb;
+ QByteArray imageData;
+ QBuffer buffer(&imageData);
+ QMimeType filemime = mimedb.mimeTypeForFile(qfio);
+ QString filetype = filemime.name();
+ QByteArray formatChars = filemime.preferredSuffix().toUpper().toLatin1();
+
+ buffer.open(QIODevice::WriteOnly);
+ imgRes.save(&buffer, formatChars.data());
+ QString Base64Image = QString::fromLatin1(imageData.toBase64().data());//is null
+ par->insertHtml(QLatin1String("<img src=\"data:") + filetype + QLatin1String(";base64,") + Base64Image + QLatin1String("\" />"));
+
+ }
+ }
+}
diff --git a/src/noteshared/noteeditorutils.h b/src/noteshared/noteeditorutils.h
index 149a308..5be91fd 100644
--- a/src/noteshared/noteeditorutils.h
+++ b/src/noteshared/noteeditorutils.h
@@ -20,6 +20,8 @@
class QTextCursor;
class QTextEdit;
+class QTextDocument;
+class QWidget;
namespace NoteShared
{
@@ -29,6 +31,7 @@ public:
NoteEditorUtils();
void addCheckmark(QTextCursor &cursor);
void insertDate(QTextEdit *editor);
+ void insertImage(QTextDocument *doc, QTextCursor &cursor, QTextEdit *par);
};
}
#endif // NOTEEDITORUTILS_H