Salome HOME
Initial version
[modules/gui.git] / src / SUIT / SUIT_DataObject.cxx
1 #include "SUIT_DataObject.h"
2
3 #include <qobject.h>
4
5 #include "SUIT_DataObjectKey.h"
6
7 #include <iostream> // for cout in dump()
8
9 /*!
10     Constructor
11 */
12
13 SUIT_DataObject::SUIT_DataObject( SUIT_DataObject* p )
14 : myParent( 0 ),
15   mySignal( 0 ),
16   myCheck( false )
17 {
18   myChildren.setAutoDelete( true );
19
20   setParent( p );
21 }
22
23 /*!
24     Destructor
25 */
26
27 SUIT_DataObject::~SUIT_DataObject()
28 {
29   SUIT_DataObject* p = myParent;
30
31   myParent = 0;
32
33   if ( p )
34     p->removeChild( this );
35
36   for ( QPtrListIterator<SUIT_DataObject> it( myChildren ); it.current(); ++it )
37     it.current()->myParent = 0;
38
39   delete mySignal;
40 }
41
42 /*!
43     Returns the root object.
44 */
45
46 SUIT_DataObject* SUIT_DataObject::root() const
47 {
48   return parent() ? parent()->root() : (SUIT_DataObject*)this;
49 }
50
51 /*!
52     Returns the first child object.
53 */
54
55 SUIT_DataObject* SUIT_DataObject::firstChild() const
56 {
57   SUIT_DataObject* child = 0;
58   if ( !myChildren.isEmpty() )
59     child = myChildren.getFirst();
60   return child;
61 }
62
63 /*!
64     Returns the last child object.
65 */
66
67 SUIT_DataObject* SUIT_DataObject::lastChild() const
68 {
69   SUIT_DataObject* child = 0;
70   if ( !myChildren.isEmpty() )
71     child = myChildren.getLast();
72   return child;
73 }
74
75 /*!
76     Returns the number of the child objects.
77 */
78
79 int SUIT_DataObject::childCount() const
80 {
81   return myChildren.count();
82 }
83
84 /*!
85     Returns the index of the specified object in the child list or -1.
86 */
87
88 int SUIT_DataObject::childPos( const SUIT_DataObject* obj ) const
89 {
90   int res = -1;
91
92   int i = 0;
93   for ( DataObjectListIterator it( myChildren ); it.current() && res == -1; ++it, i++ )
94   {
95     if ( it.current() == obj )
96       res = i;
97   }
98
99   return res;
100 }
101
102 /*!
103     Returns the child object with specified index.
104 */
105
106 SUIT_DataObject* SUIT_DataObject::childObject( const int idx ) const
107 {
108   SUIT_DataObject* child = 0;
109
110   if ( idx>= 0 && idx < (int)myChildren.count() )
111   {
112     SUIT_DataObject* that = (SUIT_DataObject*)this;
113     child = that->myChildren.at( idx );
114   }
115
116   return child;
117 }
118
119 /*!
120     Returns the next data object in the child list of the parent.
121 */
122
123 SUIT_DataObject* SUIT_DataObject::nextBrother() const
124 {
125   return myParent ? myParent->childObject( myParent->childPos( this ) + 1 ) : 0;
126 }
127
128 /*!
129     Returns the previous data object in the child list of the parent.
130 */
131
132 SUIT_DataObject* SUIT_DataObject::prevBrother() const
133 {
134   return myParent ? myParent->childObject( myParent->childPos( this ) - 1 ) : 0;
135 }
136
137 /*!
138     Returns 'true' if the object will delete children during destroying
139 */
140
141 bool SUIT_DataObject::autoDeleteChildren() const
142 {
143   return myChildren.autoDelete();
144 }
145
146 /*!
147     Specify should the object delete children during destroying
148 */
149
150 void SUIT_DataObject::setAutoDeleteChildren( const bool on )
151 {
152   myChildren.setAutoDelete( on );
153 }
154
155 /*!
156     Returns the list of the child objects. if 'rec' is 'true' then function get all sub children.
157 */
158
159 void SUIT_DataObject::children( DataObjectList& lst, const bool rec ) const
160 {
161   for ( DataObjectListIterator it( myChildren ); it.current(); ++it )
162   {
163     lst.append( it.current() );
164     if ( rec )
165       it.current()->children( lst, rec );
166   }
167 }
168
169 /*!
170     Returns the list of the child objects. if 'rec' is 'true' then function get all sub children.
171 */
172
173 DataObjectList SUIT_DataObject::children( const bool rec )
174 {
175   DataObjectList lst;
176   children( lst, rec );
177   return lst;
178 }
179
180 /*!
181     Append new child object to the end of the children list
182 */
183
184 void SUIT_DataObject::appendChild( SUIT_DataObject* theObj ) 
185 {
186   insertChild( theObj, myChildren.count() );
187 }
188
189 /*!
190     Insert new child object to the children list at specified position
191 */
192
193 void SUIT_DataObject::insertChild( SUIT_DataObject* theObj, int thePosition )
194 {
195   if ( !theObj || myChildren.find( theObj ) != -1 )
196     return;
197
198   int pos = thePosition < 0 ? myChildren.count() : thePosition;
199   myChildren.insert( QMIN( pos, (int)myChildren.count() ), theObj );
200   theObj->setParent( this );
201 }
202
203 /*!
204     Removes the specified child object reference.
205 */
206
207 void SUIT_DataObject::removeChild( SUIT_DataObject* theObj )
208 {
209   if ( !theObj )
210     return;
211
212   bool ad = myChildren.autoDelete();
213   myChildren.setAutoDelete( false );
214
215   if ( myChildren.remove( theObj ) )
216     theObj->setParent( 0 );
217
218   myChildren.setAutoDelete( ad );
219 }
220
221 /*!
222     Replaces the specified child object by another object.
223 */
224
225 bool SUIT_DataObject::replaceChild( SUIT_DataObject* src, SUIT_DataObject* trg, const bool del )
226 {
227   if ( !src || !trg )
228     return false;
229
230   int idx = childPos( trg );
231   removeChild( trg );
232
233   int pos = childPos( src );
234   if ( pos < 0 )
235   {
236     if ( idx >= 0 )
237       insertChild( trg, idx );
238     return false;
239   }
240
241   insertChild( trg, pos );
242   removeChild( src );
243
244   if ( del )
245     delete src;
246
247   return true;
248 }
249
250 /*!
251     Set the parent object. Remove itself from current parent children
252     and append itself to the new parent children list.
253 */
254
255 void SUIT_DataObject::setParent( SUIT_DataObject* theParent )
256 {
257   if ( theParent == parent() )
258     return;
259
260   if ( parent() )
261     parent()->removeChild( this );
262
263   myParent = theParent;
264
265   if ( parent() )
266     parent()->appendChild( this );
267 }
268
269 /*!
270     Returns the parent object.
271 */
272
273 SUIT_DataObject* SUIT_DataObject::parent() const
274 {
275   return myParent;
276 }
277
278
279 /*!
280     Connect to signal destroyed( SUIT_DataObject* ).
281 */
282
283 bool SUIT_DataObject::connect( QObject* reciever, const char* slot )
284 {
285   if ( !reciever || !slot )
286     return false;
287
288   if ( !mySignal )
289     mySignal = new Signal( this );
290
291   QObject::disconnect( mySignal, SIGNAL( destroyed( SUIT_DataObject* ) ), reciever, slot );
292   return QObject::connect( mySignal, SIGNAL( destroyed( SUIT_DataObject* ) ), reciever, slot );
293 }
294
295 /*!
296     Disconnect from signal destroyed( SUIT_DataObject* ).
297 */
298
299 bool SUIT_DataObject::disconnect( QObject* reciever, const char* slot )
300 {
301   if ( !reciever || !slot )
302     return false;
303
304   if ( !mySignal )
305     return true;
306
307   return QObject::disconnect( mySignal, SIGNAL( destroyed( SUIT_DataObject* ) ), reciever, slot );
308 }
309
310 /*!
311     Returns object name
312 */
313
314 QString SUIT_DataObject::name() const
315 {
316   return QString::null;
317 }
318
319 /*!
320     Returns object icon
321 */
322
323 QPixmap SUIT_DataObject::icon() const
324 {
325   return QPixmap();
326 }
327
328 /*!
329     Returns object text
330 */
331
332 QString SUIT_DataObject::text( const int ) const
333 {
334   return QString::null;
335 }
336
337 /*!
338     Returns object color
339 */
340
341 QColor SUIT_DataObject::color( const ColorRole ) const
342 {
343   return QColor();
344 }
345
346 /*!
347     Returns object tool tip
348 */
349
350 QString SUIT_DataObject::toolTip() const
351 {
352   return QString::null;
353 }
354
355 /*!
356     Returns 'true' if it is possible to drag this object
357 */
358
359 bool SUIT_DataObject::isDragable() const
360 {
361   return false;
362 }
363
364 /*!
365     Returns 'true' if it is possible to drop an object "obj" to this object.
366 */
367
368 bool SUIT_DataObject::isDropAccepted( SUIT_DataObject* )
369 {
370   return false;
371 }
372
373 /*!
374     Returns type of check possibility.
375 */
376
377 SUIT_DataObject::CheckType SUIT_DataObject::checkType() const
378 {
379   return None;
380 }
381
382 /*!
383     Returns the checked state of the object.
384 */
385
386 bool SUIT_DataObject::isOn() const
387 {
388   return myCheck;
389 }
390
391 /*!
392     Sets the checked state of the object.
393 */
394
395 void SUIT_DataObject::setOn( const bool on )
396 {
397   myCheck = on;
398 }
399
400 /*!
401     Returns object personal indentification key.
402 */
403
404 SUIT_DataObjectKey* SUIT_DataObject::key() const
405 {
406   return 0;
407 }
408
409 /*!
410    Dump this data object and its children to cout
411 */
412 void SUIT_DataObject::dump( const int indent ) const
413 {
414   QString strIndent = QString().fill( ' ', indent ); // indentation string 
415   std::cout << strIndent << name() << std::endl;     // dump to cout
416   for ( DataObjectListIterator it( myChildren ); it.current(); ++it ) // iterate all children
417     it.current()->dump( indent + 2 );  // dump every child with indent + 2 spaces
418 }