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"
25 #include <iostream> // for cout in dump()
31 SUIT_DataObject::SUIT_DataObject( SUIT_DataObject* p )
37 myChildren.setAutoDelete( true );
46 SUIT_DataObject::~SUIT_DataObject()
48 SUIT_DataObject* p = myParent;
53 p->removeChild( this );
57 mySignal->emitSignal();
58 mySignal->setOwner( 0 );
61 for ( QPtrListIterator<SUIT_DataObject> it( myChildren ); it.current(); ++it )
62 it.current()->myParent = 0;
68 Returns the root object.
71 SUIT_DataObject* SUIT_DataObject::root() const
73 return parent() ? parent()->root() : (SUIT_DataObject*)this;
77 Returns the first child object.
80 SUIT_DataObject* SUIT_DataObject::firstChild() const
82 SUIT_DataObject* child = 0;
83 if ( !myChildren.isEmpty() )
84 child = myChildren.getFirst();
89 Returns the last child object.
92 SUIT_DataObject* SUIT_DataObject::lastChild() const
94 SUIT_DataObject* child = 0;
95 if ( !myChildren.isEmpty() )
96 child = myChildren.getLast();
101 Returns the number of the child objects.
104 int SUIT_DataObject::childCount() const
106 return myChildren.count();
110 Returns the index of the specified object in the child list or -1.
113 int SUIT_DataObject::childPos( const SUIT_DataObject* obj ) const
118 for ( DataObjectListIterator it( myChildren ); it.current() && res == -1; ++it, i++ )
120 if ( it.current() == obj )
128 Returns the child object with specified index.
131 SUIT_DataObject* SUIT_DataObject::childObject( const int idx ) const
133 SUIT_DataObject* child = 0;
135 if ( idx>= 0 && idx < (int)myChildren.count() )
137 SUIT_DataObject* that = (SUIT_DataObject*)this;
138 child = that->myChildren.at( idx );
145 Returns the level of the object in the data tree.
146 0 means that object is top-level.
149 int SUIT_DataObject::level() const
152 SUIT_DataObject* p = parent();
161 Returns the next data object in the child list of the parent.
164 SUIT_DataObject* SUIT_DataObject::nextBrother() const
166 return myParent ? myParent->childObject( myParent->childPos( this ) + 1 ) : 0;
170 Returns the previous data object in the child list of the parent.
173 SUIT_DataObject* SUIT_DataObject::prevBrother() const
175 return myParent ? myParent->childObject( myParent->childPos( this ) - 1 ) : 0;
179 Returns 'true' if the object will delete children during destroying
182 bool SUIT_DataObject::autoDeleteChildren() const
184 return myChildren.autoDelete();
188 Specify should the object delete children during destroying
191 void SUIT_DataObject::setAutoDeleteChildren( const bool on )
193 myChildren.setAutoDelete( on );
197 Returns the list of the child objects. if 'rec' is 'true' then function get all sub children.
200 void SUIT_DataObject::children( DataObjectList& lst, const bool rec ) const
202 for ( DataObjectListIterator it( myChildren ); it.current(); ++it )
204 lst.append( it.current() );
206 it.current()->children( lst, rec );
211 Returns the list of the child objects. if 'rec' is 'true' then function get all sub children.
214 DataObjectList SUIT_DataObject::children( const bool rec )
217 children( lst, rec );
222 Append new child object to the end of the children list
225 void SUIT_DataObject::appendChild( SUIT_DataObject* theObj )
227 insertChild( theObj, myChildren.count() );
231 Insert new child object to the children list at specified position
234 void SUIT_DataObject::insertChild( SUIT_DataObject* theObj, int thePosition )
236 if ( !theObj || myChildren.find( theObj ) != -1 )
239 int pos = thePosition < 0 ? myChildren.count() : thePosition;
240 myChildren.insert( QMIN( pos, (int)myChildren.count() ), theObj );
241 theObj->setParent( this );
245 Removes the specified child object reference.
248 void SUIT_DataObject::removeChild( SUIT_DataObject* theObj )
253 bool ad = myChildren.autoDelete();
254 myChildren.setAutoDelete( false );
256 if ( myChildren.remove( theObj ) )
257 theObj->setParent( 0 );
259 myChildren.setAutoDelete( ad );
263 Replaces the specified child object by another object.
266 bool SUIT_DataObject::replaceChild( SUIT_DataObject* src, SUIT_DataObject* trg, const bool del )
271 int idx = childPos( trg );
274 int pos = childPos( src );
278 insertChild( trg, idx );
282 insertChild( trg, pos );
292 Transfer the all children from specified object 'obj' to self.
295 void SUIT_DataObject::reparentChildren( const SUIT_DataObject* obj )
301 obj->children( lst );
302 for ( DataObjectListIterator it( lst ); it.current(); ++it )
303 it.current()->setParent( this );
307 Set the parent object. Remove itself from current parent children
308 and append itself to the new parent children list.
311 void SUIT_DataObject::setParent( SUIT_DataObject* theParent )
313 if ( theParent == parent() )
317 parent()->removeChild( this );
319 myParent = theParent;
322 parent()->appendChild( this );
326 Returns the parent object.
329 SUIT_DataObject* SUIT_DataObject::parent() const
336 Connect to signal destroyed( SUIT_DataObject* ).
339 bool SUIT_DataObject::connect( QObject* reciever, const char* slot )
341 if ( !reciever || !slot )
345 mySignal = new Signal( this );
347 QObject::disconnect( mySignal, SIGNAL( destroyed( SUIT_DataObject* ) ), reciever, slot );
348 return QObject::connect( mySignal, SIGNAL( destroyed( SUIT_DataObject* ) ), reciever, slot );
352 Disconnect from signal destroyed( SUIT_DataObject* ).
355 bool SUIT_DataObject::disconnect( QObject* reciever, const char* slot )
357 if ( !reciever || !slot )
363 return QObject::disconnect( mySignal, SIGNAL( destroyed( SUIT_DataObject* ) ), reciever, slot );
370 void SUIT_DataObject::deleteLater()
373 mySignal = new Signal( this );
375 mySignal->emitSignal();
376 mySignal->deleteLater();
383 QString SUIT_DataObject::name() const
385 return QString::null;
392 QPixmap SUIT_DataObject::icon() const
401 QString SUIT_DataObject::text( const int ) const
403 return QString::null;
410 QColor SUIT_DataObject::color( const ColorRole ) const
416 Returns object tool tip
419 QString SUIT_DataObject::toolTip() const
421 return QString::null;
425 Returns 'true' if it is possible to drag this object
428 bool SUIT_DataObject::isDragable() const
434 Returns 'true' if it is possible to drop an object "obj" to this object.
437 bool SUIT_DataObject::isDropAccepted( SUIT_DataObject* )
443 Returns type of check possibility.
446 SUIT_DataObject::CheckType SUIT_DataObject::checkType() const
452 Returns the checked state of the object.
455 bool SUIT_DataObject::isOn() const
461 Sets the checked state of the object.
464 void SUIT_DataObject::setOn( const bool on )
469 bool SUIT_DataObject::isOpen() const
474 void SUIT_DataObject::setOpen( const bool on )
480 Returns object personal indentification key.
483 SUIT_DataObjectKey* SUIT_DataObject::key() const
489 Dump this data object and its children to cout
491 void SUIT_DataObject::dump( const int indent ) const
493 QString strIndent = QString().fill( ' ', indent ); // indentation string
494 std::cout << strIndent << name() << std::endl; // dump to cout
495 for ( DataObjectListIterator it( myChildren ); it.current(); ++it ) // iterate all children
496 it.current()->dump( indent + 2 ); // dump every child with indent + 2 spaces
500 Class: SUIT_DataObject::Signal [Internal]
503 SUIT_DataObject::Signal::Signal( SUIT_DataObject* o )
512 SUIT_DataObject::Signal::~Signal()
514 SUIT_DataObject* o = myOwner;
526 void SUIT_DataObject::Signal::setOwner( SUIT_DataObject* o )
532 emit signal destroed owner.
534 void SUIT_DataObject::Signal::emitSignal()
537 emit destroyed( myOwner );