~ubuntu-branches/ubuntu/breezy/koffice/breezy

« back to all changes in this revision

Viewing changes to lib/kofficecore/koPictureShared.cc

  • Committer: Bazaar Package Importer
  • Author(s): Ben Burton
  • Date: 2004-05-09 11:33:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040509113300-vfrdadqsvjfuhn3b
Tags: 1:1.3.1-1
* New upstream bugfix release.
* Built against newer imagemagick (closes: #246623).
* Made koffice-libs/kformula recommend/depend on latex-xft-fonts, which
  provides mathematical fonts that the formula editor can use.  Also
  patched the kformula part to make these fonts the default.
* Changed kword menu hint from "WordProcessors" to "Word processors"
  (closes: #246209).
* Spellchecker configuration is now fixed (closes: #221256, #227568).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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., 59 Temple Place - Suite 330,
 
18
   Boston, MA 02111-1307, USA.
 
19
*/
 
20
 
 
21
#include <qpainter.h>
 
22
#include <qfile.h>
 
23
 
 
24
#include <kdebug.h>
 
25
#include <kurl.h>
 
26
#include <kio/netaccess.h>
 
27
 
 
28
#include "koPictureKey.h"
 
29
#include "koPictureBase.h"
 
30
#include "koPictureImage.h"
 
31
#include "koPictureEps.h"
 
32
#include "koPictureClipart.h"
 
33
#include "koPictureWmf.h"
 
34
#include "koPictureShared.h"
 
35
 
 
36
KoPictureShared::KoPictureShared(void) : m_base(NULL)
 
37
{
 
38
}
 
39
 
 
40
KoPictureShared::~KoPictureShared(void)
 
41
{
 
42
    delete m_base;
 
43
}
 
44
 
 
45
KoPictureShared::KoPictureShared(const KoPictureShared &other)
 
46
    : QShared() // Some compilers want it explicitly!
 
47
{
 
48
    // We need to use newCopy, because we want a real copy, not just a copy of the part of KoPictureBase
 
49
    if (other.m_base)
 
50
        m_base=other.m_base->newCopy();
 
51
    else
 
52
        m_base=NULL;
 
53
}
 
54
 
 
55
KoPictureShared& KoPictureShared::operator=( const KoPictureShared &other )
 
56
{
 
57
    clear();
 
58
    kdDebug(30003) << "KoPictureShared::= before" << endl;
 
59
    if (other.m_base)
 
60
        m_base=other.m_base->newCopy();
 
61
    kdDebug(30003) << "KoPictureShared::= after" << endl;
 
62
    return *this;
 
63
}
 
64
 
 
65
KoPictureType::Type KoPictureShared::getType(void) const
 
66
{
 
67
    if (m_base)
 
68
        return m_base->getType();
 
69
    return KoPictureType::TypeUnknown;
 
70
}
 
71
 
 
72
bool KoPictureShared::isNull(void) const
 
73
{
 
74
    if (m_base)
 
75
        return m_base->isNull();
 
76
    return true;
 
77
}
 
78
 
 
79
void KoPictureShared::draw(QPainter& painter, int x, int y, int width, int height, int sx, int sy, int sw, int sh, bool fastMode)
 
80
{
 
81
    if (m_base)
 
82
        m_base->draw(painter, x, y, width, height, sx, sy, sw, sh, fastMode);
 
83
    else
 
84
    {
 
85
        // Draw a red box (easier DEBUG)
 
86
        kdWarning(30003) << "Drawing red rectangle! (KoPictureShared::draw)" << endl;
 
87
        painter.save();
 
88
        painter.setBrush(QColor(255,0,0));
 
89
        painter.drawRect(x,y,width,height);
 
90
        painter.restore();
 
91
    }
 
92
}
 
93
 
 
94
bool KoPictureShared::loadWmf(QIODevice* io)
 
95
{
 
96
    kdDebug(30003) << "KoPictureShared::loadWmf" << endl;
 
97
    if (!io)
 
98
    {
 
99
        kdError(30003) << "No QIODevice!" << endl;
 
100
        return false;
 
101
    }
 
102
 
 
103
    clear();
 
104
 
 
105
    // The extension .wmf was used (KOffice 1.1.x) for QPicture files
 
106
    // For an extern file or in the storage, .wmf can mean a real Windows Meta File.
 
107
 
 
108
    QByteArray array ( io->readAll() );
 
109
 
 
110
    if ((array[0]=='Q') && (array[1]=='P') &&(array[2]=='I') && (array[3]=='C'))
 
111
    {
 
112
        m_base=new KoPictureClipart();
 
113
        setExtension("qpic");
 
114
    }
 
115
    else
 
116
    {
 
117
        m_base=new KoPictureWmf();
 
118
        setExtension("wmf");
 
119
    }
 
120
    return m_base->load(array, m_extension);
 
121
}
 
122
 
 
123
bool KoPictureShared::loadTmp(QIODevice* io)
 
124
// We have a temp file, probably from a downloaded file
 
125
//   We must check the file type
 
126
{
 
127
    kdDebug(30003) << "KoPictureShared::loadTmp" << endl;
 
128
    if (!io)
 
129
    {
 
130
        kdError(30003) << "No QIODevice!" << endl;
 
131
        return false;
 
132
    }
 
133
 
 
134
    // The extension .wmf was used (KOffice 1.1.x) for QPicture files
 
135
    // For an extern file or in the storage, .wmf can mean a real Windows Meta File.
 
136
 
 
137
    QByteArray array=io->readAll();
 
138
    QString strExtension;
 
139
    bool flag=false;
 
140
 
 
141
    // Try to find the file type by comparing magic on the first few bytes!
 
142
    if ((array[0]==char(0x89)) && (array[1]=='P') &&(array[2]=='N') && (array[3]=='G'))
 
143
    {
 
144
        strExtension="png";
 
145
    }
 
146
    else if ((array[0]==char(0xff)) && (array[1]==char(0xd8)) &&(array[2]==char(0xff)) && (array[3]==char(0xe0)))
 
147
    {
 
148
        strExtension="jpeg";
 
149
    }
 
150
    else if ((array[0]=='B') && (array[1]=='M'))
 
151
    {
 
152
        strExtension="bmp";
 
153
    }
 
154
    else if ((array[0]==char(0xd7)) && (array[1]==char(0xcd)) &&(array[2]==char(0xc6)) && (array[3]==char(0x9a)))
 
155
    {
 
156
        strExtension="wmf";
 
157
    }
 
158
    else if ((array[0]=='<') && (array[1]=='?') &&(array[2]=='X') && (array[3]=='M') && (array[4]=='L'))
 
159
    {
 
160
        strExtension="svg";
 
161
    }
 
162
    else if ((array[0]=='Q') && (array[1]=='P') &&(array[2]=='I') && (array[3]=='C'))
 
163
    {
 
164
        strExtension="qpic";
 
165
    }
 
166
    else if ((array[0]=='%') && (array[1]=='!') &&(array[2]=='P') && (array[3]=='S'))
 
167
    {
 
168
        strExtension="eps";
 
169
    }
 
170
    else if ((array[0]==char(0xc5)) && (array[1]==char(0xd0)) && (array[2]==char(0xd3)) && (array[3]==char(0xc6)))
 
171
    {
 
172
        // So called "MS-DOS EPS file"
 
173
        strExtension="eps";
 
174
    }
 
175
    else
 
176
    {
 
177
        kdDebug(30003) << "Cannot identify the type of temp file!"
 
178
            << " Trying to convert to PNG! (in KoPictureShared::loadTmp" << endl;
 
179
 
 
180
        QBuffer buf(array);
 
181
        if (!buf.open(IO_ReadOnly))
 
182
        {
 
183
            kdError(30003) << "Could not open read buffer!" << endl;
 
184
            return false;
 
185
        }
 
186
 
 
187
        QImageIO imageIO(&buf,NULL);
 
188
 
 
189
        if (!imageIO.read())
 
190
        {
 
191
            kdError(30003) << "Could not read image!" << endl;
 
192
            return false;
 
193
        }
 
194
 
 
195
        buf.close();
 
196
 
 
197
        if (!buf.open(IO_WriteOnly))
 
198
        {
 
199
            kdError(30003) << "Could not open write buffer!" << endl;
 
200
            return false;
 
201
        }
 
202
 
 
203
        imageIO.setIODevice(&buf);
 
204
        imageIO.setFormat("PNG");
 
205
 
 
206
        if (!imageIO.write())
 
207
        {
 
208
            kdError(30003) << "Could not write converted image!" << endl;
 
209
            return false;
 
210
        }
 
211
        buf.close();
 
212
 
 
213
        strExtension="png";
 
214
    }
 
215
 
 
216
    kdDebug(30003) << "Temp file considered to be " << strExtension << endl;
 
217
 
 
218
    QBuffer buffer(array);
 
219
    buffer.open(IO_ReadOnly);
 
220
    clearAndSetMode(strExtension);
 
221
    if (m_base)
 
222
        flag=m_base->load(&buffer,strExtension);
 
223
    setExtension(strExtension);
 
224
    buffer.close();
 
225
 
 
226
    return flag;
 
227
}
 
228
 
 
229
 
 
230
 
 
231
bool KoPictureShared::loadXpm(QIODevice* io)
 
232
{
 
233
    kdDebug(30003) << "KoPictureShared::loadXpm" << endl;
 
234
    if (!io)
 
235
    {
 
236
        kdError(30003) << "No QIODevice!" << endl;
 
237
        return false;
 
238
    }
 
239
 
 
240
    clear();
 
241
 
 
242
    // Old KPresenter XPM files have char(1) instead of some "
 
243
    // Therefore we need to treat XPM separately
 
244
 
 
245
    QByteArray array=io->readAll();
 
246
 
 
247
    // As XPM files are normally only ASCII files, we can replace it without problems
 
248
 
 
249
    int pos=0;
 
250
 
 
251
    while ((pos=array.find(char(1),pos))!=-1)
 
252
    {
 
253
        array[pos]='"';
 
254
    }
 
255
 
 
256
    // Now that the XPM file is corrected, we need to load it.
 
257
 
 
258
    m_base=new KoPictureImage();
 
259
 
 
260
    QBuffer buffer(array);
 
261
    bool check = m_base->load(&buffer,"xpm");
 
262
    setExtension("xpm");
 
263
    return check;
 
264
}
 
265
 
 
266
bool KoPictureShared::save(QIODevice* io)
 
267
{
 
268
    if (!io)
 
269
        return false;
 
270
    if (m_base)
 
271
        return m_base->save(io);
 
272
    return false;
 
273
}
 
274
 
 
275
bool KoPictureShared::saveAsKOffice1Dot1(QIODevice* io)
 
276
{
 
277
    if (!io)
 
278
        return false;
 
279
    if (m_base)
 
280
        return m_base->saveAsKOffice1Dot1(io, getExtension());
 
281
    return false;
 
282
}
 
283
 
 
284
void KoPictureShared::clear(void)
 
285
{
 
286
    // Clear does not reset the key m_key!
 
287
    delete m_base;
 
288
    m_base=NULL;
 
289
}
 
290
 
 
291
void KoPictureShared::clearAndSetMode(const QString& newMode)
 
292
{
 
293
    delete m_base;
 
294
    m_base=NULL;
 
295
 
 
296
    const QString mode=newMode.lower();
 
297
 
 
298
    // TODO: WMF need to be alone!
 
299
    if ((mode=="svg") || (mode=="qpic"))
 
300
    {
 
301
        m_base=new KoPictureClipart();
 
302
    }
 
303
    else if (mode=="wmf")
 
304
    {
 
305
        m_base=new KoPictureWmf();
 
306
    }
 
307
    else if ( (mode=="eps") || (mode=="epsi") || (mode=="epsf") )
 
308
    {
 
309
        m_base=new KoPictureEps();
 
310
    }
 
311
    else
 
312
    {   // TODO: test if QImageIO really knows the file format
 
313
        m_base=new KoPictureImage();
 
314
    }
 
315
}
 
316
 
 
317
QString KoPictureShared::getExtension(void) const
 
318
{
 
319
    return m_extension;
 
320
}
 
321
 
 
322
void KoPictureShared::setExtension(const QString& extension)
 
323
{
 
324
    m_extension = extension;
 
325
}
 
326
 
 
327
QString KoPictureShared::getExtensionAsKOffice1Dot1(void) const
 
328
{
 
329
    if (isClipartAsKOffice1Dot1())
 
330
        return "wmf"; // In KOffice 1.1, all cliparts are QPicture but are named as wmf
 
331
    else
 
332
        return m_extension;
 
333
}
 
334
 
 
335
QString KoPictureShared::getMimeType(void) const
 
336
{
 
337
   if (m_base)
 
338
        return m_base->getMimeType(m_extension);
 
339
    return QString(NULL_MIME_TYPE);
 
340
}
 
341
 
 
342
bool KoPictureShared::load(QIODevice* io, const QString& extension)
 
343
{
 
344
    kdDebug(30003) << "KoPictureShared::load(QIODevice*, const QString&) " << extension << endl;
 
345
    bool flag=false;
 
346
    QString ext(extension.lower());
 
347
    if (ext=="wmf")
 
348
        flag=loadWmf(io);
 
349
    else if (ext=="tmp") // ### TODO: also remote scripts need this, don't they?
 
350
        flag=loadTmp(io);
 
351
    else
 
352
    {
 
353
        clearAndSetMode(ext);
 
354
        if (m_base)
 
355
            flag=m_base->load(io,ext);
 
356
        setExtension(ext);
 
357
    }
 
358
    if (!flag)
 
359
    {
 
360
        kdError(30003) << "File was not loaded! (KoPictureShared::load)" << endl;
 
361
    }
 
362
    return flag;
 
363
}
 
364
 
 
365
bool KoPictureShared::loadFromFile(const QString& fileName)
 
366
{
 
367
    kdDebug(30003) << "KoPictureShared::loadFromFile " << fileName << endl;
 
368
    QFile file(fileName);
 
369
    const int pos=fileName.findRev('.');
 
370
    if (pos==-1)
 
371
    {
 
372
        kdDebug(30003) << "File with no extension! Not supported!" << endl;
 
373
        return false;
 
374
    }
 
375
    QString extension=fileName.mid(pos+1);
 
376
    if (!file.open(IO_ReadOnly))
 
377
        return false;
 
378
    const bool flag=load(&file,extension);
 
379
    file.close();
 
380
    return flag;
 
381
}
 
382
 
 
383
QSize KoPictureShared::getOriginalSize(void) const
 
384
{
 
385
    if (m_base)
 
386
        return m_base->getOriginalSize();
 
387
    return QSize(0,0);
 
388
}
 
389
 
 
390
QPixmap KoPictureShared::generatePixmap(const QSize& size, bool smoothScale)
 
391
{
 
392
    if (m_base)
 
393
        return m_base->generatePixmap(size, smoothScale);
 
394
    return QPixmap();
 
395
}
 
396
 
 
397
bool KoPictureShared::isClipartAsKOffice1Dot1(void) const
 
398
{
 
399
   if (m_base)
 
400
        return m_base->isClipartAsKOffice1Dot1();
 
401
    return false;
 
402
}
 
403
 
 
404
QDragObject* KoPictureShared::dragObject( QWidget *dragSource, const char *name )
 
405
{
 
406
    if (m_base)
 
407
        return m_base->dragObject( dragSource, name );
 
408
    return 0L;
 
409
}
 
410
 
 
411
QImage KoPictureShared::generateImage(const QSize& size)
 
412
{
 
413
    if (m_base)
 
414
        return m_base->generateImage( size );
 
415
    return QImage();
 
416
}
 
417
 
 
418
bool KoPictureShared::hasAlphaBuffer() const
 
419
{
 
420
   if (m_base)
 
421
       return m_base->hasAlphaBuffer();
 
422
   return false;
 
423
}
 
424
 
 
425
void KoPictureShared::setAlphaBuffer(bool enable)
 
426
{
 
427
    if (m_base)
 
428
        m_base->setAlphaBuffer(enable);
 
429
}
 
430
 
 
431
QImage KoPictureShared::createAlphaMask(int conversion_flags) const
 
432
{
 
433
    if (m_base)
 
434
        return m_base->createAlphaMask(conversion_flags);
 
435
    return QImage();
 
436
}
 
437
 
 
438
void KoPictureShared::clearCache(void)
 
439
{
 
440
    if (m_base)
 
441
        m_base->clearCache();
 
442
}