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