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 )
283 obj->children( lst );
284 for ( DataObjectListIterator it( lst ); it.current(); ++it )
285 it.current()->setParent( this );
289 Set the parent object. Remove itself from current parent children
290 and append itself to the new parent children list.
293 void SUIT_DataObject::setParent( SUIT_DataObject* theParent )
295 if ( theParent == parent() )
299 parent()->removeChild( this );
301 myParent = theParent;
304 parent()->appendChild( this );
308 Returns the parent object.
311 SUIT_DataObject* SUIT_DataObject::parent() const
318 Connect to signal destroyed( SUIT_DataObject* ).
321 bool SUIT_DataObject::connect( QObject* reciever, const char* slot )
323 if ( !reciever || !slot )
327 mySignal = new Signal( this );
329 QObject::disconnect( mySignal, SIGNAL( destroyed( SUIT_DataObject* ) ), reciever, slot );
330 return QObject::connect( mySignal, SIGNAL( destroyed( SUIT_DataObject* ) ), reciever, slot );
334 Disconnect from signal destroyed( SUIT_DataObject* ).
337 bool SUIT_DataObject::disconnect( QObject* reciever, const char* slot )
339 if ( !reciever || !slot )
345 return QObject::disconnect( mySignal, SIGNAL( destroyed( SUIT_DataObject* ) ), reciever, slot );
352 void SUIT_DataObject::deleteLater()
355 mySignal = new Signal( this );
357 mySignal->emitSignal();
358 mySignal->deleteLater();
365 QString SUIT_DataObject::name() const
367 return QString::null;
374 QPixmap SUIT_DataObject::icon() const
383 QString SUIT_DataObject::text( const int ) const
385 return QString::null;
392 QColor SUIT_DataObject::color( const ColorRole ) const
398 Returns object tool tip
401 QString SUIT_DataObject::toolTip() const
403 return QString::null;
407 Returns 'true' if it is possible to drag this object
410 bool SUIT_DataObject::isDragable() const
416 Returns 'true' if it is possible to drop an object "obj" to this object.
419 bool SUIT_DataObject::isDropAccepted( SUIT_DataObject* )
425 Returns type of check possibility.
428 SUIT_DataObject::CheckType SUIT_DataObject::checkType() const
434 Returns the checked state of the object.
437 bool SUIT_DataObject::isOn() const
443 Sets the checked state of the object.
446 void SUIT_DataObject::setOn( const bool on )
451 bool SUIT_DataObject::isOpen() const
456 void SUIT_DataObject::setOpen( const bool on )
462 Returns object personal indentification key.
465 SUIT_DataObjectKey* SUIT_DataObject::key() const
471 Dump this data object and its children to cout
473 void SUIT_DataObject::dump( const int indent ) const
475 QString strIndent = QString().fill( ' ', indent ); // indentation string
476 std::cout << strIndent << name() << std::endl; // dump to cout
477 for ( DataObjectListIterator it( myChildren ); it.current(); ++it ) // iterate all children
478 it.current()->dump( indent + 2 ); // dump every child with indent + 2 spaces
482 Class: SUIT_DataObject::Signal [Internal]
485 SUIT_DataObject::Signal::Signal( SUIT_DataObject* o )
494 SUIT_DataObject::Signal::~Signal()
496 SUIT_DataObject* o = myOwner;
508 void SUIT_DataObject::Signal::setOwner( SUIT_DataObject* o )
514 emit signal destroed owner.
516 void SUIT_DataObject::Signal::emitSignal()
519 emit destroyed( myOwner );