Back to index

salome-gui  6.5.0
SUIT_DataObject.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File   : SUIT_DataObject.cxx
00024 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
00025 
00026 #include <QVariant>
00027 
00028 #include "SUIT_DataObject.h"
00029 #include "SUIT_DataObjectKey.h"
00030 #include <cstdio>
00031 
00032 SUIT_DataObject::Signal* SUIT_DataObject::mySignal = 0;
00033 
00050 SUIT_DataObject::SUIT_DataObject( SUIT_DataObject* p )
00051 : myParent( 0 ),
00052   myOpen( false ),
00053   myCheck( false ),
00054   myAutoDel( true ),
00055   _modified( false )
00056 {
00057   setParent( p );
00058   signal()->emitCreated( this );
00059 }
00060 
00066 SUIT_DataObject::~SUIT_DataObject()
00067 {
00068   SUIT_DataObject* p = myParent;
00069 
00070   myParent = 0;
00071 
00072   if ( p )
00073     p->removeChild( this );
00074 
00075   signal()->emitDestroyed( this );
00076 
00077   for ( DataObjectList::iterator it = myChildren.begin(); it != myChildren.end(); ++it )
00078     (*it)->myParent = 0;
00079 
00080   if ( autoDeleteChildren() )
00081   {
00082     for ( DataObjectList::iterator itr = myChildren.begin(); itr != myChildren.end(); ++itr )
00083       delete *itr;
00084   }
00085 }
00086 
00091 SUIT_DataObject* SUIT_DataObject::root() const
00092 {
00093   return parent() ? parent()->root() : (SUIT_DataObject*)this;
00094 }
00095 
00101 SUIT_DataObject* SUIT_DataObject::firstChild() const
00102 {
00103   SUIT_DataObject* child = 0;
00104   if ( !myChildren.isEmpty() )
00105     child = myChildren.first();
00106   return child;
00107 }
00108 
00114 SUIT_DataObject* SUIT_DataObject::lastChild() const
00115 {
00116   SUIT_DataObject* child = 0;
00117   if ( !myChildren.isEmpty() )
00118     child = myChildren.last();
00119   return child;
00120 }
00121 
00126 int SUIT_DataObject::childCount() const
00127 {
00128   return myChildren.count();
00129 }
00130 
00136 int SUIT_DataObject::childPos( const SUIT_DataObject* obj ) const
00137 {
00138   return myChildren.indexOf( (SUIT_DataObject*)obj );
00139 }
00140 
00146 SUIT_DataObject* SUIT_DataObject::childObject( const int idx ) const
00147 {
00148   SUIT_DataObject* child = 0;
00149 
00150   if ( idx >= 0 && idx < myChildren.count() )
00151     child = myChildren.at( idx );
00152 
00153   return child;
00154 }
00155 
00163 int SUIT_DataObject::level() const
00164 {
00165   int lev = 0;
00166   SUIT_DataObject* p = parent();
00167   while ( p ) {
00168     p = p->parent();
00169     lev++;
00170   }
00171   return lev;
00172 }
00173 
00178 int SUIT_DataObject::position() const
00179 {
00180   return myParent ? myParent->childPos( this ) : 0;
00181 }
00182 
00188 SUIT_DataObject* SUIT_DataObject::nextBrother() const
00189 {
00190   return myParent ? myParent->childObject( myParent->childPos( this ) + 1 ) : 0;
00191 }
00192 
00198 SUIT_DataObject* SUIT_DataObject::prevBrother() const
00199 {
00200   return myParent ? myParent->childObject( myParent->childPos( this ) - 1 ) : 0;
00201 }
00202 
00208 bool SUIT_DataObject::autoDeleteChildren() const
00209 {
00210   return myAutoDel;
00211 }
00212 
00222 void SUIT_DataObject::setAutoDeleteChildren( const bool on )
00223 {
00224   myAutoDel = on;
00225 }
00226 
00236 void SUIT_DataObject::children( DataObjectList& lst, const bool rec ) const
00237 {
00238   for ( DataObjectList::const_iterator it = myChildren.begin(); it != myChildren.end(); ++it )
00239   {
00240     lst.append( *it );
00241     if ( rec )
00242       (*it)->children( lst, rec );
00243   }
00244 }
00245 
00256 DataObjectList SUIT_DataObject::children( const bool rec )
00257 {
00258   DataObjectList lst;
00259   children( lst, rec );
00260   return lst;
00261 }
00262 
00267 void SUIT_DataObject::appendChild( SUIT_DataObject* obj )
00268 {
00269   insertChild( obj, myChildren.count() );
00270 }
00271 
00277 void SUIT_DataObject::insertChild( SUIT_DataObject* obj, int position )
00278 {
00279   if ( !obj || myChildren.contains( obj ) )
00280     return;
00281 
00282   int pos = position < 0 ? myChildren.count() : position;
00283   myChildren.insert( qMin( pos, (int)myChildren.count() ), obj );
00284   obj->setParent( this );
00285   signal()->emitInserted( obj, this );
00286 }
00287 
00293 void SUIT_DataObject::insertChildAtPos( SUIT_DataObject* obj, int position )
00294 {
00295   if ( !obj )return;
00296   int pos = position < 0 ? myChildren.count() : position;
00297   myChildren.insert( qMin( pos, (int)myChildren.count() ), obj );
00298   obj->assignParent( this );
00299 }
00300 
00306 void SUIT_DataObject::removeChild( SUIT_DataObject* obj, const bool del )
00307 {
00308   if ( !obj )
00309     return;
00310 
00311   if ( myChildren.removeAll( obj ) ) {
00312     signal()->emitRemoved( obj, this );
00313     obj->setParent( 0 );
00314 
00315     if ( del )
00316       obj->deleteLater();
00317   }
00318 }
00319 
00327 bool SUIT_DataObject::replaceChild( SUIT_DataObject* src, SUIT_DataObject* trg, const bool del )
00328 {
00329   if ( !src || !trg )
00330     return false;
00331 
00332   int idx = childPos( trg );
00333   removeChild( trg );
00334 
00335   int pos = childPos( src );
00336   if ( pos < 0 )
00337   {
00338     if ( idx >= 0 )
00339       insertChild( trg, idx );
00340     return false;
00341   }
00342 
00343   insertChild( trg, pos );
00344   removeChild( src );
00345 
00346   if ( del )
00347     src->deleteLater();
00348 
00349   return true;
00350 }
00351 
00356 void SUIT_DataObject::reparentChildren( const SUIT_DataObject* obj )
00357 {
00358   if ( !obj )
00359     return;
00360 
00361   DataObjectList lst;
00362   obj->children( lst );
00363   for ( DataObjectList::iterator it = lst.begin(); it != lst.end(); ++it )
00364     (*it)->setParent( this );
00365 }
00366 
00371 SUIT_DataObject* SUIT_DataObject::parent() const
00372 {
00373   return myParent;
00374 }
00375 
00380 void SUIT_DataObject::setParent( SUIT_DataObject* p )
00381 {
00382   if ( p == parent() )
00383     return;
00384 
00385   if ( parent() )
00386     parent()->removeChild( this );
00387 
00388   myParent = p;
00389 
00390   if ( parent() )
00391     parent()->appendChild( this );
00392 }
00393 
00394 void SUIT_DataObject::assignParent( SUIT_DataObject* p )
00395 {
00396   if ( p == myParent )
00397     return;
00398 
00399   myParent = p;
00400 }
00401 
00410 QString SUIT_DataObject::name() const
00411 {
00412   return QString();
00413 }
00414 
00427 QString SUIT_DataObject::text( const int id ) const
00428 {
00429   return id == NameId ? name() : QString();
00430 }
00431 
00443 QPixmap SUIT_DataObject::icon( const int /*id*/ ) const
00444 {
00445   return QPixmap();
00446 }
00447 
00460 QColor SUIT_DataObject::color( const ColorRole /*role*/, const int /*id*/ ) const
00461 {
00462   return QColor();
00463 }
00464 
00477 QString SUIT_DataObject::toolTip( const int /*id*/ ) const
00478 {
00479   return QString();
00480 }
00481 
00493 QString SUIT_DataObject::statusTip( const int /*id*/ ) const
00494 {
00495   return QString();
00496 }
00497 
00510 QString SUIT_DataObject::whatsThis( const int /*id*/ ) const
00511 {
00512   return QString();
00513 }
00514 
00526 QFont SUIT_DataObject::font( const int /*id*/ ) const
00527 {
00528   return QFont();
00529 }
00530 
00544 int SUIT_DataObject::alignment( const int /*id*/ ) const
00545 {
00546   return Qt::AlignLeft;
00547 }
00548 
00549 bool SUIT_DataObject::expandable() const
00550 {
00551   return true;
00552 }
00553 
00562 bool SUIT_DataObject::isVisible() const
00563 {
00564   return true;
00565 }
00566 
00575 bool SUIT_DataObject::isDraggable() const
00576 {
00577   return false;
00578 }
00579 
00588 bool SUIT_DataObject::isDropAccepted() const
00589 {
00590   return false;
00591 }
00592 
00601 bool SUIT_DataObject::isEnabled() const
00602 {
00603   return true;
00604 }
00605 
00614 bool SUIT_DataObject::isSelectable() const
00615 {
00616   return true;
00617 }
00618 
00629 bool SUIT_DataObject::isCheckable( const int /*id*/ ) const
00630 {
00631   return false;
00632 }
00633 
00643 bool SUIT_DataObject::renameAllowed( const int /*id*/ ) const
00644 {
00645   return false;
00646 }
00647 
00656 bool SUIT_DataObject::setName(const QString& /*name*/) {
00657   return false;
00658 }
00659 
00671 bool SUIT_DataObject::isOn( const int id ) const
00672 {
00673   return id == NameId && myCheck;
00674 }
00675 
00687 void SUIT_DataObject::setOn( const bool on, const int id )
00688 {
00689   if( id == NameId )
00690     myCheck = on;
00691 }
00692 
00698 bool SUIT_DataObject::isOpen() const
00699 {
00700   return myOpen;
00701 }
00702 
00708 void SUIT_DataObject::setOpen( const bool on )
00709 {
00710   myOpen = on;
00711 }
00712 
00724 bool SUIT_DataObject::customSorting( const int /*id*/ ) const
00725 {
00726   return false;
00727 }
00728 
00745 bool SUIT_DataObject::compare( const QVariant& /*left*/, const QVariant& /*right*/,
00746                                const int /*id*/ ) const
00747 {
00748   return false;
00749 }
00750 
00759 SUIT_DataObjectKey* SUIT_DataObject::key() const
00760 {
00761   return 0;
00762 }
00763 
00768 SUIT_DataObject::Signal* SUIT_DataObject::signal()
00769 {
00770   if ( !mySignal )
00771     mySignal = new Signal();
00772   return mySignal;
00773 }
00774 
00782 bool SUIT_DataObject::connect( const char* sig, QObject* reciever, const char* slot )
00783 {
00784   if ( !reciever || !slot )
00785     return false;
00786 
00787   signal()->disconnect( signal(), sig, reciever, slot );
00788   return signal()->connect( signal(), sig, reciever, slot );
00789 }
00790 
00798 bool SUIT_DataObject::disconnect( const char* sig, QObject* reciever, const char* slot )
00799 {
00800   if ( !reciever || !slot )
00801     return false;
00802   return signal()->disconnect( signal(), sig, reciever, slot );
00803 }
00804 
00814 void SUIT_DataObject::deleteLater()
00815 {
00816   if ( parent() )
00817     parent()->removeChild( this, false ); // to avoid infinite loop!
00818   signal()->deleteLater( this );
00819 }
00820 
00825 void SUIT_DataObject::dump( const int indent ) const
00826 {
00827   QString strIndent = QString().fill( ' ', indent ); // indentation string
00828   printf( "%s%s\n", strIndent.toLatin1().data(), name().toLatin1().data() );
00829   for ( DataObjectList::const_iterator it = myChildren.begin(); it != myChildren.end(); ++it )
00830     (*it)->dump( indent + 2 );
00831 }
00832 
00863 SUIT_DataObject::Signal::Signal()
00864 : QObject()
00865 {
00866 }
00867 
00873 SUIT_DataObject::Signal::~Signal()
00874 {
00875   for ( DataObjectList::Iterator it = myDelLaterObjects.begin();
00876         it != myDelLaterObjects.end(); ++it ) {
00877     delete *it;
00878   }
00879   myDelLaterObjects.clear();
00880 }
00881 
00886 void SUIT_DataObject::Signal::emitCreated( SUIT_DataObject* object )
00887 {
00888   if ( object )
00889     emit created( object );
00890 }
00891 
00896 void SUIT_DataObject::Signal::emitDestroyed( SUIT_DataObject* object )
00897 {
00898   if ( object ) {
00899     if ( myDelLaterObjects.contains( object ) )
00900       // object is being destroyed after calling deleteLater():
00901       // the signal has been already emitted from deleteLater()
00902       // we should avoid repeating of the object destroying from
00903       // the Signal destructor
00904       myDelLaterObjects.removeAll( object );
00905     else
00906       // object is being destroyed directly or via deleteLater()
00907       emit destroyed( object );
00908   }
00909 }
00910 
00916 void SUIT_DataObject::Signal::emitInserted( SUIT_DataObject* object, SUIT_DataObject* parent )
00917 {
00918   emit( inserted( object, parent ) );
00919 }
00920 
00926 void SUIT_DataObject::Signal::emitRemoved( SUIT_DataObject* object, SUIT_DataObject* parent )
00927 {
00928   emit( removed( object, parent ) );
00929 }
00930 
00935 void SUIT_DataObject::Signal::deleteLater( SUIT_DataObject* object )
00936 {
00937   if ( !myDelLaterObjects.contains( object ) ) {
00938     emitDestroyed( object );
00939     myDelLaterObjects.append( object );
00940   }
00941 }
00942 
00946 void SUIT_DataObject::update()
00947 {
00948 }
00949 
00960 int SUIT_DataObject::groupId() const
00961 {
00962   return 0;
00963 }
00967 QVariant SUIT_DataObject::customData(Qtx::CustomDataType /*type*/) {
00968   return QVariant();
00969 }