summaryrefslogtreecommitdiffstats
path: root/karbon/core/vimage.cc
blob: 4e8466613a671caa250b02f15b97a9c417dc1980 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* This file is part of the KDE project
   Copyright (C) 2001, 2002, 2003 The Karbon Developers
*/

#include "vimage.h"
#include "vpainter.h"
#include "vvisitor.h"
#include "vpath.h"
#include "vfill.h"
#include "vstroke.h"

#include <tqdom.h>
#include <tqimage.h>
#include <KoRect.h>

#include <render/vqpainter.h>

#include <kdebug.h>

VImage::VImage( VObject *parent, const TQString &fname ) : VObject( parent ), m_image( 0L ), m_fname( fname )
{
	m_stroke = new VStroke( this );
	m_stroke->setType( VStroke::none );
	m_fill = new VFill();
	m_image = new TQImage( m_fname );
	if( m_image->depth() != 32 )
        *m_image = m_image->convertDepth( 32 );
	m_image->setAlphaBuffer( true );
	*m_image = m_image->swapRGB();
	*m_image = m_image->mirror( false, true );
}

VImage::VImage( const VImage &other ) : VObject( other )
{
	if( other.m_image )
		m_image = new TQImage( *other.m_image );
	else
		m_image			= 0L;

	if ( other.stroke() )
		setStroke( *other.stroke() );
	if ( other.fill() )
		setFill( *other.fill() );

	m_fname			= other.m_fname;
	m_boundingBox	= other.m_boundingBox;
	m_matrix		= other.m_matrix;
}

VImage::~VImage()
{
	delete m_image;
}

void
VImage::draw( VPainter *painter, const KoRect * ) const
{
	if(
		state() == deleted ||
		state() == hidden ||
		state() == hidden_locked )
	{
		return;
	}

	if( state() == edit )
	{
		KoRect bbox = KoRect( 0, 0, m_image->width(), m_image->height() );
		KoPoint tl = bbox.topLeft().transform( m_matrix );
		KoPoint tr = bbox.topRight().transform( m_matrix );
		KoPoint bl = bbox.bottomLeft().transform( m_matrix );
		KoPoint br = bbox.bottomRight().transform( m_matrix );

	    painter->moveTo( tl );
	    painter->lineTo( tr );
	    painter->lineTo( br );
	    painter->lineTo( bl );
	    painter->lineTo( tl );

		painter->setRasterOp( TQt::XorROP );
		//painter->setPen( stroke() );
		painter->setPen( TQt::yellow );
		painter->setBrush( TQt::NoBrush );
		painter->strokePath();
		return;
	}

	//painter->setWorldMatrix( m_matrix );

	//*m_image = m_image->smoothScale( m_image->width() * zoomFactor, m_image->height() * zoomFactor, TQ_ScaleMin );
	m_boundingBox = KoRect( 0, 0, m_image->width(), m_image->height() );
	m_boundingBox = m_boundingBox.transform( m_matrix );
	if( !m_image->isNull() )
		painter->drawImage( *m_image, m_matrix );
}

void
VImage::transform( const TQWMatrix& m )
{
	//TQWMatrix m2 = m;
	//m_matrix *= m2.scale( 1.0, -1.0 );
	m_matrix *= m;
	kdDebug(38000) << "dx : " << m.dx() << ", dy : " << m.dy() << endl;
	m_boundingBox = m_boundingBox.transform( m );
}

VObject *
VImage::clone() const
{
	return new VImage( *this );
}

void
VImage::save( TQDomElement& element ) const
{
	if( state() != deleted )
	{
		TQDomElement me = element.ownerDocument().createElement( "IMAGE" );
		element.appendChild( me );

		me.setAttribute( "fname", m_fname );
		me.setAttribute( "m11", m_matrix.m11() );
		me.setAttribute( "m12", m_matrix.m12() );
		me.setAttribute( "m21", m_matrix.m21() );
		me.setAttribute( "m22", m_matrix.m22() );
		me.setAttribute( "dx", m_matrix.dx() );
		me.setAttribute( "dy", m_matrix.dy() );
	}
}

void
VImage::load( const TQDomElement& element )
{
	setState( normal );
	m_fname = element.attribute( "fname" );
	m_matrix.setMatrix( element.attribute( "m11", "1.0" ).toDouble(),
						element.attribute( "m12", "0.0" ).toDouble(),
						element.attribute( "m21", "0.0" ).toDouble(),
						element.attribute( "m22", "1.0" ).toDouble(),
						element.attribute( "dx", "0.0" ).toDouble(),
						element.attribute( "dy", "0.0" ).toDouble() );
	kdDebug(38000) << "VImage::load : " << m_fname.latin1() << endl;
	delete m_image;
	m_image = new TQImage( m_fname );
	if( m_image->depth() != 32 )
        *m_image = m_image->convertDepth( 32 );
	m_image->setAlphaBuffer( true );
	*m_image = m_image->swapRGB();
	*m_image = m_image->mirror( false, true );
	m_boundingBox = KoRect( 0, 0, m_image->width(), m_image->height() );
}

void
VImage::accept( VVisitor& visitor )
{
	visitor.visitVImage( *this );
}