karbon
vstroke.cc00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <qdom.h>
00022
00023 #include <KoGenStyles.h>
00024 #include <KoStyleStack.h>
00025 #include <KoUnit.h>
00026 #include <KoXmlNS.h>
00027
00028 #include "vobject.h"
00029 #include "vstroke.h"
00030 #include <kdebug.h>
00031
00032 VStroke::VStroke( VObject* parent, float width, const VLineCap cap, const VLineJoin join,
00033 float miterLimit )
00034 {
00035 m_parent = parent;
00036 m_type = solid;
00037 m_lineWidth = width;
00038 m_lineCap = cap;
00039 m_lineJoin = join;
00040 m_miterLimit = miterLimit;
00041 }
00042
00043 VStroke::VStroke( const VColor &c, VObject* parent, float width, const VLineCap cap, const VLineJoin join,
00044 float miterLimit )
00045 {
00046 m_parent = parent;
00047 m_type = solid;
00048 m_lineWidth = width;
00049 m_lineCap = cap;
00050 m_lineJoin = join;
00051 m_miterLimit = miterLimit;
00052 m_color = c;
00053 }
00054
00055 VStroke::VStroke( const VStroke& stroke )
00056 {
00057
00058 *this = stroke;
00059 }
00060
00061 void
00062 VStroke::setLineWidth( float width )
00063 {
00064 m_lineWidth = width;
00065
00066
00067 if( m_parent )
00068 m_parent->invalidateBoundingBox();
00069 }
00070
00071 void
00072 VStroke::save( QDomElement& element ) const
00073 {
00074 QDomElement me = element.ownerDocument().createElement( "STROKE" );
00075 element.appendChild( me );
00076
00077
00078 if( m_lineWidth != 1.0 )
00079 me.setAttribute( "lineWidth", m_lineWidth );
00080 if( !( m_lineCap == capButt ) )
00081 me.setAttribute( "lineCap", m_lineCap );
00082 if( !( m_lineJoin == joinMiter ) )
00083 me.setAttribute( "lineJoin", m_lineJoin );
00084 if( m_miterLimit != 10.0 )
00085 me.setAttribute( "miterLimit", m_miterLimit );
00086
00087 if( m_type == solid )
00088 {
00089
00090 m_color.save( me );
00091 }
00092 else if( m_type == grad )
00093 {
00094
00095 m_gradient.save( me );
00096 }
00097 else if( m_type == patt )
00098 {
00099
00100 m_pattern.save( me );
00101 }
00102
00103
00104 m_dashPattern.save( me );
00105 }
00106
00107 void
00108 VStroke::saveOasis( KoGenStyle &style ) const
00109 {
00110 if( m_type == solid )
00111 {
00112 style.addProperty( "draw:stroke", "solid" );
00113 style.addProperty( "svg:stroke-color", QColor( m_color ).name() );
00114 style.addPropertyPt( "svg:stroke-width", m_lineWidth );
00115 if( m_color.opacity() < 1 )
00116 style.addProperty( "svg:stroke-opacity", QString( "%1%" ).arg( m_color.opacity() * 100. ) );
00117 }
00118 else if( m_type == none )
00119 style.addProperty( "draw:stroke", "none" );
00120
00121
00122
00123
00124
00125 if( m_lineJoin == joinRound )
00126 style.addProperty( "draw:stroke-linejoin", "round" );
00127 else if( m_lineJoin == joinBevel )
00128 style.addProperty( "draw:stroke-linejoin", "bevel" );
00129 else if( m_lineJoin == joinMiter )
00130 style.addProperty( "draw:stroke-linejoin", "miter" );
00131 }
00132
00133 void
00134 VStroke::loadOasis( const KoStyleStack &stack )
00135 {
00136 if( stack.hasAttributeNS( KoXmlNS::draw, "stroke" ))
00137 {
00138 if( stack.attributeNS( KoXmlNS::draw, "stroke" ) == "solid" )
00139 {
00140 setType( VStroke::solid );
00141 setColor( QColor( stack.attributeNS( KoXmlNS::svg, "stroke-color" ) ) );
00142 if( stack.hasAttributeNS( KoXmlNS::svg, "stroke-opacity" ) )
00143 m_color.setOpacity( stack.attributeNS( KoXmlNS::svg, "stroke-opacity" ).remove( '%' ).toFloat() / 100. );
00144 QString join = stack.attributeNS( KoXmlNS::draw, "stroke-linejoin" );
00145 if( !join.isEmpty() )
00146 {
00147 if( join == "round" )
00148 m_lineJoin = joinRound;
00149 else if( join == "bevel" )
00150 m_lineJoin = joinBevel;
00151 else
00152 m_lineJoin = joinMiter;
00153 }
00154 }
00155 else if( stack.attributeNS( KoXmlNS::draw, "stroke" ) == "none" )
00156 setType( VStroke::none );
00157 }
00158 if( stack.hasAttributeNS( KoXmlNS::svg, "stroke-width" ) )
00159 m_lineWidth = KoUnit::parseValue( stack.attributeNS( KoXmlNS::svg, "stroke-width" ) );
00160 if( m_lineWidth < 0.0 )
00161 m_lineWidth = 0.0;
00162 }
00163
00164 void
00165 VStroke::load( const QDomElement& element )
00166 {
00167 m_type = none;
00168
00169 m_lineWidth = element.attribute( "lineWidth", "1.0" ).toDouble();
00170 if( m_lineWidth < 0.0 )
00171 m_lineWidth = 0.0;
00172
00173 switch( element.attribute( "lineCap", "0" ).toUShort() )
00174 {
00175 case 1:
00176 m_lineCap = capRound; break;
00177 case 2:
00178 m_lineCap = capSquare; break;
00179 default:
00180 m_lineCap = capButt;
00181 }
00182
00183 switch( element.attribute( "lineJoin", "0" ).toUShort() )
00184 {
00185 case 1:
00186 m_lineJoin = joinRound; break;
00187 case 2:
00188 m_lineJoin = joinBevel; break;
00189 default:
00190 m_lineJoin = joinMiter;
00191 }
00192
00193 m_miterLimit = element.attribute( "miterLimit", "10.0" ).toDouble();
00194 if( m_miterLimit < 0.0 )
00195 m_miterLimit = 0.0;
00196
00197
00198
00199 QDomNodeList list = element.childNodes();
00200 for( uint i = 0; i < list.count(); ++i )
00201 {
00202 if( list.item( i ).isElement() )
00203 {
00204 QDomElement e = list.item( i ).toElement();
00205 if( e.tagName() == "COLOR" )
00206 {
00207 m_color.load( e );
00208 m_type = solid;
00209 }
00210 else if( e.tagName() == "DASHPATTERN" )
00211 {
00212 m_dashPattern.load( e );
00213 }
00214 else if( e.tagName() == "GRADIENT" )
00215 {
00216 m_type = grad;
00217 m_gradient.load( e );
00218 }
00219 else if( e.tagName() == "PATTERN" )
00220 {
00221 m_type = patt;
00222 m_pattern.load( e );
00223 }
00224 }
00225 }
00226 }
00227
00228
00229 VStroke&
00230 VStroke::operator=( const VStroke& stroke )
00231 {
00232 if( this != &stroke )
00233 {
00234
00235 m_type = stroke.m_type;
00236
00237 m_lineWidth = stroke.m_lineWidth;
00238
00239
00240
00241
00242 m_lineCap = stroke.m_lineCap;
00243 m_lineJoin = stroke.m_lineJoin;
00244 m_miterLimit = stroke.m_miterLimit;
00245 m_color = stroke.m_color;
00246 m_dashPattern = stroke.m_dashPattern;
00247 m_gradient = stroke.m_gradient;
00248 m_pattern = stroke.m_pattern;
00249 }
00250
00251 return *this;
00252 }
00253
00254 void
00255 VStroke::transform( const QWMatrix& m )
00256 {
00257 if( type() == VStroke::grad )
00258 gradient().transform( m );
00259 else if( type() == VStroke::patt )
00260 pattern().transform( m );
00261 }
|