F:/KPlato/koffice/libs/kofficecore/KoPictureImage.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) 2002, 2003 Nicolas GOUTTE <goutte@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "KoPictureImage.h"
00022 #include "KoPictureKey.h"
00023 
00024 #include <kdebug.h>
00025 #include <kmimetype.h>
00026 
00027 #include <QBuffer>
00028 #include <QPainter>
00029 #include <QPrinter>
00030 #include <QImage>
00031 #include <QImageReader>
00032 #include <QPixmap>
00033 #include <QApplication>
00034 #include <q3dragobject.h>
00035 
00036 KoPictureImage::KoPictureImage(void) : m_cacheIsInFastMode(true)
00037 {
00038 }
00039 
00040 KoPictureImage::~KoPictureImage(void)
00041 {
00042 }
00043 
00044 KoPictureBase* KoPictureImage::newCopy(void) const
00045 {
00046     return new KoPictureImage(*this);
00047 }
00048 
00049 KoPictureType::Type KoPictureImage::getType(void) const
00050 {
00051     return KoPictureType::TypeImage;
00052 }
00053 
00054 bool KoPictureImage::isNull(void) const
00055 {
00056     return m_originalImage.isNull();
00057 }
00058 
00059 void KoPictureImage::scaleAndCreatePixmap(const QSize& size, bool fastMode)
00060 {
00061     if ((size==m_cachedSize)
00062         && ((fastMode) || (!m_cacheIsInFastMode)))
00063     {
00064         // The cached pixmap has already the right size
00065         // and:
00066         // - we are in fast mode (We do not care if the re-size was done slowly previously)
00067         // - the re-size was already done in slow mode
00068         return;
00069     }
00070 
00071     // Slow mode can be very slow, especially at high zoom levels -> configurable
00072     if ( !isSlowResizeModeAllowed() )
00073     {
00074         kDebug(30003) << "User has disallowed slow mode!" << endl;
00075         fastMode = true;
00076     }
00077 
00078     // Use QImage::scale if we have fastMode==true
00079     if ( fastMode )
00080     {
00081         m_cachedPixmap = QPixmap::fromImage( m_originalImage.scaled( size ), Qt::ColorOnly );
00082         // Always color or else B/W can be reversed
00083         m_cacheIsInFastMode=true;
00084     }
00085     else
00086     {
00087         m_cachedPixmap = QPixmap::fromImage( m_originalImage.scaled( size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ), Qt::ColorOnly );
00088         // Always color or else B/W can be reversed
00089         m_cacheIsInFastMode=false;
00090     }
00091     m_cachedSize=size;
00092 }
00093 
00094 void KoPictureImage::draw(QPainter& painter, int x, int y, int width, int height, int sx, int sy, int sw, int sh, bool fastMode)
00095 {
00096     //kDebug() << "KoImage::draw currentSize:" << currentSize.width() << "x" << currentSize.height() << endl;
00097     if ( !width || !height )
00098         return;
00099     QSize origSize = getOriginalSize();
00100     const bool scaleImage = dynamic_cast<QPrinter*>(painter.device()) != 0 // we are printing
00101         && ((width <= origSize.width()) || (height <= origSize.height()));
00102     if( scaleImage )
00103     {
00104         // use full resolution of image
00105         double xScale = double(width) / double(origSize.width());
00106         double yScale = double(height) / double(origSize.height());
00107 
00108         painter.save();
00109         painter.translate( x, y );
00110         painter.scale( xScale, yScale );
00111         // Note that sx, sy, sw and sh are unused in this case. Not a problem, since it's about printing.
00112         // Note 2: we do not cache the QPixmap. As we are printing, the next time we will probably
00113         //   need again the screen version.
00114         painter.drawImage(0, 0, m_originalImage);
00115         painter.restore();
00116     }
00117     else
00118     {
00119         QSize screenSize( width, height );
00120         //kDebug() << "KoPictureImage::draw screenSize=" << screenSize.width() << "x" << screenSize.height() << endl;
00121 
00122         scaleAndCreatePixmap(screenSize, fastMode);
00123 
00124         // sx,sy,sw,sh is meant to be used as a cliprect on the pixmap, but drawPixmap
00125         // translates it to the (x,y) point -> we need (x+sx, y+sy).
00126         painter.drawPixmap( x + sx, y + sy, m_cachedPixmap, sx, sy, sw, sh );
00127     }
00128 }
00129 
00130 bool KoPictureImage::loadData(const QByteArray& array, const QString& /* extension*/ )
00131 {
00132     m_rawData=array;
00133     // Second, create the original image
00134     QBuffer buffer( &m_rawData );
00135     buffer.open(QIODevice::ReadWrite);
00136     QImageReader imageReader( &buffer );
00137 
00138     QImage image = imageReader.read();
00139     buffer.close();
00140     if ( image.isNull() )
00141     {
00142         kError(30003) << "Image could not be loaded!" << endl;
00143         return false;
00144     }
00145     m_originalImage = image;
00146 
00147     return true;
00148 }
00149 
00150 bool KoPictureImage::save(QIODevice* io) const
00151 {
00152     kDebug() << k_funcinfo << "writing raw data. size=" << m_rawData.size() << endl;
00153     // We save the raw data, to avoid damaging the file by many load/save cycles (especially for JPEG)
00154     qint64 size = io->write(m_rawData); // WARNING: writeBlock returns Q_LONG but size() Q_ULONG!
00155     return ( size==m_rawData.size() );
00156 }
00157 
00158 QSize KoPictureImage::getOriginalSize(void) const
00159 {
00160     return m_originalImage.size();
00161 }
00162 
00163 QPixmap KoPictureImage::generatePixmap(const QSize& size, bool smoothScale)
00164 {
00165     scaleAndCreatePixmap(size,!smoothScale);
00166     return m_cachedPixmap;
00167 }
00168 
00169 QString KoPictureImage::getMimeType(const QString& extension) const
00170 {
00171     QString fileName("/tmp/temp.");
00172     fileName+=extension;
00173     // Find the mimetype only by the extension, not by file content (as the file is empty!)
00174     const QString mimetype( KMimeType::findByPath( fileName, 0 ,true )->name() );
00175     // ### TODO: use KMimeType::findByContent (but then the mimetype probably need to be cached)
00176     kDebug(30003) << "Image is mime type: " << mimetype << endl;
00177     return mimetype;
00178 }
00179 
00180 Q3DragObject* KoPictureImage::dragObject( QWidget *dragSource, const char *name )
00181 {
00182     return new Q3ImageDrag( m_originalImage, dragSource, name );
00183 }
00184 
00185 QImage KoPictureImage::generateImage(const QSize& size)
00186 {
00187     // We do not cache the image, as we will seldom need it again.
00188     return m_originalImage.scaled ( size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
00189 }
00190 
00191 void KoPictureImage::clearCache(void)
00192 {
00193     m_cachedPixmap = QPixmap();
00194     m_cacheIsInFastMode=true;
00195     m_cachedSize=QSize();
00196 }

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