karbon

vshadowdecorator.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 "vshadowdecorator.h"
00021 #include <core/vfill.h>
00022 #include <core/vstroke.h>
00023 #include <render/vpainter.h>
00024 #include <core/vvisitor.h>
00025 #include <core/vdocument.h>
00026 #include <core/vselection.h>
00027 #include <commands/vtransformcmd.h>
00028 
00029 VShadowDecorator::VShadowDecorator( VObject *object, VObject* parent, int distance, int angle, float opacity )
00030     : VObject( parent ), m_object( object ), m_distance( distance ), m_angle( angle ), m_opacity( opacity )
00031 {
00032 }
00033 
00034 VShadowDecorator::VShadowDecorator( const VShadowDecorator& other ) : VObject( other )
00035 {
00036     m_object    = other.m_object->clone();
00037     m_opacity   = other.m_opacity;
00038     m_distance  = other.m_distance;
00039     m_angle     = other.m_angle;
00040 }
00041 
00042 VShadowDecorator::~VShadowDecorator()
00043 {
00044     delete m_object;
00045 }
00046 
00047 void
00048 VShadowDecorator::draw( VPainter* painter, const KoRect* rect ) const
00049 {
00050     if( state() == deleted ||
00051         state() == hidden ||
00052         state() == hidden_locked )
00053     {
00054         return;
00055     }
00056 
00057     // make sure swallowed object has the same state
00058     m_object->setState( state() );
00059 
00060     if( state() != VObject::edit )
00061     {
00062     int shadowDx = int( m_distance * cos( m_angle / 360. * 6.2832 ) );
00063     int shadowDy = int( m_distance * sin( m_angle / 360. * 6.2832 ) );
00064 
00065     VFill *fill = new VFill( *m_object->fill() );
00066     VStroke *stroke = new VStroke( *m_object->stroke() );
00067     VColor black( Qt::black );
00068     black.setOpacity( m_opacity );
00069     if( m_object->fill()->type() != VFill::none )
00070         m_object->fill()->setColor( black );
00071     m_object->stroke()->setColor( black );
00072     QWMatrix mat = painter->worldMatrix();
00073     painter->setWorldMatrix( mat.translate( shadowDx * painter->zoomFactor(), -shadowDy * painter->zoomFactor()) );
00074     m_object->draw( painter, rect );
00075     m_object->setFill( *fill );
00076     m_object->setStroke( *stroke );
00077     painter->setWorldMatrix( mat.translate( -shadowDx* painter->zoomFactor() , shadowDy * painter->zoomFactor() ) );
00078     }
00079     m_object->draw( painter, rect );
00080 }
00081 
00082 VObject *
00083 VShadowDecorator::clone() const
00084 {
00085     return new VShadowDecorator( *this );
00086 }
00087 
00088 void
00089 VShadowDecorator::accept( VVisitor& visitor )
00090 {
00091     m_object->accept( visitor );
00092     visitor.visitVObject( *this );
00093     // do not allow swallowed object to be part of the selection
00094     document()->selection()->take( *m_object );
00095 }
00096 
00097 void 
00098 VShadowDecorator::setShadow( int distance, int angle, float opacity )
00099 {
00100     m_distance = distance; 
00101     m_angle = angle;
00102     m_opacity = opacity;    
00103 }
00104 
00105 void 
00106 VShadowDecorator::setStroke( const VStroke& stroke )
00107 {
00108     m_object->setStroke( stroke );
00109 }
00110 
00111 void 
00112 VShadowDecorator::setFill( const VFill& fill )
00113 {
00114     m_object->setFill( fill );
00115 }
00116 
00117 void 
00118 VShadowDecorator::setState( const VState state )
00119 { 
00120     m_state = state;
00121     m_object->setState( state );
00122 }
00123 
00124 void 
00125 VShadowDecorator::save( QDomElement& element ) const
00126 {
00127     if( m_state != VObject::deleted )
00128     {
00129         // save shadow as new object
00130         int shadowDx = int( m_distance * cos( m_angle / 360. * 6.2832 ) );
00131         int shadowDy = int( m_distance * sin( m_angle / 360. * 6.2832 ) );
00132     
00133         VObject *shadow = m_object->clone();
00134 
00135         VColor black( Qt::black );
00136         black.setOpacity( m_opacity );
00137         if( shadow->fill()->type() != VFill::none )
00138             shadow->fill()->setColor( black );
00139         shadow->stroke()->setColor( black );
00140         QWMatrix mat;
00141         mat.translate( shadowDx, -shadowDy );
00142         VTransformCmd trafo( 0L, mat );
00143         trafo.visit( *shadow );
00144         shadow->save( element );
00145         delete shadow;
00146         
00147         // save swallowed object
00148         m_object->save( element );
00149     }
00150 }
KDE Home | KDE Accessibility Home | Description of Access Keys