00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
#include <qpainter.h>
00022
#include <qfile.h>
00023
00024
#include <kdebug.h>
00025
#include <kurl.h>
00026
#include <kio/netaccess.h>
00027
00028
#include "koPictureKey.h"
00029
#include "koPictureBase.h"
00030
#include "koPictureImage.h"
00031
#include "koPictureEps.h"
00032
#include "koPictureClipart.h"
00033
#include "koPictureWmf.h"
00034
#include "koPictureShared.h"
00035
00036 KoPictureShared::KoPictureShared(
void) : m_base(NULL)
00037 {
00038 }
00039
00040 KoPictureShared::~KoPictureShared(
void)
00041 {
00042
delete m_base;
00043 }
00044
00045 KoPictureShared::KoPictureShared(
const KoPictureShared &other)
00046 : QShared()
00047 {
00048
00049
if (other.m_base)
00050 m_base=other.m_base->newCopy();
00051
else
00052 m_base=NULL;
00053 }
00054
00055 KoPictureShared& KoPictureShared::operator=(
const KoPictureShared &other )
00056 {
00057 clear();
00058 kdDebug(30003) <<
"KoPictureShared::= before" << endl;
00059
if (other.m_base)
00060 m_base=other.m_base->newCopy();
00061 kdDebug(30003) <<
"KoPictureShared::= after" << endl;
00062
return *
this;
00063 }
00064
00065 KoPictureType::Type KoPictureShared::getType(
void)
const
00066
{
00067
if (m_base)
00068
return m_base->getType();
00069
return KoPictureType::TypeUnknown;
00070 }
00071
00072
bool KoPictureShared::isNull(
void)
const
00073
{
00074
if (m_base)
00075
return m_base->isNull();
00076
return true;
00077 }
00078
00079
void KoPictureShared::draw(
QPainter& painter,
int x,
int y,
int width,
int height,
int sx,
int sy,
int sw,
int sh,
bool fastMode)
00080 {
00081
if (m_base)
00082 m_base->draw(painter, x, y, width, height, sx, sy, sw, sh, fastMode);
00083
else
00084 {
00085
00086 kdWarning(30003) <<
"Drawing red rectangle! (KoPictureShared::draw)" << endl;
00087 painter.save();
00088 painter.setBrush(
QColor(255,0,0));
00089 painter.drawRect(x,y,width,height);
00090 painter.restore();
00091 }
00092 }
00093
00094
bool KoPictureShared::loadWmf(
QIODevice* io)
00095 {
00096 kdDebug(30003) <<
"KoPictureShared::loadWmf" << endl;
00097
if (!io)
00098 {
00099 kdError(30003) <<
"No QIODevice!" << endl;
00100
return false;
00101 }
00102
00103 clear();
00104
00105
00106
00107
00108
QByteArray array ( io->readAll() );
00109
00110
if ((array[0]==
'Q') && (array[1]==
'P') &&(array[2]==
'I') && (array[3]==
'C'))
00111 {
00112 m_base=
new KoPictureClipart();
00113 setExtension(
"qpic");
00114 }
00115
else
00116 {
00117 m_base=
new KoPictureWmf();
00118 setExtension(
"wmf");
00119 }
00120
return m_base->load(array, m_extension);
00121 }
00122
00123
bool KoPictureShared::loadTmp(
QIODevice* io)
00124
00125
00126 {
00127 kdDebug(30003) <<
"KoPictureShared::loadTmp" << endl;
00128
if (!io)
00129 {
00130 kdError(30003) <<
"No QIODevice!" << endl;
00131
return false;
00132 }
00133
00134
00135
00136
00137
QByteArray array=io->readAll();
00138
QString strExtension;
00139
bool flag=
false;
00140
00141
00142
if ((array[0]==char(0x89)) && (array[1]==
'P') &&(array[2]==
'N') && (array[3]==
'G'))
00143 {
00144 strExtension=
"png";
00145 }
00146
else if ((array[0]==char(0xff)) && (array[1]==char(0xd8)) &&(array[2]==char(0xff)) && (array[3]==char(0xe0)))
00147 {
00148 strExtension=
"jpeg";
00149 }
00150
else if ((array[0]==
'B') && (array[1]==
'M'))
00151 {
00152 strExtension=
"bmp";
00153 }
00154
else if ((array[0]==char(0xd7)) && (array[1]==char(0xcd)) &&(array[2]==char(0xc6)) && (array[3]==char(0x9a)))
00155 {
00156 strExtension=
"wmf";
00157 }
00158
else if ((array[0]==
'<') && (array[1]==
'?') &&(array[2]==
'X') && (array[3]==
'M') && (array[4]==
'L'))
00159 {
00160 strExtension=
"svg";
00161 }
00162
else if ((array[0]==
'Q') && (array[1]==
'P') &&(array[2]==
'I') && (array[3]==
'C'))
00163 {
00164 strExtension=
"qpic";
00165 }
00166
else if ((array[0]==
'%') && (array[1]==
'!') &&(array[2]==
'P') && (array[3]==
'S'))
00167 {
00168 strExtension=
"eps";
00169 }
00170
else if ((array[0]==char(0xc5)) && (array[1]==char(0xd0)) && (array[2]==char(0xd3)) && (array[3]==char(0xc6)))
00171 {
00172
00173 strExtension=
"eps";
00174 }
00175
else
00176 {
00177 kdDebug(30003) <<
"Cannot identify the type of temp file!"
00178 <<
" Trying to convert to PNG! (in KoPictureShared::loadTmp" << endl;
00179
00180
QBuffer buf(array);
00181
if (!buf.open(IO_ReadOnly))
00182 {
00183 kdError(30003) <<
"Could not open read buffer!" << endl;
00184
return false;
00185 }
00186
00187
QImageIO imageIO(&buf,NULL);
00188
00189
if (!imageIO.read())
00190 {
00191 kdError(30003) <<
"Could not read image!" << endl;
00192
return false;
00193 }
00194
00195 buf.close();
00196
00197
if (!buf.open(IO_WriteOnly))
00198 {
00199 kdError(30003) <<
"Could not open write buffer!" << endl;
00200
return false;
00201 }
00202
00203 imageIO.setIODevice(&buf);
00204 imageIO.setFormat(
"PNG");
00205
00206
if (!imageIO.write())
00207 {
00208 kdError(30003) <<
"Could not write converted image!" << endl;
00209
return false;
00210 }
00211 buf.close();
00212
00213 strExtension=
"png";
00214 }
00215
00216 kdDebug(30003) <<
"Temp file considered to be " << strExtension << endl;
00217
00218
QBuffer buffer(array);
00219 buffer.open(IO_ReadOnly);
00220 clearAndSetMode(strExtension);
00221
if (m_base)
00222 flag=m_base->load(&buffer,strExtension);
00223 setExtension(strExtension);
00224 buffer.close();
00225
00226
return flag;
00227 }
00228
00229
00230
00231
bool KoPictureShared::loadXpm(
QIODevice* io)
00232 {
00233 kdDebug(30003) <<
"KoPictureShared::loadXpm" << endl;
00234
if (!io)
00235 {
00236 kdError(30003) <<
"No QIODevice!" << endl;
00237
return false;
00238 }
00239
00240 clear();
00241
00242
00243
00244
00245
QByteArray array=io->readAll();
00246
00247
00248
00249
int pos=0;
00250
00251
while ((pos=array.find(
char(1),pos))!=-1)
00252 {
00253 array[pos]=
'"';
00254 }
00255
00256
00257
00258 m_base=
new KoPictureImage();
00259
00260
QBuffer buffer(array);
00261
bool check = m_base->load(&buffer,
"xpm");
00262 setExtension(
"xpm");
00263
return check;
00264 }
00265
00266
bool KoPictureShared::save(
QIODevice* io)
00267 {
00268
if (!io)
00269
return false;
00270
if (m_base)
00271
return m_base->save(io);
00272
return false;
00273 }
00274
00275
bool KoPictureShared::saveAsKOffice1Dot1(
QIODevice* io)
00276 {
00277
if (!io)
00278
return false;
00279
if (m_base)
00280
return m_base->saveAsKOffice1Dot1(io, getExtension());
00281
return false;
00282 }
00283
00284
void KoPictureShared::clear(
void)
00285 {
00286
00287
delete m_base;
00288 m_base=NULL;
00289 }
00290
00291
void KoPictureShared::clearAndSetMode(
const QString& newMode)
00292 {
00293
delete m_base;
00294 m_base=NULL;
00295
00296
const QString mode=newMode.lower();
00297
00298
00299
if ((mode==
"svg") || (mode==
"qpic"))
00300 {
00301 m_base=
new KoPictureClipart();
00302 }
00303
else if (mode==
"wmf")
00304 {
00305 m_base=
new KoPictureWmf();
00306 }
00307
else if ( (mode==
"eps") || (mode==
"epsi") || (mode==
"epsf") )
00308 {
00309 m_base=
new KoPictureEps();
00310 }
00311
else
00312 {
00313 m_base=
new KoPictureImage();
00314 }
00315 }
00316
00317
QString KoPictureShared::getExtension(
void)
const
00318
{
00319
return m_extension;
00320 }
00321
00322
void KoPictureShared::setExtension(
const QString& extension)
00323 {
00324 m_extension = extension;
00325 }
00326
00327
QString KoPictureShared::getExtensionAsKOffice1Dot1(
void)
const
00328
{
00329
if (isClipartAsKOffice1Dot1())
00330
return "wmf";
00331
else
00332
return m_extension;
00333 }
00334
00335
QString KoPictureShared::getMimeType(
void)
const
00336
{
00337
if (m_base)
00338
return m_base->getMimeType(m_extension);
00339
return QString(NULL_MIME_TYPE);
00340 }
00341
00342
bool KoPictureShared::load(
QIODevice* io,
const QString& extension)
00343 {
00344 kdDebug(30003) <<
"KoPictureShared::load(QIODevice*, const QString&) " << extension << endl;
00345
bool flag=
false;
00346
QString ext(extension.lower());
00347
if (ext==
"wmf")
00348 flag=loadWmf(io);
00349
else if (ext==
"tmp")
00350 flag=loadTmp(io);
00351
else
00352 {
00353 clearAndSetMode(ext);
00354
if (m_base)
00355 flag=m_base->load(io,ext);
00356 setExtension(ext);
00357 }
00358
if (!flag)
00359 {
00360 kdError(30003) <<
"File was not loaded! (KoPictureShared::load)" << endl;
00361 }
00362
return flag;
00363 }
00364
00365
bool KoPictureShared::loadFromFile(
const QString& fileName)
00366 {
00367 kdDebug(30003) <<
"KoPictureShared::loadFromFile " << fileName << endl;
00368
QFile file(fileName);
00369
const int pos=fileName.findRev(
'.');
00370
if (pos==-1)
00371 {
00372 kdDebug(30003) <<
"File with no extension! Not supported!" << endl;
00373
return false;
00374 }
00375
QString extension=fileName.mid(pos+1);
00376
if (!file.open(IO_ReadOnly))
00377
return false;
00378
const bool flag=load(&file,extension);
00379 file.close();
00380
return flag;
00381 }
00382
00383
QSize KoPictureShared::getOriginalSize(
void)
const
00384
{
00385
if (m_base)
00386
return m_base->getOriginalSize();
00387
return QSize(0,0);
00388 }
00389
00390
QPixmap KoPictureShared::generatePixmap(
const QSize& size,
bool smoothScale)
00391 {
00392
if (m_base)
00393
return m_base->generatePixmap(size, smoothScale);
00394
return QPixmap();
00395 }
00396
00397
bool KoPictureShared::isClipartAsKOffice1Dot1(
void)
const
00398
{
00399
if (m_base)
00400
return m_base->isClipartAsKOffice1Dot1();
00401
return false;
00402 }
00403
00404
QDragObject* KoPictureShared::dragObject(
QWidget *dragSource,
const char *name )
00405 {
00406
if (m_base)
00407
return m_base->dragObject( dragSource, name );
00408
return 0L;
00409 }
00410
00411
QImage KoPictureShared::generateImage(
const QSize& size)
00412 {
00413
if (m_base)
00414
return m_base->generateImage( size );
00415
return QImage();
00416 }
00417
00418
bool KoPictureShared::hasAlphaBuffer()
const
00419
{
00420
if (m_base)
00421
return m_base->hasAlphaBuffer();
00422
return false;
00423 }
00424
00425
void KoPictureShared::setAlphaBuffer(
bool enable)
00426 {
00427
if (m_base)
00428 m_base->setAlphaBuffer(enable);
00429 }
00430
00431
QImage KoPictureShared::createAlphaMask(
int conversion_flags)
const
00432
{
00433
if (m_base)
00434
return m_base->createAlphaMask(conversion_flags);
00435
return QImage();
00436 }
00437
00438
void KoPictureShared::clearCache(
void)
00439 {
00440
if (m_base)
00441 m_base->clearCache();
00442 }