lib

KoPictureWmf.cpp

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., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, 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 QSize KoPictureWmf::getOriginalSize(void) const
00113 {
00114     return m_originalSize;
00115 }
00116 
00117 QPixmap KoPictureWmf::generatePixmap(const QSize& size, bool /*smoothScale*/)
00118 {
00119     // Not sure if it works, but it worked for KoPictureFilePreviewWidget::setClipart
00120     QPixmap pixmap(size);
00121     QPainter p;
00122 
00123     p.begin( &pixmap );
00124     p.setBackgroundColor( Qt::white );
00125     pixmap.fill( Qt::white );
00126 
00127     if ( m_originalSize.width() && m_originalSize.height() )
00128         p.scale( (double)pixmap.width() / (double)m_originalSize.width(), (double)pixmap.height() / (double)m_originalSize.height() );
00129     p.drawPicture( m_clipart );
00130     p.end();
00131     return pixmap;
00132 }
00133 
00134 QString KoPictureWmf::getMimeType(const QString& /* extension */) const
00135 {
00136     return "image/x-wmf";
00137 }
KDE Home | KDE Accessibility Home | Description of Access Keys