Salome HOME
Merge from OCC_development_generic_2006
[modules/gui.git] / src / LightApp / LightApp_OBSelector.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
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.
7 // 
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/
18 //
19 #include "LightApp_OBSelector.h"
20
21 #include "LightApp_DataOwner.h"
22 #include "LightApp_DataObject.h"
23
24 #include <OB_Browser.h>
25
26 #include <SUIT_DataObjectIterator.h>
27 #include <qdatetime.h>
28
29 /*!
30   Constructor
31 */
32 LightApp_OBSelector::LightApp_OBSelector( OB_Browser* ob, SUIT_SelectionMgr* mgr )
33 : SUIT_Selector( mgr, ob ),
34   myBrowser( ob )
35 {
36   if ( myBrowser ) {
37     connect( myBrowser, SIGNAL( selectionChanged() ), this, SLOT( onSelectionChanged() ) );
38   }    
39
40   setModified();
41 }
42
43 /*!
44   Destructor
45 */
46 LightApp_OBSelector::~LightApp_OBSelector()
47 {
48 }
49
50 /*!
51   Gets browser.
52 */
53 OB_Browser* LightApp_OBSelector::browser() const
54 {
55   return myBrowser;
56 }
57
58 /*!
59   Gets selection.
60 */
61 void LightApp_OBSelector::getSelection( SUIT_DataOwnerPtrList& theList ) const
62 {
63   if (mySelectedList.count() == 0 ) {
64     if ( !myBrowser )
65       return;
66     DataObjectList objlist;
67     myBrowser->getSelected( objlist );
68     LightApp_OBSelector* that = (LightApp_OBSelector*)this;
69     for ( DataObjectListIterator it( objlist ); it.current(); ++it )
70     {
71       LightApp_DataObject* obj = dynamic_cast<LightApp_DataObject*>( it.current() );
72       if ( obj )
73       {
74         Handle(SALOME_InteractiveObject) aSObj = new SALOME_InteractiveObject
75           ( obj->entry(), obj->componentDataType(), obj->name() );
76         LightApp_DataOwner* owner = new LightApp_DataOwner( aSObj  );
77         that->mySelectedList.append( SUIT_DataOwnerPtr( owner ) );
78       }
79     }
80   }
81   theList = mySelectedList;
82 }
83
84 /*!Sets selection.*/
85 void LightApp_OBSelector::setSelection( const SUIT_DataOwnerPtrList& theList )
86 {
87   if ( !myBrowser )
88     return;
89
90   if( myEntries.count() == 0 ||
91       myModifiedTime < myBrowser->getModifiedTime() )
92     fillEntries( myEntries );
93
94   DataObjectList objList;
95   for ( SUIT_DataOwnerPtrList::const_iterator it = theList.begin(); it != theList.end(); ++it )
96   {
97     const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*it).operator->() );
98     if ( owner && myEntries.contains( owner->entry() ) )
99       objList.append( myEntries[owner->entry()] );
100   }
101
102   myBrowser->setSelected( objList );
103 }
104
105 /*!On selection changed.*/
106 void LightApp_OBSelector::onSelectionChanged()
107 {
108   QTime t1 = QTime::currentTime();
109   mySelectedList.clear();
110   selectionChanged();
111   QTime t2 = QTime::currentTime();
112   qDebug( QString( "selection time = %1 msecs" ).arg( t1.msecsTo( t2 ) ) );
113 }
114
115 /*!Fill entries.*/
116 void LightApp_OBSelector::fillEntries( QMap<QString, LightApp_DataObject*>& entires )
117 {
118   entires.clear();
119
120   if ( !myBrowser )
121     return;
122
123   for ( SUIT_DataObjectIterator it( myBrowser->getRootObject(),
124                                     SUIT_DataObjectIterator::DepthLeft ); it.current(); ++it )
125   {
126     LightApp_DataObject* obj = dynamic_cast<LightApp_DataObject*>( it.current() );
127     if ( obj )
128       entires.insert( obj->entry(), obj );
129   }
130
131   setModified();
132 }
133
134 /*!Update modified time.*/
135 void LightApp_OBSelector::setModified()
136 {
137   myModifiedTime = clock();
138 }