Salome HOME
PAL10670 - it is necessary to increase speed of selection and popup construction...
[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
41 /*!
42   Destructor
43 */
44 LightApp_OBSelector::~LightApp_OBSelector()
45 {
46 }
47
48 /*!
49   Gets browser.
50 */
51 OB_Browser* LightApp_OBSelector::browser() const
52 {
53   return myBrowser;
54 }
55
56 /*!
57   Gets selection.
58 */
59 void LightApp_OBSelector::getSelection( SUIT_DataOwnerPtrList& theList ) const
60 {
61   if (mySelectedList.count() == 0 ) {
62     if ( !myBrowser )
63       return;
64     DataObjectList objlist;
65     myBrowser->getSelected( objlist );
66     LightApp_OBSelector* that = (LightApp_OBSelector*)this;
67     for ( DataObjectListIterator it( objlist ); it.current(); ++it )
68     {
69       LightApp_DataObject* obj = dynamic_cast<LightApp_DataObject*>( it.current() );
70       if ( obj )
71       {
72         Handle(SALOME_InteractiveObject) aSObj = new SALOME_InteractiveObject
73           ( obj->entry(), obj->componentDataType(), obj->name() );
74         LightApp_DataOwner* owner = new LightApp_DataOwner( aSObj  );
75         that->mySelectedList.append( SUIT_DataOwnerPtr( owner ) );
76       }
77     }
78   }
79   theList = mySelectedList;
80 }
81
82 /*!Sets selection.*/
83 void LightApp_OBSelector::setSelection( const SUIT_DataOwnerPtrList& theList )
84 {
85   if ( !myBrowser )
86     return;
87
88   QMap<QString, LightApp_DataObject*> themap;
89   fillEntries( themap );
90
91   DataObjectList objList;
92   for ( SUIT_DataOwnerPtrList::const_iterator it = theList.begin(); it != theList.end(); ++it )
93   {
94     const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*it).operator->() );
95     if ( owner && themap.contains( owner->entry() ) )
96       objList.append( themap[owner->entry()] );
97   }
98
99   myBrowser->setSelected( objList );
100 }
101
102 /*!On selection changed.*/
103 void LightApp_OBSelector::onSelectionChanged()
104 {
105   QTime t1 = QTime::currentTime();
106   mySelectedList.clear();
107   selectionChanged();
108   QTime t2 = QTime::currentTime();
109   qDebug( QString( "selection time = %1 msecs" ).arg( t1.msecsTo( t2 ) ) );
110 }
111
112 /*!Fill entries.*/
113 void LightApp_OBSelector::fillEntries( QMap<QString, LightApp_DataObject*>& entires )
114 {
115   entires.clear();
116
117   if ( !myBrowser )
118     return;
119
120   for ( SUIT_DataObjectIterator it( myBrowser->getRootObject(),
121                                     SUIT_DataObjectIterator::DepthLeft ); it.current(); ++it )
122   {
123     LightApp_DataObject* obj = dynamic_cast<LightApp_DataObject*>( it.current() );
124     if ( obj )
125       entires.insert( obj->entry(), obj );
126   }
127 }