summaryrefslogtreecommitdiffstats
path: root/karbon/tools/vselecttool.cc
blob: 19338038b6e7eb2fbf7b36e41ce95153a5a17d09 (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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/* This file is part of the KDE project
   Copyright (C) 2001, 2002, 2003 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 <stdlib.h>

#include <qcursor.h>
#include <qlabel.h>
#include <qradiobutton.h>
#include <qbuttongroup.h>

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

#include <karbon_part.h>
#include <karbon_view.h>
#include <render/vpainter.h>
#include <render/vpainterfactory.h>
#include <core/vselection.h>
#include "vselecttool.h"
#include <commands/vtransformcmd.h>
#include <visitors/vselectiondesc.h>
#include <visitors/vselectobjects.h>
#include <widgets/vcanvas.h>

VSelectOptionsWidget::VSelectOptionsWidget( KarbonPart *part )
	: KDialogBase( 0L, "", true, i18n( "Selection" ), Ok | Cancel ), m_part( part )
{
	QButtonGroup *group = new QButtonGroup( 1, Qt::Horizontal, i18n( "Selection Mode" ), this );

	new QRadioButton( i18n( "Select in current layer" ), group );
	new QRadioButton( i18n( "Select in visible layers" ), group );
	new QRadioButton( i18n( "Select in selected layers" ), group );

	group->setRadioButtonExclusive( true );
	group->setButton( part->document().selectionMode() );

	connect( group, SIGNAL( clicked( int ) ), this, SLOT( modeChange( int ) ) );

	group->setInsideMargin( 4 );
	group->setInsideSpacing( 2 );

	setMainWidget( group );
	setFixedSize( baseSize() );
} // VSelectOptionsWidget::VSelectOptionsWidget

void VSelectOptionsWidget::modeChange( int mode )
{
	m_part->document().setSelectionMode( (VDocument::VSelectionMode)mode );
} // VSelectOptionsWidget::modeChanged

VSelectTool::VSelectTool( KarbonView *view )
	: VTool( view, "tool_select" ), m_state( normal )
{
	m_lock = false;
	m_add = true;
	m_objects.setAutoDelete( true );
	m_optionsWidget = new VSelectOptionsWidget( view->part() );
	registerTool( this );
	connect( view, SIGNAL( selectionChange() ), this, SLOT( updateStatusBar() ) );
}

VSelectTool::~VSelectTool()
{
	delete m_optionsWidget;
}

void
VSelectTool::activate()
{
	VTool::activate();
	view()->setCursor( QCursor( Qt::arrowCursor ) );
	view()->part()->document().selection()->showHandle();
	view()->part()->document().selection()->setSelectObjects();
	view()->part()->document().selection()->setState( VObject::selected );
	view()->part()->document().selection()->selectNodes();
	view()->repaintAll( view()->part()->document().selection()->boundingBox() );
	updateStatusBar();
}

QString
VSelectTool::statusText()
{
	return i18n( "Select" );
}

QString VSelectTool::contextHelp()
{
	QString s = i18n( "<qt><b>Selection tool:</b><br>" );
	s += i18n( "<i>Select in current layer:</i><br>The selection is made in the layer selected in the layers docker.<br><br>" );
	s += i18n( "<i>Select in visible layers:</i><br>The selection is made in the visible layers (eye in the layers docker).<br><br>" );
	s += i18n( "<i>Select in selected layers:</i><br>The selection is made in the checked layers in the layers docker.<br><br>" );
	s += i18n( "<i>Position using arrow keys</i><br>The selection can be positioned up, down, left and right using the corresponding arrow keys." );
	return s;
} // VSelectTool::contextHelp

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

	KoRect rect = view()->part()->document().selection()->boundingBox();

	if( m_state != normal )
	{
		VObjectListIterator itr = m_objects;
		for( ; itr.current(); ++itr )
		{
			itr.current()->draw( painter, &itr.current()->boundingBox() );
		}
	}
	else if( m_state == normal )
	{
		painter->setPen( Qt::DotLine );
		painter->newPath();
		painter->moveTo( KoPoint( first().x(), first().y() ) );
		painter->lineTo( KoPoint( m_current.x(), first().y() ) );
		painter->lineTo( KoPoint( m_current.x(), m_current.y() ) );
		painter->lineTo( KoPoint( first().x(), m_current.y() ) );
		painter->lineTo( KoPoint( first().x(), first().y() ) );
		painter->strokePath();

		m_state = normal;
	}
}

void
VSelectTool::setCursor() const
{
	if( m_state != normal || !view() ) return;
	switch( view()->part()->document().selection()->handleNode( last() ) )
	{
		case node_lt:
		case node_rb:
			view()->setCursor( QCursor( Qt::SizeFDiagCursor ) );
			break;
		case node_rt:
		case node_lb:
			view()->setCursor( QCursor( Qt::SizeBDiagCursor ) );
			break;
		case node_lm:
		case node_rm:
			view()->setCursor( QCursor( Qt::SizeHorCursor ) );
			break;
		case node_mt:
		case node_mb:
			view()->setCursor( QCursor( Qt::SizeVerCursor ) );
			break;
		default:
			view()->setCursor( QCursor( Qt::arrowCursor ) );
	}
}

void
VSelectTool::mouseButtonPress()
{
	// we are adding to the selection
	m_add = true;

	m_current = first();

	m_activeNode = view()->part()->document().selection()->handleNode( first() );
	KoRect rect = view()->part()->document().selection()->boundingBox();

	if( m_activeNode != node_none )
		m_state = scaling;
	else if( rect.contains( m_current ) && m_state == normal )
		m_state = moving;

	recalc();

	// undraw selection bounding box
	view()->part()->document().selection()->setState( VObject::edit );
	view()->repaintAll( rect );
	view()->part()->document().selection()->setState( VObject::selected );

	draw();
}

void
VSelectTool::rightMouseButtonPress()
{
	// we are removing from the selection
	m_add = false;

	m_current = first();

	recalc();

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

	draw();
}

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

	recalc();

	draw();
}

void
VSelectTool::rightMouseButtonRelease()
{
	m_state = normal;
	m_add = true;

	if( ctrlPressed() )
	{
		// unselect the topmost object under the mouse cursor
		VObjectList newSelection;
		VSelectObjects selector( newSelection, first() );
		if( selector.visit( view()->part()->document() ) )
			view()->part()->document().selection()->take( *newSelection.last() );

		view()->part()->repaintAllViews( view()->part()->document().selection()->boundingBox() );
		view()->selectionChanged();

		updateStatusBar();
	}
	else if( view()->part()->document().selection()->objects().count() > 0 )
	{
		view()->showSelectionPopupMenu( QCursor::pos() );
	}
}

void
VSelectTool::mouseButtonRelease()
{
	m_state = normal;
	m_add = true;

	// selection of next underlying object
	if( shiftPressed() ) 
	{
		VObjectList newSelection;
		VObjectList oldSelection = view()->part()->document().selection()->objects();

		// clear selection if not in multi-slection-mode
		if( ! ctrlPressed() )
			view()->part()->document().selection()->clear();

		// get a list of all object under the mouse cursor
		VSelectObjects selector( newSelection, first(), true, true );
		if( selector.visit( view()->part()->document() ) )
		{
			// determine the last selected object of the object stack
			VObject *lastMatched = 0L;
			VObjectListIterator it( newSelection );
			for( ; it.current(); ++it )
			{
				if( oldSelection.contains( it.current() ) )
					lastMatched = it.current();
			}
			
			// select the next underlying object or the first if:
			// - none is selected
			// - the stack's bottom object was the last selected object
			if( lastMatched && lastMatched != newSelection.first() )
				view()->part()->document().selection()->append( newSelection.at( newSelection.find( lastMatched )-1 ) );
			else
				view()->part()->document().selection()->append( newSelection.last() );
		}
	}
	else	
	{
		// clear selection if not in multi-slection-mode
		if( ! ctrlPressed() )
			view()->part()->document().selection()->clear();

		// append the topmost object under the mouse cursor to the selection
		VObjectList newSelection;
		VSelectObjects selector( newSelection, first() );
		if( selector.visit( view()->part()->document() ) )
			view()->part()->document().selection()->append( newSelection.last() );
	}

	view()->part()->repaintAllViews( view()->part()->document().selection()->boundingBox() );
	view()->selectionChanged();

	updateStatusBar();
}

void
VSelectTool::mouseDragRelease()
{
	if( m_state == normal )
	{
		// Y mirroring
		KoPoint fp = first();
		KoPoint lp = last();
		if( ! ctrlPressed() )
			view()->part()->document().selection()->clear();
		
		KoRect selRect = KoRect( fp.x(), fp.y(), lp.x() - fp.x(), lp.y() - fp.y() ).normalize();
		if( m_add )
			view()->part()->document().selection()->append( selRect );
		else
			view()->part()->document().selection()->take( selRect );
		view()->part()->repaintAllViews( selRect );
	}
	else if( m_state == moving )
	{
		m_state = normal;
		recalc();
		if( m_lock )
			view()->part()->addCommand(
				 new VTranslateCmd(
					&view()->part()->document(),
					abs( int( m_distx ) ) >= abs( int( m_disty ) ) ? qRound( m_distx ) : 0,
					abs( int( m_distx ) ) <= abs( int( m_disty ) ) ? qRound( m_disty ) : 0, altPressed() ),
				true );
		else
			view()->part()->addCommand(
				new VTranslateCmd( &view()->part()->document(), qRound( m_distx ), qRound( m_disty ), altPressed() ),
				true );
	}
	else if( m_state == scaling )
	{
		m_state = normal;
		view()->part()->addCommand(
			new VScaleCmd( &view()->part()->document(), m_sp, m_s1, m_s2, altPressed() ),
			true );
		m_s1 = m_s2 = 1;
	}

	view()->selectionChanged();
	m_lock = false;
	updateStatusBar();
}

void
VSelectTool::arrowKeyReleased( Qt::Key key )
{
	int dx = 0;
	int dy = 0;
	switch( key )
	{
		case Qt::Key_Up: dy = 10; break;
		case Qt::Key_Down: dy = -10; break;
		case Qt::Key_Right: dx = 10; break;
		case Qt::Key_Left: dx = -10; break;
		default: return;
	}
	m_state = normal;
	view()->part()->addCommand(
		new VTranslateCmd(
			&view()->part()->document(),
			dx, dy ),
		true );
	view()->selectionChanged();
	updateStatusBar();
}

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

	switch( key )
	{
		// increase/decrease the handle size
		case Qt::Key_I:
		{
			uint handleSize = selection->handleSize();
			if( shiftPressed() ) 
				selection->setHandleSize( ++handleSize );
			else if( handleSize > 1 )
				selection->setHandleSize( --handleSize );
		}
		break;
		default: return false;
	}

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

	return true;
}

void
VSelectTool::updateStatusBar() const
{
	if( ! view() ) 
		return;

	if( ! view()->part() )
		return;

	int objcount = view()->part()->document().selection()->objects().count();
	if( objcount > 0 )
	{
		KoRect rect = view()->part()->document().selection()->boundingBox();

		double x = KoUnit::toUserValue( rect.x(), view()->part()->unit() );
		double y = KoUnit::toUserValue( rect.y(), view()->part()->unit() );
		double r = KoUnit::toUserValue( rect.right(), view()->part()->unit() );
		double b = KoUnit::toUserValue( rect.bottom(), view()->part()->unit() );

		// print bottom-left (%1,%2), top-right (%3,%4) corner of selection bounding box and document unit (%5)
		QString selectMessage = i18n( "[(left,bottom), (right,top)] (actual unit)", "Selection [(%1, %2), (%3, %4)] (%5)").arg( x, 0, 'f', 1 ).arg( y, 0, 'f', 1 ).arg( r, 0, 'f', 1 ).arg( b, 0, 'f', 1 ).arg( view()->part()->unitName() );

		VSelectionDescription selectionDesc;
		selectionDesc.visit( *view()->part()->document().selection() );
		selectMessage += QString( "(%1)" ).arg( selectionDesc.description() );

		view()->statusMessage()->setText( selectMessage );
	}
	else
		view()->statusMessage()->setText( i18n( "No selection" ) );
}

void
VSelectTool::mouseDragCtrlPressed()
{
	m_lock = true;
}

void
VSelectTool::mouseDragCtrlReleased()
{
	m_lock = false;
}

void
VSelectTool::mouseDragShiftPressed()
{
	draw();

	recalc();

	draw();
}

void
VSelectTool::mouseDragShiftReleased()
{
	draw();

	recalc();

	draw();
}

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

void
VSelectTool::recalc()
{
	if( m_state == normal )
	{
		m_current = last();
	}
	else
	{
		VTransformCmd* cmd;
		KoPoint _first = view()->canvasWidget()->snapToGrid( first() );
		KoPoint _last = view()->canvasWidget()->snapToGrid( last() );
		KoRect rect = view()->part()->document().selection()->boundingBox();

		if( m_state == moving )
		{
			KoPoint p( rect.x() + last().x() - first().x(), rect.bottom() + last().y() - first().y() );
			p = view()->canvasWidget()->snapToGrid( p );
			m_distx = p.x() - rect.x();
			m_disty = p.y() - rect.bottom();
			if( m_lock )
				cmd = new VTranslateCmd( 0L, abs( int( m_distx ) ) >= abs( int( m_disty ) ) ? m_distx : 0,
											 abs( int( m_distx ) ) <= abs( int( m_disty ) ) ? m_disty : 0 );
			else
				cmd = new VTranslateCmd( 0L, m_distx, m_disty );
		}
		else
		{
			if( m_activeNode == node_lb )
			{
				m_sp = KoPoint( rect.right(), rect.bottom() );
				m_s1 = ( rect.right() - _last.x() ) / double( rect.width() );
				m_s2 = ( rect.bottom() - _last.y() ) / double( rect.height() );
			}
			else if( m_activeNode == node_mb )
			{
				m_sp = KoPoint( ( ( rect.right() + rect.left() ) / 2 ), rect.bottom() );
				m_s1 = 1;
				m_s2 = ( rect.bottom() - _last.y() ) / double( rect.height() );
			}
			else if( m_activeNode == node_rb )
			{
				m_sp = KoPoint( rect.x(), rect.bottom() );
				m_s1 = ( _last.x() - rect.x() ) / double( rect.width() );
				m_s2 = ( rect.bottom() - _last.y() ) / double( rect.height() );
			}
			else if( m_activeNode == node_rm)
			{
				m_sp = KoPoint( rect.x(), ( rect.bottom() + rect.top() )  / 2 );
				m_s1 = ( _last.x() - rect.x() ) / double( rect.width() );
				m_s2 = 1;
			}
			else if( m_activeNode == node_rt )
			{
				m_sp = KoPoint( rect.x(), rect.y() );
				m_s1 = ( _last.x() - rect.x() ) / double( rect.width() );
				m_s2 = ( _last.y() - rect.y() ) / double( rect.height() );
			}
			else if( m_activeNode == node_mt )
			{
				m_sp = KoPoint( ( ( rect.right() + rect.left() ) / 2 ), rect.y() );
				m_s1 = 1;
				m_s2 = ( _last.y() - rect.y() ) / double( rect.height() );
			}
			else if( m_activeNode == node_lt )
			{
				m_sp = KoPoint( rect.right(), rect.y() );
				m_s1 = ( rect.right() - _last.x() ) / double( rect.width() );
				m_s2 = ( _last.y() - rect.y() ) / double( rect.height() );
			}
			else if( m_activeNode == node_lm )
			{
				m_sp = KoPoint( rect.right(), ( rect.bottom() + rect.top() )  / 2 );
				m_s1 = ( rect.right() - _last.x() ) / double( rect.width() );
				m_s2 = 1;
			}

			if( shiftPressed() )
				m_s1 = m_s2 = kMax( m_s1, m_s2 );
			cmd = new VScaleCmd( 0L, m_sp, m_s1, m_s2 );
		}

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

		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 );

				cmd->visit( *copy );

				m_objects.append( copy );
			}
		}

		delete( cmd );
	}
}

bool
VSelectTool::showDialog() const
{
	return m_optionsWidget->exec() == QDialog::Accepted;
}

void 
VSelectTool::refreshUnit()
{
	updateStatusBar();
}

void
VSelectTool::setup( KActionCollection *collection )
{
       m_action = static_cast<KRadioAction *>(collection -> action( name() ) );

	if( m_action == 0 )
	{
		m_action = new KRadioAction( i18n( "Select Tool" ), "14_select", Qt::SHIFT+Qt::Key_H, this, SLOT( activate() ), collection, name() );
		m_action->setToolTip( i18n( "Select" ) );
		m_action->setExclusiveGroup( "select" );
		//m_ownAction = true;
	}
}

#include "vselecttool.moc"