KoPicture.cpp
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>
4
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.
9
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.
14
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., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include <tqpainter.h>
22#include <tqfile.h>
23
24#include <kdebug.h>
25#include <kurl.h>
26#include <tdeio/netaccess.h>
27
28#include "KoPictureKey.h"
29#include "KoPictureBase.h"
30#include "KoPictureShared.h"
31#include "KoPicture.h"
32
33uint KoPicture::uniqueValue = 0;
34
35
36KoPicture::KoPicture(void) : m_sharedData(NULL)
37{
38 m_uniqueName = "Pictures"+ TQString::number(uniqueValue++);
39}
40
42{
43 unlinkSharedData();
44}
45
46TQString KoPicture::uniqueName() const
47{
48 return m_uniqueName;
49}
50
52{
53 m_sharedData=NULL;
54 (*this)=other;
55}
56
57void KoPicture::assignPictureId( uint _id)
58{
59 if ( m_sharedData )
60 m_sharedData->assignPictureId(_id);
61}
62
63TQString KoPicture::uniquePictureId() const
64{
65 if ( m_sharedData )
66 return m_sharedData->uniquePictureId();
67 else
68 return TQString();
69}
70
72{
73 //kdDebug(30003) << "KoPicture::= before" << endl;
74 if (other.m_sharedData)
75 other.linkSharedData();
76 if (m_sharedData)
77 unlinkSharedData();
78 m_sharedData=other.m_sharedData;
79 m_key=other.m_key;
80 //kdDebug(30003) << "KoPicture::= after" << endl;
81 return *this;
82}
83
84void KoPicture::unlinkSharedData(void)
85{
86 if (m_sharedData && m_sharedData->deref())
87 delete m_sharedData;
88
89 m_sharedData=NULL;
90}
91
92void KoPicture::linkSharedData(void) const
93{
94 if (m_sharedData)
95 m_sharedData->ref();
96}
97
98void KoPicture::createSharedData(void)
99{
100 if (!m_sharedData)
101 {
102 m_sharedData=new KoPictureShared();
103 // Do not call m_sharedData->ref()
104 }
105}
106
107KoPictureType::Type KoPicture::getType(void) const
108{
109 if (m_sharedData)
110 return m_sharedData->getType();
111 return KoPictureType::TypeUnknown;
112}
113
115{
116 return m_key;
117}
118
120{
121 m_key=key;
122}
123
124
125bool KoPicture::isNull(void) const
126{
127 if (m_sharedData)
128 return m_sharedData->isNull();
129 return true;
130}
131
132void KoPicture::draw(TQPainter& painter, int x, int y, int width, int height, int sx, int sy, int sw, int sh, bool fastMode)
133{
134 if (m_sharedData)
135 m_sharedData->draw(painter, x, y, width, height, sx, sy, sw, sh, fastMode);
136 else
137 {
138 // Draw a white box
139 kdWarning(30003) << "Drawing white rectangle! (KoPicture::draw)" << endl;
140 painter.save();
141 painter.setBrush(TQColor(255, 255, 255));
142 painter.drawRect(x,y,width,height);
143 painter.restore();
144 }
145}
146
147bool KoPicture::loadXpm(TQIODevice* io)
148{
149 kdDebug(30003) << "KoPicture::loadXpm" << endl;
150 if (!io)
151 {
152 kdError(30003) << "No TQIODevice!" << endl;
153 return false;
154 }
155 createSharedData();
156 return m_sharedData->loadXpm(io);
157}
158
159bool KoPicture::save(TQIODevice* io) const
160{
161 if (!io)
162 return false;
163 if (m_sharedData)
164 return m_sharedData->save(io);
165 return false;
166}
167
169{
170 if ( m_sharedData )
171 return m_sharedData->saveAsBase64( writer );
172 return false;
173}
174
176{
177 unlinkSharedData();
178}
179
180void KoPicture::clearAndSetMode(const TQString& newMode)
181{
182 createSharedData();
183 m_sharedData->clearAndSetMode(newMode);
184}
185
186TQString KoPicture::getExtension(void) const
187{
188 if (m_sharedData)
189 return m_sharedData->getExtension();
190 return "null"; // Just a dummy
191}
192
193TQString KoPicture::getMimeType(void) const
194{
195 if (m_sharedData)
196 return m_sharedData->getMimeType();
197 return TQString(NULL_MIME_TYPE);
198}
199
200bool KoPicture::load(TQIODevice* io, const TQString& extension)
201{
202 kdDebug(30003) << "KoPicture::load(TQIODevice*, const TQString&) " << extension << endl;
203 createSharedData();
204
205 return m_sharedData->load(io,extension);
206}
207
208bool KoPicture::loadFromFile(const TQString& fileName)
209{
210 kdDebug(30003) << "KoPicture::loadFromFile " << fileName << endl;
211 createSharedData();
212 return m_sharedData->loadFromFile(fileName);
213}
214
215bool KoPicture::loadFromBase64( const TQCString& str )
216{
217 createSharedData();
218 return m_sharedData->loadFromBase64( str );
219}
220
222{
223 if (m_sharedData)
224 return m_sharedData->getOriginalSize();
225 return TQSize(0,0);
226}
227
228TQPixmap KoPicture::generatePixmap(const TQSize& size, bool smoothScale)
229{
230 if (m_sharedData)
231 return m_sharedData->generatePixmap(size, smoothScale);
232 return TQPixmap();
233}
234
235bool KoPicture::setKeyAndDownloadPicture(const KURL& url, TQWidget *window)
236{
237 bool result=false;
238
239 TQString tmpFileName;
240 if ( TDEIO::NetAccess::download(url, tmpFileName, window) )
241 {
242 KoPictureKey key;
243 key.setKeyFromFile( tmpFileName );
244 setKey( key );
245 result=loadFromFile( tmpFileName );
246 TDEIO::NetAccess::removeTempFile( tmpFileName );
247 }
248
249 return result;
250}
251
252TQDragObject* KoPicture::dragObject( TQWidget *dragSource, const char *name )
253{
254 if (m_sharedData)
255 return m_sharedData->dragObject( dragSource, name );
256 return 0L;
257}
258
259TQImage KoPicture::generateImage(const TQSize& size)
260{
261 if (m_sharedData)
262 return m_sharedData->generateImage( size );
263 return TQImage();
264}
265
267{
268 if (m_sharedData)
269 return m_sharedData->hasAlphaBuffer();
270 return false;
271}
272
274{
275 if (m_sharedData)
276 m_sharedData->setAlphaBuffer(enable);
277}
278
279TQImage KoPicture::createAlphaMask(int conversion_flags) const
280{
281 if (m_sharedData)
282 return m_sharedData->createAlphaMask(conversion_flags);
283 return TQImage();
284}
285
287{
288 if (m_sharedData)
289 m_sharedData->clearCache();
290}
KoPictureKey is the structure describing a picture in a unique way.
void setKeyFromFile(const TQString &filename)
Sets the key according to filename, including modification time.
KoPicture is a container class for various types of pictures supported by KOffice.
Definition KoPicture.h:44
~KoPicture(void)
Destructor.
Definition KoPicture.cpp:41
TQString getExtension(void) const
bool setKeyAndDownloadPicture(const KURL &url, TQWidget *window)
Download and set the key for a possibly remote file.
bool saveAsBase64(KoXmlWriter &writer) const
OASIS FlatXML support: Save picture as base64-encoded data into an XML writer.
bool loadFromBase64(const TQCString &str)
Load the picture from base64-encoded data.
void clear(void)
Reset the KoPicture (but not the key!)
TQString getMimeType(void) const
void setKey(const KoPictureKey &key)
Set the key structure describing the picture in a unique way.
bool loadFromFile(const TQString &fileName)
Load the picture from a file named fileName.
void clearAndSetMode(const TQString &newMode)
Clear and set the mode of this KoPicture.
bool isNull(void) const
Returns true if the picture is null.
TQSize getOriginalSize(void) const
void clearCache(void)
Clear any cache.
bool save(TQIODevice *io) const
Save picture into a TQIODevice.
void draw(TQPainter &painter, int x, int y, int width, int height, int sx=0, int sy=0, int sw=-1, int sh=-1, bool fastMode=false)
Draw the picture in a painter.
bool loadXpm(TQIODevice *io)
Load a potentially broken XPM file (for old files of KPresenter)
TQPixmap generatePixmap(const TQSize &size, bool smoothScale=false)
KoPictureKey getKey(void) const
Retrieve the key structure describing the picture in a unique way.
KoPicture & operator=(const KoPicture &other)
Assignment operator.
Definition KoPicture.cpp:71
void setAlphaBuffer(bool enable)
Respect the image alpha buffer.
bool hasAlphaBuffer() const
TQDragObject * dragObject(TQWidget *dragSource=0L, const char *name=0L)
Create a dragobject containing this picture.
TQImage generateImage(const TQSize &size)
Generate a TQImage (always in slow mode)
TQImage createAlphaMask(int conversion_flags=0) const
Creates an alpha mask for the picture (first you have to call setAlphaBuffer).
KoPicture(void)
Default constructor.
Definition KoPicture.cpp:36
A class for writing out XML (to any TQIODevice), with a special attention on performance.
Definition KoXmlWriter.h:37