karbon

vobject.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002, The Karbon Developers
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <qdom.h>
00021 
00022 #include "vdocument.h"
00023 #include "vfill.h"
00024 #include "vobject.h"
00025 #include "vobject_iface.h"
00026 #include "vstroke.h"
00027 
00028 #include <KoStore.h>
00029 #include <KoGenStyles.h>
00030 #include <KoStyleStack.h>
00031 #include <KoXmlWriter.h>
00032 #include <KoXmlNS.h>
00033 #include <KoOasisLoadingContext.h>
00034 #include <KoOasisStyles.h>
00035 
00036 VObject::VObject( VObject* parent, VState state ) : m_dcop( 0L )
00037 {
00038     m_stroke = 0L;
00039     m_fill = 0L;
00040     
00041     m_parent = parent;
00042     m_state = state;
00043 
00044     invalidateBoundingBox();
00045 }
00046 
00047 VObject::VObject( const VObject& obj )
00048 {
00049     m_stroke = 0L;
00050     m_fill = 0L;
00051 
00052     m_parent = obj.m_parent;
00053     m_state = obj.m_state;
00054 
00055     invalidateBoundingBox();
00056     m_dcop = 0L;
00057 
00058     VDocument *srcDoc = obj.document();
00059     if( srcDoc && !srcDoc->objectName( &obj ).isEmpty() )
00060     {
00061         VDocument *dstDoc = document();
00062         if( dstDoc )
00063             dstDoc->setObjectName( this, srcDoc->objectName( &obj ) );
00064     }
00065 }
00066 
00067 VObject::~VObject()
00068 {
00069     delete( m_stroke );
00070     delete( m_fill );
00071     delete m_dcop;
00072 }
00073 
00074 DCOPObject *
00075 VObject::dcopObject()
00076 {
00077     if ( !m_dcop )
00078         m_dcop = new VObjectIface( this );
00079 
00080     return m_dcop;
00081 }
00082 
00083 void
00084 VObject::setStroke( const VStroke& stroke )
00085 {
00086     if( !m_stroke )
00087         m_stroke = new VStroke( this );
00088 
00089     *m_stroke = stroke;
00090 }
00091 
00092 void
00093 VObject::setFill( const VFill& fill )
00094 {
00095     if( !m_fill )
00096         m_fill = new VFill();
00097 
00098     *m_fill = fill;
00099 }
00100 
00101 void
00102 VObject::save( QDomElement& element ) const
00103 {
00104     if( m_stroke )
00105         m_stroke->save( element );
00106 
00107     if( m_fill )
00108         m_fill->save( element );
00109 
00110     VDocument *doc = document();
00111     if( doc && !doc->objectName( this ).isEmpty() )
00112         element.setAttribute( "ID", QString( doc->objectName( this ) ) );
00113 }
00114 
00115 void
00116 VObject::saveOasis( KoStore *, KoXmlWriter *docWriter, KoGenStyles &mainStyles, int &index ) const
00117 {
00118     if( !name().isEmpty() )
00119         docWriter->addAttribute( "draw:name", name() );
00120 
00121     QWMatrix mat;
00122     mat.scale( 1, -1 );
00123     mat.translate( 0, -document()->height() );
00124 
00125     KoGenStyle styleobjectauto( VDocument::STYLE_GRAPHICAUTO, "graphic" );
00126     saveOasisFill( mainStyles, styleobjectauto );
00127     if( m_stroke )
00128     {
00129         // mirror stroke before saving
00130         VStroke stroke( *m_stroke );
00131         stroke.transform( mat );
00132         stroke.saveOasis( styleobjectauto );
00133     }
00134     QString st = mainStyles.lookup( styleobjectauto, "st" );
00135     if(document())
00136             docWriter->addAttribute( "draw:id",  "obj" + QString::number( index ) );
00137     docWriter->addAttribute( "draw:style-name", st );
00138 }
00139 
00140 void
00141 VObject::saveOasisFill( KoGenStyles &mainStyles, KoGenStyle &stylesobjectauto ) const
00142 {
00143     if( m_fill )
00144     {
00145         QWMatrix mat;
00146         mat.scale( 1, -1 );
00147         mat.translate( 0, -document()->height() );
00148 
00149         // mirror fill before saving
00150         VFill fill( *m_fill );
00151         fill.transform( mat );
00152         fill.saveOasis( mainStyles, stylesobjectauto );
00153     }
00154 }
00155 
00156 void
00157 VObject::load( const QDomElement& element )
00158 {
00159     if( !m_stroke )
00160         m_stroke = new VStroke( this );
00161 
00162     if( !m_fill )
00163         m_fill = new VFill();
00164 
00165 
00166     if( element.tagName() == "STROKE" )
00167     {
00168         m_stroke->load( element );
00169     }
00170     else if( element.tagName() == "FILL" )
00171     {
00172         m_fill->load( element );
00173     }
00174 
00175     VDocument *doc = document();
00176     if( doc && !element.attribute( "ID" ).isEmpty() )
00177         doc->setObjectName( this, element.attribute( "ID" ) );
00178 }
00179 
00180 bool
00181 VObject::loadOasis( const QDomElement &object, KoOasisLoadingContext &context )
00182 {
00183     if( !m_stroke )
00184         m_stroke = new VStroke( this );
00185 
00186     if( !m_fill )
00187         m_fill = new VFill();
00188 
00189     if( object.hasAttributeNS( KoXmlNS::draw, "style-name" ) )
00190         context.fillStyleStack( object, KoXmlNS::draw, "style-name", "graphic" );
00191 
00192     KoStyleStack &styleStack = context.styleStack();
00193     styleStack.setTypeProperties( "graphic" );
00194     m_stroke->loadOasis( styleStack );
00195     m_fill->loadOasis( object, context, this );
00196 
00197     if( object.hasAttributeNS( KoXmlNS::draw, "name" ) )
00198         setName( object.attributeNS( KoXmlNS::draw, "name", QString::null ) );
00199 
00200     return true;
00201 }
00202 
00203 void
00204 VObject::addStyles( const QDomElement* style, KoOasisLoadingContext & context )
00205 {
00206     if(style)
00207     {
00208         // this function is necessary as parent styles can have parents themself
00209         if( style->hasAttributeNS( KoXmlNS::style, "parent-style-name" ) )
00210             addStyles( context.oasisStyles().findStyle( style->attributeNS( KoXmlNS::style, "parent-style-name", QString::null ) ), context );
00211         context.addStyles( style, "style-name" );
00212     }
00213 }
00214 
00215 VDocument *
00216 VObject::document() const
00217 {
00218     VObject *obj = (VObject *)this;
00219     while( obj->parent() )
00220         obj = obj->parent();
00221     return dynamic_cast<VDocument *>( obj );
00222 }
00223 
00224 QString
00225 VObject::name() const
00226 {
00227     return document() ? document()->objectName( this ) : QString();
00228 }
00229 
00230 void
00231 VObject::setName( const QString &s )
00232 {
00233     if( document() )
00234         document()->setObjectName( this, s );
00235 }
00236 
KDE Home | KDE Accessibility Home | Description of Access Keys