summaryrefslogtreecommitdiffstats
path: root/karbon/plugins/shadoweffect
diff options
context:
space:
mode:
Diffstat (limited to 'karbon/plugins/shadoweffect')
-rw-r--r--karbon/plugins/shadoweffect/Makefile.am15
-rw-r--r--karbon/plugins/shadoweffect/shadoweffectplugin.cc237
-rw-r--r--karbon/plugins/shadoweffect/shadoweffectplugin.h86
-rw-r--r--karbon/plugins/shadoweffect/shadoweffectplugin.rc11
-rw-r--r--karbon/plugins/shadoweffect/vshadowdecorator.cc150
-rw-r--r--karbon/plugins/shadoweffect/vshadowdecorator.h64
6 files changed, 563 insertions, 0 deletions
diff --git a/karbon/plugins/shadoweffect/Makefile.am b/karbon/plugins/shadoweffect/Makefile.am
new file mode 100644
index 00000000..73d9144f
--- /dev/null
+++ b/karbon/plugins/shadoweffect/Makefile.am
@@ -0,0 +1,15 @@
+INCLUDES = $(KOFFICE_INCLUDES) $(KOPAINTER_INCLUDES) -I$(top_srcdir)/karbon -I$(top_srcdir)/karbon/core $(all_includes)
+
+kde_module_LTLIBRARIES = karbon_shadoweffectplugin.la
+
+karbon_shadoweffectplugin_la_SOURCES = shadoweffectplugin.cc vshadowdecorator.cc
+karbon_shadoweffectplugin_la_LIBADD = $(LIB_KPARTS) $(LIB_KOFFICEUI) \
+ ../../libkarboncommon.la
+
+karbon_shadoweffectplugin_la_LDFLAGS = $(all_libraries) -module $(KDE_PLUGIN)
+
+partpluginsdir = $(kde_datadir)/karbon/kpartplugins
+partplugins_DATA = shadoweffectplugin.rc
+
+METASOURCES = AUTO
+
diff --git a/karbon/plugins/shadoweffect/shadoweffectplugin.cc b/karbon/plugins/shadoweffect/shadoweffectplugin.cc
new file mode 100644
index 00000000..c1a0aa33
--- /dev/null
+++ b/karbon/plugins/shadoweffect/shadoweffectplugin.cc
@@ -0,0 +1,237 @@
+/* This file is part of the KDE project
+ Copyright (C) 2002, 2003 The Karbon Developers
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+*/
+
+#include "shadoweffectplugin.h"
+#include "klocale.h"
+#include <karbon_view.h>
+#include <karbon_part.h>
+#include <kgenericfactory.h>
+#include <kdebug.h>
+#include <qgroupbox.h>
+#include <qlabel.h>
+
+#include <knuminput.h>
+#include <core/vgroup.h>
+#include <core/vpath.h>
+#include <core/vsegment.h>
+#include <core/vselection.h>
+#include <core/vdocument.h>
+#include "vshadowdecorator.h"
+
+typedef KGenericFactory<ShadowEffectPlugin, KarbonView> ShadowEffectPluginFactory;
+K_EXPORT_COMPONENT_FACTORY( karbon_shadoweffectplugin, ShadowEffectPluginFactory( "karbonshadoweffectplugin" ) )
+
+ShadowEffectPlugin::ShadowEffectPlugin( KarbonView *parent, const char* name, const QStringList & )
+: Plugin( parent, name )
+{
+ new KAction(
+ i18n( "&Shadow Effect..." ), "shadowRB", 0, this,
+ SLOT( slotShadowEffect() ), actionCollection(), "object_shadow" );
+
+ m_shadowEffectDlg = new VShadowEffectDlg();
+ m_shadowEffectDlg->setDistance( 2 );
+ m_shadowEffectDlg->setAngle( 0 );
+}
+
+void
+ShadowEffectPlugin::slotShadowEffect()
+{
+ KarbonPart *part = ((KarbonView *)parent())->part();
+ if( part && m_shadowEffectDlg->exec() )
+ part->addCommand( new VCreateShadowCmd( &part->document(), m_shadowEffectDlg->distance(), m_shadowEffectDlg->angle(), double( m_shadowEffectDlg->opacity() ) / 255.0 ), true );
+}
+
+VShadowEffectDlg::VShadowEffectDlg( QWidget* parent, const char* name )
+ : KDialogBase( parent, name, true, i18n( "Create Shadow Effect" ), Ok | Cancel )
+{
+ // add input fields on the left:
+ QGroupBox* group = new QGroupBox( 2, Qt::Horizontal, i18n( "Properties" ), this );
+ new QLabel( i18n( "Distance:" ), group );
+ m_distance = new KIntNumInput( group );
+ m_distance->setRange( -1000, 1000, 1, true );
+ m_distance->setValue( 2 );
+ new QLabel( i18n( "Angle:" ), group );
+ m_angle = new KIntNumInput( group );
+ m_angle->setRange( 0, 360, 10, true );
+ m_angle->setValue( 0 );
+ new QLabel( i18n( "Opacity:" ), group );
+ m_opacity = new KIntNumInput( group );
+ m_opacity->setRange( 0, 100, 1, true );
+ m_opacity->setValue( 100 );
+ group->setMinimumWidth( 300 );
+ m_opacity->setSuffix(i18n("%"));
+
+ // signals and slots:
+ connect( this, SIGNAL( okClicked() ), this, SLOT( accept() ) );
+ connect( this, SIGNAL( cancelClicked() ), this, SLOT( reject() ) );
+
+ setMainWidget( group );
+}
+
+void
+VShadowEffectDlg::setDistance( int d )
+{
+ m_distance->setValue( d );
+}
+
+void
+VShadowEffectDlg::setAngle( int a )
+{
+ m_angle->setValue( a );
+}
+
+void
+VShadowEffectDlg::setOpacity( int o )
+{
+ m_angle->setValue( o );
+}
+
+int
+VShadowEffectDlg::distance() const
+{
+ return m_distance->value();
+}
+
+int
+VShadowEffectDlg::angle() const
+{
+ return m_angle->value();
+}
+
+int
+VShadowEffectDlg::opacity() const
+{
+ return m_opacity->value();
+}
+
+VCreateShadowCmd::VCreateShadowCmd( VDocument* doc, int distance, int angle, float opacity )
+ : VCommand( doc, i18n( "Create Shadow" ) ), m_distance( distance ), m_angle( angle ), m_opacity( opacity )
+{
+ // Set members.
+ m_oldObjects = document()->selection()->clone();
+ m_newObjects = 0L;
+}
+
+VCreateShadowCmd::~VCreateShadowCmd()
+{
+ delete( m_oldObjects );
+ delete( m_newObjects );
+}
+
+void
+VCreateShadowCmd::execute()
+{
+ // Did we have at least once a success? Otherwise we don't get inserted
+ // into the command history.
+ bool successful = false;
+
+
+ // Create new shapes if they don't exist yet.
+ if( !m_newObjects )
+ {
+ m_newObjects = new VSelection();
+
+ // Pointer to temporary object.
+ VObject* newObject;
+
+ VObjectListIterator itr( m_oldObjects->objects() );
+
+ for( ; itr.current(); ++itr )
+ {
+ // Clone object and visit the clone.
+ VShadowDecorator *shadow = dynamic_cast<VShadowDecorator *>( itr.current() );
+ if( shadow )
+ {
+ //kdDebug() << "Its a decorator!!!" << endl;
+ shadow->setShadow( m_distance, m_angle, m_opacity );
+ newObject = 0L;
+ }
+ else
+ newObject = new VShadowDecorator( itr.current()->clone(), 0L, m_distance, m_angle, m_opacity );
+
+ successful = true;
+
+ if(newObject)
+ {
+ // Insert new shape right before old shape.
+ itr.current()->parent()->insertInfrontOf(
+ newObject, itr.current() );
+
+ // Add new shape to list of new objects.
+ m_newObjects->append( newObject );
+ }
+ }
+ }
+
+ // Nothing to do.
+ if( m_newObjects->objects().count() == 0 )
+ return;
+
+ VObjectListIterator itr( m_oldObjects->objects() );
+
+ // Hide old objects.
+ for( ; itr.current(); ++itr )
+ {
+ document()->selection()->take( *itr.current() );
+ itr.current()->setState( VObject::deleted );
+ }
+
+ // Show new objects.
+ for( itr = m_newObjects->objects(); itr.current(); ++itr )
+ {
+ itr.current()->setState( VObject::normal );
+ document()->selection()->append( itr.current() );
+ }
+
+ successful = true;
+
+ // Tell command history wether we had success at least once.
+ setSuccess( successful );
+}
+
+void
+VCreateShadowCmd::unexecute()
+{
+ // Nothing to do.
+ if( m_newObjects->objects().count() == 0 )
+ return;
+
+
+ VObjectListIterator itr( m_oldObjects->objects() );
+
+ // Show old objects.
+ for( ; itr.current(); ++itr )
+ {
+ itr.current()->setState( VObject::normal );
+ document()->selection()->append( itr.current() );
+ }
+
+ // Hide new objects.
+ for( itr = m_newObjects->objects(); itr.current(); ++itr )
+ {
+ document()->selection()->take( *itr.current() );
+ itr.current()->setState( VObject::deleted );
+ }
+
+ // Reset success for command history.
+ setSuccess( false );
+}
+
+#include "shadoweffectplugin.moc"
+
diff --git a/karbon/plugins/shadoweffect/shadoweffectplugin.h b/karbon/plugins/shadoweffect/shadoweffectplugin.h
new file mode 100644
index 00000000..9a97da98
--- /dev/null
+++ b/karbon/plugins/shadoweffect/shadoweffectplugin.h
@@ -0,0 +1,86 @@
+/* This file is part of the KDE project
+ Copyright (C) 2002, 2003, The Karbon Developers
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+*/
+
+#ifndef __SHADOWEFFECTPLUGIN_H__
+#define __SHADOWEFFECTPLUGIN_H__
+
+#include <kparts/plugin.h>
+#include <kdialogbase.h>
+#include <commands/vcommand.h>
+
+class KarbonView;
+class VSelection;
+class VShadowEffectDlg;
+
+class ShadowEffectPlugin : public KParts::Plugin
+{
+ Q_OBJECT
+public:
+ ShadowEffectPlugin( KarbonView *parent, const char* name, const QStringList & );
+ virtual ~ShadowEffectPlugin() {}
+
+private slots:
+ void slotShadowEffect();
+
+private:
+ VShadowEffectDlg *m_shadowEffectDlg;
+};
+
+class KIntNumInput;
+
+class VShadowEffectDlg : public KDialogBase
+{
+ Q_OBJECT
+
+public:
+ VShadowEffectDlg( QWidget* parent = 0L, const char* name = 0L );
+
+ void setAngle( int );
+ void setDistance( int );
+ void setOpacity( int );
+
+ int angle() const;
+ int distance() const;
+ int opacity() const;
+
+private:
+ KIntNumInput *m_angle;
+ KIntNumInput *m_distance;
+ KIntNumInput *m_opacity;
+};
+
+class VCreateShadowCmd : public VCommand
+{
+public:
+ VCreateShadowCmd( VDocument* doc, int distance, int angle, float opacity );
+ virtual ~VCreateShadowCmd();
+
+ virtual void execute();
+ virtual void unexecute();
+ virtual bool changesSelection() const { return true; }
+private:
+ VSelection *m_oldObjects;
+ VSelection *m_newObjects;
+ int m_distance;
+ int m_angle;
+ float m_opacity;
+};
+
+#endif
+
diff --git a/karbon/plugins/shadoweffect/shadoweffectplugin.rc b/karbon/plugins/shadoweffect/shadoweffectplugin.rc
new file mode 100644
index 00000000..2cfe75af
--- /dev/null
+++ b/karbon/plugins/shadoweffect/shadoweffectplugin.rc
@@ -0,0 +1,11 @@
+<!DOCTYPE kpartgui>
+<kpartplugin name="shadoweffectplugin" library="karbon_shadoweffectplugin">
+<MenuBar>
+ <Menu name="effects">
+ <Action name="object_shadow"/>
+ </Menu>
+</MenuBar>
+<ToolBar name="Effects" hidden="true" fullWidth="false">
+ <Action name="object_shadow"/>
+</ToolBar>
+</kpartplugin>
diff --git a/karbon/plugins/shadoweffect/vshadowdecorator.cc b/karbon/plugins/shadoweffect/vshadowdecorator.cc
new file mode 100644
index 00000000..238673ba
--- /dev/null
+++ b/karbon/plugins/shadoweffect/vshadowdecorator.cc
@@ -0,0 +1,150 @@
+/* This file is part of the KDE project
+ Copyright (C) 2002, The Karbon Developers
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+*/
+
+#include "vshadowdecorator.h"
+#include <core/vfill.h>
+#include <core/vstroke.h>
+#include <render/vpainter.h>
+#include <core/vvisitor.h>
+#include <core/vdocument.h>
+#include <core/vselection.h>
+#include <commands/vtransformcmd.h>
+
+VShadowDecorator::VShadowDecorator( VObject *object, VObject* parent, int distance, int angle, float opacity )
+ : VObject( parent ), m_object( object ), m_distance( distance ), m_angle( angle ), m_opacity( opacity )
+{
+}
+
+VShadowDecorator::VShadowDecorator( const VShadowDecorator& other ) : VObject( other )
+{
+ m_object = other.m_object->clone();
+ m_opacity = other.m_opacity;
+ m_distance = other.m_distance;
+ m_angle = other.m_angle;
+}
+
+VShadowDecorator::~VShadowDecorator()
+{
+ delete m_object;
+}
+
+void
+VShadowDecorator::draw( VPainter* painter, const KoRect* rect ) const
+{
+ if( state() == deleted ||
+ state() == hidden ||
+ state() == hidden_locked )
+ {
+ return;
+ }
+
+ // make sure swallowed object has the same state
+ m_object->setState( state() );
+
+ if( state() != VObject::edit )
+ {
+ int shadowDx = int( m_distance * cos( m_angle / 360. * 6.2832 ) );
+ int shadowDy = int( m_distance * sin( m_angle / 360. * 6.2832 ) );
+
+ VFill *fill = new VFill( *m_object->fill() );
+ VStroke *stroke = new VStroke( *m_object->stroke() );
+ VColor black( Qt::black );
+ black.setOpacity( m_opacity );
+ if( m_object->fill()->type() != VFill::none )
+ m_object->fill()->setColor( black );
+ m_object->stroke()->setColor( black );
+ QWMatrix mat = painter->worldMatrix();
+ painter->setWorldMatrix( mat.translate( shadowDx * painter->zoomFactor(), -shadowDy * painter->zoomFactor()) );
+ m_object->draw( painter, rect );
+ m_object->setFill( *fill );
+ m_object->setStroke( *stroke );
+ painter->setWorldMatrix( mat.translate( -shadowDx* painter->zoomFactor() , shadowDy * painter->zoomFactor() ) );
+ }
+ m_object->draw( painter, rect );
+}
+
+VObject *
+VShadowDecorator::clone() const
+{
+ return new VShadowDecorator( *this );
+}
+
+void
+VShadowDecorator::accept( VVisitor& visitor )
+{
+ m_object->accept( visitor );
+ visitor.visitVObject( *this );
+ // do not allow swallowed object to be part of the selection
+ document()->selection()->take( *m_object );
+}
+
+void
+VShadowDecorator::setShadow( int distance, int angle, float opacity )
+{
+ m_distance = distance;
+ m_angle = angle;
+ m_opacity = opacity;
+}
+
+void
+VShadowDecorator::setStroke( const VStroke& stroke )
+{
+ m_object->setStroke( stroke );
+}
+
+void
+VShadowDecorator::setFill( const VFill& fill )
+{
+ m_object->setFill( fill );
+}
+
+void
+VShadowDecorator::setState( const VState state )
+{
+ m_state = state;
+ m_object->setState( state );
+}
+
+void
+VShadowDecorator::save( QDomElement& element ) const
+{
+ if( m_state != VObject::deleted )
+ {
+ // save shadow as new object
+ int shadowDx = int( m_distance * cos( m_angle / 360. * 6.2832 ) );
+ int shadowDy = int( m_distance * sin( m_angle / 360. * 6.2832 ) );
+
+ VObject *shadow = m_object->clone();
+
+ VColor black( Qt::black );
+ black.setOpacity( m_opacity );
+ if( shadow->fill()->type() != VFill::none )
+ shadow->fill()->setColor( black );
+ shadow->stroke()->setColor( black );
+ QWMatrix mat;
+ mat.translate( shadowDx, -shadowDy );
+ VTransformCmd trafo( 0L, mat );
+ trafo.visit( *shadow );
+ shadow->save( element );
+ delete shadow;
+
+ // save swallowed object
+ m_object->save( element );
+ }
+}
diff --git a/karbon/plugins/shadoweffect/vshadowdecorator.h b/karbon/plugins/shadoweffect/vshadowdecorator.h
new file mode 100644
index 00000000..3ec57e88
--- /dev/null
+++ b/karbon/plugins/shadoweffect/vshadowdecorator.h
@@ -0,0 +1,64 @@
+/* This file is part of the KDE project
+ Copyright (C) 2001, 2002, 2003 The Karbon Developers
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+*/
+
+#ifndef __VSHADOWDECORATOR_H__
+#define __VSHADOWDECORATOR_H__
+
+
+#include "vobject.h"
+#include <koffice_export.h>
+
+/**
+ */
+class KARBONBASE_EXPORT VShadowDecorator : public VObject
+{
+public:
+ VShadowDecorator( VObject* object, VObject* parent, int distance = 2, int angle = 0, float opacity = 1.0 );
+ VShadowDecorator( const VShadowDecorator& obj );
+
+ virtual ~VShadowDecorator();
+
+ virtual void draw( VPainter* /*painter*/, const KoRect* /*rect*/ = 0L ) const;
+
+ virtual const KoRect& boundingBox() const { return m_object->boundingBox(); }
+ VStroke* stroke() const { return m_object->stroke(); }
+ virtual void setStroke( const VStroke& stroke );
+ VFill* fill() const { return m_object->fill(); }
+ virtual void setFill( const VFill& fill );
+
+ virtual void accept( VVisitor& /*visitor*/ );
+
+ virtual void save( QDomElement& ) const;
+ virtual void load( const QDomElement& ) {}
+
+ virtual VObject* clone() const;
+
+ VState state() const { return m_state; }
+ virtual void setState( const VState state );
+
+ void setShadow( int distance = 2, int angle = 0, float opacity = 1.0 );
+protected:
+ VObject *m_object;
+ int m_distance;
+ int m_angle;
+ float m_opacity;
+};
+
+#endif
+