]> SALOME platform Git repositories - modules/gui.git/blob - src/LightApp/LightApp_OBSelector.cxx
Salome HOME
ede0e004c088ba36e5e91609b5dddbe20326069c
[modules/gui.git] / src / LightApp / LightApp_OBSelector.cxx
1 //  Copyright (C) 2007-2008  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.
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 // File   : LightApp_OBSelector.cxx
23 // Author :
24 //
25 #include "LightApp_OBSelector.h"
26
27 #include "LightApp_DataOwner.h"
28 #include "LightApp_DataObject.h"
29 #include "LightApp_Application.h"
30 #include <SUIT_DataBrowser.h>
31 #include <SUIT_Session.h>
32 #include <SUIT_DataObjectIterator.h>
33 #include <QTime>
34 #include <time.h>
35
36 /*!
37   \class LightApp_OBSelector
38   \brief Object browser selection handler class.
39 */
40
41 /*!
42   \brief Constructor.
43   \param ob object browser
44   \param mgr selection manager
45 */
46 LightApp_OBSelector::LightApp_OBSelector( SUIT_DataBrowser* ob, SUIT_SelectionMgr* mgr )
47 : SUIT_Selector( mgr, ob ),
48   myBrowser( ob )
49 {
50   if ( myBrowser ) {
51     connect( myBrowser, SIGNAL( selectionChanged() ), this, SLOT( onSelectionChanged() ) );
52   }
53   setModified();
54 }
55
56 /*!
57   \brief Destructor.
58 */
59 LightApp_OBSelector::~LightApp_OBSelector()
60 {
61 }
62
63 /*!
64   \brief Get object browser.
65   \return a pointer to the object browser
66 */
67 SUIT_DataBrowser* LightApp_OBSelector::browser() const
68 {
69   return myBrowser;
70 }
71
72 /*!
73   \brief Get selector type.
74   \return selector type
75 */
76 QString LightApp_OBSelector::type() const
77
78   return "ObjectBrowser"; 
79 }
80
81 /*!
82   \brief Get the time of the last selection changing.
83   \return latest selection changing time
84 */
85 unsigned long LightApp_OBSelector::getModifiedTime() const
86 {
87   return myModifiedTime;
88 }
89
90 /*!
91   \brief Update the time of the latest selection changing.
92 */
93 void LightApp_OBSelector::setModified()
94 {
95   myModifiedTime = clock();
96 }
97
98 /*!
99   \brief Called when the Object browser selection is changed.
100 */
101 void LightApp_OBSelector::onSelectionChanged()
102 {
103   QTime t1 = QTime::currentTime();
104   mySelectedList.clear();
105   selectionChanged();
106   QTime t2 = QTime::currentTime();
107   qDebug( QString( "selection time = %1 msecs" ).arg( t1.msecsTo( t2 ) ).toLatin1().constData() );
108 }
109
110 /*!
111   \brief Get list of currently selected objects.
112   \param theList list to be filled with the selected objects owners
113 */
114 void LightApp_OBSelector::getSelection( SUIT_DataOwnerPtrList& theList ) const
115 {
116   if ( mySelectedList.count() == 0 ) {
117     SUIT_Session* session = SUIT_Session::session();
118     SUIT_Application* sapp = session ? session->activeApplication() : 0;
119     LightApp_Application* app = dynamic_cast<LightApp_Application*>( sapp );
120     if( !app || !myBrowser )
121       return;
122
123     DataObjectList objlist;
124     myBrowser->getSelected( objlist );
125     LightApp_OBSelector* that = (LightApp_OBSelector*)this;
126     QListIterator<SUIT_DataObject*> it( objlist );
127     while ( it.hasNext() ) {
128       LightApp_DataObject* obj = dynamic_cast<LightApp_DataObject*>( it.next() );
129       if ( obj && app->checkDataObject( obj) ) {
130 #ifndef DISABLE_SALOMEOBJECT
131         Handle(SALOME_InteractiveObject) aSObj = new SALOME_InteractiveObject
132           ( obj->entry().toLatin1().constData(),
133             obj->componentDataType().toLatin1().constData(),
134             obj->name().toLatin1().constData() );
135         LightApp_DataOwner* owner = new LightApp_DataOwner( aSObj  );
136 #else
137         LightApp_DataOwner* owner = new LightApp_DataOwner( obj->entry() );
138 #endif
139         that->mySelectedList.append( SUIT_DataOwnerPtr( owner ) );
140       }
141     }
142   }
143   theList = mySelectedList;
144 }
145
146 /*!
147   \brief Set selection.
148   \param theList list of the object owners to be set selected
149 */
150 void LightApp_OBSelector::setSelection( const SUIT_DataOwnerPtrList& theList )
151 {
152   if ( !myBrowser )
153     return;
154
155   if( myEntries.count() == 0 || myModifiedTime < myBrowser->getModifiedTime() )
156     fillEntries( myEntries );
157
158   DataObjectList objList;
159   for ( SUIT_DataOwnerPtrList::const_iterator it = theList.begin(); 
160         it != theList.end(); ++it ) {
161     const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*it).operator->() );
162     if ( owner && myEntries.contains( owner->entry() ) )
163       objList.append( myEntries[owner->entry()] );
164   }
165
166   myBrowser->setSelected( objList );
167   mySelectedList.clear();
168 }
169
170 /*!
171   \brief Fill map of the data objects currently shown in the Object Browser.
172   \param entries map to be filled
173 */
174 void LightApp_OBSelector::fillEntries( QMap<QString, LightApp_DataObject*>& entries )
175 {
176   entries.clear();
177
178   if ( !myBrowser )
179     return;
180
181   for ( SUIT_DataObjectIterator it( myBrowser->root(),
182                                     SUIT_DataObjectIterator::DepthLeft ); it.current(); ++it ) {
183     LightApp_DataObject* obj = dynamic_cast<LightApp_DataObject*>( it.current() );
184     if ( obj )
185       entries.insert( obj->entry(), obj );
186   }
187
188   setModified();
189 }
190