1 #include "SUIT_DataObject.h"
5 #include "SUIT_DataObjectKey.h"
7 #include <iostream> // for cout in dump()
13 SUIT_DataObject::SUIT_DataObject( SUIT_DataObject* p )
19 myChildren.setAutoDelete( true );
28 SUIT_DataObject::~SUIT_DataObject()
30 SUIT_DataObject* p = myParent;
35 p->removeChild( this );
39 mySignal->emitSignal();
40 mySignal->setOwner( 0 );
43 for ( QPtrListIterator<SUIT_DataObject> it( myChildren ); it.current(); ++it )
44 it.current()->myParent = 0;
50 Returns the root object.
53 SUIT_DataObject* SUIT_DataObject::root() const
55 return parent() ? parent()->root() : (SUIT_DataObject*)this;
59 Returns the first child object.
62 SUIT_DataObject* SUIT_DataObject::firstChild() const
64 SUIT_DataObject* child = 0;
65 if ( !myChildren.isEmpty() )
66 child = myChildren.getFirst();
71 Returns the last child object.
74 SUIT_DataObject* SUIT_DataObject::lastChild() const
76 SUIT_DataObject* child = 0;
77 if ( !myChildren.isEmpty() )
78 child = myChildren.getLast();
83 Returns the number of the child objects.
86 int SUIT_DataObject::childCount() const
88 return myChildren.count();
92 Returns the index of the specified object in the child list or -1.
95 int SUIT_DataObject::childPos( const SUIT_DataObject* obj ) const
100 for ( DataObjectListIterator it( myChildren ); it.current() && res == -1; ++it, i++ )
102 if ( it.current() == obj )
110 Returns the child object with specified index.
113 SUIT_DataObject* SUIT_DataObject::childObject( const int idx ) const
115 SUIT_DataObject* child = 0;
117 if ( idx>= 0 && idx < (int)myChildren.count() )
119 SUIT_DataObject* that = (SUIT_DataObject*)this;
120 child = that->myChildren.at( idx );
127 Returns the level of the object in the data tree.
128 0 means that object is top-level.
131 int SUIT_DataObject::level() const
134 SUIT_DataObject* p = parent();
143 Returns the next data object in the child list of the parent.
146 SUIT_DataObject* SUIT_DataObject::nextBrother() const
148 return myParent ? myParent->childObject( myParent->childPos( this ) + 1 ) : 0;
152 Returns the previous data object in the child list of the parent.
155 SUIT_DataObject* SUIT_DataObject::prevBrother() const
157 return myParent ? myParent->childObject( myParent->childPos( this ) - 1 ) : 0;
161 Returns 'true' if the object will delete children during destroying
164 bool SUIT_DataObject::autoDeleteChildren() const
166 return myChildren.autoDelete();
170 Specify should the object delete children during destroying
173 void SUIT_DataObject::setAutoDeleteChildren( const bool on )
175 myChildren.setAutoDelete( on );
179 Returns the list of the child objects. if 'rec' is 'true' then function get all sub children.
182 void SUIT_DataObject::children( DataObjectList& lst, const bool rec ) const
184 for ( DataObjectListIterator it( myChildren ); it.current(); ++it )
186 lst.append( it.current() );
188 it.current()->children( lst, rec );
193 Returns the list of the child objects. if 'rec' is 'true' then function get all sub children.
196 DataObjectList SUIT_DataObject::children( const bool rec )
199 children( lst, rec );
204 Append new child object to the end of the children list
207 void SUIT_DataObject::appendChild( SUIT_DataObject* theObj )
209 insertChild( theObj, myChildren.count() );
213 Insert new child object to the children list at specified position
216 void SUIT_DataObject::insertChild( SUIT_DataObject* theObj, int thePosition )
218 if ( !theObj || myChildren.find( theObj ) != -1 )
221 int pos = thePosition < 0 ? myChildren.count() : thePosition;
222 myChildren.insert( QMIN( pos, (int)myChildren.count() ), theObj );
223 theObj->setParent( this );
227 Removes the specified child object reference.
230 void SUIT_DataObject::removeChild( SUIT_DataObject* theObj )
235 bool ad = myChildren.autoDelete();
236 myChildren.setAutoDelete( false );
238 if ( myChildren.remove( theObj ) )
239 theObj->setParent( 0 );
241 myChildren.setAutoDelete( ad );
245 Replaces the specified child object by another object.
248 bool SUIT_DataObject::replaceChild( SUIT_DataObject* src, SUIT_DataObject* trg, const bool del )
253 int idx = childPos( trg );
256 int pos = childPos( src );
260 insertChild( trg, idx );
264 insertChild( trg, pos );
274 Transfer the all children from specified object 'obj' to self.
277 void SUIT_DataObject::reparentChildren( const SUIT_DataObject* obj )
280 obj->children( lst );
281 for ( DataObjectListIterator it( lst ); it.current(); ++it )
282 it.current()->setParent( this );
286 Set the parent object. Remove itself from current parent children
287 and append itself to the new parent children list.
290 void SUIT_DataObject::setParent( SUIT_DataObject* theParent )
292 if ( theParent == parent() )
296 parent()->removeChild( this );
298 myParent = theParent;
301 parent()->appendChild( this );
305 Returns the parent object.
308 SUIT_DataObject* SUIT_DataObject::parent() const
315 Connect to signal destroyed( SUIT_DataObject* ).
318 bool SUIT_DataObject::connect( QObject* reciever, const char* slot )
320 if ( !reciever || !slot )
324 mySignal = new Signal( this );
326 QObject::disconnect( mySignal, SIGNAL( destroyed( SUIT_DataObject* ) ), reciever, slot );
327 return QObject::connect( mySignal, SIGNAL( destroyed( SUIT_DataObject* ) ), reciever, slot );
331 Disconnect from signal destroyed( SUIT_DataObject* ).
334 bool SUIT_DataObject::disconnect( QObject* reciever, const char* slot )
336 if ( !reciever || !slot )
342 return QObject::disconnect( mySignal, SIGNAL( destroyed( SUIT_DataObject* ) ), reciever, slot );
349 void SUIT_DataObject::deleteLater()
352 mySignal = new Signal( this );
354 mySignal->emitSignal();
355 mySignal->deleteLater();
362 QString SUIT_DataObject::name() const
364 return QString::null;
371 QPixmap SUIT_DataObject::icon() const
380 QString SUIT_DataObject::text( const int ) const
382 return QString::null;
389 QColor SUIT_DataObject::color( const ColorRole ) const
395 Returns object tool tip
398 QString SUIT_DataObject::toolTip() const
400 return QString::null;
404 Returns 'true' if it is possible to drag this object
407 bool SUIT_DataObject::isDragable() const
413 Returns 'true' if it is possible to drop an object "obj" to this object.
416 bool SUIT_DataObject::isDropAccepted( SUIT_DataObject* )
422 Returns type of check possibility.
425 SUIT_DataObject::CheckType SUIT_DataObject::checkType() const
431 Returns the checked state of the object.
434 bool SUIT_DataObject::isOn() const
440 Sets the checked state of the object.
443 void SUIT_DataObject::setOn( const bool on )
448 bool SUIT_DataObject::isOpen() const
453 void SUIT_DataObject::setOpen( const bool on )
459 Returns object personal indentification key.
462 SUIT_DataObjectKey* SUIT_DataObject::key() const
468 Dump this data object and its children to cout
470 void SUIT_DataObject::dump( const int indent ) const
472 QString strIndent = QString().fill( ' ', indent ); // indentation string
473 std::cout << strIndent << name() << std::endl; // dump to cout
474 for ( DataObjectListIterator it( myChildren ); it.current(); ++it ) // iterate all children
475 it.current()->dump( indent + 2 ); // dump every child with indent + 2 spaces
479 Class: SUIT_DataObject::Signal [Internal]
482 SUIT_DataObject::Signal::Signal( SUIT_DataObject* o )
491 SUIT_DataObject::Signal::~Signal()
493 SUIT_DataObject* o = myOwner;
505 void SUIT_DataObject::Signal::setOwner( SUIT_DataObject* o )
511 emit signal destroed owner.
513 void SUIT_DataObject::Signal::emitSignal()
516 emit destroyed( myOwner );