karbon

vimage.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001, 2002, 2003 The Karbon Developers
3*/
4
5#include "vimage.h"
6#include "vpainter.h"
7#include "vvisitor.h"
8#include "vpath.h"
9#include "vfill.h"
10#include "vstroke.h"
11
12#include <tqdom.h>
13#include <tqimage.h>
14#include <KoRect.h>
15
16#include <render/vqpainter.h>
17
18#include <kdebug.h>
19
20VImage::VImage( VObject *parent, const TQString &fname ) : VObject( parent ), m_image( 0L ), m_fname( fname )
21{
22 m_stroke = new VStroke( this );
23 m_stroke->setType( VStroke::none );
24 m_fill = new VFill();
25 m_image = new TQImage( m_fname );
26 if( m_image->depth() != 32 )
27 *m_image = m_image->convertDepth( 32 );
28 m_image->setAlphaBuffer( true );
29 *m_image = m_image->swapRGB();
30 *m_image = m_image->mirror( false, true );
31}
32
33VImage::VImage( const VImage &other ) : VObject( other )
34{
35 if( other.m_image )
36 m_image = new TQImage( *other.m_image );
37 else
38 m_image = 0L;
39
40 if ( other.stroke() )
41 setStroke( *other.stroke() );
42 if ( other.fill() )
43 setFill( *other.fill() );
44
45 m_fname = other.m_fname;
46 m_boundingBox = other.m_boundingBox;
47 m_matrix = other.m_matrix;
48}
49
50VImage::~VImage()
51{
52 delete m_image;
53}
54
55void
56VImage::draw( VPainter *painter, const KoRect * ) const
57{
58 if(
59 state() == deleted ||
60 state() == hidden ||
61 state() == hidden_locked )
62 {
63 return;
64 }
65
66 if( state() == edit )
67 {
68 KoRect bbox = KoRect( 0, 0, m_image->width(), m_image->height() );
69 KoPoint tl = bbox.topLeft().transform( m_matrix );
70 KoPoint tr = bbox.topRight().transform( m_matrix );
71 KoPoint bl = bbox.bottomLeft().transform( m_matrix );
72 KoPoint br = bbox.bottomRight().transform( m_matrix );
73
74 painter->moveTo( tl );
75 painter->lineTo( tr );
76 painter->lineTo( br );
77 painter->lineTo( bl );
78 painter->lineTo( tl );
79
80 painter->setRasterOp( TQt::XorROP );
81 //painter->setPen( stroke() );
82 painter->setPen( TQt::yellow );
83 painter->setBrush( TQt::NoBrush );
84 painter->strokePath();
85 return;
86 }
87
88 //painter->setWorldMatrix( m_matrix );
89
90 //*m_image = m_image->smoothScale( m_image->width() * zoomFactor, m_image->height() * zoomFactor, TQImage::ScaleMin );
91 m_boundingBox = KoRect( 0, 0, m_image->width(), m_image->height() );
92 m_boundingBox = m_boundingBox.transform( m_matrix );
93 if( !m_image->isNull() )
94 painter->drawImage( *m_image, m_matrix );
95}
96
97void
98VImage::transform( const TQWMatrix& m )
99{
100 //TQWMatrix m2 = m;
101 //m_matrix *= m2.scale( 1.0, -1.0 );
102 m_matrix *= m;
103 kdDebug(38000) << "dx : " << m.dx() << ", dy : " << m.dy() << endl;
104 m_boundingBox = m_boundingBox.transform( m );
105}
106
107VObject *
108VImage::clone() const
109{
110 return new VImage( *this );
111}
112
113void
114VImage::save( TQDomElement& element ) const
115{
116 if( state() != deleted )
117 {
118 TQDomElement me = element.ownerDocument().createElement( "IMAGE" );
119 element.appendChild( me );
120
121 me.setAttribute( "fname", m_fname );
122 me.setAttribute( "m11", m_matrix.m11() );
123 me.setAttribute( "m12", m_matrix.m12() );
124 me.setAttribute( "m21", m_matrix.m21() );
125 me.setAttribute( "m22", m_matrix.m22() );
126 me.setAttribute( "dx", m_matrix.dx() );
127 me.setAttribute( "dy", m_matrix.dy() );
128 }
129}
130
131void
132VImage::load( const TQDomElement& element )
133{
134 setState( normal );
135 m_fname = element.attribute( "fname" );
136 m_matrix.setMatrix( element.attribute( "m11", "1.0" ).toDouble(),
137 element.attribute( "m12", "0.0" ).toDouble(),
138 element.attribute( "m21", "0.0" ).toDouble(),
139 element.attribute( "m22", "1.0" ).toDouble(),
140 element.attribute( "dx", "0.0" ).toDouble(),
141 element.attribute( "dy", "0.0" ).toDouble() );
142 kdDebug(38000) << "VImage::load : " << m_fname.latin1() << endl;
143 delete m_image;
144 m_image = new TQImage( m_fname );
145 if( m_image->depth() != 32 )
146 *m_image = m_image->convertDepth( 32 );
147 m_image->setAlphaBuffer( true );
148 *m_image = m_image->swapRGB();
149 *m_image = m_image->mirror( false, true );
150 m_boundingBox = KoRect( 0, 0, m_image->width(), m_image->height() );
151}
152
153void
154VImage::accept( VVisitor& visitor )
155{
156 visitor.visitVImage( *this );
157}
158
Manages the fill of shapes.
Definition vfill.h:45
The base class for all karbon objects.
Definition vobject.h:50
Manages stroke properties.
Definition vstroke.h:49
The abstract visitor class.
Definition vvisitor.h:51
virtual void visitVImage(VImage &img)
Visit method for a VImage.
Definition vvisitor.cpp:107