karbon

insertknotsplugin.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 
00021 #include "insertknotsplugin.h"
00022 #include <karbon_view.h>
00023 #include <karbon_part.h>
00024 #include <core/vpath.h>
00025 #include <core/vsegment.h>
00026 #include <kgenericfactory.h>
00027 #include <kdebug.h>
00028 #include <qgroupbox.h>
00029 #include <qlabel.h>
00030 
00031 #include <knuminput.h>
00032 
00033 typedef KGenericFactory<InsertKnotsPlugin, KarbonView> InsertKnotsPluginFactory;
00034 K_EXPORT_COMPONENT_FACTORY( karbon_insertknotsplugin, InsertKnotsPluginFactory( "karboninsertknotsplugin" ) )
00035 
00036 InsertKnotsPlugin::InsertKnotsPlugin( KarbonView *parent, const char* name, const QStringList & ) : Plugin( parent, name )
00037 {
00038     new KAction(
00039         i18n( "&Insert Knots..." ), "14_insertknots", 0, this,
00040         SLOT( slotInsertKnots() ), actionCollection(), "path_insert_knots" );
00041 
00042     m_insertKnotsDlg = new VInsertKnotsDlg();
00043 }
00044 
00045 void
00046 InsertKnotsPlugin::slotInsertKnots()
00047 {
00048     KarbonPart *part = ((KarbonView *)parent())->part();
00049     if( part && m_insertKnotsDlg->exec() )
00050         part->addCommand( new VInsertKnotsCmd( &part->document(), m_insertKnotsDlg->knots() ), true );
00051 }
00052 
00053 VInsertKnotsDlg::VInsertKnotsDlg( QWidget* parent, const char* name )
00054     : KDialogBase( parent, name, true,  i18n( "Insert Knots" ), Ok | Cancel )
00055 {
00056     // add input fields:
00057     QGroupBox* group = new QGroupBox( 2, Qt::Horizontal, i18n( "Properties" ), this );
00058 
00059     new QLabel( i18n( "Knots:" ), group );
00060     m_knots = new KIntSpinBox( group );
00061     m_knots->setMinValue( 1 );
00062     group->setMinimumWidth( 300 );
00063 
00064     // signals and slots:
00065     connect( this, SIGNAL( okClicked() ), this, SLOT( accept() ) );
00066     connect( this, SIGNAL( cancelClicked() ), this, SLOT( reject() ) );
00067 
00068     setMainWidget( group );
00069     setFixedSize( baseSize() );
00070 }
00071 
00072 uint
00073 VInsertKnotsDlg::knots() const
00074 {
00075     return m_knots->value();
00076 }
00077 
00078 void
00079 VInsertKnotsDlg::setKnots( uint value )
00080 {
00081     m_knots->setValue( value );
00082 }
00083 
00084 
00085 VInsertKnotsCmd::VInsertKnotsCmd( VDocument* doc, uint knots )
00086     : VReplacingCmd( doc, i18n( "Insert Knots" ) )
00087 {
00088     m_knots = knots > 0 ? knots : 1;
00089 }
00090 
00091 void
00092 VInsertKnotsCmd::visitVSubpath( VSubpath& path )
00093 {
00094     path.first();
00095 
00096     double length;
00097 
00098     // Ommit first segment.
00099     while( path.next() )
00100     {
00101         length = path.current()->length();
00102 
00103         for( uint i = m_knots; i > 0; --i )
00104         {
00105             path.insert(
00106                 path.current()->splitAt(
00107                     path.current()->lengthParam( length / ( m_knots + 1.0 ) ) ) );
00108 
00109             path.next();
00110         }
00111 
00112         if( !success() )
00113             setSuccess();
00114     }
00115 }
00116 
00117 #include "insertknotsplugin.moc"
00118 
KDE Home | KDE Accessibility Home | Description of Access Keys