FreeCAD

Форк
0
/
mtextedit.cpp 
104 строки · 4.1 Кб
1
/*
2
** Copyright (C) 2013 Jiří Procházka (Hobrasoft)
3
** Contact: http://www.hobrasoft.cz/
4
**
5
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
6
** Contact: http://www.qt-project.org/legal
7
**
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.
16
**
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.
20
**
21
** $QT_END_LICENSE$
22
*/
23
// clazy:excludeall=qstring-arg
24

25
#include "PreCompiled.h"
26
#ifndef _PreComp_
27
# include <cstdlib>
28
# include <QBuffer>
29
# include <QByteArray>
30
# include <QImage>
31
# include <QTextCursor>
32
#endif
33

34
#include "mtextedit.h"
35

36

37
MTextEdit::MTextEdit(QWidget *parent) : QTextEdit(parent) {
38
}
39

40

41
bool MTextEdit::canInsertFromMimeData(const QMimeData *source) const {
42
    return source->hasImage() || QTextEdit::canInsertFromMimeData(source);
43
}
44

45

46
void MTextEdit::insertFromMimeData(const QMimeData *source) {
47
    if (source->hasImage()) {
48
        QStringList formats = source->formats();
49
        QString format;
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; }
62
            }
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á
66
            return;
67
            }
68
        }
69
    QTextEdit::insertFromMimeData(source);
70
}
71

72

73
QMimeData *MTextEdit::createMimeDataFromSelection() const {
74
    return QTextEdit::createMimeDataFromSelection();
75
}
76

77

78
void MTextEdit::dropImage(const QImage& image, const QString& format) {
79
    QByteArray bytes;
80
    QBuffer buffer(&bytes);
81
    buffer.open(QIODevice::WriteOnly);
82
    image.save(&buffer, format.toLocal8Bit().data());
83
    buffer.close();
84
    QByteArray base64 = bytes.toBase64();
85
    QByteArray base64l;
86
    for (int i=0; i<base64.size(); i++) {
87
        base64l.append(base64[i]);
88
        if (i%80 == 0) {
89
            base64l.append("\n");
90
            }
91
        }
92

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()))
100
                                );
101
    cursor.insertImage    ( imageFormat );
102
}
103

104
#include <Mod/TechDraw/Gui/moc_mtextedit.cpp>
105

106

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.