summaryrefslogtreecommitdiffstats
path: root/karbon/plugins/roundcorners/roundcornersplugin.cpp
blob: 1841c5eb1f37341300aa93dcf6337a550d38be65 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/* 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 "roundcornersplugin.h"
#include <karbon_view.h>
#include <karbon_part.h>
#include <core/vpath.h>
#include <core/vsegment.h>
#include <kgenericfactory.h>
#include <kdebug.h>
#include <tqgroupbox.h>
#include <tqlabel.h>

#include <knuminput.h>

typedef KGenericFactory<VRoundCornersPlugin, KarbonView> VRoundCornersPluginFactory;
K_EXPORT_COMPONENT_FACTORY( karbon_roundcornersplugin, VRoundCornersPluginFactory( "karbonroundcornersplugin" ) )

VRoundCornersPlugin::VRoundCornersPlugin( KarbonView *parent, const char* name, const TQStringList & ) : Plugin( TQT_TQOBJECT(parent), name )
{
	new TDEAction(
		i18n( "&Round Corners..." ), "14_roundcorners", 0, this,
		TQT_SLOT( slotRoundCorners() ), actionCollection(), "path_round_corners" );

	m_roundCornersDlg = new VRoundCornersDlg();
	m_roundCornersDlg->setRadius( 10.0 );
}

VRoundCornersPlugin::~VRoundCornersPlugin()
{
}

void
VRoundCornersPlugin::slotRoundCorners()
{
	KarbonPart *part = ((KarbonView *)parent())->part();
	if( part && m_roundCornersDlg->exec() )
		part->addCommand( new VRoundCornersCmd( &part->document(), m_roundCornersDlg->radius() ), true );
}


VRoundCornersDlg::VRoundCornersDlg( TQWidget* parent, const char* name )
	: KDialogBase( parent, name, true,  i18n( "Polygonize" ), Ok | Cancel  )
{
	// add input:
	TQGroupBox* group = new TQGroupBox( 2, Qt::Horizontal, i18n( "Properties" ), this );

	new TQLabel( i18n( "Round corners:" ), group );
	m_radius = new KDoubleNumInput( group );
	group->setMinimumWidth( 300 );

	// signals and slots:
	connect( this, TQT_SIGNAL( okClicked() ), this, TQT_SLOT( accept() ) );
	connect( this, TQT_SIGNAL( cancelClicked() ), this, TQT_SLOT( reject() ) );

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

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

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



VRoundCornersCmd::VRoundCornersCmd( VDocument* doc, double radius )
	: VReplacingCmd( doc, i18n( "Round Corners" ) )
{
	// Set members.
	m_radius = radius > 0.0 ? radius : 1.0;
}

void
VRoundCornersCmd::visitVSubpath( VSubpath& path )
{
	// Optimize and avoid a crash.
	if( path.isEmpty() )
		return;

	// Note: we modiy segments from path. that doesn't hurt, since we
	// replace "path" with the temporary path "newPath" afterwards.

	VSubpath newPath( 0L );

	path.first();
	// Skip "begin".
	path.next();

	/* This algorithm is worked out by <kudling AT kde DOT org> to produce similar results as
	 * the "round corners" algorithms found in other applications. Neither code nor
	 * algorithms from any 3rd party is used though.
	 *
	 * We want to replace all corners with round corners having "radius" m_radius.
	 * The algorithm doesn't really produce circular arcs, but that's ok since
	 * the algorithm achieves nice looking results and is generic enough to be applied
	 * to all kind of paths.
	 * Note also, that this algorithm doesn't touch smooth joins (in the sense of
	 * VSegment::isSmooth() ).
	 *
	 * We'll manipulate the input path for bookkeeping purposes and construct a new
	 * temporary path in parallel. We finally replace the input path with the new path.
	 *
	 *
	 * Without restricting generality, let's assume the input path is closed and
	 * contains segments which build a rectangle.
	 *
	 *           2
	 *    O------------O
	 *    |            |        Numbers reflect the segments' order
	 *   3|            |1       in the path. We neglect the "begin"
	 *    |            |        segment here.
	 *    O------------O
	 *           0
	 *
	 * There are three unique steps to process. The second step is processed
	 * many times in a loop.
	 *
	 * 1) Begin
	 *    -----
	 *    Split the first segment of the input path (called "path[0]" here)
	 *    at parameter t
	 *
	 *        t = path[0]->param( m_radius )
	 *
	 *    and move newPath to this new knot. If current segment is too small
	 *    (smaller than 2 * m_radius), we always set t = 0.5 here and in the further
	 *    steps as well.
	 *
	 *    path:                 new path:
	 *
	 *           2
	 *    O------------O
	 *    |            |
	 *  3 |            | 1                    The current segment is marked with "#"s.
	 *    |            |
	 *    O##O#########O        ...O
	 *           0                     0
	 *
	 * 2) Loop
	 *    ----
	 *    The loop step is iterated over all segments. After each appliance the index n
	 *    is incremented and the loop step is reapplied until no untouched segment is left.
	 *
	 *    Split the current segment path[n] of the input path at parameter t
	 *
	 *        t = path[n]->param( path[n]->length() - m_radius )
	 *
	 *    and add the first subsegment of the curent segment to newPath.
	 *
	 *    path:                 new path:
	 *
	 *           2
	 *    O------------O
	 *    |            |
	 *  3 |            | 1
	 *    |            |
	 *    O--O######O##O           O------O...
	 *           0                     0
	 *
	 *    Now make the second next segment (the original path[1] segment in our example)
	 *    the current one. Split it at parameter t
	 *
	 *        t = path[n]->param( m_radius )
	 *
	 *    path:                 new path:
	 *
	 *           2
	 *    O------------O
	 *    |            #
	 *  3 |            O 1
	 *    |            #
	 *    O--O------O--O           O------O...
	 *           0                     0
	 *
	 *    Make the first subsegment of the current segment the current one.
	 *
	 *    path:                 new path:
	 *
	 *           2
	 *    O------------O
	 *    |            |
	 *  3 |            O 1                   O
	 *    |            #                    /.1
	 *    O--O------O--O           O------O...
	 *           0                     0
	 *
	 * 3) End
	 *    ---
	 *
	 *    path:                 new path:
	 *
	 *           2                     4
	 *    O--O------O--O        5 .O------O. 3
	 *    |            |         /          \
	 *  3 O            O 1    6 O            O 2
	 *    |            |      7 .\          /
	 *    O--O------O--O        ...O------O. 1
	 *           0                     0
	 */

	double length;
	double param;

	// "Begin" step.
	// =============

	// Convert flat beziers to lines.
	if( path.current()->isFlat() )
		path.current()->setDegree( 1 );

	if( path.getLast()->isFlat() )
		path.getLast()->setDegree( 1 );

	if(
		path.isClosed() &&
		// Don't touch smooth joins.
		!path.getLast()->isSmooth( *path.current() ) )
	{
		length = path.current()->length();

		param = length > 2 * m_radius
			? path.current()->lengthParam( m_radius )
			: 0.5;


		path.insert(
			path.current()->splitAt( param ) );

		newPath.moveTo(
			path.current()->knot() );

		path.next();


		if( !success() )
			setSuccess();
	}
	else
	{
		newPath.moveTo(
			path.current()->prev()->knot() );
	}


	// "Loop" step.
	// ============

	while(
		path.current() &&
		path.current()->next() )
	{
		// Convert flat beziers to lines.
		if( path.current()->isFlat() )
			path.current()->setDegree( 1 );

		if( path.current()->next()->isFlat() )
			path.current()->next()->setDegree( 1 );


		// Don't touch smooth joins.
		if( path.current()->isSmooth() )
		{
			newPath.append( path.current()->clone() );
			path.next();
			continue;
		}


		// Split the current segment at param( m_radius ) counting
		// from the end.
		length = path.current()->length();

		// If the current segment is too short to be split, just don't split it
		// because it was split already a t=0.5 during the former step.
		if( length > m_radius )
		{
			param = path.current()->lengthParam( length - m_radius );

			path.insert(
				path.current()->splitAt( param ) );
			newPath.append(
				path.current()->clone() );

			path.next();
		}


		// Jump to the next untouched segment.
		path.next();


		// Split the current segment at param( m_radius ).
		length = path.current()->length();

		param = length > 2 * m_radius
			? path.current()->lengthParam( m_radius )
			: 0.5;

		path.insert(
			path.current()->splitAt( param ) );


		// Round corner.
		newPath.curveTo(
			path.current()->prev()->pointAt( 0.5 ),
			path.current()->pointAt( 0.5 ),
			path.current()->knot() );


		if( !success() )
			setSuccess();

		path.next();
	}


	// "End" step.
	// ===========

	if( path.isClosed() )
	{
		// Convert flat beziers to lines.
		if( path.current()->isFlat() )
			path.current()->setDegree( 1 );

		if( path.getFirst()->next()->isFlat() )
			path.getFirst()->next()->setDegree( 1 );

		// Don't touch smooth joins.
		if( !path.current()->isSmooth( *path.getFirst()->next() ) )
		{
			length = path.current()->length();

			// If the current segment is too short to be split, just don't split it
			// because it was split already at t=0.5 during the former step.
			if( length > m_radius )
			{
				param = path.current()->lengthParam( length - m_radius );

				path.insert(
					path.current()->splitAt( param ) );
				newPath.append(
					path.current()->clone() );

				path.next();
			}


			path.first();
			path.next();

			// Round corner.
			newPath.curveTo(
				path.getLast()->pointAt( 0.5 ),
				path.current()->pointAt( 0.5 ),
				path.current()->knot() );


			if( !success() )
				setSuccess();
		}
		else
			newPath.append( path.current()->clone() );

		newPath.close();
	}
	else
		newPath.append( path.current()->clone() );


	path = newPath;

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

#include "roundcornersplugin.moc"