Salome HOME
Merge 'oscar/porting_to_V9' branch.
[modules/gui.git] / src / LightApp / LightApp_OBSelector.cxx
1 // Copyright (C) 2007-2019  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File   : LightApp_OBSelector.cxx
24 // Author :
25 //
26 #include "LightApp_OBSelector.h"
27
28 #include "LightApp_DataOwner.h"
29 #include "LightApp_DataObject.h"
30 #include "LightApp_Application.h"
31 #include <SUIT_DataBrowser.h>
32 #include <SUIT_Session.h>
33 #include <SUIT_DataObjectIterator.h>
34 #include <QTime>
35 #include <time.h>
36
37 /*!
38   \class LightApp_OBSelector
39   \brief Object browser selection handler class.
40 */
41
42 /*!
43   \brief Constructor.
44   \param ob object browser
45   \param mgr selection manager
46 */
47 LightApp_OBSelector::LightApp_OBSelector( SUIT_DataBrowser* ob, SUIT_SelectionMgr* mgr )
48 : SUIT_Selector( mgr, ob ),
49   myBrowser( ob )
50 {
51   if ( myBrowser ) {
52     connect( myBrowser, SIGNAL( selectionChanged() ), this, SLOT( onSelectionChanged() ) );
53   }
54   setModified();
55 }
56
57 /*!
58   \brief Destructor.
59 */
60 LightApp_OBSelector::~LightApp_OBSelector()
61 {
62 }
63
64 /*!
65   \brief Get object browser.
66   \return a pointer to the object browser
67 */
68 SUIT_DataBrowser* LightApp_OBSelector::browser() const
69 {
70   return myBrowser;
71 }
72
73 /*!
74   \brief Get selector type.
75   \return selector type
76 */
77 QString LightApp_OBSelector::type() const
78
79   return "ObjectBrowser"; 
80 }
81
82 /*!
83   \brief Get the time of the last selection changing.
84   \return latest selection changing time
85 */
86 unsigned long LightApp_OBSelector::getModifiedTime() const
87 {
88   return myModifiedTime;
89 }
90
91 /*!
92   \brief Update the time of the latest selection changing.
93 */
94 void LightApp_OBSelector::setModified()
95 {
96   myModifiedTime = clock();
97 }
98
99 /*!
100   \brief Called when the Object browser selection is changed.
101 */
102 void LightApp_OBSelector::onSelectionChanged()
103 {
104   QTime t1 = QTime::currentTime();
105   mySelectedList.clear();
106   selectionChanged();
107   QTime t2 = QTime::currentTime();
108 #ifdef _DEBUG_
109   qDebug( QString( "selection time = %1 msecs" ).arg( t1.msecsTo( t2 ) ).toLatin1().constData() );
110 #endif
111 }
112
113 /*!
114   \brief Get list of currently selected objects.
115   \param theList list to be filled with the selected objects owners
116 */
117 void LightApp_OBSelector::getSelection( SUIT_DataOwnerPtrList& theList ) const
118 {
119   if ( mySelectedList.count() == 0 ) {
120     SUIT_Session* session = SUIT_Session::session();
121     SUIT_Application* sapp = session ? session->activeApplication() : 0;
122     LightApp_Application* app = dynamic_cast<LightApp_Application*>( sapp );
123     if( !app || !myBrowser )
124       return;
125
126     DataObjectList objlist;
127     myBrowser->getSelected( objlist );
128     LightApp_OBSelector* that = (LightApp_OBSelector*)this;
129     QListIterator<SUIT_DataObject*> it( objlist );
130     while ( it.hasNext() ) {
131       LightApp_DataObject* obj = dynamic_cast<LightApp_DataObject*>( it.next() );
132       if ( obj && app->checkDataObject( obj) ) {
133 #ifndef DISABLE_SALOMEOBJECT
134         Handle(SALOME_InteractiveObject) aSObj = new SALOME_InteractiveObject
135           ( obj->entry().toUtf8().constData(),
136             obj->componentDataType().toLatin1().constData(),
137             obj->name().toUtf8().constData() );
138         LightApp_DataOwner* owner = new LightApp_DataOwner( aSObj  );
139 #else
140         LightApp_DataOwner* owner = new LightApp_DataOwner( obj->entry() );
141 #endif
142         that->mySelectedList.append( SUIT_DataOwnerPtr( owner ) );
143       }
144     }
145   }
146   theList = mySelectedList;
147 }
148
149 /*!
150   \brief Set selection.
151   \param theList list of the object owners to be set selected
152 */
153 void LightApp_OBSelector::setSelection( const SUIT_DataOwnerPtrList& theList )
154 {
155   if ( !myBrowser )
156     return;
157
158   if( myEntries.count() == 0 || myModifiedTime < myBrowser->getModifiedTime() )
159     fillEntries( myEntries );
160
161   DataObjectList objList;
162   for ( SUIT_DataOwnerPtrList::const_iterator it = theList.begin(); 
163         it != theList.end(); ++it ) {
164     const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*it).operator->() );
165     if ( owner && myEntries.contains( owner->entry() ) )
166       objList.append( myEntries[owner->entry()] );
167   }
168
169   myBrowser->setSelected( objList );
170   mySelectedList.clear();
171 }
172
173 /*!
174   \brief Fill map of the data objects currently shown in the Object Browser.
175   \param entries map to be filled
176 */
177 void LightApp_OBSelector::fillEntries( QMap<QString, LightApp_DataObject*>& entries )
178 {
179   entries.clear();
180
181   if ( !myBrowser )
182     return;
183
184   for ( SUIT_DataObjectIterator it( myBrowser->root(),
185                                     SUIT_DataObjectIterator::DepthLeft ); it.current(); ++it ) {
186     LightApp_DataObject* obj = dynamic_cast<LightApp_DataObject*>( it.current() );
187     if ( obj )
188       entries.insert( obj->entry(), obj );
189   }
190
191   setModified();
192 }
193