summaryrefslogtreecommitdiffstats
path: root/karbon/plugins/whirlpinch/whirlpinchplugin.cpp
blob: 13b2afe59aa73661730f02f879275951bc59d988 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* 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 <core/vselection.h>
#include "whirlpinchplugin.h"
#include <karbon_view.h>
#include <karbon_part.h>
#include <kgenericfactory.h>
#include <core/vdocument.h>
#include <core/vcomposite.h>
#include <core/vpath.h>
#include <core/vsegment.h>
#include <core/vglobal.h>

#include <kdebug.h>

#include <tqgroupbox.h>
#include <tqlabel.h>

#include <knuminput.h>

typedef KGenericFactory<WhirlPinchPlugin, KarbonView> WhirlPinchPluginFactory;
K_EXPORT_COMPONENT_FACTORY( karbon_whirlpinchplugin, WhirlPinchPluginFactory( "karbonwhirlpinchplugin" ) )

WhirlPinchPlugin::WhirlPinchPlugin( KarbonView *parent, const char* name, const TQStringList & ) : Plugin( parent, name )
{
	new TDEAction(
		i18n( "&Whirl/Pinch..." ), "14_whirl", 0, this,
		TQ_SLOT( slotWhirlPinch() ), actionCollection(), "path_whirlpinch" );

	m_whirlPinchDlg = new VWhirlPinchDlg();
	m_whirlPinchDlg->setAngle( 20.0 );
	m_whirlPinchDlg->setPinch( 0.0 );
	m_whirlPinchDlg->setRadius( 100.0 );
}

void
WhirlPinchPlugin::slotWhirlPinch()
{
	KarbonPart *part = ((KarbonView *)parent())->part();
	if( part && m_whirlPinchDlg->exec() )
		part->addCommand( new VWhirlPinchCmd( &part->document(), m_whirlPinchDlg->angle(), m_whirlPinchDlg->pinch(), m_whirlPinchDlg->radius() ), true );
}

VWhirlPinchDlg::VWhirlPinchDlg( TQWidget* parent, const char* name )
	: KDialogBase( parent, name, true, i18n( "Whirl Pinch" ), Ok | Cancel )
{
	// add input fields:
	TQGroupBox* group = new TQGroupBox( 2, TQt::Horizontal, i18n( "Properties" ), this );

	new TQLabel( i18n( "Angle:" ), group );
	m_angle = new KDoubleNumInput( group );
	new TQLabel( i18n( "Pinch:" ), group );
	m_pinch = new KDoubleNumInput( group );
	new TQLabel( i18n( "Radius:" ), group );
	m_radius = new KDoubleNumInput( group );
	group->setMinimumWidth( 300 );

	// signals and slots:
	connect( this, TQ_SIGNAL( okClicked() ), this, TQ_SLOT( accept() ) );
	connect( this, TQ_SIGNAL( cancelClicked() ), this, TQ_SLOT( reject() ) );

	setMainWidget( group );
	setFixedSize( baseSize() );
}

double
VWhirlPinchDlg::angle() const
{
	return m_angle->value();
}

double
VWhirlPinchDlg::pinch() const
{
	return m_pinch->value();
}

double
VWhirlPinchDlg::radius() const
{
	return m_radius->value();
}

void
VWhirlPinchDlg::setAngle( double value )
{
	m_angle->setValue( value);
}

void
VWhirlPinchDlg::setPinch( double value )
{
	m_pinch->setValue(value);
}

void
VWhirlPinchDlg::setRadius( double value )
{
	m_radius->setValue( value);
}

VWhirlPinchCmd::VWhirlPinchCmd( VDocument* doc,
	double angle, double pinch, double radius )
	: VReplacingCmd( doc, i18n( "Whirl Pinch" ) )
{
	m_angle = angle;
	m_pinch = pinch;
	m_radius = radius;
	m_center = document()->selection()->boundingBox().center();
}

VWhirlPinchCmd::~VWhirlPinchCmd()
{
}

void
VWhirlPinchCmd::visitVPath( VPath& composite )
{
	// first subdivide:
//	VInsertKnots insertKnots( 2 );
//	insertKnots.visit( composite );

	VVisitor::visitVPath( composite );
}

void
VWhirlPinchCmd::visitVSubpath( VSubpath& path )
{
	TQWMatrix m;
	KoPoint delta;
	double dist;

	path.first();

	VSegment *curr = path.current();

	while( curr )
	{
// TODO: selfmade this function since it's gone:
//		path.current()->convertToCurve();

		// Apply effect to each segment node.
		for( int i = 0; i < curr->degree(); ++i )
		{
			// calculate distance from whirl center to actual point
			delta = curr->point( i ) - m_center;
			dist = sqrt( delta.x() * delta.x() + delta.y() * delta.y() );
	
			// check if point is inside the whirl radius
			if( dist < m_radius )
			{
				m.reset();
	
				// normalize distance to whirl radius
				dist /= m_radius;
	
				double scale = pow( sin( VGlobal::pi_2 * dist ), -m_pinch );
				// pinch:
				m.translate( m_center.x(), m_center.y() );
				m.scale( scale, scale );
	
				// whirl:
				m.rotate( m_angle * ( 1.0 - dist ) * ( 1.0 - dist ) );
				m.translate( -m_center.x(), -m_center.y() );
	
				path.current()->setPoint( i, curr->point( i ).transform( m ) );
			}

		}

		if( !success() )
			setSuccess();

		curr = path.next();
	}

	// Invalidate bounding box once.
	path.invalidateBoundingBox();
}

#include "whirlpinchplugin.moc"