Thumbnail.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 
00026 #ifndef _PreComp_
00027 # include <QBuffer>
00028 # include <QByteArray>
00029 # include <QDateTime>
00030 # include <QImage>
00031 #endif
00032 
00033 #include "Thumbnail.h"
00034 #include "BitmapFactory.h"
00035 #include "View3DInventorViewer.h"
00036 #include <Base/Writer.h>
00037 #include <Base/Reader.h>
00038 #include <App/Application.h>
00039 
00040 using namespace Gui;
00041 
00042 Thumbnail::Thumbnail(int s) : viewer(0), size(s)
00043 {
00044 }
00045 
00046 Thumbnail::~Thumbnail()
00047 {
00048 }
00049 
00050 void Thumbnail::setViewer(View3DInventorViewer* v)
00051 {
00052     this->viewer = v;
00053 }
00054 
00055 void Thumbnail::setSize(int s)
00056 {
00057     this->size = s;
00058 }
00059 
00060 void Thumbnail::setFileName(const char* fn)
00061 {
00062     this->uri = QUrl::fromLocalFile(QString::fromUtf8(fn));
00063 }
00064 
00065 unsigned int Thumbnail::getMemSize (void) const
00066 {
00067     return 0;
00068 }
00069 
00070 void Thumbnail::Save (Base::Writer &writer) const
00071 {
00072     // It's only possible to add extra information if force of XML is disabled
00073     if (writer.isForceXML() == false)
00074         writer.addFile("thumbnails/Thumbnail.png", this);
00075 }
00076 
00077 void Thumbnail::Restore(Base::XMLReader &reader)
00078 {
00079     //reader.addFile("Thumbnail.png",this);
00080 }
00081 
00082 void Thumbnail::SaveDocFile (Base::Writer &writer) const
00083 {
00084     if (!this->viewer)
00085         return;
00086     QImage img;
00087     try {
00088         this->viewer->savePicture(this->size, this->size, View3DInventorViewer::Current, img);
00089     }
00090     catch (...) {
00091         return; // offscreen rendering failed
00092     }
00093 
00094     QPixmap px = Gui::BitmapFactory().pixmap(App::Application::Config()["AppIcon"].c_str());
00095     px = BitmapFactory().merge(QPixmap::fromImage(img),px,BitmapFactoryInst::BottomRight);
00096 
00097     // according to specification add some meta-information to the image
00098     uint mt = QDateTime::currentDateTime().toTime_t();
00099     QString mtime = QString::fromAscii("%1").arg(mt);
00100     img.setText(QLatin1String("Software"), QString::fromUtf8(App::GetApplication().getExecutableName()));
00101     img.setText(QLatin1String("Thumb::Mimetype"), QLatin1String("application/x-extension-fcstd"));
00102     img.setText(QLatin1String("Thumb::MTime"), mtime);
00103     img.setText(QLatin1String("Thumb::URI"), this->uri.toString());
00104 
00105     QByteArray ba;
00106     QBuffer buffer(&ba);
00107     buffer.open(QIODevice::WriteOnly);
00108     px.save(&buffer, "PNG");
00109     writer.Stream().write(ba.constData(), ba.length());
00110 }
00111 
00112 void Thumbnail::RestoreDocFile(Base::Reader &reader)
00113 {
00114 }

Generated on Wed Nov 23 19:00:45 2011 for FreeCAD by  doxygen 1.6.1