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

« back to all changes in this revision

Viewing changes to lib/kofficecore/koPictureClipart.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) 2001 David Faure <faure@kde.org>
 
4
   Copyright (C) 2002 Nicolas GOUTTE <goutte@kde.org>
 
5
 
 
6
   This library is free software; you can redistribute it and/or
 
7
   modify it under the terms of the GNU Library General Public
 
8
   License as published by the Free Software Foundation; either
 
9
   version 2 of the License, or (at your option) any later version.
 
10
 
 
11
   This library is distributed in the hope that it will be useful,
 
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
   Library General Public License for more details.
 
15
 
 
16
   You should have received a copy of the GNU Library General Public License
 
17
   along with this library; see the file COPYING.LIB.  If not, write to
 
18
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
19
   Boston, MA 02111-1307, USA.
 
20
*/
 
21
 
 
22
#include <qbuffer.h>
 
23
#include <qpainter.h>
 
24
#include <qpicture.h>
 
25
#include <qpixmap.h>
 
26
 
 
27
#include <kdebug.h>
 
28
#include <kdeversion.h>
 
29
#if ! KDE_IS_VERSION( 3,1,90 )
 
30
#include <kdebugclasses.h>
 
31
#endif
 
32
 
 
33
#include "koPictureKey.h"
 
34
#include "koPictureBase.h"
 
35
#include "koPictureClipart.h"
 
36
 
 
37
KoPictureClipart::KoPictureClipart(void) : m_clipart(KoPictureType::formatVersionQPicture)
 
38
{
 
39
}
 
40
 
 
41
KoPictureClipart::~KoPictureClipart(void)
 
42
{
 
43
}
 
44
 
 
45
KoPictureBase* KoPictureClipart::newCopy(void) const
 
46
{
 
47
    return new KoPictureClipart(*this);
 
48
}
 
49
 
 
50
KoPictureType::Type KoPictureClipart::getType(void) const
 
51
{
 
52
    return KoPictureType::TypeClipart;
 
53
}
 
54
 
 
55
bool KoPictureClipart::isNull(void) const
 
56
{
 
57
    return m_clipart.isNull();
 
58
}
 
59
 
 
60
void KoPictureClipart::drawQPicture(QPicture& clipart, QPainter& painter,
 
61
    int x, int y, int width, int height, int sx, int sy, int sw, int sh)
 
62
{
 
63
    kdDebug(30003) << "Drawing KoPictureClipart " << this << endl;
 
64
    kdDebug(30003) << "  x=" << x << " y=" << y << " width=" << width << " height=" << height << endl;
 
65
    kdDebug(30003) << "  sx=" << sx << " sy=" << sy << " sw=" << sw << " sh=" << sh << endl;
 
66
    painter.save();
 
67
    // Thanks to Harri, Qt3 makes it much easier than Qt2 ;)
 
68
    QRect br = clipart.boundingRect();
 
69
    kdDebug(30003) << "  Bounding rect. " << br << endl;
 
70
 
 
71
    painter.translate(x,y); // Translating must be done before scaling!
 
72
    if ( br.width() && br.height() )
 
73
        painter.scale(double(width)/double(br.width()),double(height)/double(br.height()));
 
74
    else
 
75
        kdWarning(30003) << "Null bounding rectangle: " << br.width() << " x " << br.height() << endl;
 
76
    painter.drawPicture(0,0,clipart);
 
77
    painter.restore();
 
78
}
 
79
 
 
80
void KoPictureClipart::draw(QPainter& painter, int x, int y, int width, int height, int sx, int sy, int sw, int sh, bool /*fastMode*/)
 
81
{
 
82
    drawQPicture(m_clipart, painter, x, y, width, height, sx, sy, sw, sh);
 
83
}
 
84
 
 
85
bool KoPictureClipart::load(const QByteArray& array, const QString& extension)
 
