FreeCAD
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#endif33
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
43using namespace Gui;44
45Thumbnail::Thumbnail(int s) : size(s)46{
47}
48
49Thumbnail::~Thumbnail() = default;50
51void Thumbnail::setViewer(View3DInventorViewer* v)52{
53this->viewer = v;54}
55
56void Thumbnail::setSize(int s)57{
58this->size = s;59}
60
61void Thumbnail::setFileName(const char* fn)62{
63this->uri = QUrl::fromLocalFile(QString::fromUtf8(fn));64}
65
66unsigned int Thumbnail::getMemSize () const67{
68return 0;69}
70
71void Thumbnail::Save (Base::Writer &writer) const72{
73// It's only possible to add extra information if force of XML is disabled74if (!writer.isForceXML())75writer.addFile("thumbnails/Thumbnail.png", this);76}
77
78void Thumbnail::Restore(Base::XMLReader &reader)79{
80Q_UNUSED(reader);81//reader.addFile("Thumbnail.png",this);82}
83
84void Thumbnail::SaveDocFile (Base::Writer &writer) const85{
86if (!this->viewer)87return;88QImage img;89if (this->viewer->thread() != QThread::currentThread()) {90qWarning("Cannot create a thumbnail from non-GUI thread");91return;92}93
94QColor invalid;95this->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 snapshot98QPixmap appIcon = Gui::BitmapFactory().pixmap(App::Application::Config()["AppIcon"].c_str());99QPixmap px = appIcon;100if (!img.isNull()) {101// Create a small "Fc" Application icon in the bottom right of the thumbnail102if (App::GetApplication().GetParameterGroupByPath103("User parameter:BaseApp/Preferences/Document")->GetBool("AddThumbnailLogo",true)) {104// only scale app icon if an offscreen image could be created105appIcon = appIcon.scaled(this->size / 4, this->size /4, Qt::KeepAspectRatio, Qt::SmoothTransformation);106px = BitmapFactory().merge(QPixmap::fromImage(img), appIcon, BitmapFactoryInst::BottomRight);107}108else {109px = QPixmap::fromImage(img);110}111}112
113if (!px.isNull()) {114// according to specification add some meta-information to the image115qint64 mt = QDateTime::currentDateTimeUtc().toSecsSinceEpoch();116QString mtime = QString::fromLatin1("%1").arg(mt);117img.setText(QLatin1String("Software"), qApp->applicationName());118img.setText(QLatin1String("Thumb::Mimetype"), QLatin1String("application/x-extension-fcstd"));119img.setText(QLatin1String("Thumb::MTime"), mtime);120img.setText(QLatin1String("Thumb::URI"), this->uri.toString());121
122QByteArray ba;123QBuffer buffer(&ba);124buffer.open(QIODevice::WriteOnly);125px.save(&buffer, "PNG");126writer.Stream().write(ba.constData(), ba.length());127}128}
129
130void Thumbnail::RestoreDocFile(Base::Reader &reader)131{
132Q_UNUSED(reader);133}
134