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#include <tdeversion.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
37KoPictureClipart::KoPictureClipart(void) : m_clipart(KoPictureType::formatVersionTQPicture)
38{
39}
40
41KoPictureClipart::~KoPictureClipart(void)
42{
43}
44
45KoPictureBase* KoPictureClipart::newCopy(void) const
46{
47 return new KoPictureClipart(*this);
48}
49
50KoPictureType::Type KoPictureClipart::getType(void) const
51{
52 return KoPictureType::TypeClipart;
53}
54
55bool KoPictureClipart::isNull(void) const
56{
57 return m_clipart.isNull();
58}
59
60void KoPictureClipart::drawTQPicture(TQPicture& clipart, TQPainter& 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, TQt3 makes it much easier than TQt2 ;)
68 TQRect 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
80void KoPictureClipart::draw(TQPainter& painter, int x, int y, int width, int height, int sx, int sy, int sw, int sh, bool /*fastMode*/)
81{
82 drawTQPicture(m_clipart, painter, x, y, width, height, sx, sy, sw, sh);
83}
84
85bool KoPictureClipart::loadData(const TQByteArray& array, const TQString& 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 TQBuffer buffer(m_rawData);
91 buffer.open(IO_ReadOnly);
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 TQPicture has failed! (KoPictureClipart::load)" << endl;
106 check = false;
107 }
108 }
109 buffer.close();
110 return check;
111}
112
113bool KoPictureClipart::save(TQIODevice* io) const
114{
115 // We save the raw data, as the SVG supposrt in TQPicture is poor
116 TQ_ULONG size=io->writeBlock(m_rawData); // WARNING: writeBlock returns TQ_LONG but size() TQ_ULONG!
117 return (size==m_rawData.size());
118}
119
120TQSize KoPictureClipart::getOriginalSize(void) const
121{
122 return m_clipart.boundingRect().size();
123}
124
125TQPixmap KoPictureClipart::generatePixmap(const TQSize& size, bool /*smoothScale*/)
126{
127 // Not sure if it works, but it worked for KoPictureFilePreviewWidget::setClipart
128 TQPixmap pixmap(size);
129 TQPainter p;
130
131 p.begin( &pixmap );
132 p.setBackgroundColor( TQt::white );
133 pixmap.fill( TQt::white );
134
135 TQRect br = m_clipart.boundingRect();
136 if ( br.width() && br.height() )
137 p.scale( (double)pixmap.width() / (double)br.width(), (double)pixmap.height() / (double)br.height() );
138 p.drawPicture( m_clipart );
139 p.end();
140 return pixmap;
141}
142
143TQString KoPictureClipart::getMimeType(const TQString& extension) const
144{
145 if (extension=="svg")
146 return "image/svg+xml";
147 else
148 return "image/x-vnd.trolltech.qpicture";
149}
150