| 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 |
|
| 24 |
#include <QApplication> |
| 25 |
#include <QBuffer> |
| 26 |
#include <QByteArray> |
| 27 |
#include <QDateTime> |
| 28 |
#include <QImage> |
| 29 |
#include <QThread> |
| 30 |
|
| 31 |
|
| 32 |
#include <App/Application.h> |
| 33 |
#include <Base/Reader.h> |
| 34 |
#include <Base/Writer.h> |
| 35 |
#ifdef _MSC_VER |
| 36 |
# include <zipios++/zipios-config.h> |
| 37 |
#endif |
| 38 |
#include <zipios++/zipfile.h> |
| 39 |
|
| 40 |
#include "Thumbnail.h" |
| 41 |
#include "BitmapFactory.h" |
| 42 |
#include "View3DInventorViewer.h" |
| 43 |
|
| 44 |
|
| 45 |
using namespace Gui; |
| 46 |
|
| 47 |
Thumbnail::Thumbnail(int s) |
| 48 |
: size(s) |
| 49 |
{} |
| 50 |
|
| 51 |
Thumbnail::~Thumbnail() = default; |
| 52 |
|
| 53 |
void Thumbnail::setViewer(View3DInventorViewer* v) |
| 54 |
{ |
| 55 |
this->viewer = v; |
| 56 |
} |
| 57 |
|
| 58 |
void Thumbnail::setSize(int s) |
| 59 |
{ |
| 60 |
this->size = s; |
| 61 |
} |
| 62 |
|
| 63 |
void Thumbnail::setFileName(const char* fn) |
| 64 |
{ |
| 65 |
this->uri = QUrl::fromLocalFile(QString::fromUtf8(fn)); |
| 66 |
} |
| 67 |
|
| 68 |
unsigned int Thumbnail::getMemSize() const |
| 69 |
{ |
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
void Thumbnail::Save(Base::Writer& writer) const |
| 74 |
{ |
| 75 |
// It's only possible to add extra information if force of XML is disabled |
| 76 |
if (!writer.isForceXML()) { |
| 77 |
writer.addFile("thumbnails/Thumbnail.png", this); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
void Thumbnail::Restore(Base::XMLReader& reader) |
| 82 |
{ |
| 83 |
Q_UNUSED(reader); |
| 84 |
// reader.addFile("Thumbnail.png",this); |
| 85 |
} |
| 86 |
|
| 87 |
void Thumbnail::SaveDocFile(Base::Writer& writer) const |
| 88 |
{ |
| 89 |
QImage img; |
| 90 |
bool created = false; |
| 91 |
|
| 92 |
// 1. Try to create the thumbnail from the viewer |
| 93 |
if (this->viewer) { |
| 94 |
if (this->viewer->thread() != QThread::currentThread()) { |
| 95 |
qWarning("Cannot create a thumbnail from non-GUI thread"); |
| 96 |
} |
| 97 |
else { |
| 98 |
QColor invalid; |
| 99 |
this->viewer->imageFromFramebuffer(this->size, this->size, 4, invalid, img); |
| 100 |
created = !img.isNull(); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
// 2. If creation failed (e.g. no viewer or background thread), try to restore from the existing file |
| 105 |
if (!created) { |
| 106 |
QString filename = this->uri.toLocalFile(); |
| 107 |
Base::FileInfo fi(filename.toUtf8().constData()); |
| 108 |
if (fi.exists()) { |
| 109 |
try { |
| 110 |
zipios::ZipFile zf(fi.filePath()); |
| 111 |
// getEntry uses default MatchPath=MATCH. |
| 112 |
zipios::ConstEntryPointer entry = zf.getEntry("thumbnails/Thumbnail.png"); |
| 113 |
if (entry && entry->isValid()) { |
| 114 |
// getInputStream returns a pointer that must be deleted |
| 115 |
std::istream* is = zf.getInputStream(entry); |
| 116 |
if (is) { |
| 117 |
if (is->good()) { |
| 118 |
writer.Stream() << is->rdbuf(); |
| 119 |
delete is; |
| 120 |
return; |
| 121 |
} |
| 122 |
delete is; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
catch (const std::exception&) { |
| 127 |
// If the file isn't a zip or is locked, we ignore it and proceed to fallback |
| 128 |
} |
| 129 |
catch (...) { |
| 130 |
// Ignore unknown exceptions |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
// If we still have no image and no viewer to generate one, we can do nothing more |
| 136 |
if (!this->viewer) { |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
// Get app icon and resize to half size to insert in topbottom position over the current view |
| 141 |
// snapshot |
| 142 |
QPixmap appIcon = Gui::BitmapFactory().pixmap(App::Application::Config()["AppIcon"].c_str()); |
| 143 |
QPixmap px = appIcon; |
| 144 |
if (!img.isNull()) { |
| 145 |
// Create a small "Fc" Application icon in the bottom right of the thumbnail |
| 146 |
if (App::GetApplication() |
| 147 |
.GetParameterGroupByPath("User parameter:BaseApp/Preferences/Document") |
| 148 |
->GetBool("AddThumbnailLogo", false)) { |
| 149 |
// only scale app icon if an offscreen image could be created |
| 150 |
appIcon = appIcon.scaled( |
| 151 |
this->size / 4, |
| 152 |
this->size / 4, |
| 153 |
Qt::KeepAspectRatio, |
| 154 |
Qt::SmoothTransformation |
| 155 |
); |
| 156 |
px = BitmapFactory().merge(QPixmap::fromImage(img), appIcon, BitmapFactoryInst::BottomRight); |
| 157 |
} |
| 158 |
else { |
| 159 |
px = QPixmap::fromImage(img); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
if (!px.isNull()) { |
| 164 |
// according to specification add some meta-information to the image |
| 165 |
qint64 mt = QDateTime::currentDateTimeUtc().toSecsSinceEpoch(); |
| 166 |
QString mtime = QStringLiteral("%1").arg(mt); |
| 167 |
img.setText(QLatin1String("Software"), qApp->applicationName()); |
| 168 |
img.setText(QLatin1String("Thumb::Mimetype"), QLatin1String("application/x-extension-fcstd")); |
| 169 |
img.setText(QLatin1String("Thumb::MTime"), mtime); |
| 170 |
img.setText(QLatin1String("Thumb::URI"), this->uri.toString()); |
| 171 |
|
| 172 |
QByteArray ba; |
| 173 |
QBuffer buffer(&ba); |
| 174 |
buffer.open(QIODevice::WriteOnly); |
| 175 |
px.save(&buffer, "PNG"); |
| 176 |
writer.Stream().write(ba.constData(), ba.length()); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
void Thumbnail::RestoreDocFile(Base::Reader& reader) |
| 181 |
{ |
| 182 |
Q_UNUSED(reader); |
| 183 |
} |