lib

KoPictureClipart.cpp
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., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20*/
21
22#include <tqbuffer.h>
23#include <tqpainter.h>
24#include <tqpicture.h>
25#include <tqpixmap.h>
26
27#include <kdebug.h>
28
29#include "KoPictureKey.h"
30#include "KoPictureBase.h"
31#include "KoPictureClipart.h"
32
33KoPictureClipart::KoPictureClipart(void) : m_clipart(KoPictureType::formatVersionTQPicture)
34{
35}
36
37KoPictureClipart::~KoPictureClipart(void)
38{
39}
40
41KoPictureBase* KoPictureClipart::newCopy(void) const
42{
43 return new KoPictureClipart(*this);
44}
45
46KoPictureType::Type KoPictureClipart::getType(void) const
47{
48 return KoPictureType::TypeClipart;
49}
50
51bool KoPictureClipart::isNull(void) const
52{
53 return m_clipart.isNull();
54}
55
56void KoPictureClipart::drawTQPicture(TQPicture& clipart, TQPainter& painter,
57 int x, int y, int width, int height, int sx, int sy, int sw, int sh)
58{
59 kdDebug(30003) << "Drawing KoPictureClipart " << this << endl;
60 kdDebug(30003) << " x=" << x << " y=" << y << " width=" << width << " height=" << height << endl;
61 kdDebug(30003) << " sx=" << sx << " sy=" << sy << " sw=" << sw << " sh=" << sh << endl;
62 painter.save();
63 // Thanks to Harri, TQt3 makes it much easier than TQt2 ;)
64 TQRect br = clipart.boundingRect();
65 kdDebug(30003) << " Bounding rect. " << br << endl;
66
67 painter.translate(x,y); // Translating must be done before scaling!
68 if ( br.width() && br.height() )
69 painter.scale(double(width)/double(br.width()),double(height)/double(br.height()));
70 else
71 kdWarning(30003) << "Null bounding rectangle: " << br.width() << " x " << br.height() << endl;
72 painter.drawPicture(0,0,clipart);
73 painter.restore();
74}
75
76void KoPictureClipart::draw(TQPainter& painter, int x, int y, int width, int height, int sx, int sy, int sw, int sh, bool /*fastMode*/)
77{
78 drawTQPicture(m_clipart, painter, x, y, width, height, sx, sy, sw, sh);
79}
80
81bool KoPictureClipart::loadData(const TQByteArray& array, const TQString& extension)
82{
83 // Second, create the original clipart
84 kdDebug(30003) << "Trying to load clipart... (Size:" << m_rawData.size() << ")" << endl;
85 m_rawData=array;
86 TQBuffer buffer(m_rawData);
87 buffer.open(IO_ReadOnly);
88 bool check = true;
89 if (extension=="svg")
90 {
91 if (!m_clipart.load(&buffer, "svg"))
92 {
93 kdWarning(30003) << "Loading SVG has failed! (KoPictureClipart::load)" << endl;
94 check = false;
95 }
96 }
97 else
98 {
99 if (!m_clipart.load(&buffer, NULL))
100 {
101 kdWarning(30003) << "Loading TQPicture has failed! (KoPictureClipart::load)" << endl;
102 check = false;
103 }
104 }
105 buffer.close();
106 return check;
107}
108
109bool KoPictureClipart::save(TQIODevice* io) const
110{
111 // We save the raw data, as the SVG supposrt in TQPicture is poor
112 TQ_ULONG size=io->writeBlock(m_rawData); // WARNING: writeBlock returns TQ_LONG but size() TQ_ULONG!
113 return (size==m_rawData.size());
114}
115
116TQSize KoPictureClipart::getOriginalSize(void) const
117{
118 return m_clipart.boundingRect().size();
119}
120
121TQPixmap KoPictureClipart::generatePixmap(const TQSize& size, bool /*smoothScale*/)
122{
123 // Not sure if it works, but it worked for KoPictureFilePreviewWidget::setClipart
124 TQPixmap pixmap(size);
125 TQPainter p;
126
127 p.begin( &pixmap );
128 p.setBackgroundColor( TQt::white );
129 pixmap.fill( TQt::white );
130
131 TQRect br = m_clipart.boundingRect();
132 if ( br.width() && br.height() )
133 p.scale( (double)pixmap.width() / (double)br.width(), (double)pixmap.height() / (double)br.height() );
134 p.drawPicture( m_clipart );
135 p.end();
136 return pixmap;
137}
138
139TQString KoPictureClipart::getMimeType(const TQString& extension) const
140{
141 if (extension=="svg")
142 return "image/svg+xml";
143 else
144 return "image/x-vnd.trolltech.qpicture";
145}
146