shadoweffectplugin.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002, 2003 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 "shadoweffectplugin.h"
21#include "tdelocale.h"
22#include <karbon_view.h>
23#include <karbon_part.h>
24#include <kgenericfactory.h>
25#include <kdebug.h>
26#include <tqgroupbox.h>
27#include <tqlabel.h>
28
29#include <knuminput.h>
30#include <core/vgroup.h>
31#include <core/vpath.h>
32#include <core/vsegment.h>
33#include <core/vselection.h>
34#include <core/vdocument.h>
35#include "vshadowdecorator.h"
36
37typedef KGenericFactory<ShadowEffectPlugin, KarbonView> ShadowEffectPluginFactory;
38K_EXPORT_COMPONENT_FACTORY( karbon_shadoweffectplugin, ShadowEffectPluginFactory( "karbonshadoweffectplugin" ) )
39
40ShadowEffectPlugin::ShadowEffectPlugin( KarbonView *parent, const char* name, const TQStringList & )
41: Plugin( parent, name )
42{
43 new TDEAction(
44 i18n( "&Shadow Effect..." ), "shadowRB", 0, this,
45 TQ_SLOT( slotShadowEffect() ), actionCollection(), "object_shadow" );
46
47 m_shadowEffectDlg = new VShadowEffectDlg();
48 m_shadowEffectDlg->setDistance( 2 );
49 m_shadowEffectDlg->setAngle( 0 );
50}
51
52void
53ShadowEffectPlugin::slotShadowEffect()
54{
55 KarbonPart *part = ((KarbonView *)parent())->part();
56 if( part && m_shadowEffectDlg->exec() )
57 part->addCommand( new VCreateShadowCmd( &part->document(), m_shadowEffectDlg->distance(), m_shadowEffectDlg->angle(), double( m_shadowEffectDlg->opacity() ) / 255.0 ), true );
58}
59
60VShadowEffectDlg::VShadowEffectDlg( TQWidget* parent, const char* name )
61 : KDialogBase( parent, name, true, i18n( "Create Shadow Effect" ), Ok | Cancel )
62{
63 // add input fields on the left:
64 TQGroupBox* group = new TQGroupBox( 2, TQt::Horizontal, i18n( "Properties" ), this );
65 new TQLabel( i18n( "Distance:" ), group );
66 m_distance = new KIntNumInput( group );
67 m_distance->setRange( -1000, 1000, 1, true );
68 m_distance->setValue( 2 );
69 new TQLabel( i18n( "Angle:" ), group );
70 m_angle = new KIntNumInput( group );
71 m_angle->setRange( 0, 360, 10, true );
72 m_angle->setValue( 0 );
73 new TQLabel( i18n( "Opacity:" ), group );
74 m_opacity = new KIntNumInput( group );
75 m_opacity->setRange( 0, 100, 1, true );
76 m_opacity->setValue( 100 );
77 group->setMinimumWidth( 300 );
78 m_opacity->setSuffix(i18n("%"));
79
80 // signals and slots:
81 connect( this, TQ_SIGNAL( okClicked() ), this, TQ_SLOT( accept() ) );
82 connect( this, TQ_SIGNAL( cancelClicked() ), this, TQ_SLOT( reject() ) );
83
84 setMainWidget( group );
85}
86
87void
88VShadowEffectDlg::setDistance( int d )
89{
90 m_distance->setValue( d );
91}
92
93void
94VShadowEffectDlg::setAngle( int a )
95{
96 m_angle->setValue( a );
97}
98
99void
100VShadowEffectDlg::setOpacity( int o )
101{
102 m_angle->setValue( o );
103}
104
105int
106VShadowEffectDlg::distance() const
107{
108 return m_distance->value();
109}
110
111int
112VShadowEffectDlg::angle() const
113{
114 return m_angle->value();
115}
116
117int
118VShadowEffectDlg::opacity() const
119{
120 return m_opacity->value();
121}
122
123VCreateShadowCmd::VCreateShadowCmd( VDocument* doc, int distance, int angle, float opacity )
124 : VCommand( doc, i18n( "Create Shadow" ) ), m_distance( distance ), m_angle( angle ), m_opacity( opacity )
125{
126 // Set members.
127 m_oldObjects = document()->selection()->clone();
128 m_newObjects = 0L;
129}
130
131VCreateShadowCmd::~VCreateShadowCmd()
132{
133 delete( m_oldObjects );
134 delete( m_newObjects );
135}
136
137void
138VCreateShadowCmd::execute()
139{
140 // Did we have at least once a success? Otherwise we don't get inserted
141 // into the command history.
142 bool successful = false;
143
144
145 // Create new shapes if they don't exist yet.
146 if( !m_newObjects )
147 {
148 m_newObjects = new VSelection();
149
150 // Pointer to temporary object.
151 VObject* newObject;
152
153 VObjectListIterator itr( m_oldObjects->objects() );
154
155 for( ; itr.current(); ++itr )
156 {
157 // Clone object and visit the clone.
158 VShadowDecorator *shadow = dynamic_cast<VShadowDecorator *>( itr.current() );
159 if( shadow )
160 {
161 //kdDebug() << "Its a decorator!!!" << endl;
162 shadow->setShadow( m_distance, m_angle, m_opacity );
163 newObject = 0L;
164 }
165 else
166 newObject = new VShadowDecorator( itr.current()->clone(), 0L, m_distance, m_angle, m_opacity );
167
168 successful = true;
169
170 if(newObject)
171 {
172 // Insert new shape right before old shape.
173 itr.current()->parent()->insertInfrontOf(
174 newObject, itr.current() );
175
176 // Add new shape to list of new objects.
177 m_newObjects->append( newObject );
178 }
179 }
180 }
181
182 // Nothing to do.
183 if( m_newObjects->objects().count() == 0 )
184 return;
185
186 VObjectListIterator itr( m_oldObjects->objects() );
187
188 // Hide old objects.
189 for( ; itr.current(); ++itr )
190 {
191 document()->selection()->take( *itr.current() );
192 itr.current()->setState( VObject::deleted );
193 }
194
195 // Show new objects.
196 for( itr = m_newObjects->objects(); itr.current(); ++itr )
197 {
198 itr.current()->setState( VObject::normal );
199 document()->selection()->append( itr.current() );
200 }
201
202 successful = true;
203
204 // Tell command history wether we had success at least once.
205 setSuccess( successful );
206}
207
208void
209VCreateShadowCmd::unexecute()
210{
211 // Nothing to do.
212 if( m_newObjects->objects().count() == 0 )
213 return;
214
215
216 VObjectListIterator itr( m_oldObjects->objects() );
217
218 // Show old objects.
219 for( ; itr.current(); ++itr )
220 {
221 itr.current()->setState( VObject::normal );
222 document()->selection()->append( itr.current() );
223 }
224
225 // Hide new objects.
226 for( itr = m_newObjects->objects(); itr.current(); ++itr )
227 {
228 document()->selection()->take( *itr.current() );
229 itr.current()->setState( VObject::deleted );
230 }
231
232 // Reset success for command history.
233 setSuccess( false );
234}
235
236#include "shadoweffectplugin.moc"
237
Keeps track of visual per document properties.
Definition karbon_part.h:46
void addCommand(VCommand *cmd, bool repaint=false)
insert a command into the undo/redo-history:
The base class for all karbon commands.
Definition vcommand.h:42
All non-visual, static doc info is in here.
Definition vdocument.h:53
The base class for all karbon objects.
Definition vobject.h:50
@ deleted
deleted, nearly dead
Definition vobject.h:58
@ normal
visible, not active
Definition vobject.h:54
VSelection manages a set of selected vobjects.
Definition vselection.h:62