karbon

vimage.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001, 2002, 2003 The Karbon Developers
00003 */
00004 
00005 #include "vimage.h"
00006 #include "vpainter.h"
00007 #include "vvisitor.h"
00008 #include "vpath.h"
00009 #include "vfill.h"
00010 #include "vstroke.h"
00011 
00012 #include <qdom.h>
00013 #include <qimage.h>
00014 #include <KoRect.h>
00015 
00016 #include <render/vqpainter.h>
00017 
00018 #include <kdebug.h>
00019 
00020 VImage::VImage( VObject *parent, const QString &fname ) : VObject( parent ), m_image( 0L ), m_fname( fname )
00021 {
00022     m_stroke = new VStroke( this );
00023     m_fill = new VFill();
00024     m_image = new QImage( m_fname );
00025     if( m_image->depth() != 32 )
00026         *m_image = m_image->convertDepth( 32 );
00027     m_image->setAlphaBuffer( true );
00028     *m_image = m_image->swapRGB();
00029     *m_image = m_image->mirror( false, true );
00030 }
00031 
00032 VImage::VImage( const VImage &other ) : VObject( other )
00033 {
00034     if( other.m_image )
00035         m_image = new QImage( *other.m_image );
00036     else
00037         m_image         = 0L;
00038     m_fname         = other.m_fname;
00039     m_boundingBox   = other.m_boundingBox;
00040     m_matrix        = other.m_matrix;
00041 }
00042 
00043 VImage::~VImage()
00044 {
00045     delete m_image;
00046 }
00047 
00048 void
00049 VImage::draw( VPainter *painter, const KoRect * ) const
00050 {
00051     if(
00052         state() == deleted ||
00053         state() == hidden ||
00054         state() == hidden_locked )
00055     {
00056         return;
00057     }
00058 
00059     if( state() == edit )
00060     {
00061         KoRect bbox = KoRect( 0, 0, m_image->width(), m_image->height() );
00062         KoPoint tl = bbox.topLeft().transform( m_matrix );
00063         KoPoint tr = bbox.topRight().transform( m_matrix );
00064         KoPoint bl = bbox.bottomLeft().transform( m_matrix );
00065         KoPoint br = bbox.bottomRight().transform( m_matrix );
00066 
00067         painter->moveTo( tl );
00068         painter->lineTo( tr );
00069         painter->lineTo( br );
00070         painter->lineTo( bl );
00071         painter->lineTo( tl );
00072 
00073         painter->setRasterOp( Qt::XorROP );
00074         //painter->setPen( stroke() );
00075         painter->setPen( Qt::yellow );
00076         painter->setBrush( Qt::NoBrush );
00077         painter->strokePath();
00078         return;
00079     }
00080 
00081     //painter->setWorldMatrix( m_matrix );
00082 
00083     //*m_image = m_image->smoothScale( m_image->width() * zoomFactor, m_image->height() * zoomFactor, QImage::ScaleMin );
00084     m_boundingBox = KoRect( 0, 0, m_image->width(), m_image->height() );
00085     m_boundingBox = m_boundingBox.transform( m_matrix );
00086     if( !m_image->isNull() )
00087         painter->drawImage( *m_image, m_matrix );
00088 }
00089 
00090 void
00091 VImage::transform( const QWMatrix& m )
00092 {
00093     //QWMatrix m2 = m;
00094     //m_matrix *= m2.scale( 1.0, -1.0 );
00095     m_matrix *= m;
00096     kdDebug(38000) << "dx : " << m.dx() << ", dy : " << m.dy() << endl;
00097     m_boundingBox = m_boundingBox.transform( m );
00098 }
00099 
00100 VObject *
00101 VImage::clone() const
00102 {
00103     return new VImage( *this );
00104 }
00105 
00106 void
00107 VImage::save( QDomElement& element ) const
00108 {
00109     if( state() != deleted )
00110     {
00111         QDomElement me = element.ownerDocument().createElement( "IMAGE" );
00112         element.appendChild( me );
00113 
00114         me.setAttribute( "fname", m_fname );
00115         me.setAttribute( "m11", m_matrix.m11() );
00116         me.setAttribute( "m12", m_matrix.m12() );
00117         me.setAttribute( "m21", m_matrix.m21() );
00118         me.setAttribute( "m22", m_matrix.m22() );
00119         me.setAttribute( "dx", m_matrix.dx() );
00120         me.setAttribute( "dy", m_matrix.dy() );
00121     }
00122 }
00123 
00124 void
00125 VImage::load( const QDomElement& element )
00126 {
00127     setState( normal );
00128     m_fname = element.attribute( "fname" );
00129     m_matrix.setMatrix( element.attribute( "m11", "1.0" ).toDouble(),
00130                         element.attribute( "m12", "0.0" ).toDouble(),
00131                         element.attribute( "m21", "0.0" ).toDouble(),
00132                         element.attribute( "m22", "1.0" ).toDouble(),
00133                         element.attribute( "dx", "0.0" ).toDouble(),
00134                         element.attribute( "dy", "0.0" ).toDouble() );
00135     kdDebug(38000) << "VImage::load : " << m_fname.latin1() << endl;
00136     delete m_image;
00137     m_image = new QImage( m_fname );
00138     if( m_image->depth() != 32 )
00139         *m_image = m_image->convertDepth( 32 );
00140     m_image->setAlphaBuffer( true );
00141     *m_image = m_image->swapRGB();
00142     *m_image = m_image->mirror( false, true );
00143     m_boundingBox = KoRect( 0, 0, m_image->width(), m_image->height() );
00144 }
00145 
00146 void
00147 VImage::accept( VVisitor& visitor )
00148 {
00149     visitor.visitVImage( *this );
00150 }
00151 
KDE Home | KDE Accessibility Home | Description of Access Keys