karbon

vfill.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001, The Karbon Developers
3 Copyright (C) 2002, The Karbon Developers
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include <tqdom.h>
22#include <kdebug.h>
23
24#include <KoGenStyles.h>
25#include <KoOasisLoadingContext.h>
26#include <KoOasisStyles.h>
27#include <KoXmlNS.h>
28
29#include "vfill.h"
30
31VFill::VFill()
32 : m_type( none )
33{
34 /*m_gradient.addStop( VColor( TQt::red.rgb() ), 0.0 );
35 m_gradient.addStop( VColor( TQt::yellow.rgb() ), 1.0 );
36 m_gradient.setOrigin( KoPoint( 0, 0 ) );
37 m_gradient.setVector( KoPoint( 0, 50 ) );
38 m_gradient.setSpreadMethod( gradient_spread_reflect );*/
39 //kdDebug(38000) << "Size of VFill : " << sizeof(*this) << endl;
40}
41
42VFill::VFill( const VColor &c )
43 : m_type( solid )
44{
45 m_color = c;
46 //kdDebug(38000) << "Size of VFill : " << sizeof(*this) << endl;
47}
48
49VFill::VFill( const VFill& fill )
50{
51 // doesn't copy parent:
52 *this = fill;
53}
54
55void
56VFill::save( TQDomElement& element ) const
57{
58 TQDomElement me = element.ownerDocument().createElement( "FILL" );
59 element.appendChild( me );
60
61 if( !( m_type == none ) )
62 {
63 // save color:
64 m_color.save( me );
65 }
66 if( m_type == grad )
67 {
68 // save gradient:
69 m_gradient.save( me );
70 }
71 else if( m_type == patt )
72 {
73 // save pattern:
74 m_pattern.save( me );
75 }
76}
77
78void
79VFill::saveOasis( KoGenStyles &mainStyles, KoGenStyle &style ) const
80{
81 if( m_type == solid )
82 {
83 style.addProperty( "draw:fill", "solid" );
84 style.addProperty( "draw:fill-color", TQColor( m_color ).name() );
85 if( m_color.opacity() < 1 )
86 style.addProperty( "draw:opacity", TQString( "%1%" ).arg( m_color.opacity() * 100. ) );
87 }
88 else if( m_type == grad )
89 {
90 style.addProperty( "draw:fill", "gradient" );
91 TQString grad = m_gradient.saveOasis( mainStyles );
92 style.addProperty( "draw:fill-gradient-name", grad );
93 if( m_color.opacity() < 1 )
94 style.addProperty( "draw:opacity", TQString( "%1%" ).arg( m_color.opacity() * 100. ) );
95 }
96 else if( m_type == patt )
97 style.addProperty( "draw:fill", "hatch" );
98 else
99 style.addProperty( "draw:fill", "none" );
100}
101
102void
103VFill::loadOasis( const TQDomElement &/*object*/, KoOasisLoadingContext &context, VObject* parent )
104{
105 KoStyleStack &stack = context.styleStack();
106 if( stack.hasAttributeNS( KoXmlNS::draw, "fill" ) )
107 {
108 if( stack.attributeNS( KoXmlNS::draw, "fill" ) == "solid" )
109 {
110 setType( VFill::solid );
111 setColor( TQColor( stack.attributeNS( KoXmlNS::draw, "fill-color" ) ) );
112 }
113 else if( stack.attributeNS( KoXmlNS::draw, "fill" ) == "gradient" )
114 {
115 setType( VFill::grad );
116 TQString style = stack.attributeNS( KoXmlNS::draw, "fill-gradient-name" );
117 kdDebug()<<" style gradient name :"<<style<<endl;
118 TQDomElement *grad = context.oasisStyles().drawStyles()[ style ];
119 kdDebug()<<" style gradient name :"<< grad <<endl;
120 if( grad )
121 m_gradient.loadOasis( *grad, stack, parent );
122 }
123 if( stack.hasAttributeNS( KoXmlNS::draw, "opacity" ) )
124 m_color.setOpacity( stack.attributeNS( KoXmlNS::draw, "opacity" ).remove( '%' ).toFloat() / 100. );
125 }
126}
127
128void
129VFill::load( const TQDomElement& element )
130{
131 m_type = none;
132
133 // load color:
134 TQDomNodeList list = element.childNodes();
135 for( uint i = 0; i < list.count(); ++i )
136 {
137 if( list.item( i ).isElement() )
138 {
139 TQDomElement e = list.item( i ).toElement();
140 if( e.tagName() == "COLOR" )
141 {
142 m_type = solid;
143 m_color.load( e );
144 }
145 if( e.tagName() == "GRADIENT" )
146 {
147 m_type = grad;
148 m_gradient.load( e );
149 }
150 else if( e.tagName() == "PATTERN" )
151 {
152 m_type = patt;
153 m_pattern.load( e );
154 }
155 }
156 }
157}
158
159VFill&
160VFill::operator=( const VFill& fill )
161{
162 if( this != &fill )
163 {
164 // dont copy the parent!
165 m_type = fill.m_type;
166 m_color = fill.m_color;
167 m_gradient = fill.m_gradient;
168 m_pattern = fill.m_pattern;
169 }
170
171 return *this;
172}
173
174void
175VFill::transform( const TQWMatrix& m )
176{
177 if( type() == VFill::grad )
178 gradient().transform( m );
179 else if( type() == VFill::patt )
180 pattern().transform( m );
181}
This class keeps track of color properties.
Definition vcolor.h:41
void setOpacity(float opacity)
Sets the color opacity.
Definition vcolor.h:138
void load(const TQDomElement &element)
Load this color's state from xml and initialize it accordingly.
Definition vcolor.cpp:321
float opacity() const
Returns the color opacity.
Definition vcolor.h:131
void save(TQDomElement &element) const
Save this color's state to xml.
Definition vcolor.cpp:297
Manages the fill of shapes.
Definition vfill.h:45
@ solid
no fill at all
Definition vfill.h:50
@ grad
solid fill
Definition vfill.h:51
@ patt
gradient fill
Definition vfill.h:52
The base class for all karbon objects.
Definition vobject.h:50