karbon

vselection.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001, The Karbon Developers
3 Copyright (C) 2002, The Karbon Developers
4
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.
9
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.
14
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., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21
22#include "vdocument.h"
23#include "vdrawselection.h"
24#include "vpainter.h"
25#include "vselection.h"
26#include "vselectnodes.h"
27#include "vselectobjects.h"
28#include "vvisitor.h"
29#include "vcolor.h"
30#include "vfill.h"
31#include "vstroke.h"
32
33uint VSelection::m_handleNodeSize = 3;
34
36 : VObject( parent ), m_showhandle( true )
37{
38 m_handleRect = new KoRect[ 10 ];
39 setStroke( VStroke( VColor( TQt::black ) ) );
40 setFill( VFill() );
41
42 m_selectObjects = true;
43}
44
46 : VObject( selection ), VVisitor()
47{
48 m_handleRect = new KoRect[ 10 ];
49
50 VObjectListIterator itr = selection.m_objects;
51 for ( ; itr.current() ; ++itr )
52 append( itr.current() ); // Don't clone objects here.
53
54 m_showhandle = true;
55 m_selectObjects = selection.m_selectObjects;
56}
57
59{
60 //clear();
61 delete[]( m_handleRect );
62}
63
66{
67 return new VSelection( *this );
68}
69
70void
72{
73 visitor.visitVSelection( *this );
74}
75
76void
78{
79 m_objects.removeRef( &object );
80 if( object.state() >= selected )
81 object.setState( normal );
83}
84
85bool
86VSelection::take( const KoRect& rect, bool selectObjects, bool exclusive )
87{
88 bool success = false;
89
90 if( selectObjects )
91 {
92 VSelectObjects op( m_objects, rect, false );
93 if( op.visit( *static_cast<VDocument*>( parent() ) ) )
94 {
96 success = true;
97 }
98 }
99 else
100 {
101 VObjectListIterator itr( m_objects );
102
103 // Try to deselect all that have at least one node contained in the rect
104 for ( ; itr.current(); ++itr )
105 {
106 VSelectNodes op( rect, false, exclusive );
107
108 if( op.visit( *itr.current() ) )
109 {
110 success = true;
111 }
112 }
113 }
114
116
117 return success;
118}
119
120void
122{
123 clear();
124
125 VSelectObjects op( m_objects );
126 op.visit( *static_cast<VDocument*>( parent() ) );
127 selectNodes();
128
130}
131
132void
134{
135 // only append if item is not deleted or not already in list
136 if( object->state() != deleted )
137 {
138 if( ! m_objects.containsRef( object ) )
139 m_objects.append( object );
140 object->setState( selected );
142 }
143}
144
145void
146VSelection::append( const VObjectList &objects )
147{
148 VObjectListIterator itr = objects;
149 for( ; itr.current(); ++itr )
150 append( itr.current() );
151}
152
153bool
154VSelection::append( const KoRect& rect, bool selectObjects, bool exclusive )
155{
156 bool success = false;
157
158 if( selectObjects )
159 {
160 //m_objects.clear();
161 VSelectObjects op( m_objects, rect );
162 if( op.visit( *static_cast<VDocument*>( parent() ) ) )
163 {
164 selectNodes();
165 success = true;
166 }
167 }
168 else
169 {
170 VObjectListIterator itr( m_objects );
171 VObjectList notSelected;
172
173 // Try to select all that have at least one node contained in the rect
174 for ( ; itr.current(); ++itr )
175 {
176 VSelectNodes op( rect, true, exclusive );
177
178 if( op.visit( *itr.current() ) )
179 success = true;
180 else
181 notSelected.append( itr.current());
182 }
183 // Remove all that were not selected from this selection
184 VObjectListIterator jtr( notSelected );
185 for ( ; jtr.current(); ++jtr )
186 take( *( jtr.current() ) );
187 }
188
190
191 return success;
192}
193
194void
196{
197 VSelectNodes op( true );
198
199 VObjectListIterator itr = m_objects;
200 for( ; itr.current(); ++itr )
201 {
202 op.visit( *itr.current() );
203
204 //if( itr.current()->state() != deleted )
205 // itr.current()->setState( normal );
206 }
207
208 m_objects.clear();
210}
211
212void
213VSelection::draw( VPainter* painter, double zoomFactor ) const
214{
215 if( objects().count() == 0 || state() == VObject::edit )
216 return;
217
218 VDrawSelection op( m_objects, painter, !m_selectObjects, m_handleNodeSize );
219 op.visitVSelection( (VSelection &)*this );
220
221 // get bounding box:
222 const KoRect& rect = boundingBox();
223
224 // calculate displaycoords of big handle rect:
225 m_handleRect[ 0 ].setCoords( tqRound( rect.left() ), tqRound( rect.top() ),
226 tqRound( rect.right() ), tqRound( rect.bottom() ) );
227
228 KoPoint center = m_handleRect[ 0 ].center();
229
230 double handleNodeSize = m_handleNodeSize / zoomFactor;
231
232 // calculate displaycoords of nodes:
233 m_handleRect[ node_lb ].setRect( m_handleRect[0].left() - handleNodeSize, m_handleRect[0].top() - handleNodeSize, 2 * handleNodeSize, 2 * handleNodeSize );
234 m_handleRect[ node_mb ].setRect( center.x() - handleNodeSize, m_handleRect[0].top() - handleNodeSize, 2 * handleNodeSize, 2 * handleNodeSize );
235 m_handleRect[ node_rb ].setRect( m_handleRect[0].right() - handleNodeSize - (1 / zoomFactor), m_handleRect[0].top() - handleNodeSize, 2 * handleNodeSize, 2 * handleNodeSize );
236 m_handleRect[ node_rm ].setRect( m_handleRect[0].right() - handleNodeSize - (1 / zoomFactor), center.y() - handleNodeSize, 2 * handleNodeSize, 2 * handleNodeSize );
237 m_handleRect[ node_rt ].setRect( m_handleRect[0].right() - handleNodeSize - (1 / zoomFactor) , m_handleRect[0].bottom() - handleNodeSize - (1 / zoomFactor), 2 * handleNodeSize, 2 * handleNodeSize );
238 m_handleRect[ node_mt ].setRect( center.x() - handleNodeSize, m_handleRect[0].bottom() - handleNodeSize - (1 / zoomFactor), 2 * handleNodeSize, 2 * handleNodeSize );
239 m_handleRect[ node_lt ].setRect( m_handleRect[0].left() - handleNodeSize, m_handleRect[0].bottom() - handleNodeSize - (1 / zoomFactor), 2 * handleNodeSize, 2 * handleNodeSize );
240 m_handleRect[ node_lm ].setRect( m_handleRect[0].left() - handleNodeSize, center.y() - handleNodeSize, 2 * handleNodeSize, 2 * handleNodeSize );
241
242 if( !m_showhandle ) return;
243
244 // draw handle rect:
245 painter->setPen( TQt::blue.light() );
246 painter->setBrush( TQt::NoBrush );
247
248 painter->drawRect( KoRect( m_handleRect[ 0 ].x() * zoomFactor, m_handleRect[ 0 ].y() * zoomFactor,
249 m_handleRect[ 0 ].width() * zoomFactor, m_handleRect[ 0 ].height() * zoomFactor ) );
250 painter->setPen( TQt::blue.light() );
251
252 // draw nodes:
253 if( state() == VObject::selected )
254 {
255 painter->setPen( TQt::blue.light() );
256 painter->setBrush( TQt::white );
257
258 KoRect temp;
259 for( uint i = node_lt; i <= node_rb; ++i )
260 {
261 if( i != node_mm )
262 {
263 temp.setRect( zoomFactor * m_handleRect[ i ].left(),
264 zoomFactor * m_handleRect[ i ].top(),
265 2 * m_handleNodeSize + 1, 2 * m_handleNodeSize + 1 );
266 painter->drawRect( temp );
267 }
268 }
269 }
270}
271
272const KoRect&
274{
275// disable bbox caching for selection since there is no reliable
276// way to get notified of a bbox change:
277// if( m_boundingBoxIsInvalid )
278// {
279 // clear:
280 m_boundingBox = KoRect();
281
282 VObjectListIterator itr = m_objects;
283 for( ; itr.current(); ++itr )
284 m_boundingBox |= itr.current()->boundingBox();
285
286// m_boundingBoxIsInvalid = false;
287// }
288
289 return m_boundingBox;
290}
291
292
293VHandleNode
294VSelection::handleNode( const KoPoint &point ) const
295{
296 for( uint i = node_lt; i <= node_rb; ++i )
297 {
298 if( m_handleRect[i].contains( point ) )
299 return static_cast<VHandleNode>( i );
300 }
301
302 return node_none;
303}
304
305TQPtrList<VSegment>
306VSelection::getSegments( const KoRect& rect )
307{
308 VTestNodes op( rect );
309
310 VObjectListIterator itr = m_objects;
311 for( ; itr.current(); ++itr )
312 op.visit( *itr.current() );
313
314 return op.result();
315}
316
317void
319{
320 VSelectNodes op( select );
321
322 VObjectListIterator itr = m_objects;
323 for( ; itr.current(); ++itr )
324 {
325 op.visit( *itr.current() );
326 }
327}
328
This class keeps track of color properties.
Definition vcolor.h:41
All non-visual, static doc info is in here.
Definition vdocument.h:53
Helper class to draw the outline of a composite path, including (?) optionally its bezier helper line...
Manages the fill of shapes.
Definition vfill.h:45
VObject(VObject *parent, VState state=edit)
Constructs a new object that is child of parent and has the given state.
Definition vobject.cpp:36
VObject * parent() const
Returns pointer to current parent object.
Definition vobject.h:147
@ selected
visible, active and can be manipulated by tools
Definition vobject.h:61
@ deleted
deleted, nearly dead
Definition vobject.h:58
@ normal
visible, not active
Definition vobject.h:54
@ edit
visible, active and is currently manipulated by a tool
Definition vobject.h:62
virtual void setStroke(const VStroke &stroke)
Sets the stroke to a given new stroke.
Definition vobject.cpp:84
void invalidateBoundingBox()
Invalidates the bounding box, so it has to be recalculated.
Definition vobject.h:127
KoRect m_boundingBox
the object's bounding box
Definition vobject.h:285
virtual void setFill(const VFill &fill)
Sets the fill to a given new fill.
Definition vobject.cpp:93
VState state() const
Get the state the object is in.
Definition vobject.h:154
This visitor visits a selection and selects objects that are contained in a paramater selection recta...
VSelection manages a set of selected vobjects.
Definition vselection.h:62
const VObjectList & objects() const
Read only access to the selected objects.
Definition vselection.h:136
void append()
Adds all objects to the selection.
void clear()
Removes the references to all objects, not the objects themselves.
void draw(VPainter *painter, double zoomFactor) const
Paint selected objects outline and handle.
virtual ~VSelection()
Destroys the selection.
void selectNodes(bool select=true)
Selects or deselects all nodes.
TQPtrList< VSegment > getSegments(const KoRect &rect)
Returns a list of segments that have at least one control point inside the specified rect.
virtual void accept(VVisitor &visitor)
Accept a VVisitor.
virtual VSelection * clone() const
Create an exact copy of this object.
virtual const KoRect & boundingBox() const
Calculates the tightest bounding box around the object.
VSelection(VObject *parent=0L)
Constructs a vselection with the specified parent.
void take(VObject &object)
Removes the reference to the object, not the object itself.
VHandleNode handleNode(const KoPoint &point) const
Returns the handle node id, the KoPoint is inside.
Manages stroke properties.
Definition vstroke.h:49
bool success() const
Return if the operation was a success or not.
Definition vvisitor.h:125
virtual bool visit(VObject &object)
General visit method.
Definition vvisitor.cpp:31
VVisitor()
Constructs a new visitor class.
Definition vvisitor.h:56
virtual void visitVSelection(VSelection &selection)
Visit method for a VSelection.
Definition vvisitor.cpp:91