F:/KPlato/koffice/libs/kofficecore/KoPictureClipart.cpp

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

Généré le Wed Nov 22 23:41:03 2006 pour KPlato par  doxygen 1.5.1-p1