00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <QPainter>
00022 #include <QFile>
00023
00024 #include <QPixmap>
00025
00026 #include <kdebug.h>
00027 #include <kurl.h>
00028 #include <kio/netaccess.h>
00029
00030 #include "KoPictureKey.h"
00031 #include "KoPictureBase.h"
00032 #include "KoPictureShared.h"
00033 #include "KoPicture.h"
00034
00035 uint KoPicture::uniqueValue = 0;
00036
00037
00038 KoPicture::KoPicture(void) : m_sharedData(NULL)
00039 {
00040 m_uniqueName = "Pictures"+ QString::number(uniqueValue++);
00041 }
00042
00043 KoPicture::~KoPicture(void)
00044 {
00045 unlinkSharedData();
00046 }
00047
00048 QString KoPicture::uniqueName() const
00049 {
00050 return m_uniqueName;
00051 }
00052
00053 KoPicture::KoPicture(const KoPicture &other)
00054 {
00055 m_sharedData=NULL;
00056 (*this)=other;
00057 }
00058
00059 void KoPicture::assignPictureId( uint _id)
00060 {
00061 if ( m_sharedData )
00062 m_sharedData->assignPictureId(_id);
00063 }
00064
00065 QString KoPicture::uniquePictureId() const
00066 {
00067 if ( m_sharedData )
00068 return m_sharedData->uniquePictureId();
00069 else
00070 return QString();
00071 }
00072
00073 KoPicture& KoPicture::operator=( const KoPicture &other )
00074 {
00075
00076 if (other.m_sharedData)
00077 other.linkSharedData();
00078 if (m_sharedData)
00079 unlinkSharedData();
00080 m_sharedData=other.m_sharedData;
00081 m_key=other.m_key;
00082
00083 return *this;
00084 }
00085
00086 void KoPicture::unlinkSharedData(void)
00087 {
00088 if (m_sharedData && m_sharedData->deref())
00089 delete m_sharedData;
00090
00091 m_sharedData=NULL;
00092 }
00093
00094 void KoPicture::linkSharedData(void) const
00095 {
00096 if (m_sharedData)
00097 m_sharedData->ref();
00098 }
00099
00100 void KoPicture::createSharedData(void)
00101 {
00102 if (!m_sharedData)
00103 {
00104 m_sharedData=new KoPictureShared();
00105
00106 }
00107 }
00108
00109 KoPictureType::Type KoPicture::getType(void) const
00110 {
00111 if (m_sharedData)
00112 return m_sharedData->getType();
00113 return KoPictureType::TypeUnknown;
00114 }
00115
00116 KoPictureKey KoPicture::getKey(void) const
00117 {
00118 return m_key;
00119 }
00120
00121 void KoPicture::setKey(const KoPictureKey& key)
00122 {
00123 m_key=key;
00124 }
00125
00126
00127 bool KoPicture::isNull(void) const
00128 {
00129 if (m_sharedData)
00130 return m_sharedData->isNull();
00131 return true;
00132 }
00133
00134 void KoPicture::draw(QPainter& painter, int x, int y, int width, int height, int sx, int sy, int sw, int sh, bool fastMode)
00135 {
00136 if (m_sharedData)
00137 m_sharedData->draw(painter, x, y, width, height, sx, sy, sw, sh, fastMode);
00138 else
00139 {
00140
00141 kWarning(30003) << "Drawing white rectangle! (KoPicture::draw)" << endl;
00142 painter.save();
00143 painter.setBrush(QColor(255, 255, 255));
00144 painter.drawRect(x,y,width,height);
00145 painter.restore();
00146 }
00147 }
00148
00149 bool KoPicture::loadXpm(QIODevice* io)
00150 {
00151 kDebug(30003) << "KoPicture::loadXpm" << endl;
00152 if (!io)
00153 {
00154 kError(30003) << "No QIODevice!" << endl;
00155 return false;
00156 }
00157 createSharedData();
00158 return m_sharedData->loadXpm(io);
00159 }
00160
00161 bool KoPicture::save(QIODevice* io) const
00162 {
00163 if (!io)
00164 return false;
00165 if (m_sharedData)
00166 return m_sharedData->save(io);
00167 return false;
00168 }
00169
00170 bool KoPicture::saveAsBase64( KoXmlWriter& writer ) const
00171 {
00172 if ( m_sharedData )
00173 return m_sharedData->saveAsBase64( writer );
00174 return false;
00175 }
00176
00177 void KoPicture::clear(void)
00178 {
00179 unlinkSharedData();
00180 }
00181
00182 void KoPicture::clearAndSetMode(const QString& newMode)
00183 {
00184 createSharedData();
00185 m_sharedData->clearAndSetMode(newMode);
00186 }
00187
00188 QString KoPicture::getExtension(void) const
00189 {
00190 if (m_sharedData)
00191 return m_sharedData->getExtension();
00192 return "null";
00193 }
00194
00195 QString KoPicture::getMimeType(void) const
00196 {
00197 if (m_sharedData)
00198 return m_sharedData->getMimeType();
00199 return QString(NULL_MIME_TYPE);
00200 }
00201
00202 bool KoPicture::load(QIODevice* io, const QString& extension)
00203 {
00204 kDebug(30003) << "KoPicture::load(QIODevice*, const QString&) " << extension << endl;
00205 createSharedData();
00206
00207 return m_sharedData->load(io,extension);
00208 }
00209
00210 bool KoPicture::loadFromFile(const QString& fileName)
00211 {
00212 kDebug(30003) << "KoPicture::loadFromFile " << fileName << endl;
00213 createSharedData();
00214 return m_sharedData->loadFromFile(fileName);
00215 }
00216
00217 bool KoPicture::loadFromBase64( const QByteArray& str )
00218 {
00219 createSharedData();
00220 return m_sharedData->loadFromBase64( str );
00221 }
00222
00223 QSize KoPicture::getOriginalSize(void) const
00224 {
00225 if (m_sharedData)
00226 return m_sharedData->getOriginalSize();
00227 return QSize(0,0);
00228 }
00229
00230 QPixmap KoPicture::generatePixmap(const QSize& size, bool smoothScale)
00231 {
00232 if (m_sharedData)
00233 return m_sharedData->generatePixmap(size, smoothScale);
00234 return QPixmap();
00235 }
00236
00237 bool KoPicture::setKeyAndDownloadPicture(const KUrl& url, QWidget *window)
00238 {
00239 bool result=false;
00240
00241 QString tmpFileName;
00242 if ( KIO::NetAccess::download(url, tmpFileName, window) )
00243 {
00244 KoPictureKey key;
00245 key.setKeyFromFile( tmpFileName );
00246 setKey( key );
00247 result=loadFromFile( tmpFileName );
00248 KIO::NetAccess::removeTempFile( tmpFileName );
00249 }
00250
00251 return result;
00252 }
00253
00254 Q3DragObject* KoPicture::dragObject( QWidget *dragSource, const char *name )
00255 {
00256 if (m_sharedData)
00257 return m_sharedData->dragObject( dragSource, name );
00258 return 0L;
00259 }
00260
00261 QImage KoPicture::generateImage(const QSize& size)
00262 {
00263 if (m_sharedData)
00264 return m_sharedData->generateImage( size );
00265 return QImage();
00266 }
00267
00268 bool KoPicture::hasAlphaBuffer() const
00269 {
00270 if (m_sharedData)
00271 return m_sharedData->hasAlphaBuffer();
00272 return false;
00273 }
00274
00275 void KoPicture::setAlphaBuffer(bool enable)
00276 {
00277 if (m_sharedData)
00278 m_sharedData->setAlphaBuffer(enable);
00279 }
00280
00281 QImage KoPicture::createAlphaMask(Qt::ImageConversionFlags flags) const
00282 {
00283 if (m_sharedData)
00284 return m_sharedData->createAlphaMask(flags);
00285 return QImage();
00286 }
00287
00288 void KoPicture::clearCache(void)
00289 {
00290 if (m_sharedData)
00291 m_sharedData->clearCache();
00292 }