insertknotsplugin.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
21#include "insertknotsplugin.h"
22#include <karbon_view.h>
23#include <karbon_part.h>
24#include <core/vpath.h>
25#include <core/vsegment.h>
26#include <kgenericfactory.h>
27#include <kdebug.h>
28#include <tqgroupbox.h>
29#include <tqlabel.h>
30
31#include <knuminput.h>
32
33typedef KGenericFactory<InsertKnotsPlugin, KarbonView> InsertKnotsPluginFactory;
34K_EXPORT_COMPONENT_FACTORY( karbon_insertknotsplugin, InsertKnotsPluginFactory( "karboninsertknotsplugin" ) )
35
36InsertKnotsPlugin::InsertKnotsPlugin( KarbonView *parent, const char* name, const TQStringList & ) : Plugin( parent, name )
37{
38 new TDEAction(
39 i18n( "&Insert Knots..." ), "14_insertknots", 0, this,
40 TQ_SLOT( slotInsertKnots() ), actionCollection(), "path_insert_knots" );
41
42 m_insertKnotsDlg = new VInsertKnotsDlg();
43}
44
45void
46InsertKnotsPlugin::slotInsertKnots()
47{
48 KarbonPart *part = ((KarbonView *)parent())->part();
49 if( part && m_insertKnotsDlg->exec() )
50 part->addCommand( new VInsertKnotsCmd( &part->document(), m_insertKnotsDlg->knots() ), true );
51}
52
53VInsertKnotsDlg::VInsertKnotsDlg( TQWidget* parent, const char* name )
54 : KDialogBase( parent, name, true, i18n( "Insert Knots" ), Ok | Cancel )
55{
56 // add input fields:
57 TQGroupBox* group = new TQGroupBox( 2, TQt::Horizontal, i18n( "Properties" ), this );
58
59 new TQLabel( i18n( "Knots:" ), group );
60 m_knots = new KIntSpinBox( group );
61 m_knots->setMinValue( 1 );
62 group->setMinimumWidth( 300 );
63
64 // signals and slots:
65 connect( this, TQ_SIGNAL( okClicked() ), this, TQ_SLOT( accept() ) );
66 connect( this, TQ_SIGNAL( cancelClicked() ), this, TQ_SLOT( reject() ) );
67
68 setMainWidget( group );
69 setFixedSize( baseSize() );
70}
71
72uint
73VInsertKnotsDlg::knots() const
74{
75 return m_knots->value();
76}
77
78void
79VInsertKnotsDlg::setKnots( uint value )
80{
81 m_knots->setValue( value );
82}
83
84
85VInsertKnotsCmd::VInsertKnotsCmd( VDocument* doc, uint knots )
86 : VReplacingCmd( doc, i18n( "Insert Knots" ) )
87{
88 m_knots = knots > 0 ? knots : 1;
89}
90
91void
92VInsertKnotsCmd::visitVSubpath( VSubpath& path )
93{
94 path.first();
95
96 double length;
97
98 // Ommit first segment.
99 while( path.next() )
100 {
101 length = path.current()->length();
102
103 for( uint i = m_knots; i > 0; --i )
104 {
105 path.insert(
106 path.current()->splitAt(
107 path.current()->lengthParam( length / ( m_knots + 1.0 ) ) ) );
108
109 path.next();
110 }
111
112 if( !success() )
113 setSuccess();
114 }
115}
116
117#include "insertknotsplugin.moc"
118
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:
All non-visual, static doc info is in here.
Definition vdocument.h:53
VReplacingCmd is a generic command.
double length(double t=1.0) const
Calculates the arclength from p0 to the point parametrized by 0 <= t <= 1.
Definition vsegment.cpp:291
double lengthParam(double len) const
Calculates the parameter of a point located at arclength len.
Definition vsegment.cpp:396
VSegment * splitAt(double t)
Splits the segment at parameter 0 <= t <= 1.
Definition vsegment.cpp:857
VSubpath provides a sophisticated list of VSegment.
Definition vpath.h:43