Salome HOME
bc295796a39b85a6a4c1e3ee6908121a40de8933
[modules/gui.git] / src / SALOME_PYQT / SALOME_PYQT_GUILight / SALOME_PYQT_Selector.cxx
1 // Copyright (C) 2007-2022  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   myLocalEntries.clear();
55   mySelectedList.clear();
56   myEntries.clear();
57   myModifiedTime = 0;
58   if (myPyModule)
59     {
60       connect(myPyModule, SIGNAL(localSelectionChanged()), this, SLOT(onSelectionChanged()));
61     }
62   setModified();
63 }
64
65 /*!
66  \brief Destructor.
67  */
68 SALOME_PYQT_Selector::~SALOME_PYQT_Selector()
69 {
70   MESSAGE("destructor");
71 }
72
73 /*!
74  \brief Reset selection internally. Used on close study.
75  */
76 void SALOME_PYQT_Selector::clear()
77 {
78   MESSAGE("clear");
79   myLocalEntries.clear();
80   mySelectedList.clear();
81   myEntries.clear();
82 }
83
84 /*!
85  \brief Get Python module interface instance.
86  \return a pointer to the Python module interface instance.
87  */
88 SALOME_PYQT_ModuleLight* SALOME_PYQT_Selector::pyModule() const
89 {
90   MESSAGE("pyModule");
91   return myPyModule;
92 }
93
94 /*!
95  \brief Get selector type.
96  \return selector type
97  */
98 QString SALOME_PYQT_Selector::type() const
99 {
100   MESSAGE("type");
101   return "PYQT_Module";
102 }
103
104 /*!
105  \brief Get the time of the last selection changing.
106  \return latest selection changing time
107  */
108 unsigned long SALOME_PYQT_Selector::getModifiedTime() const
109 {
110   MESSAGE("getModifiedTime");
111   return myModifiedTime;
112 }
113
114 /*!
115  \brief Update the time of the latest selection changing.
116  */
117 void SALOME_PYQT_Selector::setModified()
118 {
119   MESSAGE("setModified");
120   myModifiedTime = clock();
121 }
122
123 /*!
124  \brief Called when the Object browser selection is changed.
125  */
126 void SALOME_PYQT_Selector::onSelectionChanged()
127 {
128   MESSAGE("onSelectionChanged");
129   QTime t1 = QTime::currentTime();
130   mySelectedList.clear();
131   selectionChanged();
132   QTime t2 = QTime::currentTime();
133   qDebug( QString( "selection time = %1 msecs" ).arg( t1.msecsTo( t2 ) ).toLatin1().constData());
134 }
135
136 /*!
137  \brief Get list of currently selected objects.
138  \param theList list to be filled with the selected objects owners
139  */
140 void SALOME_PYQT_Selector::getSelection(SUIT_DataOwnerPtrList& theList) const
141 {
142   MESSAGE("getSelection");
143   if (mySelectedList.count() == 0)
144     {
145       MESSAGE("mySelectedList.count(): " << mySelectedList.count() << " myLocalEntries.size(): "<< myLocalEntries.size());
146       SALOME_PYQT_Selector* that = (SALOME_PYQT_Selector*) this; // because of const...
147       for (int i = 0; i < myLocalEntries.size(); i++)
148         {
149           SALOME_PYQT_BorrowedDataObjectLight *obj = new SALOME_PYQT_BorrowedDataObjectLight(myLocalEntries[i]);
150 #ifndef DISABLE_SALOMEOBJECT
151           Handle(SALOME_InteractiveObject)aSObj = new SALOME_InteractiveObject
152           ( obj->entry().toUtf8().constData(),
153               obj->componentDataType().toLatin1().constData(),
154               obj->name().toUtf8().constData() );
155           LightApp_DataOwner* owner = new LightApp_DataOwner(aSObj);
156 #else
157           LightApp_DataOwner* owner = new LightApp_DataOwner( obj->entry() );
158 #endif
159           that->mySelectedList.append(SUIT_DataOwnerPtr(owner));
160         }
161     }
162   theList = mySelectedList;
163 }
164
165 /*!
166  \brief Set selection.
167  \param theList list of the object owners to be set selected
168  */
169 void SALOME_PYQT_Selector::setSelection(const SUIT_DataOwnerPtrList& theList)
170 {
171   MESSAGE("setSelection");
172   if (!myPyModule)
173     return;
174
175   myEntries.clear();
176   for (SUIT_DataOwnerPtrList::const_iterator it = theList.begin(); it != theList.end(); ++it)
177     {
178       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>((*it).operator->());
179       if (owner)
180         {
181           QString entry = owner->entry();
182           myEntries.append(entry);
183           MESSAGE("Selected: "<< entry.toStdString());
184         }
185     }
186   fillEntries(myEntries);
187   mySelectedList.clear();
188 }
189
190 /*!
191  \brief Fill map of the data objects currently shown in the Object Browser.
192  \param entries map to be filled
193  */
194 void SALOME_PYQT_Selector::fillEntries(QStringList& entries)
195 {
196   MESSAGE("fillEntries");
197   if (!myPyModule)
198     return;
199   myPyModule->setSelected(entries);
200   setModified();
201 }
202