26#include <kfilterdev.h>
27#include <tdeio/netaccess.h>
29#include "KoPictureKey.h"
30#include "KoPictureBase.h"
31#include "KoPictureImage.h"
32#include "KoPictureEps.h"
33#include "KoPictureClipart.h"
34#include "KoPictureWmf.h"
35#include "KoPictureShared.h"
39KoPictureShared::KoPictureShared(
void) : m_base(NULL)
43void KoPictureShared::assignPictureId( uint _id)
48TQString KoPictureShared::uniquePictureId()
const
50 return "Pictures"+ TQString::number(m_pictureId);
53KoPictureShared::~KoPictureShared(
void)
58KoPictureShared::KoPictureShared(
const KoPictureShared &other)
63 m_base=other.m_base->newCopy();
68KoPictureShared& KoPictureShared::operator=(
const KoPictureShared &other )
71 kdDebug(30003) <<
"KoPictureShared::= before" << endl;
73 m_base=other.m_base->newCopy();
74 kdDebug(30003) <<
"KoPictureShared::= after" << endl;
78KoPictureType::Type KoPictureShared::getType(
void)
const
81 return m_base->getType();
82 return KoPictureType::TypeUnknown;
85bool KoPictureShared::isNull(
void)
const
88 return m_base->isNull();
92void KoPictureShared::draw(TQPainter& painter,
int x,
int y,
int width,
int height,
int sx,
int sy,
int sw,
int sh,
bool fastMode)
95 m_base->draw(painter, x, y, width, height, sx, sy, sw, sh, fastMode);
99 kdWarning(30003) <<
"Drawing red rectangle! (KoPictureShared::draw)" << endl;
101 painter.setBrush(TQColor(255,0,0));
102 painter.drawRect(x,y,width,height);
107bool KoPictureShared::loadWmf(TQIODevice* io)
109 kdDebug(30003) <<
"KoPictureShared::loadWmf" << endl;
112 kdError(30003) <<
"No TQIODevice!" << endl;
121 TQByteArray array ( io->readAll() );
123 if ((array[0]==
'Q') && (array[1]==
'P') &&(array[2]==
'I') && (array[3]==
'C'))
125 m_base=
new KoPictureClipart();
126 setExtension(
"qpic");
130 m_base=
new KoPictureWmf();
133 return m_base->loadData(array, m_extension);
136bool KoPictureShared::loadTmp(TQIODevice* io)
140 kdDebug(30003) <<
"KoPictureShared::loadTmp" << endl;
143 kdError(30003) <<
"No TQIODevice!" << endl;
150 TQByteArray array ( io->readAll() );
151 return identifyAndLoad( array );
154bool KoPictureShared::identifyAndLoad( TQByteArray array )
156 if ( array.size() < 5 )
158 kdError(30003) <<
"Picture is less than 5 bytes long!" << endl;
162 TQString strExtension;
167 if ((array[0]==
char(0x89)) && (array[1]==
'P') &&(array[2]==
'N') && (array[3]==
'G'))
171 else if ((array[0]==
char(0xff)) && (array[1]==
char(0xd8)) &&(array[2]==
char(0xff)) && (array[3]==
char(0xe0)))
175 else if ((array[0]==
'B') && (array[1]==
'M'))
179 else if ((array[0]==
char(0xd7)) && (array[1]==
char(0xcd)) &&(array[2]==
char(0xc6)) && (array[3]==
char(0x9a)))
183 else if ((array[0]==
'<') && (array[1]==
'?') && ( array[2]==
'x' ) && (array[3]==
'm') && ( array[4]==
'l' ) )
187 else if ((array[0]==
'Q') && (array[1]==
'P') &&(array[2]==
'I') && (array[3]==
'C'))
191 else if ((array[0]==
'%') && (array[1]==
'!') &&(array[2]==
'P') && (array[3]==
'S'))
195 else if ((array[0]==
char(0xc5)) && (array[1]==
char(0xd0)) && (array[2]==
char(0xd3)) && (array[3]==
char(0xc6)))
200 else if ((array[0]==
'G') && (array[1]==
'I') && (array[2]==
'F') && (array[3]==
'8'))
205 else if ( ( array[0] ==
char( 0037 ) ) && ( array[1] ==
char( 0213 ) ) )
208 TQBuffer buffer(array);
209 buffer.open(IO_ReadOnly);
211 const bool flag = loadCompressed( &buffer,
"application/x-gzip",
"tmp" );
215 else if ( ( array[0] ==
'B' ) && ( array[1] ==
'Z' ) && ( array[2] ==
'h') )
218 TQBuffer buffer(array);
219 buffer.open(IO_ReadOnly);
220 const bool flag = loadCompressed( &buffer,
"application/x-bzip2",
"tmp" );
226 kdDebug(30003) <<
"Cannot identify the type of temp file!"
227 <<
" Trying to convert to PNG! (in KoPictureShared::loadTmp" << endl;
232 TQBuffer buf( array.copy() );
233 if (!buf.open(IO_ReadOnly))
235 kdError(30003) <<
"Could not open read buffer!" << endl;
239 TQImageIO imageIO(&buf,NULL);
243 kdError(30003) <<
"Could not read image!" << endl;
249 if ( !buf.open( IO_WriteOnly | IO_Truncate ) )
251 kdError(30003) <<
"Could not open write buffer!" << endl;
255 imageIO.setIODevice(&buf);
256 imageIO.setFormat(
"PNG");
258 if (!imageIO.write())
260 kdError(30003) <<
"Could not write converted image!" << endl;
265 array = buf.buffer();
270 kdDebug(30003) <<
"Temp file considered to be " << strExtension << endl;
272 clearAndSetMode(strExtension);
274 flag = m_base->loadData(array,strExtension);
275 setExtension(strExtension);
282bool KoPictureShared::loadXpm(TQIODevice* io)
284 kdDebug(30003) <<
"KoPictureShared::loadXpm" << endl;
287 kdError(30003) <<
"No TQIODevice!" << endl;
296 TQByteArray array=io->readAll();
302 while ((pos=array.find(
char(1),pos))!=-1)
309 m_base=
new KoPictureImage();
311 TQBuffer buffer(array);
312 bool check = m_base->load(&buffer,
"xpm");
317bool KoPictureShared::save(TQIODevice* io)
const
322 return m_base->save(io);
326bool KoPictureShared::saveAsBase64(
KoXmlWriter& writer )
const
329 m_base->saveAsBase64( writer );
333void KoPictureShared::clear(
void)
340void KoPictureShared::clearAndSetMode(
const TQString& newMode)
345 const TQString mode=newMode.lower();
347 if ((mode==
"svg") || (mode==
"qpic"))
349 m_base=
new KoPictureClipart();
351 else if (mode==
"wmf")
353 m_base=
new KoPictureWmf();
355 else if ( (mode==
"eps") || (mode==
"epsi") || (mode==
"epsf") )
357 m_base=
new KoPictureEps();
361 m_base=
new KoPictureImage();
365TQString KoPictureShared::getExtension(
void)
const
370void KoPictureShared::setExtension(
const TQString& extension)
372 m_extension = extension;
375TQString KoPictureShared::getMimeType(
void)
const
378 return m_base->getMimeType(m_extension);
379 return TQString(NULL_MIME_TYPE);
383bool KoPictureShared::loadFromBase64(
const TQCString& str )
387 KCodecs::base64Decode( str, data );
388 return identifyAndLoad( data );
391bool KoPictureShared::load(TQIODevice* io,
const TQString& extension)
393 kdDebug(30003) <<
"KoPictureShared::load(TQIODevice*, const TQString&) " << extension << endl;
395 TQString ext(extension.lower());
400 else if ( ext ==
"bz2" )
402 flag = loadCompressed( io,
"application/x-bzip2",
"tmp" );
404 else if ( ext ==
"gz" )
406 flag = loadCompressed( io,
"application/x-gzip",
"tmp" );
408 else if ( ext ==
"svgz" )
410 flag = loadCompressed( io,
"application/x-gzip",
"svg" );
414 clearAndSetMode(ext);
416 flag = m_base->load(io, ext);
421 kdError(30003) <<
"File was not loaded! (KoPictureShared::load)" << endl;
426bool KoPictureShared::loadFromFile(
const TQString& fileName)
428 kdDebug(30003) <<
"KoPictureShared::loadFromFile " << fileName << endl;
429 if ( fileName.isEmpty() )
431 kdError(30003) <<
"Cannot load file with empty name!" << endl;
434 TQFile file(fileName);
435 if (!file.open(IO_ReadOnly))
439 const int pos=fileName.findRev(
'.');
442 kdDebug(30003) <<
"File with no extension!" << endl;
444 flag = loadTmp( &file );
448 const TQString extension( fileName.mid( pos+1 ) );
450 flag = load( &file, extension );
456TQSize KoPictureShared::getOriginalSize(
void)
const
459 return m_base->getOriginalSize();
463TQPixmap KoPictureShared::generatePixmap(
const TQSize& size,
bool smoothScale)
466 return m_base->generatePixmap(size, smoothScale);
470TQDragObject* KoPictureShared::dragObject( TQWidget *dragSource,
const char *name )
473 return m_base->dragObject( dragSource, name );
477TQImage KoPictureShared::generateImage(
const TQSize& size)
480 return m_base->generateImage( size );
484bool KoPictureShared::hasAlphaBuffer()
const
487 return m_base->hasAlphaBuffer();
491void KoPictureShared::setAlphaBuffer(
bool enable)
494 m_base->setAlphaBuffer(enable);
497TQImage KoPictureShared::createAlphaMask(
int conversion_flags)
const
500 return m_base->createAlphaMask(conversion_flags);
504void KoPictureShared::clearCache(
void)
507 m_base->clearCache();
510bool KoPictureShared::loadCompressed( TQIODevice* io,
const TQString& mimeType,
const TQString& extension )
513 TQIODevice* in = KFilterDev::device( io, mimeType,
false);
517 kdError(30003) <<
"Cannot create device for uncompressing! Aborting!" << endl;
522 if ( !in->open( IO_ReadOnly ) )
524 kdError(30003) <<
"Cannot open file for uncompressing! Aborting!" << endl;
529 const bool flag = load( in, extension );
A class for writing out XML (to any TQIODevice), with a special attention on performance.
TDEAction * clear(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)