lib Library API Documentation

koPictureClipart.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 "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::load(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_ReadWrite); 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) 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 bool KoPictureClipart::saveAsKOffice1Dot1(QIODevice* io, const QString& extension) 00121 { 00122 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) 00123 00124 bool result=false; 00125 if (extension=="svg") 00126 { 00127 // SVG: convert it to QPicture 00128 QBuffer buffer(m_rawData); 00129 buffer.open(IO_ReadWrite); 00130 if (picture.load(&buffer,"svg")) 00131 { 00132 result=picture.save(io,NULL); 00133 } 00134 buffer.close(); 00135 } 00136 else if (extension=="qpic") 00137 { 00138 // We cannot do much with a QPicture, we cannot convert it to previous formats 00139 result=save(io); 00140 } 00141 else 00142 { 00143 kdWarning(30003)<< "Unsupported clipart extension " << extension << " (KoPictureClipart::saveAsKOffice1Dot1)" << endl; 00144 result=save(io); // Always save something! 00145 } 00146 00147 return result; 00148 } 00149 00150 QSize KoPictureClipart::getOriginalSize(void) const 00151 { 00152 return m_clipart.boundingRect().size(); 00153 } 00154 00155 QPixmap KoPictureClipart::generatePixmap(const QSize& size, bool /*smoothScale*/) 00156 { 00157 // Not sure if it works, but it worked for KoPictureFilePreviewWidget::setClipart 00158 QPixmap pixmap(size); 00159 QPainter p; 00160 00161 p.begin( &pixmap ); 00162 p.setBackgroundColor( Qt::white ); 00163 pixmap.fill( Qt::white ); 00164 00165 QRect br = m_clipart.boundingRect(); 00166 if ( br.width() && br.height() ) 00167 p.scale( (double)pixmap.width() / (double)br.width(), (double)pixmap.height() / (double)br.height() ); 00168 p.drawPicture( m_clipart ); 00169 p.end(); 00170 return pixmap; 00171 } 00172 00173 bool KoPictureClipart::isClipartAsKOffice1Dot1(void) const 00174 { 00175 return true; 00176 } 00177 00178 QString KoPictureClipart::getMimeType(const QString& extension) const 00179 { 00180 if (extension=="svg") 00181 return "image/svg+xml"; 00182 else 00183 return "image/x-vnd.trolltech.qpicture"; 00184 } 00185
KDE Logo
This file is part of the documentation for lib Library Version 1.3.5.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Nov 17 06:54:17 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003