lib

KoPictureClipart.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 "KoPictureKey.h"
00034 #include "KoPictureBase.h"
00035 #include "KoPictureClipart.h"
00036 
00037 KoPictureClipart::KoPictureClipart(void) : m_clipart(KoPictureType::formatVersionQPicture)
00038 {
00039 }
00040 
00041 KoPictureClipart::~KoPictureClipart(void)
00042 {
00043 }
00044 
00045 KoPictureBase* KoPictureClipart::newCopy(void) const
00046 {
00047     return new KoPictureClipart(*this);
00048 }
00049 
00050 KoPictureType::Type KoPictureClipart::getType(void) const
00051 {
00052     return KoPictureType::TypeClipart;
00053 }
00054 
00055 bool KoPictureClipart::isNull(void) const
00056 {
00057     return m_clipart.isNull();
00058 }
00059 
00060 void KoPictureClipart::drawQPicture(QPicture& clipart, QPainter& painter,
00061     int x, int y, int width, int height, int sx, int sy, int sw, int sh)
00062 {
00063     kdDebug(30003) << "Drawing KoPictureClipart " << this << endl;
00064     kdDebug(30003) << "  x=" << x << " y=" << y << " width=" << width << " height=" << height << endl;
00065     kdDebug(30003) << "  sx=" << sx << " sy=" << sy << " sw=" << sw << " sh=" << sh << endl;
00066     painter.save();
00067     // Thanks to Harri, Qt3 makes it much easier than Qt2 ;)
00068     QRect br = clipart.boundingRect();
00069     kdDebug(30003) << "  Bounding rect. " << br << endl;
00070 
00071     painter.translate(x,y); // Translating must be done before scaling!
00072     if ( br.width() && br.height() )
00073         painter.scale(double(width)/double(br.width()),double(height)/double(br.height()));
00074     else
00075         kdWarning(30003) << "Null bounding rectangle: " << br.width() << " x " << br.height() << endl;
00076     painter.drawPicture(0,0,clipart);
00077     painter.restore();
00078 }
00079 
00080 void KoPictureClipart::draw(QPainter& painter, int x, int y, int width, int height, int sx, int sy, int sw, int sh, bool /*fastMode*/)
00081 {
00082     drawQPicture(m_clipart, painter, x, y, width, height, sx, sy, sw, sh);
00083 }
00084 
00085 bool KoPictureClipart::loadData(const QByteArray& array, const QString& extension)
00086 {
00087     // Second, create the original clipart
00088     kdDebug(30003) << "Trying to load clipart... (Size:" << m_rawData.size() << ")" << endl;
00089     m_rawData=array;
00090     QBuffer buffer(m_rawData);
00091     buffer.open(IO_ReadOnly);
00092     bool check = true;
00093     if (extension=="svg")
00094     {
00095         if (!m_clipart.load(&buffer, "svg"))
00096         {
00097             kdWarning(30003) << "Loading SVG has failed! (KoPictureClipart::load)" << endl;
00098             check = false;
00099         }
00100     }
00101     else
00102     {
00103         if (!m_clipart.load(&buffer, NULL))
00104         {
00105             kdWarning(30003) << "Loading QPicture has failed! (KoPictureClipart::load)" << endl;
00106             check = false;
00107         }
00108     }
00109     buffer.close();
00110     return check;
00111 }
00112 
00113 bool KoPictureClipart::save(QIODevice* io) const
00114 {
00115     // We save the raw data, as the SVG supposrt in QPicture is poor
00116     Q_ULONG size=io->writeBlock(m_rawData); // WARNING: writeBlock returns Q_LONG but size() Q_ULONG!
00117     return (size==m_rawData.size());
00118 }
00119 
00120 QSize KoPictureClipart::getOriginalSize(void) const
00121 {
00122     return m_clipart.boundingRect().size();
00123 }
00124 
00125 QPixmap KoPictureClipart::generatePixmap(const QSize& size, bool /*smoothScale*/)
00126 {
00127     // Not sure if it works, but it worked for KoPictureFilePreviewWidget::setClipart
00128     QPixmap pixmap(size);
00129     QPainter p;
00130 
00131     p.begin( &pixmap );
00132     p.setBackgroundColor( Qt::white );
00133     pixmap.fill( Qt::white );
00134 
00135     QRect br = m_clipart.boundingRect();
00136     if ( br.width() && br.height() )
00137         p.scale( (double)pixmap.width() / (double)br.width(), (double)pixmap.height() / (double)br.height() );
00138     p.drawPicture( m_clipart );
00139     p.end();
00140     return pixmap;
00141 }
00142 
00143 QString KoPictureClipart::getMimeType(const QString& extension) const
00144 {
00145     if (extension=="svg")
00146         return "image/svg+xml";
00147     else
00148         return "image/x-vnd.trolltech.qpicture";
00149 }
00150 
KDE Home | KDE Accessibility Home | Description of Access Keys