karbon

vstroke.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
23#include <KoGenStyles.h>
24#include <KoStyleStack.h>
25#include <KoUnit.h>
26#include <KoXmlNS.h>
27
28#include "vobject.h"
29#include "vstroke.h"
30#include <kdebug.h>
31
32VStroke::VStroke()
33: m_parent(0L), m_lineWidth(1.0), m_lineCap(capButt), m_lineJoin(joinMiter), m_miterLimit(10.0), m_type(none)
34{}
35
36VStroke::VStroke( VObject* parent, float width, const VLineCap cap, const VLineJoin join,
37 float miterLimit )
38{
39 m_parent = parent;
40 m_type = solid;
41 m_lineWidth = width;
42 m_lineCap = cap;
43 m_lineJoin = join;
44 m_miterLimit = miterLimit;
45}
46
47VStroke::VStroke( const VColor &c, VObject* parent, float width, const VLineCap cap, const VLineJoin join,
48 float miterLimit )
49{
50 m_parent = parent;
51 m_type = solid;
52 m_lineWidth = width;
53 m_lineCap = cap;
54 m_lineJoin = join;
55 m_miterLimit = miterLimit;
56 m_color = c;
57}
58
59VStroke::VStroke( const VStroke& stroke )
60{
61 // doesn't copy parent:
62 *this = stroke;
63}
64
65void
66VStroke::setLineWidth( float width )
67{
68 m_lineWidth = width;
69
70 // tell our parent so he can update his bbox:
71 if( m_parent )
72 m_parent->invalidateBoundingBox();
73}
74
75void
76VStroke::save( TQDomElement& element ) const
77{
78 TQDomElement me = element.ownerDocument().createElement( "STROKE" );
79 element.appendChild( me );
80
81 // save stroke parameters:
82 if( m_lineWidth != 1.0 )
83 me.setAttribute( "lineWidth", m_lineWidth );
84 if( !( m_lineCap == capButt ) )
85 me.setAttribute( "lineCap", m_lineCap );
86 if( !( m_lineJoin == joinMiter ) )
87 me.setAttribute( "lineJoin", m_lineJoin );
88 if( m_miterLimit != 10.0 )
89 me.setAttribute( "miterLimit", m_miterLimit );
90
91 if( m_type == solid )
92 {
93 // save color:
94 m_color.save( me );
95 }
96 else if( m_type == grad )
97 {
98 // save gradient:
99 m_gradient.save( me );
100 }
101 else if( m_type == patt )
102 {
103 // save pattern:
104 m_pattern.save( me );
105 }
106
107 // save dashpattern:
108 m_dashPattern.save( me );
109}
110
111void
112VStroke::saveOasis( KoGenStyle &style ) const
113{
114 if( m_type == solid )
115 {
116 style.addProperty( "draw:stroke", "solid" );
117 style.addProperty( "svg:stroke-color", TQColor( m_color ).name() );
118 style.addPropertyPt( "svg:stroke-width", m_lineWidth );
119 if( m_color.opacity() < 1 )
120 style.addProperty( "svg:stroke-opacity", TQString( "%1%" ).arg( m_color.opacity() * 100. ) );
121 }
122 else if( m_type == none )
123 style.addProperty( "draw:stroke", "none" );
124 /*else if( m_type == grad )
125 style.addProperty( "draw:stroke", "gradient" );
126 else if( m_type == patt )
127 style.addProperty( "draw:stroke", "hatch" );*/
128
129 if( m_lineJoin == joinRound )
130 style.addProperty( "draw:stroke-linejoin", "round" );
131 else if( m_lineJoin == joinBevel )
132 style.addProperty( "draw:stroke-linejoin", "bevel" );
133 else if( m_lineJoin == joinMiter )
134 style.addProperty( "draw:stroke-linejoin", "miter" );
135}
136
137void
138VStroke::loadOasis( const KoStyleStack &stack )
139{
140 if( stack.hasAttributeNS( KoXmlNS::draw, "stroke" ))
141 {
142 if( stack.attributeNS( KoXmlNS::draw, "stroke" ) == "solid" )
143 {
144 setType( VStroke::solid );
145 setColor( TQColor( stack.attributeNS( KoXmlNS::svg, "stroke-color" ) ) );
146 if( stack.hasAttributeNS( KoXmlNS::svg, "stroke-opacity" ) )
147 m_color.setOpacity( stack.attributeNS( KoXmlNS::svg, "stroke-opacity" ).remove( '%' ).toFloat() / 100. );
148 TQString join = stack.attributeNS( KoXmlNS::draw, "stroke-linejoin" );
149 if( !join.isEmpty() )
150 {
151 if( join == "round" )
152 m_lineJoin = joinRound;
153 else if( join == "bevel" )
154 m_lineJoin = joinBevel;
155 else
156 m_lineJoin = joinMiter;
157 }
158 }
159 else if( stack.attributeNS( KoXmlNS::draw, "stroke" ) == "none" )
160 setType( VStroke::none );
161 }
162 if( stack.hasAttributeNS( KoXmlNS::svg, "stroke-width" ) )
163 m_lineWidth = KoUnit::parseValue( stack.attributeNS( KoXmlNS::svg, "stroke-width" ) );
164 if( m_lineWidth < 0.0 )
165 m_lineWidth = 0.0;
166}
167
168void
169VStroke::load( const TQDomElement& element )
170{
171 m_type = none;
172 // load stroke parameters:
173 m_lineWidth = element.attribute( "lineWidth", "1.0" ).toDouble();
174 if( m_lineWidth < 0.0 )
175 m_lineWidth = 0.0;
176
177 switch( element.attribute( "lineCap", "0" ).toUShort() )
178 {
179 case 1:
180 m_lineCap = capRound; break;
181 case 2:
182 m_lineCap = capSquare; break;
183 default:
184 m_lineCap = capButt;
185 }
186
187 switch( element.attribute( "lineJoin", "0" ).toUShort() )
188 {
189 case 1:
190 m_lineJoin = joinRound; break;
191 case 2:
192 m_lineJoin = joinBevel; break;
193 default:
194 m_lineJoin = joinMiter;
195 }
196
197 m_miterLimit = element.attribute( "miterLimit", "10.0" ).toDouble();
198 if( m_miterLimit < 0.0 )
199 m_miterLimit = 0.0;
200
201
202 // load color:
203 TQDomNodeList list = element.childNodes();
204 for( uint i = 0; i < list.count(); ++i )
205 {
206 if( list.item( i ).isElement() )
207 {
208 TQDomElement e = list.item( i ).toElement();
209 if( e.tagName() == "COLOR" )
210 {
211 m_color.load( e );
212 m_type = solid;
213 }
214 else if( e.tagName() == "DASHPATTERN" )
215 {
216 m_dashPattern.load( e );
217 }
218 else if( e.tagName() == "GRADIENT" )
219 {
220 m_type = grad;
221 m_gradient.load( e );
222 }
223 else if( e.tagName() == "PATTERN" )
224 {
225 m_type = patt;
226 m_pattern.load( e );
227 }
228 }
229 }
230}
231
232
233VStroke&
234VStroke::operator=( const VStroke& stroke )
235{
236 if( this != &stroke )
237 {
238 // dont copy the parent!
239 m_type = stroke.m_type;
240
241 m_lineWidth = stroke.m_lineWidth;
242 // Tell our parent about the linewidth change, so he can update his bbox:
243 //if( m_parent )
244 // m_parent->invalidateBoundingBox();
245
246 m_lineCap = stroke.m_lineCap;
247 m_lineJoin = stroke.m_lineJoin;
248 m_miterLimit = stroke.m_miterLimit;
249 m_color = stroke.m_color;
250 m_dashPattern = stroke.m_dashPattern;
251 m_gradient = stroke.m_gradient;
252 m_pattern = stroke.m_pattern;
253 }
254
255 return *this;
256}
257
258void
259VStroke::transform( const TQWMatrix& m )
260{
261 if( type() == VStroke::grad )
262 gradient().transform( m );
263 else if( type() == VStroke::patt )
264 pattern().transform( m );
265}
This class keeps track of color properties.
Definition vcolor.h:41
The base class for all karbon objects.
Definition vobject.h:50
Manages stroke properties.
Definition vstroke.h:49
@ patt
gradient as stroke
Definition vstroke.h:56
@ grad
solid stroke
Definition vstroke.h:55
@ solid
no stroke at all
Definition vstroke.h:54