86
{
 
87
    // Second, create the original clipart
 
88
    kdDebug(30003) << "Trying to load clipart... (Size:" << m_rawData.size() << ")" << endl;
 
89
    m_rawData=array;
 
90
    QBuffer buffer(m_rawData);
 
91
    buffer.open(IO_ReadWrite);
 
92
    bool check = true;
 
93
    if (extension=="svg")
 
94
    {
 
95
        if (!m_clipart.load(&buffer, "svg"))
 
96
        {
 
97
            kdWarning(30003) << "Loading SVG has failed! (KoPictureClipart::load)" << endl;
 
98
            check = false;
 
99
        }
 
100
    }
 
101
    else
 
102
    {
 
103
        if (!m_clipart.load(&buffer, NULL))
 
104
        {
 
105
            kdWarning(30003) << "Loading QPicture has failed! (KoPictureClipart::load)" << endl;
 
106
            check = false;
 
107
        }
 
108
    }
 
109
    buffer.close();
 
110
    return check;
 
111
}
 
112
 
 
113
bool KoPictureClipart::save(QIODevice* io)
 
114
{
 
115
    // We save the raw data, as the SVG supposrt in QPicture is poor
 
116
    Q_ULONG size=io->writeBlock(m_rawData); // WARNING: writeBlock returns Q_LONG but size() Q_ULONG!
 
117
    return (size==m_rawData.size());
 
118
}
 
119
 
 
120
bool KoPictureClipart::saveAsKOffice1Dot1(QIODevice* io, const QString& extension)
 
121
{
 
122
    QPicture picture(3); //compatibility with QT 2.1 and later (KOffice 1.1.x was with QT 2.3.1 or QT 3.0.x)
 
123
 
 
124
    bool result=false;
 
125
    if (extension=="svg")
 
126
    {
 
127
        // SVG: convert it to QPicture
 
128
        QBuffer buffer(m_rawData);
 
129
        buffer.open(IO_ReadWrite);
 
130
        if (picture.load(&buffer,"svg"))
 
131
        {
 
132
            result=picture.save(io,NULL);
 
133
        }
 
134
        buffer.close();
 
135
    }
 
136
    else if (extension=="qpic")
 
137
    {
 
138
        // We cannot do much with a QPicture, we cannot convert it to previous formats
 
139
        result=save(io);
 
140
    }
 
141
    else
 
142
    {
 
143
        kdWarning(30003)<< "Unsupported clipart extension " << extension << " (KoPictureClipart::saveAsKOffice1Dot1)" << endl;
 
144
        result=save(io); // Always save something!
 
145
    }
 
146
 
 
147
    return result;
 
148
}
 
149
 
 
150
QSize KoPictureClipart::getOriginalSize(void) const
 
151
{
 
152
    return m_clipart.boundingRect().size();
 
153
}
 
154
 
 
155
QPixmap KoPictureClipart::generatePixmap(const QSize& size, bool /*smoothScale*/)
 
156
{
 
157
    // Not sure if it works, but it worked for KoPictureFilePreviewWidget::setClipart
 
158
    QPixmap pixmap(size);
 
159
    QPainter p;
 
160
 
 
161
    p.begin( &pixmap );
 
162
    p.setBackgroundColor( Qt::white );
 
163
    pixmap.fill( Qt::white );
 
164
 
 
165
    QRect br = m_clipart.boundingRect();
 
166
    if ( br.width() && br.height() )
 
167
        p.scale( (double)pixmap.width() / (double)br.width(), (double)pixmap.height() / (double)br.height() );
 
168
    p.drawPicture( m_clipart );
 
169
    p.end();
 
170
    return pixmap;
 
171
}
 
172
 
 
173
bool KoPictureClipart::isClipartAsKOffice1Dot1(void) const
 
174
{
 
175
    return true;
 
176
}
 
177
 
 
178
QString KoPictureClipart::getMimeType(const QString& extension) const
 
179
{
 
180
    if (extension=="svg")
 
181
        return "image/svg+xml";
 
182
    else
 
183
        return "image/x-vnd.trolltech.qpicture";
 
184
}
 
185