vshadowdecorator.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002, The Karbon Developers
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "vshadowdecorator.h"
21#include <core/vfill.h>
22#include <core/vstroke.h>
23#include <render/vpainter.h>
24#include <core/vvisitor.h>
25#include <core/vdocument.h>
26#include <core/vselection.h>
27#include <commands/vtransformcmd.h>
28
29VShadowDecorator::VShadowDecorator( VObject *object, VObject* parent, int distance, int angle, float opacity )
30 : VObject( parent ), m_object( object ), m_distance( distance ), m_angle( angle ), m_opacity( opacity )
31{
32}
33
34VShadowDecorator::VShadowDecorator( const VShadowDecorator& other ) : VObject( other )
35{
36 m_object = other.m_object->clone();
37 m_opacity = other.m_opacity;
38 m_distance = other.m_distance;
39 m_angle = other.m_angle;
40}
41
42VShadowDecorator::~VShadowDecorator()
43{
44 delete m_object;
45}
46
47void
48VShadowDecorator::draw( VPainter* painter, const KoRect* rect ) const
49{
50 if( state() == deleted ||
51 state() == hidden ||
52 state() == hidden_locked )
53 {
54 return;
55 }
56
57 // make sure swallowed object has the same state
58 m_object->setState( state() );
59
60 if( state() != VObject::edit )
61 {
62 int shadowDx = int( m_distance * cos( m_angle / 360. * 6.2832 ) );
63 int shadowDy = int( m_distance * sin( m_angle / 360. * 6.2832 ) );
64
65 VFill *fill = new VFill( *m_object->fill() );
66 VStroke *stroke = new VStroke( *m_object->stroke() );
67 VColor black( TQt::black );
68 black.setOpacity( m_opacity );
69 if( m_object->fill()->type() != VFill::none )
70 m_object->fill()->setColor( black );
71 m_object->stroke()->setColor( black );
72 TQWMatrix mat = painter->worldMatrix();
73 painter->setWorldMatrix( mat.translate( shadowDx * painter->zoomFactor(), -shadowDy * painter->zoomFactor()) );
74 m_object->draw( painter, rect );
75 m_object->setFill( *fill );
76 m_object->setStroke( *stroke );
77 painter->setWorldMatrix( mat.translate( -shadowDx* painter->zoomFactor() , shadowDy * painter->zoomFactor() ) );
78 }
79 m_object->draw( painter, rect );
80}
81
82VObject *
83VShadowDecorator::clone() const
84{
85 return new VShadowDecorator( *this );
86}
87
88void
89VShadowDecorator::accept( VVisitor& visitor )
90{
91 m_object->accept( visitor );
92 visitor.visitVObject( *this );
93 // do not allow swallowed object to be part of the selection
94 document()->selection()->take( *m_object );
95}
96
97void
98VShadowDecorator::setShadow( int distance, int angle, float opacity )
99{
100 m_distance = distance;
101 m_angle = angle;
102 m_opacity = opacity;
103}
104
105void
106VShadowDecorator::setStroke( const VStroke& stroke )
107{
108 m_object->setStroke( stroke );
109}
110
111void
112VShadowDecorator::setFill( const VFill& fill )
113{
114 m_object->setFill( fill );
115}
116
117void
118VShadowDecorator::setState( const VState state )
119{
120 m_state = state;
121 m_object->setState( state );
122}
123
124void
125VShadowDecorator::save( TQDomElement& element ) const
126{
127 if( m_state != VObject::deleted )
128 {
129 // save shadow as new object
130 int shadowDx = int( m_distance * cos( m_angle / 360. * 6.2832 ) );
131 int shadowDy = int( m_distance * sin( m_angle / 360. * 6.2832 ) );
132
133 VObject *shadow = m_object->clone();
134
135 VColor black( TQt::black );
136 black.setOpacity( m_opacity );
137 if( shadow->fill()->type() != VFill::none )
138 shadow->fill()->setColor( black );
139 shadow->stroke()->setColor( black );
140 TQWMatrix mat;
141 mat.translate( shadowDx, -shadowDy );
142 VTransformCmd trafo( 0L, mat );
143 trafo.visit( *shadow );
144 shadow->save( element );
145 delete shadow;
146
147 // save swallowed object
148 m_object->save( element );
149 }
150}
This class keeps track of color properties.
Definition vcolor.h:41
Manages the fill of shapes.
Definition vfill.h:45
The base class for all karbon objects.
Definition vobject.h:50
virtual VObject * clone() const =0
Create an exact copy of this object.
virtual void setState(const VState state)
Sets the state to a specified new state.
Definition vobject.h:163
@ deleted
deleted, nearly dead
Definition vobject.h:58
@ edit
visible, active and is currently manipulated by a tool
Definition vobject.h:62
virtual VFill * fill() const
Gets the object's actual fill.
Definition vobject.h:177
virtual VStroke * stroke() const
Gets the object's actual stroke.
Definition vobject.h:170
virtual void save(TQDomElement &element) const
Save this object's state to xml.
Definition vobject.cpp:102
Manages stroke properties.
Definition vstroke.h:49
The abstract visitor class.
Definition vvisitor.h:51
virtual void visitVObject(VObject &object)
Visit method for a VObject.
Definition vvisitor.cpp:112