2
** Copyright (C) 2013 Jiří Procházka (Hobrasoft)
3
** Contact: http://www.hobrasoft.cz/
5
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
6
** Contact: http://www.qt-project.org/legal
8
** $QT_BEGIN_LICENSE:LGPL$
9
** GNU Lesser General Public License Usage
10
** This file is under the terms of the GNU Lesser General Public License
11
** version 2.1 as published by the Free Software Foundation and appearing
12
** in the file LICENSE.LGPL included in the packaging of this file.
13
** Please review the following information to ensure the
14
** GNU Lesser General Public License version 2.1 requirements
15
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17
** In addition, as a special exception, Digia gives you certain additional
18
** rights. These rights are described in the Digia Qt LGPL Exception
19
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
23
// clazy:excludeall=qstring-arg
25
#include "PreCompiled.h"
31
# include <QTextCursor>
37
MTextEdit::MTextEdit(QWidget *parent) : QTextEdit(parent) {
41
bool MTextEdit::canInsertFromMimeData(const QMimeData *source) const {
42
return source->hasImage() || QTextEdit::canInsertFromMimeData(source);
46
void MTextEdit::insertFromMimeData(const QMimeData *source) {
47
if (source->hasImage()) {
48
QStringList formats = source->formats();
50
for (int i=0; i<formats.size(); i++) {
51
if (formats[i] == QLatin1String("image/bmp")) { format = QString::fromLatin1("BMP"); break; }
52
if (formats[i] == QLatin1String("image/jpeg")) { format = QString::fromLatin1("JPG"); break; }
53
if (formats[i] == QLatin1String("image/jpg")) { format = QString::fromLatin1("JPG"); break; }
54
if (formats[i] == QLatin1String("image/gif")) { format = QString::fromLatin1("GIF"); break; }
55
if (formats[i] == QLatin1String("image/png")) { format = QString::fromLatin1("PNG"); break; }
56
if (formats[i] == QLatin1String("image/pbm")) { format = QString::fromLatin1("PBM"); break; }
57
if (formats[i] == QLatin1String("image/pgm")) { format = QString::fromLatin1("PGM"); break; }
58
if (formats[i] == QLatin1String("image/ppm")) { format = QString::fromLatin1("PPM"); break; }
59
if (formats[i] == QLatin1String("image/tiff")) { format = QString::fromLatin1("TIFF"); break; }
60
if (formats[i] == QLatin1String("image/xbm")) { format = QString::fromLatin1("XBM"); break; }
61
if (formats[i] == QLatin1String("image/xpm")) { format = QString::fromLatin1("XPM"); break; }
63
if (!format.isEmpty()) {
64
// dropImage(qvariant_cast<QImage>(source->imageData()), format);
65
dropImage(qvariant_cast<QImage>(source->imageData()), QString::fromLatin1("JPG")); // Sorry, ale cokoli jiného dlouho trvá
69
QTextEdit::insertFromMimeData(source);
73
QMimeData *MTextEdit::createMimeDataFromSelection() const {
74
return QTextEdit::createMimeDataFromSelection();
78
void MTextEdit::dropImage(const QImage& image, const QString& format) {
80
QBuffer buffer(&bytes);
81
buffer.open(QIODevice::WriteOnly);
82
image.save(&buffer, format.toLocal8Bit().data());
84
QByteArray base64 = bytes.toBase64();
86
for (int i=0; i<base64.size(); i++) {
87
base64l.append(base64[i]);
93
QTextCursor cursor = textCursor();
94
QTextImageFormat imageFormat;
95
imageFormat.setWidth ( image.width() );
96
imageFormat.setHeight ( image.height() );
97
imageFormat.setName ( QString::fromLatin1("data:image/%1;base64, %2")
98
.arg(QString::fromLatin1("%1.%2").arg(rand()).arg(format))
99
.arg(QString::fromLatin1(base64l.data()))
101
cursor.insertImage ( imageFormat );
104
#include <Mod/TechDraw/Gui/moc_mtextedit.cpp>