lib Library API Documentation

koPictureShared.cc

00001 /* This file is part of the KDE project 00002 Copyright (c) 2001 Simon Hausmann <hausmann@kde.org> 00003 Copyright (C) 2002, 2003 Nicolas GOUTTE <goutte@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, USA. 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() // Some compilers want it explicitly! 00047 { 00048 // We need to use newCopy, because we want a real copy, not just a copy of the part of KoPictureBase 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 // Draw a red box (easier DEBUG) 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 // The extension .wmf was used (KOffice 1.1.x) for QPicture files 00106 // For an extern file or in the storage, .wmf can mean a real Windows Meta File. 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 // We have a temp file, probably from a downloaded file 00125 // We must check the file type 00126 { 00127 kdDebug(30003) << "KoPictureShared::loadTmp" << endl; 00128 if (!io) 00129 { 00130 kdError(30003) << "No QIODevice!" << endl; 00131 return false; 00132 } 00133 00134 // The extension .wmf was used (KOffice 1.1.x) for QPicture files 00135 // For an extern file or in the storage, .wmf can mean a real Windows Meta File. 00136 00137 QByteArray array=io->readAll(); 00138 QString strExtension; 00139 bool flag=false; 00140 00141 // Try to find the file type by comparing magic on the first few bytes! 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 // So called "MS-DOS EPS file" 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 // Old KPresenter XPM files have char(1) instead of some " 00243 // Therefore we need to treat XPM separately 00244 00245 QByteArray array=io->readAll(); 00246 00247 // As XPM files are normally only ASCII files, we can replace it without problems 00248 00249 int pos=0; 00250 00251 while ((pos=array.find(char(1),pos))!=-1) 00252 { 00253 array[pos]='"'; 00254 } 00255 00256 // Now that the XPM file is corrected, we need to load it. 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 // Clear does not reset the key m_key! 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 // TODO: WMF need to be alone! 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 { // TODO: test if QImageIO really knows the file format 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"; // In KOffice 1.1, all cliparts are QPicture but are named as 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") // ### TODO: also remote scripts need this, don't they? 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 }
KDE Logo
This file is part of the documentation for lib Library Version 1.3.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Sep 28 04:04:01 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003