summaryrefslogtreecommitdiffstats
path: root/karbon/core/vfill.cpp
blob: dfa4d5dfb9deb6bb6f15b71f509a6b821d3b7eae (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* This file is part of the KDE project
   Copyright (C) 2001, The Karbon Developers
   Copyright (C) 2002, The Karbon Developers

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <tqdom.h>
#include <kdebug.h>

#include <KoGenStyles.h>
#include <KoOasisLoadingContext.h>
#include <KoOasisStyles.h>
#include <KoXmlNS.h>

#include "vfill.h"

VFill::VFill()
	: m_type( none )
{
	/*m_gradient.addStop( VColor( TQt::red.rgb() ), 0.0 );
	m_gradient.addStop( VColor( TQt::yellow.rgb() ), 1.0 );
	m_gradient.setOrigin( KoPoint( 0, 0 ) );
	m_gradient.setVector( KoPoint( 0, 50 ) );
	m_gradient.setSpreadMethod( gradient_spread_reflect );*/
	//kdDebug(38000) << "Size of VFill : " << sizeof(*this) << endl;
}

VFill::VFill( const VColor &c )
	: m_type( solid )
{
	m_color = c;
	//kdDebug(38000) << "Size of VFill : " << sizeof(*this) << endl;
}

VFill::VFill( const VFill& fill )
{
	// doesn't copy parent:
	*this = fill;
}

void
VFill::save( TQDomElement& element ) const
{
	TQDomElement me = element.ownerDocument().createElement( "FILL" );
	element.appendChild( me );

	if( !( m_type == none ) )
	{
		// save color:
		m_color.save( me );
	}
	if( m_type == grad )
	{
		// save gradient:
		m_gradient.save( me );
	}
	else if( m_type == patt )
	{
		// save pattern:
		m_pattern.save( me );
	}
}

void
VFill::saveOasis( KoGenStyles &mainStyles, KoGenStyle &style ) const
{
	if( m_type == solid )
	{
		style.addProperty( "draw:fill", "solid" );
		style.addProperty( "draw:fill-color", TQColor( m_color ).name() );
		if( m_color.opacity() < 1 )
			style.addProperty( "draw:opacity", TQString( "%1%" ).arg( m_color.opacity() * 100. ) );
	}
	else if( m_type == grad )
	{
		style.addProperty( "draw:fill", "gradient" );
		TQString grad = m_gradient.saveOasis( mainStyles );
		style.addProperty( "draw:fill-gradient-name", grad );
		if( m_color.opacity() < 1 )
			style.addProperty( "draw:opacity", TQString( "%1%" ).arg( m_color.opacity() * 100. ) );
	}
	else if( m_type == patt )
		style.addProperty( "draw:fill", "hatch" );
	else
		style.addProperty( "draw:fill", "none" );
}

void
VFill::loadOasis( const TQDomElement &/*object*/, KoOasisLoadingContext &context, VObject* parent )
{
	KoStyleStack &stack = context.styleStack();
	if( stack.hasAttributeNS( KoXmlNS::draw, "fill" ) )
	{
		if( stack.attributeNS( KoXmlNS::draw, "fill" ) == "solid" )
		{
			setType( VFill::solid );
			setColor( TQColor( stack.attributeNS( KoXmlNS::draw, "fill-color" ) ) );
		}
		else if( stack.attributeNS( KoXmlNS::draw, "fill" ) == "gradient" )
		{
			setType( VFill::grad );
			TQString style = stack.attributeNS( KoXmlNS::draw, "fill-gradient-name" );
			kdDebug()<<" style gradient name :"<<style<<endl;
			TQDomElement *grad = context.oasisStyles().drawStyles()[ style ];
			kdDebug()<<" style gradient name :"<< grad <<endl;
			if( grad )
				m_gradient.loadOasis( *grad, stack, parent );
		}
		if( stack.hasAttributeNS( KoXmlNS::draw, "opacity" ) )
			m_color.setOpacity( stack.attributeNS( KoXmlNS::draw, "opacity" ).remove( '%' ).toFloat() / 100. );
	}
}

void
VFill::load( const TQDomElement& element )
{
	m_type = none;

	// load color:
	TQDomNodeList list = element.childNodes();
	for( uint i = 0; i < list.count(); ++i )
	{
		if( list.item( i ).isElement() )
		{
			TQDomElement e = list.item( i ).toElement();
			if( e.tagName() == "COLOR" )
			{
				m_type = solid;
				m_color.load( e );
			}
			if( e.tagName() == "GRADIENT" )
			{
				m_type = grad;
				m_gradient.load( e );
			}
			else if( e.tagName() == "PATTERN" )
			{
				m_type = patt;
				m_pattern.load( e );
			}
		}
	}
}

VFill&
VFill::operator=( const VFill& fill )
{
	if( this != &fill )
	{
		// dont copy the parent!
		m_type = fill.m_type;
		m_color = fill.m_color;
		m_gradient = fill.m_gradient;
		m_pattern = fill.m_pattern;
	}

	return *this;
}

void 
VFill::transform( const TQWMatrix& m )
{
	if( type() == VFill::grad )
		gradient().transform( m );
	else if( type() == VFill::patt )
		pattern().transform( m );
}