FreeCAD

Форк
0
/
Thumbnail.cpp 
133 строки · 4.8 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net>     *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This library is free software; you can redistribute it and/or         *
7
 *   modify it under the terms of the GNU Library General Public           *
8
 *   License as published by the Free Software Foundation; either          *
9
 *   version 2 of the License, or (at your option) any later version.      *
10
 *                                                                         *
11
 *   This library  is distributed in the hope that it will be useful,      *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU Library General Public License for more details.                  *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Library General Public     *
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include "PreCompiled.h"
24

25
#ifndef _PreComp_
26
# include <QApplication>
27
# include <QBuffer>
28
# include <QByteArray>
29
# include <QDateTime>
30
# include <QImage>
31
# include <QThread>
32
#endif
33

34
#include <App/Application.h>
35
#include <Base/Reader.h>
36
#include <Base/Writer.h>
37

38
#include "Thumbnail.h"
39
#include "BitmapFactory.h"
40
#include "View3DInventorViewer.h"
41

42

43
using namespace Gui;
44

45
Thumbnail::Thumbnail(int s) : size(s)
46
{
47
}
48

49
Thumbnail::~Thumbnail() = default;
50

51
void Thumbnail::setViewer(View3DInventorViewer* v)
52
{
53
    this->viewer = v;
54
}
55

56
void Thumbnail::setSize(int s)
57
{
58
    this->size = s;
59
}
60

61
void Thumbnail::setFileName(const char* fn)
62
{
63
    this->uri = QUrl::fromLocalFile(QString::fromUtf8(fn));
64
}
65

66
unsigned int Thumbnail::getMemSize () const
67
{
68
    return 0;
69
}
70

71
void Thumbnail::Save (Base::Writer &writer) const
72
{
73
    // It's only possible to add extra information if force of XML is disabled
74
    if (!writer.isForceXML())
75
        writer.addFile("thumbnails/Thumbnail.png", this);
76
}
77

78
void Thumbnail::Restore(Base::XMLReader &reader)
79
{
80
    Q_UNUSED(reader);
81
    //reader.addFile("Thumbnail.png",this);
82
}
83

84
void Thumbnail::SaveDocFile (Base::Writer &writer) const
85
{
86
    if (!this->viewer)
87
        return;
88
    QImage img;
89
    if (this->viewer->thread() != QThread::currentThread()) {
90
        qWarning("Cannot create a thumbnail from non-GUI thread");
91
        return;
92
    }
93

94
    QColor invalid;
95
    this->viewer->imageFromFramebuffer(this->size, this->size, 4, invalid, img);
96

97
    // Get app icon and resize to half size to insert in topbottom position over the current view snapshot
98
    QPixmap appIcon = Gui::BitmapFactory().pixmap(App::Application::Config()["AppIcon"].c_str());
99
    QPixmap px =  appIcon;
100
    if (!img.isNull()) {
101
        // Create a small "Fc" Application icon in the bottom right of the thumbnail
102
        if (App::GetApplication().GetParameterGroupByPath
103
            ("User parameter:BaseApp/Preferences/Document")->GetBool("AddThumbnailLogo",true)) {
104
            // only scale app icon if an offscreen image could be created
105
            appIcon = appIcon.scaled(this->size / 4, this->size /4, Qt::KeepAspectRatio, Qt::SmoothTransformation);
106
            px = BitmapFactory().merge(QPixmap::fromImage(img), appIcon, BitmapFactoryInst::BottomRight);
107
        }
108
        else {
109
            px = QPixmap::fromImage(img);
110
        }
111
    }
112

113
    if (!px.isNull()) {
114
        // according to specification add some meta-information to the image
115
        qint64 mt = QDateTime::currentDateTimeUtc().toSecsSinceEpoch();
116
        QString mtime = QString::fromLatin1("%1").arg(mt);
117
        img.setText(QLatin1String("Software"), qApp->applicationName());
118
        img.setText(QLatin1String("Thumb::Mimetype"), QLatin1String("application/x-extension-fcstd"));
119
        img.setText(QLatin1String("Thumb::MTime"), mtime);
120
        img.setText(QLatin1String("Thumb::URI"), this->uri.toString());
121

122
        QByteArray ba;
123
        QBuffer buffer(&ba);
124
        buffer.open(QIODevice::WriteOnly);
125
        px.save(&buffer, "PNG");
126
        writer.Stream().write(ba.constData(), ba.length());
127
    }
128
}
129

130
void Thumbnail::RestoreDocFile(Base::Reader &reader)
131
{
132
    Q_UNUSED(reader);
133
}
134

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

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

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

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