summaryrefslogtreecommitdiffstats
path: root/karbon/tools/vselectnodestool.cc
blob: 2a3c38bba200d18c05f17be9e18193c7254d2533 (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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/* 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 <math.h>

#include <tqcursor.h>
#include <tqlabel.h>

#include <tdelocale.h>
#include <KoPoint.h>
#include <KoRect.h>

#include <karbon_part.h>
#include <karbon_view.h>

#include <render/vpainter.h>
#include <render/vpainterfactory.h>
#include <visitors/vselectnodes.h>
#include <commands/vtransformcmd.h>
#include <visitors/vdrawselection.h>
#include <core/vselection.h>
#include <core/vcursor.h>
#include "vselectnodestool.h"
#include <vtransformnodes.h>
#include <commands/vdeletenodescmd.h>
#include <widgets/vcanvas.h>

#include <kdebug.h>

VSelectNodesTool::VSelectNodesTool( KarbonView* view )
	: VTool( view, "tool_select_nodes" ), m_state( normal ), m_select( true )
{
	registerTool( this );
}

VSelectNodesTool::~VSelectNodesTool()
{
}

void
VSelectNodesTool::activate()
{
	if( view() )
	{
		view()->setCursor( VCursor::needleArrow() );
		view()->part()->document().selection()->showHandle( false );
		view()->part()->document().selection()->setSelectObjects( false );
		// deselect all nodes
		view()->part()->document().selection()->selectNodes( false );
		view()->repaintAll( view()->part()->document().selection()->boundingBox() );
	}
	VTool::activate();
}

TQString
VSelectNodesTool::statusText()
{
	if( m_state == normal )
		return i18n( "Editing Nodes" );
	else
		return TQString( "" );
}

void
VSelectNodesTool::draw()
{
	VPainter *painter = view()->painterFactory()->editpainter();
	painter->setZoomFactor( view()->zoom() );
	painter->setRasterOp( TQt::NotROP );

	if( m_state == dragging )
	{
		painter->setPen( TQt::DotLine );
		painter->newPath();
		painter->moveTo( KoPoint( m_first.x(), m_first.y() ) );
		painter->lineTo( KoPoint( m_current.x(), m_first.y() ) );
		painter->lineTo( KoPoint( m_current.x(), m_current.y() ) );
		painter->lineTo( KoPoint( m_first.x(), m_current.y() ) );
		painter->lineTo( KoPoint( m_first.x(), m_first.y() ) );
		painter->strokePath();
	}
	else
	{
		VDrawSelection op( m_objects, painter, true, VSelection::handleSize() );
		VObjectListIterator itr = m_objects;
        for( ; itr.current(); ++itr )
			op.visit( *( itr.current() ) );
	}
}

void
VSelectNodesTool::setCursor() const
{
	if( m_state >= moving ) 
	{
		view()->setCursor( VCursor::needleMoveArrow() );
		return;
	}

	KoRect selrect = calcSelRect( last() );

	TQPtrList<VSegment> segments = view()->part()->document().selection()->getSegments( selrect );
	if( segments.count() > 0  )
	{
		VSegment* seg = segments.at( 0 );
		for( int i = 0; i < seg->degree(); ++i )
			if( seg->pointIsSelected( i ) && selrect.contains( seg->point( i ) ) )
			{
				view()->setCursor( VCursor::needleMoveArrow() );
				break;
			}
	}
	else
		view()->setCursor( VCursor::needleArrow() );
}

void
VSelectNodesTool::mouseButtonPress()
{
	m_first = m_current = first();

	m_state = normal;
	m_select = true;

	recalc();

	view()->part()->document().selection()->setState( VObject::edit );
	view()->repaintAll( view()->part()->document().selection()->boundingBox() );
	view()->part()->document().selection()->setState( VObject::selected );

	VSelection* selection = view()->part()->document().selection();

	KoRect selrect = calcSelRect( m_current );

	// get segments with control points inside selection rect
	TQPtrList<VSegment> segments = selection->getSegments( selrect );
	if( segments.count() > 0 )
	{
		VSegment *seg = segments.at( 0 );
		VSegment* prev = seg->prev();
		VSegment* next = seg->next();

		// allow moving bezier points only if one of the bezier points is within the selection rect
		// and no neighboring knot is selected
		if( segments.count() == 1 && ! selrect.contains( seg->knot() ) && ! seg->knotIsSelected() 
		&& ( prev && ! prev->knotIsSelected() ) )
		{
			if( selrect.contains( seg->point( 1 ) ) )
			{
				m_state = movingbezier1;
				if( next )
					next->selectPoint( 0, false );
			}
			else if( selrect.contains( seg->point( 0 ) ) )
			{
				m_state = movingbezier2;
				if( prev ) 
					prev->selectPoint( 1, false );
			}
		}
		else
		{
			for( VSegment *seg = segments.first(); seg; seg = segments.next() )
			{
				for( int i = 0; i < seg->degree(); ++i )
				{
					if( seg->pointIsSelected( i ) && selrect.contains( seg->point( i ) ) )
					{
						m_state = moving;
						break;
					}
				}
				if( m_state == moving )
					break;
			}
		}

		double minDist = -1.0;
		// use the nearest control point of all the segments as starting point
		for( VSegment *seg = segments.first(); seg; seg = segments.next() )
		{
			for( int i = 0; i < seg->degree(); ++i )
			{
				if( selrect.contains( seg->point( i ) ) )
				{
					KoPoint vDist = seg->point( i ) - m_current;
					double dist = vDist.x()*vDist.x() + vDist.y()*vDist.y();
					if( minDist < 0.0 || dist < minDist )
					{
						m_first = seg->point( i );
						minDist = dist;
					}
				}
			}
		}
		recalc();
	}
	else
		m_state = dragging;

	draw();
}

void 
VSelectNodesTool::rightMouseButtonPress()
{
	m_first = m_current = first();

	m_state = normal;
	m_select = false;

	recalc();

	view()->part()->document().selection()->setState( VObject::edit );
	view()->repaintAll( view()->part()->document().selection()->boundingBox() );
	view()->part()->document().selection()->setState( VObject::selected );

	draw();
}

bool
VSelectNodesTool::keyReleased( TQt::Key key )
{
	VSelection* selection = view()->part()->document().selection();

	switch( key )
	{
		// increase/decrease the handle size
		case TQt::Key_I:
		{
			uint handleSize = selection->handleSize();
			if( shiftPressed() ) 
				selection->setHandleSize( ++handleSize );
			else if( handleSize > 1 )
				selection->setHandleSize( --handleSize );
		}
		break;
		case TQt::Key_Delete:
			if( selection->objects().count() > 0 )
				view()->part()->addCommand( new VDeleteNodeCmd( &view()->part()->document() ), true );
		break;
		default: return false;
	}

	if( view() )
		view()->repaintAll( selection->boundingBox() );

	return true;
}

void
VSelectNodesTool::mouseButtonRelease()
{
	// erase old object:
	draw();

	VSelection* selection = view()->part()->document().selection();

	KoRect selrect = calcSelRect( last() );

	if( ctrlPressed() )
		selection->append( selrect.normalize(), false, false );
	else
		selection->append( selrect.normalize(), false, true );

	view()->selectionChanged();
	view()->part()->repaintAllViews();
	m_state = normal;
}

void 
VSelectNodesTool::rightMouseButtonRelease()
{
	// erase old object:
	draw();

	VSelection* selection = view()->part()->document().selection();

	KoRect selrect = calcSelRect( last() );

	selection->take( selrect.normalize(), false, false );

	view()->selectionChanged();
	view()->part()->repaintAllViews();
	m_state = normal;
}

void
VSelectNodesTool::mouseDrag()
{
	draw();

	recalc();

	draw();
}

void
VSelectNodesTool::mouseDragRelease()
{
	if( m_state >= moving )
	{
		view()->part()->document().selection()->setState( VObject::selected );
		VCommand *cmd;
		TQPtrList<VSegment> segments;
		KoPoint _last = view()->canvasWidget()->snapToGrid( last() );
		if( m_state == movingbezier1 || m_state == movingbezier2 )
		{
			KoRect selrect = calcSelRect( m_first );
			segments = view()->part()->document().selection()->getSegments( selrect );
			cmd = new VTranslateBezierCmd( &view()->part()->document(), segments.at( 0 ),
					tqRound( ( _last.x() - m_first.x() ) ),
					tqRound( ( _last.y() - m_first.y() ) ),
					m_state == movingbezier2 );
		}
		else
		{
			cmd = new VTranslatePointCmd(
					&view()->part()->document(),
					tqRound( ( _last.x() - m_first.x() ) ),
					tqRound( ( _last.y() - m_first.y() ) ) );
		}
		view()->part()->addCommand( cmd, true );
		m_state = normal;
	}
	else
	{
		KoPoint fp = m_first;
		KoPoint lp = last();

		if ( (fabs(lp.x() - fp.x()) + fabs(lp.y()-fp.y())) < 3.0 )
		{
			// AK - should take the middle point here
			fp = last() - KoPoint(8.0, 8.0);
			lp = last() + KoPoint(8.0, 8.0);
		}

		// erase old object:
		draw();

		if( m_select )
		{
			view()->part()->document().selection()->append();	// select all
			view()->part()->document().selection()->append(
				KoRect( fp.x(), fp.y(), lp.x() - fp.x(), lp.y() - fp.y() ).normalize(),
				false, true );
		}
		else
		{
			view()->part()->document().selection()->take(
				KoRect( fp.x(), fp.y(), lp.x() - fp.x(), lp.y() - fp.y() ).normalize(),
				false, false );
		}
		view()->selectionChanged();
		view()->part()->repaintAllViews();
		m_state = normal;
	}
}

void
VSelectNodesTool::cancel()
{
	// Erase old object:
	if ( isDragging() )
	{
		draw();
		m_state = normal;
		view()->repaintAll( view()->part()->document().selection()->boundingBox() );
	}
}

void
VSelectNodesTool::recalc()
{
	if( m_state == dragging )
	{
		m_current = last();
	}
	else if( m_state == moving || m_state == movingbezier1 || m_state == movingbezier2 )
	{
		KoPoint _last = view()->canvasWidget()->snapToGrid( last() );
		double distx = _last.x() - m_first.x();
		double disty = _last.y() - m_first.y();
		// move operation
		TQWMatrix mat;
		mat.translate( distx, disty );

		// Copy selected objects and transform:
		m_objects.clear();
		VObject* copy;

		VTransformNodes op( mat );

		VObjectListIterator itr = view()->part()->document().selection()->objects();
		for ( ; itr.current() ; ++itr )
		{
			if( itr.current()->state() != VObject::deleted )
			{
				copy = itr.current()->clone();
				copy->setState( VObject::edit );
				op.visit( *copy );
				m_objects.append( copy );
			}
		}
	}
}

void
VSelectNodesTool::setup( TDEActionCollection *collection )
{
	m_action = static_cast<TDERadioAction *>(collection -> action( name() ) );

	if( m_action == 0 )
	{
		m_action = new TDERadioAction( i18n( "Select Nodes Tool" ), "14_selectnodes", TQt::SHIFT+TQt::Key_H, this, TQT_SLOT( activate() ), collection, name() );
		m_action->setToolTip( i18n( "Select Nodes" ) );
		m_action->setExclusiveGroup( "select" );
		//m_ownAction = true;
	}
}

KoRect 
VSelectNodesTool::calcSelRect( const KoPoint &pos ) const
{
	double tolerance = view()->part()->document().selection()->handleSize() / view()->zoom();
	return KoRect( pos.x() - tolerance, pos.y() - tolerance, 2 * tolerance + 1.0, 2 * tolerance + 1.0 );
}