1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/
19 #include "SUIT_DataObject.h"
23 #include "SUIT_DataObjectKey.h"
29 SUIT_DataObject::SUIT_DataObject( SUIT_DataObject* p )
35 myChildren.setAutoDelete( true );
44 SUIT_DataObject::~SUIT_DataObject()
46 SUIT_DataObject* p = myParent;
51 p->removeChild( this );
55 mySignal->emitSignal();
56 mySignal->setOwner( 0 );
59 for ( QPtrListIterator<SUIT_DataObject> it( myChildren ); it.current(); ++it )
60 it.current()->myParent = 0;
66 Returns the root object.
69 SUIT_DataObject* SUIT_DataObject::root() const
71 return parent() ? parent()->root() : (SUIT_DataObject*)this;
75 Returns the first child object.
78 SUIT_DataObject* SUIT_DataObject::firstChild() const
80 SUIT_DataObject* child = 0;
81 if ( !myChildren.isEmpty() )
82 child = myChildren.getFirst();
87 Returns the last child object.
90 SUIT_DataObject* SUIT_DataObject::lastChild() const
92 SUIT_DataObject* child = 0;
93 if ( !myChildren.isEmpty() )
94 child = myChildren.getLast();
99 Returns the number of the child objects.
102 int SUIT_DataObject::childCount() const
104 return myChildren.count();
108 Returns the index of the specified object in the child list or -1.
111 int SUIT_DataObject::childPos( const SUIT_DataObject* obj ) const
116 for ( DataObjectListIterator it( myChildren ); it.current() && res == -1; ++it, i++ )
118 if ( it.current() == obj )
126 Returns the child object with specified index.
129 SUIT_DataObject* SUIT_DataObject::childObject( const int idx ) const
131 SUIT_DataObject* child = 0;
133 if ( idx>= 0 && idx < (int)myChildren.count() )
135 SUIT_DataObject* that = (SUIT_DataObject*)this;
136 child = that->myChildren.at( idx );
143 Returns the level of the object in the data tree.
144 0 means that object is top-level.
147 int SUIT_DataObject::level() const
150 SUIT_DataObject* p = parent();
159 Returns the next data object in the child list of the parent.
162 SUIT_DataObject* SUIT_DataObject::nextBrother() const
164 return myParent ? myParent->childObject( myParent->childPos( this ) + 1 ) : 0;
168 Returns the previous data object in the child list of the parent.
171 SUIT_DataObject* SUIT_DataObject::prevBrother() const
173 return myParent ? myParent->childObject( myParent->childPos( this ) - 1 ) : 0;
177 Returns 'true' if the object will delete children during destroying
180 bool SUIT_DataObject::autoDeleteChildren() const
182 return myChildren.autoDelete();
186 Specify should the object delete children during destroying
189 void SUIT_DataObject::setAutoDeleteChildren( const bool on )
191 myChildren.setAutoDelete( on );
195 Returns the list of the child objects. if 'rec' is 'true' then function get all sub children.
198 void SUIT_DataObject::children( DataObjectList& lst, const bool rec ) const
200 for ( DataObjectListIterator it( myChildren ); it.current(); ++it )
202 lst.append( it.current() );
204 it.current()->children( lst, rec );
209 Returns the list of the child objects. if 'rec' is 'true' then function get all sub children.
212 DataObjectList SUIT_DataObject::children( const bool rec )
215 children( lst, rec );
220 Append new child object to the end of the children list
223 void SUIT_DataObject::appendChild( SUIT_DataObject* theObj )
225 insertChild( theObj, myChildren.count() );
229 Insert new child object to the children list at specified position
232 void SUIT_DataObject::insertChild( SUIT_DataObject* theObj, int thePosition )
234 if ( !theObj || myChildren.find( theObj ) != -1 )
237 int pos = thePosition < 0 ? myChildren.count() : thePosition;
238 myChildren.insert( QMIN( pos, (int)myChildren.count() ), theObj );
239 theObj->setParent( this );
243 Removes the specified child object reference.
246 void SUIT_DataObject::removeChild( SUIT_DataObject* theObj )
251 bool ad = myChildren.autoDelete();
252 myChildren.setAutoDelete( false );
254 if ( myChildren.remove( theObj ) )
255 theObj->setParent( 0 );
257 myChildren.setAutoDelete( ad );
261 Replaces the specified child object by another object.
264 bool SUIT_DataObject::replaceChild( SUIT_DataObject* src, SUIT_DataObject* trg, const bool del )
269 int idx = childPos( trg );
272 int pos = childPos( src );
276 insertChild( trg, idx );
280 insertChild( trg, pos );
290 Transfer the all children from specified object 'obj' to self.
293 void SUIT_DataObject::reparentChildren( const SUIT_DataObject* obj )
299 obj->children( lst );
300 for ( DataObjectListIterator it( lst ); it.current(); ++it )
301 it.current()->setParent( this );
305 Set the parent object. Remove itself from current parent children
306 and append itself to the new parent children list.
309 void SUIT_DataObject::setParent( SUIT_DataObject* theParent )
311 if ( theParent == parent() )
315 parent()->removeChild( this );
317 myParent = theParent;
320 parent()->appendChild( this );
324 Returns the parent object.
327 SUIT_DataObject* SUIT_DataObject::parent() const
334 Connect to signal destroyed( SUIT_DataObject* ).
337 bool SUIT_DataObject::connect( QObject* reciever, const char* slot )
339 if ( !reciever || !slot )
343 mySignal = new Signal( this );
345 QObject::disconnect( mySignal, SIGNAL( destroyed( SUIT_DataObject* ) ), reciever, slot );
346 return QObject::connect( mySignal, SIGNAL( destroyed( SUIT_DataObject* ) ), reciever, slot );
350 Disconnect from signal destroyed( SUIT_DataObject* ).
353 bool SUIT_DataObject::disconnect( QObject* reciever, const char* slot )
355 if ( !reciever || !slot )
361 return QObject::disconnect( mySignal, SIGNAL( destroyed( SUIT_DataObject* ) ), reciever, slot );
368 void SUIT_DataObject::deleteLater()
371 mySignal = new Signal( this );
373 mySignal->emitSignal();
374 mySignal->deleteLater();
381 QString SUIT_DataObject::name() const
383 return QString::null;
390 QPixmap SUIT_DataObject::icon() const
399 QString SUIT_DataObject::text( const int ) const
401 return QString::null;
408 QColor SUIT_DataObject::color( const ColorRole ) const
414 Returns object tool tip
417 QString SUIT_DataObject::toolTip() const
419 return QString::null;
423 Returns 'true' if it is possible to drag this object
426 bool SUIT_DataObject::isDragable() const
432 Returns 'true' if it is possible to drop an object "obj" to this object.
435 bool SUIT_DataObject::isDropAccepted( SUIT_DataObject* )
441 Returns type of check possibility.
444 SUIT_DataObject::CheckType SUIT_DataObject::checkType() const
450 Returns the checked state of the object.
453 bool SUIT_DataObject::isOn() const
459 Sets the checked state of the object.
462 void SUIT_DataObject::setOn( const bool on )
468 \return the opened state of the object (used in Object Browser).
470 bool SUIT_DataObject::isOpen() const
476 Sets the opened state of the object (used in Object Browser).
478 void SUIT_DataObject::setOpen( const bool on )
484 Returns object personal indentification key.
487 SUIT_DataObjectKey* SUIT_DataObject::key() const
493 Dump this data object and its children to cout
495 void SUIT_DataObject::dump( const int indent ) const
497 QString strIndent = QString().fill( ' ', indent ); // indentation string
498 printf( "%s%s\n", strIndent.latin1(), name().latin1() );
499 for ( DataObjectListIterator it( myChildren ); it.current(); ++it ) // iterate all children
500 it.current()->dump( indent + 2 ); // dump every child with indent + 2 spaces
504 Class: SUIT_DataObject::Signal [Internal]
507 SUIT_DataObject::Signal::Signal( SUIT_DataObject* o )
516 SUIT_DataObject::Signal::~Signal()
518 SUIT_DataObject* o = myOwner;
530 void SUIT_DataObject::Signal::setOwner( SUIT_DataObject* o )
536 emit signal destroed owner.
538 void SUIT_DataObject::Signal::emitSignal()
541 emit destroyed( myOwner );