1
/* This file is part of the KDE project
2
Copyright (C) 2001, The Karbon Developers
3
Copyright (C) 2002, The Karbon Developers
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public
7
License as published by the Free Software Foundation; either
8
version 2 of the License, or (at your option) any later version.
10
This library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public License
16
along with this library; see the file COPYING.LIB. If not, write to
17
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
Boston, MA 02111-1307, USA.
22
#include "vdocument.h"
23
#include "vdrawselection.h"
25
#include "vselection.h"
26
#include "vselectnodes.h"
27
#include "vselectobjects.h"
33
VSelection::VSelection( VObject* parent )
34
: VObject( parent ), m_showhandle( true )
36
m_handleRect = new KoRect[ 10 ];
37
setStroke( VStroke( VColor( Qt::black ) ) );
40
m_selectObjects = true;
43
VSelection::VSelection( const VSelection& selection )
44
: VObject( selection ), VVisitor()
46
m_handleRect = new KoRect[ 10 ];
48
VObjectListIterator itr = selection.m_objects;
49
for ( ; itr.current() ; ++itr )
50
append( itr.current() ); // Don't clone objects here.
53
m_selectObjects = selection.m_selectObjects;
56
VSelection::~VSelection()
59
delete[]( m_handleRect );
63
VSelection::clone() const
65
return new VSelection( *this );
69
VSelection::accept( VVisitor& visitor )
71
visitor.visitVSelection( *this );
75
VSelection::take( VObject& object )
77
m_objects.removeRef( &object );
78
if( object.state() >= selected )
79
object.setState( normal );
80
invalidateBoundingBox();
88
VSelectObjects op( m_objects );
89
op.visit( *static_cast<VDocument*>( parent() ) );
92
invalidateBoundingBox();
96
VSelection::append( VObject* object )
98
if( object->state() != deleted )
100
m_objects.append( object );
101
object->setState( selected );
102
invalidateBoundingBox();
107
VSelection::append( const VObjectList &objects )
109
VObjectListIterator itr = objects;
110
for( ; itr.current(); ++itr )
111
append( itr.current() );
115
VSelection::append( const KoRect& rect, bool selectObjects, bool exclusive )
117
bool success = false;
122
VSelectObjects op( m_objects, rect );
123
if( op.visit( *static_cast<VDocument*>( parent() ) ) )
131
VObjectListIterator itr( m_objects );
132
VObjectList notSelected;
134
// Try to select all that have at least one node contained in the rect
135
for ( ; itr.current(); ++itr )
137
VSelectNodes op( rect, true, exclusive );
139
if( op.visit( *itr.current() ) )
142
notSelected.append( itr.current());
144
// Remove all that were not selected from this selection
145
VObjectListIterator jtr( notSelected );
146
for ( ; jtr.current(); ++jtr )
147
take( *( jtr.current() ) );
150
invalidateBoundingBox();
158
VSelectNodes op( true );
160
VObjectListIterator itr = m_objects;
161
for( ; itr.current(); ++itr )
163
op.visit( *itr.current() );
165
//if( itr.current()->state() != deleted )
166
// itr.current()->setState( normal );
170
invalidateBoundingBox();
174
VSelection::draw( VPainter* painter, double zoomFactor ) const
176
if( objects().count() == 0 || state() == VObject::edit )
179
VDrawSelection op( m_objects, painter, !m_selectObjects );
180
op.visitVSelection( (VSelection &)*this );
183
const KoRect& rect = boundingBox();
185
// calculate displaycoords of big handle rect:
186
m_handleRect[ 0 ].setCoords( qRound( rect.left() ), qRound( rect.top() ),
187
qRound( rect.right() ), qRound( rect.bottom() ) );
189
KoPoint center = m_handleRect[ 0 ].center();
191
double handleNodeSize = m_handleNodeSize / zoomFactor;
193
// calculate displaycoords of nodes:
194
m_handleRect[ node_lb ].setRect( m_handleRect[0].left() - handleNodeSize, m_handleRect[0].top() - handleNodeSize, 2 * handleNodeSize, 2 * handleNodeSize );
195
m_handleRect[ node_mb ].setRect( center.x() - handleNodeSize, m_handleRect[0].top() - handleNodeSize, 2 * handleNodeSize, 2 * handleNodeSize );
196
m_handleRect[ node_rb ].setRect( m_handleRect[0].right() - handleNodeSize - (1 / zoomFactor), m_handleRect[0].top() - handleNodeSize, 2 * handleNodeSize, 2 * handleNodeSize );
197
m_handleRect[ node_rm ].setRect( m_handleRect[0].right() - handleNodeSize - (1 / zoomFactor), center.y() - handleNodeSize, 2 * handleNodeSize, 2 * handleNodeSize );
198
m_handleRect[ node_rt ].setRect( m_handleRect[0].right() - handleNodeSize - (1 / zoomFactor) , m_handleRect[0].bottom() - handleNodeSize - (1 / zoomFactor), 2 * handleNodeSize, 2 * handleNodeSize );
199
m_handleRect[ node_mt ].setRect( center.x() - handleNodeSize, m_handleRect[0].bottom() - handleNodeSize - (1 / zoomFactor), 2 * handleNodeSize, 2 * handleNodeSize );
200
m_handleRect[ node_lt ].setRect( m_handleRect[0].left() - handleNodeSize, m_handleRect[0].bottom() - handleNodeSize - (1 / zoomFactor), 2 * handleNodeSize, 2 * handleNodeSize );
201
m_handleRect[ node_lm ].setRect( m_handleRect[0].left() - handleNodeSize, center.y() - handleNodeSize, 2 * handleNodeSize, 2 * handleNodeSize );
203
if( !m_showhandle ) return;
206
painter->setPen( Qt::blue.light() );
207
painter->setBrush( Qt::NoBrush );
209
painter->drawRect( KoRect( m_handleRect[ 0 ].x() * zoomFactor, m_handleRect[ 0 ].y() * zoomFactor,
210
m_handleRect[ 0 ].width() * zoomFactor, m_handleRect[ 0 ].height() * zoomFactor ) );
211
painter->setPen( Qt::blue.light() );
214
if( state() == VObject::selected )
216
painter->setPen( Qt::blue.light() );
217
painter->setBrush( Qt::white );
220
for( uint i = node_lt; i <= node_rb; ++i )
224
temp.setRect( zoomFactor * m_handleRect[ i ].left(),
225
zoomFactor * m_handleRect[ i ].top(),
226
2 * m_handleNodeSize + 1, 2 * m_handleNodeSize + 1 );
227
painter->drawRect( temp );
234
VSelection::boundingBox() const
236
// disable bbox caching for selection since there is no reliable
237
// way to get notified of a bbox change:
238
// if( m_boundingBoxIsInvalid )
241
m_boundingBox = KoRect();
243
VObjectListIterator itr = m_objects;
244
for( ; itr.current(); ++itr )
245
m_boundingBox |= itr.current()->boundingBox();
247
// m_boundingBoxIsInvalid = false;
250
return m_boundingBox;
255
VSelection::handleNode( const KoPoint &point ) const
257
for( uint i = node_lt; i <= node_rb; ++i )
259
if( m_handleRect[i].contains( point ) )
260
return static_cast<VHandleNode>( i );
267
VSelection::getSegments( const KoRect& rect )
269
VTestNodes op( rect );
271
VObjectListIterator itr = m_objects;
272
for( ; itr.current(); ++itr )
273
op.visit( *itr.current() );
279
VSelection::selectNodes( bool select )
281
VSelectNodes op( select );
283
VObjectListIterator itr = m_objects;
284
for( ; itr.current(); ++itr )
286
op.visit( *itr.current() );