karbon

shadoweffectplugin.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002, 2003 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 "shadoweffectplugin.h"
00021 #include "klocale.h"
00022 #include <karbon_view.h>
00023 #include <karbon_part.h>
00024 #include <kgenericfactory.h>
00025 #include <kdebug.h>
00026 #include <qgroupbox.h>
00027 #include <qlabel.h>
00028 
00029 #include <knuminput.h>
00030 #include <core/vgroup.h>
00031 #include <core/vpath.h>
00032 #include <core/vsegment.h>
00033 #include <core/vselection.h>
00034 #include <core/vdocument.h>
00035 #include "vshadowdecorator.h"
00036 
00037 typedef KGenericFactory<ShadowEffectPlugin, KarbonView> ShadowEffectPluginFactory;
00038 K_EXPORT_COMPONENT_FACTORY( karbon_shadoweffectplugin, ShadowEffectPluginFactory( "karbonshadoweffectplugin" ) )
00039 
00040 ShadowEffectPlugin::ShadowEffectPlugin( KarbonView *parent, const char* name, const QStringList & )
00041 : Plugin( parent, name )
00042 {
00043     new KAction(
00044         i18n( "&Shadow Effect..." ), "shadowRB", 0, this,
00045         SLOT( slotShadowEffect() ), actionCollection(), "object_shadow" );
00046 
00047     m_shadowEffectDlg = new VShadowEffectDlg();
00048     m_shadowEffectDlg->setDistance( 2 );
00049     m_shadowEffectDlg->setAngle( 0 );
00050 }
00051 
00052 void
00053 ShadowEffectPlugin::slotShadowEffect()
00054 {
00055     KarbonPart *part = ((KarbonView *)parent())->part();
00056     if( part && m_shadowEffectDlg->exec() )
00057         part->addCommand( new VCreateShadowCmd( &part->document(), m_shadowEffectDlg->distance(), m_shadowEffectDlg->angle(), double( m_shadowEffectDlg->opacity() ) / 255.0 ), true );
00058 }
00059 
00060 VShadowEffectDlg::VShadowEffectDlg( QWidget* parent, const char* name )
00061     : KDialogBase( parent, name, true,  i18n( "Create Shadow Effect" ), Ok | Cancel )
00062 {
00063     // add input fields on the left:
00064     QGroupBox* group = new QGroupBox( 2, Qt::Horizontal, i18n( "Properties" ), this );
00065     new QLabel( i18n( "Distance:" ), group );
00066     m_distance = new KIntNumInput( group );
00067     m_distance->setRange( -1000, 1000, 1, true );
00068     m_distance->setValue( 2 );
00069     new QLabel( i18n( "Angle:" ), group );
00070     m_angle = new KIntNumInput( group );
00071     m_angle->setRange( 0, 360, 10, true );
00072     m_angle->setValue( 0 );
00073     new QLabel( i18n( "Opacity:" ), group );
00074     m_opacity = new KIntNumInput( group );
00075     m_opacity->setRange( 0, 100, 1, true );
00076     m_opacity->setValue( 100 );
00077     group->setMinimumWidth( 300 );
00078     m_opacity->setSuffix(i18n("%"));
00079 
00080     // signals and slots:
00081     connect( this, SIGNAL( okClicked() ), this, SLOT( accept() ) );
00082     connect( this, SIGNAL( cancelClicked() ), this, SLOT( reject() ) );
00083 
00084     setMainWidget( group );
00085 }
00086 
00087 void
00088 VShadowEffectDlg::setDistance( int d )
00089 {
00090     m_distance->setValue( d );
00091 }
00092 
00093 void
00094 VShadowEffectDlg::setAngle( int a )
00095 {
00096     m_angle->setValue( a );
00097 }
00098 
00099 void
00100 VShadowEffectDlg::setOpacity( int o )
00101 {
00102     m_angle->setValue( o );
00103 }
00104 
00105 int
00106 VShadowEffectDlg::distance() const
00107 {
00108     return m_distance->value();
00109 }
00110 
00111 int
00112 VShadowEffectDlg::angle() const
00113 {
00114     return m_angle->value();
00115 }
00116 
00117 int
00118 VShadowEffectDlg::opacity() const
00119 {
00120     return m_opacity->value();
00121 }
00122 
00123 VCreateShadowCmd::VCreateShadowCmd( VDocument* doc, int distance, int angle, float opacity )
00124     : VCommand( doc, i18n( "Create Shadow" ) ), m_distance( distance ), m_angle( angle ), m_opacity( opacity )
00125 {
00126     // Set members.
00127     m_oldObjects = document()->selection()->clone();
00128     m_newObjects = 0L;
00129 }
00130 
00131 VCreateShadowCmd::~VCreateShadowCmd()
00132 {
00133     delete( m_oldObjects );
00134     delete( m_newObjects );
00135 }
00136 
00137 void
00138 VCreateShadowCmd::execute()
00139 {
00140     // Did we have at least once a success? Otherwise we don't get inserted
00141     // into the command history.
00142     bool successful = false;
00143 
00144 
00145     // Create new shapes if they don't exist yet.
00146     if( !m_newObjects )
00147     {
00148         m_newObjects = new VSelection();
00149 
00150         // Pointer to temporary object.
00151         VObject* newObject;
00152 
00153         VObjectListIterator itr( m_oldObjects->objects() );
00154 
00155         for( ; itr.current(); ++itr )
00156         {
00157             // Clone object and visit the clone.
00158             VShadowDecorator *shadow = dynamic_cast<VShadowDecorator *>( itr.current() );
00159             if( shadow )
00160             {
00161                 //kdDebug() <<  "Its a decorator!!!" << endl;
00162                 shadow->setShadow( m_distance, m_angle, m_opacity );
00163                 newObject = 0L;
00164             }
00165             else
00166                 newObject = new VShadowDecorator( itr.current()->clone(), 0L, m_distance, m_angle, m_opacity );
00167 
00168             successful = true;
00169 
00170             if(newObject)
00171             {
00172                 // Insert new shape right before old shape.
00173                 itr.current()->parent()->insertInfrontOf( 
00174                     newObject, itr.current() );
00175 
00176                 // Add new shape to list of new objects.
00177                 m_newObjects->append( newObject );
00178             }
00179         }
00180     }
00181 
00182     // Nothing to do.
00183     if( m_newObjects->objects().count() == 0 )
00184         return; 
00185     
00186     VObjectListIterator itr( m_oldObjects->objects() );
00187 
00188     // Hide old objects.
00189     for( ; itr.current(); ++itr )
00190     {
00191         document()->selection()->take( *itr.current() );
00192         itr.current()->setState( VObject::deleted );
00193     }
00194 
00195     // Show new objects.
00196     for( itr = m_newObjects->objects(); itr.current(); ++itr )
00197     {
00198         itr.current()->setState( VObject::normal );
00199         document()->selection()->append( itr.current() );
00200     }
00201 
00202     successful = true;
00203 
00204     // Tell command history wether we had success at least once.
00205     setSuccess( successful );
00206 }
00207 
00208 void
00209 VCreateShadowCmd::unexecute()
00210 {
00211     // Nothing to do.
00212     if( m_newObjects->objects().count() == 0 )
00213         return;
00214 
00215 
00216     VObjectListIterator itr( m_oldObjects->objects() );
00217 
00218     // Show old objects.
00219     for( ; itr.current(); ++itr )
00220     {
00221         itr.current()->setState( VObject::normal );
00222         document()->selection()->append( itr.current() );
00223     }
00224 
00225     // Hide new objects.
00226     for( itr = m_newObjects->objects(); itr.current(); ++itr )
00227     {
00228         document()->selection()->take( *itr.current() );
00229         itr.current()->setState( VObject::deleted );
00230     }
00231 
00232     // Reset success for command history.
00233     setSuccess( false );
00234 }
00235 
00236 #include "shadoweffectplugin.moc"
00237 
KDE Home | KDE Accessibility Home | Description of Access Keys