karbon

vfill.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001, The Karbon Developers
00003    Copyright (C) 2002, The Karbon Developers
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include <qdom.h>
00022 #include <kdebug.h>
00023 
00024 #include <KoGenStyles.h>
00025 #include <KoOasisLoadingContext.h>
00026 #include <KoOasisStyles.h>
00027 #include <KoXmlNS.h>
00028 
00029 #include "vfill.h"
00030 
00031 VFill::VFill()
00032     : m_type( none )
00033 {
00034     /*m_gradient.addStop( VColor( Qt::red.rgb() ), 0.0 );
00035     m_gradient.addStop( VColor( Qt::yellow.rgb() ), 1.0 );
00036     m_gradient.setOrigin( KoPoint( 0, 0 ) );
00037     m_gradient.setVector( KoPoint( 0, 50 ) );
00038     m_gradient.setSpreadMethod( gradient_spread_reflect );*/
00039     //kdDebug(38000) << "Size of VFill : " << sizeof(*this) << endl;
00040 }
00041 
00042 VFill::VFill( const VColor &c )
00043     : m_type( solid )
00044 {
00045     m_color = c;
00046     //kdDebug(38000) << "Size of VFill : " << sizeof(*this) << endl;
00047 }
00048 
00049 VFill::VFill( const VFill& fill )
00050 {
00051     // doesn't copy parent:
00052     *this = fill;
00053 }
00054 
00055 void
00056 VFill::save( QDomElement& element ) const
00057 {
00058     QDomElement me = element.ownerDocument().createElement( "FILL" );
00059     element.appendChild( me );
00060 
00061     if( !( m_type == none ) )
00062     {
00063         // save color:
00064         m_color.save( me );
00065     }
00066     if( m_type == grad )
00067     {
00068         // save gradient:
00069         m_gradient.save( me );
00070     }
00071     else if( m_type == patt )
00072     {
00073         // save pattern:
00074         m_pattern.save( me );
00075     }
00076 }
00077 
00078 void
00079 VFill::saveOasis( KoGenStyles &mainStyles, KoGenStyle &style ) const
00080 {
00081     if( m_type == solid )
00082     {
00083         style.addProperty( "draw:fill", "solid" );
00084         style.addProperty( "draw:fill-color", QColor( m_color ).name() );
00085         if( m_color.opacity() < 1 )
00086             style.addProperty( "draw:opacity", QString( "%1%" ).arg( m_color.opacity() * 100. ) );
00087     }
00088     else if( m_type == grad )
00089     {
00090         style.addProperty( "draw:fill", "gradient" );
00091         QString grad = m_gradient.saveOasis( mainStyles );
00092         style.addProperty( "draw:fill-gradient-name", grad );
00093         if( m_color.opacity() < 1 )
00094             style.addProperty( "draw:opacity", QString( "%1%" ).arg( m_color.opacity() * 100. ) );
00095     }
00096     else if( m_type == patt )
00097         style.addProperty( "draw:fill", "hatch" );
00098     else
00099         style.addProperty( "draw:fill", "none" );
00100 }
00101 
00102 void
00103 VFill::loadOasis( const QDomElement &/*object*/, KoOasisLoadingContext &context, VObject* parent )
00104 {
00105     KoStyleStack &stack = context.styleStack();
00106     if( stack.hasAttributeNS( KoXmlNS::draw, "fill" ) )
00107     {
00108         if( stack.attributeNS( KoXmlNS::draw, "fill" ) == "solid" )
00109         {
00110             setType( VFill::solid );
00111             setColor( QColor( stack.attributeNS( KoXmlNS::draw, "fill-color" ) ) );
00112         }
00113         else if( stack.attributeNS( KoXmlNS::draw, "fill" ) == "gradient" )
00114         {
00115             setType( VFill::grad );
00116             QString style = stack.attributeNS( KoXmlNS::draw, "fill-gradient-name" );
00117             kdDebug()<<" style gradient name :"<<style<<endl;
00118             QDomElement *grad = context.oasisStyles().drawStyles()[ style ];
00119             kdDebug()<<" style gradient name :"<< grad <<endl;
00120             if( grad )
00121                 m_gradient.loadOasis( *grad, stack, parent );
00122         }
00123         if( stack.hasAttributeNS( KoXmlNS::draw, "opacity" ) )
00124             m_color.setOpacity( stack.attributeNS( KoXmlNS::draw, "opacity" ).remove( '%' ).toFloat() / 100. );
00125     }
00126 }
00127 
00128 void
00129 VFill::load( const QDomElement& element )
00130 {
00131     m_type = none;
00132 
00133     // load color:
00134     QDomNodeList list = element.childNodes();
00135     for( uint i = 0; i < list.count(); ++i )
00136     {
00137         if( list.item( i ).isElement() )
00138         {
00139             QDomElement e = list.item( i ).toElement();
00140             if( e.tagName() == "COLOR" )
00141             {
00142                 m_type = solid;
00143                 m_color.load( e );
00144             }
00145             if( e.tagName() == "GRADIENT" )
00146             {
00147                 m_type = grad;
00148                 m_gradient.load( e );
00149             }
00150             else if( e.tagName() == "PATTERN" )
00151             {
00152                 m_type = patt;
00153                 m_pattern.load( e );
00154             }
00155         }
00156     }
00157 }
00158 
00159 VFill&
00160 VFill::operator=( const VFill& fill )
00161 {
00162     if( this != &fill )
00163     {
00164         // dont copy the parent!
00165         m_type = fill.m_type;
00166         m_color = fill.m_color;
00167         m_gradient = fill.m_gradient;
00168         m_pattern = fill.m_pattern;
00169     }
00170 
00171     return *this;
00172 }
00173 
00174 void 
00175 VFill::transform( const QWMatrix& m )
00176 {
00177     if( type() == VFill::grad )
00178         gradient().transform( m );
00179     else if( type() == VFill::patt )
00180         pattern().transform( m );
00181 }
KDE Home | KDE Accessibility Home | Description of Access Keys