lib Library API Documentation

koPictureWmf.cc

00001 /* This file is part of the KDE project
00002    Copyright (c) 2001 Simon Hausmann <hausmann@kde.org>
00003    Copyright (c) 2001 David Faure <faure@kde.org>
00004    Copyright (C) 2002 Nicolas GOUTTE <goutte@kde.org>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019    Boston, MA 02111-1307, USA.
00020 */
00021 
00022 #include <qbuffer.h>
00023 #include <qpainter.h>
00024 #include <qpicture.h>
00025 #include <qpixmap.h>
00026 
00027 #include <kdebug.h>
00028 #include <kdeversion.h>
00029 #if ! KDE_IS_VERSION( 3,1,90 )
00030 #include <kdebugclasses.h>
00031 #endif
00032 
00033 #include <kowmfpaint.h>
00034 #include "koPictureKey.h"
00035 #include "koPictureBase.h"
00036 #include "koPictureWmf.h"
00037 
00038 KoPictureWmf::KoPictureWmf(void) : m_clipart(KoPictureType::formatVersionQPicture)
00039 {
00040 }
00041 
00042 KoPictureWmf::~KoPictureWmf(void)
00043 {
00044 }
00045 
00046 KoPictureBase* KoPictureWmf::newCopy(void) const
00047 {
00048     return new KoPictureWmf(*this);
00049 }
00050 
00051 KoPictureType::Type KoPictureWmf::getType(void) const
00052 {
00053     return KoPictureType::TypeWmf;
00054 }
00055 
00056 bool KoPictureWmf::isNull(void) const
00057 {
00058     return m_clipart.isNull();
00059 }
00060 
00061 void KoPictureWmf::drawQPicture(QPicture& clipart, QPainter& painter,
00062     int x, int y, int width, int height, int sx, int sy, int sw, int sh)
00063 {
00064     kdDebug(30003) << "Drawing KoPictureWmf " << this << endl;
00065     kdDebug(30003) << "  x=" << x << " y=" << y << " width=" << width << " height=" << height << endl;
00066     kdDebug(30003) << "  sx=" << sx << " sy=" << sy << " sw=" << sw << " sh=" << sh << endl;
00067     painter.save();
00068     // Thanks to Harri, Qt3 makes it much easier than Qt2 ;)
00069     QRect br = clipart.boundingRect();
00070     kdDebug(30003) << "  Bounding rect. " << br << endl;
00071 
00072     painter.translate(x,y); // Translating must be done before scaling!
00073     if ( br.width() && br.height() )
00074         painter.scale(double(width)/double(br.width()),double(height)/double(br.height()));
00075     else
00076         kdWarning(30003) << "Null bounding rectangle: " << br.width() << " x " << br.height() << endl;
00077     painter.drawPicture(0,0,clipart);
00078     painter.restore();
00079 }
00080 
00081 void KoPictureWmf::draw(QPainter& painter, int x, int y, int width, int height, int sx, int sy, int sw, int sh, bool /*fastMode*/)
00082 {
00083     drawQPicture(m_clipart, painter, x, y, width, height, sx, sy, sw, sh);
00084 }
00085 
00086 bool KoPictureWmf::loadData(const QByteArray& array, const QString& /* extension */)
00087 {
00088     // Second, create the original clipart
00089     kdDebug(30003) << "Trying to load clipart... (Size:" << array.size() << ")" << endl;
00090     m_rawData=array;
00091 
00092     KoWmfPaint wmf;
00093     if (!wmf.load(array))
00094     {
00095         kdWarning(30003) << "Loading WMF has failed! (KoPictureWmf::load)" << endl;
00096         return false;
00097     }
00098     m_originalSize = wmf.boundingRect().size();
00099     // draw wmf file with relative coordinate
00100     wmf.play(m_clipart, true);
00101 
00102     return true;
00103 }
00104 
00105 bool KoPictureWmf::save(QIODevice* io) const
00106 {
00107     // We save the raw data, as the SVG supposrt in QPicture is poor
00108     Q_ULONG size=io->writeBlock(m_rawData); // WARNING: writeBlock returns Q_LONG but size() Q_ULONG!
00109     return (size==m_rawData.size());
00110 }
00111 
00112 bool KoPictureWmf::saveAsKOffice1Dot1(QIODevice* io, const QString& /* extension */) const
00113 {
00114     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)
00115 
00116     bool result=false;
00117     KoWmfPaint wmf;
00118     if (wmf.load(m_rawData))
00119     {
00120         wmf.play(picture, true);
00121         result=picture.save(io,NULL);
00122     }
00123     return result;
00124 }
00125 
00126 QSize KoPictureWmf::getOriginalSize(void) const
00127 {
00128     return m_originalSize;
00129 }
00130 
00131 QPixmap KoPictureWmf::generatePixmap(const QSize& size, bool /*smoothScale*/)
00132 {
00133     // Not sure if it works, but it worked for KoPictureFilePreviewWidget::setClipart
00134     QPixmap pixmap(size);
00135     QPainter p;
00136 
00137     p.begin( &pixmap );
00138     p.setBackgroundColor( Qt::white );
00139     pixmap.fill( Qt::white );
00140 
00141     if ( m_originalSize.width() && m_originalSize.height() )
00142         p.scale( (double)pixmap.width() / (double)m_originalSize.width(), (double)pixmap.height() / (double)m_originalSize.height() );
00143     p.drawPicture( m_clipart );
00144     p.end();
00145     return pixmap;
00146 }
00147 
00148 bool KoPictureWmf::isClipartAsKOffice1Dot1(void) const
00149 {
00150     return true;
00151 }
00152 
00153 QString KoPictureWmf::getMimeType(const QString& /* extension */) const
00154 {
00155     return "image/x-wmf";
00156 }
KDE Logo
This file is part of the documentation for lib Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:40:04 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003