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_stroke->setType( VStroke::none );
00024     m_fill = new VFill();
00025     m_image = new QImage( m_fname );
00026     if( m_image->depth() != 32 )
00027         *m_image = m_image->convertDepth( 32 );
00028     m_image->setAlphaBuffer( true );
00029     *m_image = m_image->swapRGB();
00030     *m_image = m_image->mirror( false, true );
00031 }
00032 
00033 VImage::VImage( const VImage &other ) : VObject( other )
00034 {
00035     if( other.m_image )
00036         m_image = new QImage( *other.m_image );
00037     else
00038         m_image         = 0L;
00039 
00040     if ( other.stroke() )
00041         setStroke( *other.stroke() );
00042     if ( other.fill() )
00043         setFill( *other.fill() );
00044 
00045     m_fname         = other.m_fname;
00046     m_boundingBox   = other.m_boundingBox;
00047     m_matrix        = other.m_matrix;
00048 }
00049 
00050 VImage::~VImage()
00051 {
00052     delete m_image;
00053 }
00054 
00055 void
00056 VImage::draw( VPainter *painter, const KoRect * ) const
00057 {
00058     if(
00059         state() == deleted ||
00060         state() == hidden ||
00061         state() == hidden_locked )
00062     {
00063         return;
00064     }
00065 
00066     if( state() == edit )
00067     {
00068         KoRect bbox = KoRect( 0, 0, m_image->width(), m_image->height() );
00069         KoPoint tl = bbox.topLeft().transform( m_matrix );
00070         KoPoint tr = bbox.topRight().transform( m_matrix );
00071         KoPoint bl = bbox.bottomLeft().transform( m_matrix );
00072         KoPoint br = bbox.bottomRight().transform( m_matrix );
00073 
00074         painter->moveTo( tl );
00075         painter->lineTo( tr );
00076         painter->lineTo( br );
00077         painter->lineTo( bl );
00078         painter->lineTo( tl );
00079 
00080         painter->setRasterOp( Qt::XorROP );
00081         //painter->setPen( stroke() );
00082         painter->setPen( Qt::yellow );
00083         painter->setBrush( Qt::NoBrush );
00084         painter->strokePath();
00085         return;
00086     }
00087 
00088     //painter->setWorldMatrix( m_matrix );
00089 
00090     //*m_image = m_image->smoothScale( m_image->width() * zoomFactor, m_image->height() * zoomFactor, QImage::ScaleMin );
00091     m_boundingBox = KoRect( 0, 0, m_image->width(), m_image->height() );
00092     m_boundingBox = m_boundingBox.transform( m_matrix );
00093     if( !m_image->isNull() )
00094         painter->drawImage( *m_image, m_matrix );
00095 }
00096 
00097 void
00098 VImage::transform( const QWMatrix& m )
00099 {
00100     //QWMatrix m2 = m;
00101     //m_matrix *= m2.scale( 1.0, -1.0 );
00102     m_matrix *= m;
00103     kdDebug(38000) << "dx : " << m.dx() << ", dy : " << m.dy() << endl;
00104     m_boundingBox = m_boundingBox.transform( m );
00105 }
00106 
00107 VObject *
00108 VImage::clone() const
00109 {
00110     return new VImage( *this );
00111 }
00112 
00113 void
00114 VImage::save( QDomElement& element ) const
00115 {
00116     if( state() != deleted )
00117     {
00118         QDomElement me = element.ownerDocument().createElement( "IMAGE" );
00119         element.appendChild( me );
00120 
00121         me.setAttribute( "fname", m_fname );
00122         me.setAttribute( "m11", m_matrix.m11() );
00123         me.setAttribute( "m12", m_matrix.m12() );
00124         me.setAttribute( "m21", m_matrix.m21() );
00125         me.setAttribute( "m22", m_matrix.m22() );
00126         me.setAttribute( "dx", m_matrix.dx() );
00127         me.setAttribute( "dy", m_matrix.dy() );
00128     }
00129 }
00130 
00131 void
00132 VImage::load( const QDomElement& element )
00133 {
00134     setState( normal );
00135     m_fname = element.attribute( "fname" );
00136     m_matrix.setMatrix( element.attribute( "m11", "1.0" ).toDouble(),
00137                         element.attribute( "m12", "0.0" ).toDouble(),
00138                         element.attribute( "m21", "0.0" ).toDouble(),
00139                         element.attribute( "m22", "1.0" ).toDouble(),
00140                         element.attribute( "dx", "0.0" ).toDouble(),
00141                         element.attribute( "dy", "0.0" ).toDouble() );
00142     kdDebug(38000) << "VImage::load : " << m_fname.latin1() << endl;
00143     delete m_image;
00144     m_image = new QImage( m_fname );
00145     if( m_image->depth() != 32 )
00146         *m_image = m_image->convertDepth( 32 );
00147     m_image->setAlphaBuffer( true );
00148     *m_image = m_image->swapRGB();
00149     *m_image = m_image->mirror( false, true );
00150     m_boundingBox = KoRect( 0, 0, m_image->width(), m_image->height() );
00151 }
00152 
00153 void
00154 VImage::accept( VVisitor& visitor )
00155 {
00156     visitor.visitVImage( *this );
00157 }
00158 
KDE Home | KDE Accessibility Home | Description of Access Keys