1
/* This file is part of the KDE project
2
Copyright (c) 2001 Simon Hausmann <hausmann@kde.org>
3
Copyright (C) 2002, 2003 Nicolas GOUTTE <goutte@kde.org>
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public
7
License as published by the Free Software Foundation; either
8
version 2 of the License, or (at your option) any later version.
10
This library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public License
16
along with this library; see the file COPYING.LIB. If not, write to
17
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
Boston, MA 02111-1307, USA.
26
#include <kio/netaccess.h>
28
#include "koPictureKey.h"
29
#include "koPictureBase.h"
30
#include "koPictureImage.h"
31
#include "koPictureEps.h"
32
#include "koPictureClipart.h"
33
#include "koPictureWmf.h"
34
#include "koPictureShared.h"
36
KoPictureShared::KoPictureShared(void) : m_base(NULL)
40
KoPictureShared::~KoPictureShared(void)
45
KoPictureShared::KoPictureShared(const KoPictureShared &other)
46
: QShared() // Some compilers want it explicitly!
48
// We need to use newCopy, because we want a real copy, not just a copy of the part of KoPictureBase
50
m_base=other.m_base->newCopy();
55
KoPictureShared& KoPictureShared::operator=( const KoPictureShared &other )
58
kdDebug(30003) << "KoPictureShared::= before" << endl;
60
m_base=other.m_base->newCopy();
61
kdDebug(30003) << "KoPictureShared::= after" << endl;
65
KoPictureType::Type KoPictureShared::getType(void) const
68
return m_base->getType();
69
return KoPictureType::TypeUnknown;
72
bool KoPictureShared::isNull(void) const
75
return m_base->isNull();
79
void KoPictureShared::draw(QPainter& painter, int x, int y, int width, int height, int sx, int sy, int sw, int sh, bool fastMode)
82
m_base->draw(painter, x, y, width, height, sx, sy, sw, sh, fastMode);
85
// Draw a red box (easier DEBUG)
86
kdWarning(30003) << "Drawing red rectangle! (KoPictureShared::draw)" << endl;
88
painter.setBrush(QColor(255,0,0));
89
painter.drawRect(x,y,width,height);
94
bool KoPictureShared::loadWmf(QIODevice* io)
96
kdDebug(30003) << "KoPictureShared::loadWmf" << endl;
99
kdError(30003) << "No QIODevice!" << endl;
105
// The extension .wmf was used (KOffice 1.1.x) for QPicture files
106
// For an extern file or in the storage, .wmf can mean a real Windows Meta File.
108
QByteArray array ( io->readAll() );
110
if ((array[0]=='Q') && (array[1]=='P') &&(array[2]=='I') && (array[3]=='C'))
112
m_base=new KoPictureClipart();
113
setExtension("qpic");
117
m_base=new KoPictureWmf();
120
return m_base->load(array, m_extension);
123
bool KoPictureShared::loadTmp(QIODevice* io)
124
// We have a temp file, probably from a downloaded file
125
// We must check the file type
127
kdDebug(30003) << "KoPictureShared::loadTmp" << endl;
130
kdError(30003) << "No QIODevice!" << endl;
134
// The extension .wmf was used (KOffice 1.1.x) for QPicture files
135
// For an extern file or in the storage, .wmf can mean a real Windows Meta File.
137
QByteArray array=io->readAll();
138
QString strExtension;
141
// Try to find the file type by comparing magic on the first few bytes!
142
if ((array[0]==char(0x89)) && (array[1]=='P') &&(array[2]=='N') && (array[3]=='G'))
146
else if ((array[0]==char(0xff)) && (array[1]==char(0xd8)) &&(array[2]==char(0xff)) && (array[3]==char(0xe0)))
150
else if ((array[0]=='B') && (array[1]=='M'))
154
else if ((array[0]==char(0xd7)) && (array[1]==char(0xcd)) &&(array[2]==char(0xc6)) && (array[3]==char(0x9a)))
158
else if ((array[0]=='<') && (array[1]=='?') &&(array[2]=='X') && (array[3]=='M') && (array[4]=='L'))
162
else if ((array[0]=='Q') && (array[1]=='P') &&(array[2]=='I') && (array[3]=='C'))
166
else if ((array[0]=='%') && (array[1]=='!') &&(array[2]=='P') && (array[3]=='S'))
170
else if ((array[0]==char(0xc5)) && (array[1]==char(0xd0)) && (array[2]==char(0xd3)) && (array[3]==char(0xc6)))
172
// So called "MS-DOS EPS file"
177
kdDebug(30003) << "Cannot identify the type of temp file!"
178
<< " Trying to convert to PNG! (in KoPictureShared::loadTmp" << endl;
181
if (!buf.open(IO_ReadOnly))
183
kdError(30003) << "Could not open read buffer!" << endl;
187
QImageIO imageIO(&buf,NULL);
191
kdError(30003) << "Could not read image!" << endl;
197
if (!buf.open(IO_WriteOnly))
199
kdError(30003) << "Could not open write buffer!" << endl;
203
imageIO.setIODevice(&buf);
204
imageIO.setFormat("PNG");
206
if (!imageIO.write())
208
kdError(30003) << "Could not write converted image!" << endl;
216
kdDebug(30003) << "Temp file considered to be " << strExtension << endl;
218
QBuffer buffer(array);
219
buffer.open(IO_ReadOnly);
220
clearAndSetMode(strExtension);
222
flag=m_base->load(&buffer,strExtension);
223
setExtension(strExtension);
231
bool KoPictureShared::loadXpm(QIODevice* io)
233
kdDebug(30003) << "KoPictureShared::loadXpm" << endl;
236
kdError(30003) << "No QIODevice!" << endl;
242
// Old KPresenter XPM files have char(1) instead of some "
243
// Therefore we need to treat XPM separately
245
QByteArray array=io->readAll();
247
// As XPM files are normally only ASCII files, we can replace it without problems
251
while ((pos=array.find(char(1),pos))!=-1)
256
// Now that the XPM file is corrected, we need to load it.
258
m_base=new KoPictureImage();
260
QBuffer buffer(array);
261
bool check = m_base->load(&buffer,"xpm");
266
bool KoPictureShared::save(QIODevice* io)
271
return m_base->save(io);
275
bool KoPictureShared::saveAsKOffice1Dot1(QIODevice* io)
280
return m_base->saveAsKOffice1Dot1(io, getExtension());
284
void KoPictureShared::clear(void)
286
// Clear does not reset the key m_key!
291
void KoPictureShared::clearAndSetMode(const QString& newMode)
296
const QString mode=newMode.lower();
298
// TODO: WMF need to be alone!
299
if ((mode=="svg") || (mode=="qpic"))
301
m_base=new KoPictureClipart();
303
else if (mode=="wmf")
305
m_base=new KoPictureWmf();
307
else if ( (mode=="eps") || (mode=="epsi") || (mode=="epsf") )
309
m_base=new KoPictureEps();
312
{ // TODO: test if QImageIO really knows the file format
313
m_base=new KoPictureImage();
317
QString KoPictureShared::getExtension(void) const
322
void KoPictureShared::setExtension(const QString& extension)
324
m_extension = extension;
327
QString KoPictureShared::getExtensionAsKOffice1Dot1(void) const
329
if (isClipartAsKOffice1Dot1())
330
return "wmf"; // In KOffice 1.1, all cliparts are QPicture but are named as wmf
335
QString KoPictureShared::getMimeType(void) const
338
return m_base->getMimeType(m_extension);
339
return QString(NULL_MIME_TYPE);
342
bool KoPictureShared::load(QIODevice* io, const QString& extension)
344
kdDebug(30003) << "KoPictureShared::load(QIODevice*, const QString&) " << extension << endl;
346
QString ext(extension.lower());
349
else if (ext=="tmp") // ### TODO: also remote scripts need this, don't they?
353
clearAndSetMode(ext);
355
flag=m_base->load(io,ext);
360
kdError(30003) << "File was not loaded! (KoPictureShared::load)" << endl;
365
bool KoPictureShared::loadFromFile(const QString& fileName)
367
kdDebug(30003) << "KoPictureShared::loadFromFile " << fileName << endl;
368
QFile file(fileName);
369
const int pos=fileName.findRev('.');
372
kdDebug(30003) << "File with no extension! Not supported!" << endl;
375
QString extension=fileName.mid(pos+1);
376
if (!file.open(IO_ReadOnly))
378
const bool flag=load(&file,extension);
383
QSize KoPictureShared::getOriginalSize(void) const
386
return m_base->getOriginalSize();
390
QPixmap KoPictureShared::generatePixmap(const QSize& size, bool smoothScale)
393
return m_base->generatePixmap(size, smoothScale);
397
bool KoPictureShared::isClipartAsKOffice1Dot1(void) const
400
return m_base->isClipartAsKOffice1Dot1();
404
QDragObject* KoPictureShared::dragObject( QWidget *dragSource, const char *name )
407
return m_base->dragObject( dragSource, name );
411
QImage KoPictureShared::generateImage(const QSize& size)
414
return m_base->generateImage( size );
418
bool KoPictureShared::hasAlphaBuffer() const
421
return m_base->hasAlphaBuffer();
425
void KoPictureShared::setAlphaBuffer(bool enable)
428
m_base->setAlphaBuffer(enable);
431
QImage KoPictureShared::createAlphaMask(int conversion_flags) const
434
return m_base->createAlphaMask(conversion_flags);
438
void KoPictureShared::clearCache(void)
441
m_base->clearCache();