Salome HOME
dc25d6e4fcbddfce344f99230a8cf504d29b425b
[modules/smesh.git] / src / StdMeshersGUI / StdMeshersGUI_ObjectReferenceParamWdg.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   : StdMeshersGUI_ObjectReferenceParamWdg.cxx
24 // Author : Open CASCADE S.A.S.
25 // SMESH includes
26 //
27 #include "StdMeshersGUI_ObjectReferenceParamWdg.h"
28
29 #include <SMESHGUI.h>
30 #include <SMESHGUI_VTKUtils.h>
31 #include <SMESH_TypeFilter.hxx>
32
33 // SALOME GUI includes
34 #include <SUIT_ResourceMgr.h>
35 #include <LightApp_SelectionMgr.h>
36 #include <SVTK_ViewWindow.h>
37 #include <SALOME_ListIO.hxx>
38
39 // SALOME KERNEL incldues
40 #include <SALOMEDSClient_SObject.hxx>
41 #include <SALOMEDSClient_Study.hxx>
42
43 // Qt includes
44 #include <QPushButton>
45 #include <QLineEdit>
46 #include <QHBoxLayout>
47
48 #define SPACING 6
49
50 //================================================================================
51 /*!
52  * \brief Constructor initialized by filter
53   * \param f - object filter
54  */
55 //================================================================================
56
57 StdMeshersGUI_ObjectReferenceParamWdg::StdMeshersGUI_ObjectReferenceParamWdg
58 ( SUIT_SelectionFilter* f, QWidget* parent, bool multiSelection 
59   : QWidget( parent ), myMultiSelection( multiSelection )
60 {
61   myFilter = f;
62   init();
63 }
64
65 //================================================================================
66 /*!
67  * \brief Constructor initialized by object type
68   * \param MeshObjectType - type of object to select
69  */
70 //================================================================================
71
72 StdMeshersGUI_ObjectReferenceParamWdg::StdMeshersGUI_ObjectReferenceParamWdg
73 ( SMESH::MeshObjectType objType, QWidget* parent, bool multiSelection )
74   : QWidget( parent ), myMultiSelection( multiSelection )
75 {
76   myFilter = new SMESH_TypeFilter( objType );
77   init();
78 }
79
80 //================================================================================
81 /*!
82  * \brief Destructor
83  */
84 //================================================================================
85
86 StdMeshersGUI_ObjectReferenceParamWdg::~StdMeshersGUI_ObjectReferenceParamWdg()
87 {
88   if ( myFilter )
89   {
90     mySelectionMgr->removeFilter( myFilter );
91     delete myFilter;
92   }
93 }
94
95 //================================================================================
96 /*!
97  * \brief Create a leayout, initialize fields
98  */
99 //================================================================================
100
101 void StdMeshersGUI_ObjectReferenceParamWdg::init()
102 {
103   QHBoxLayout* aHBox = new QHBoxLayout(this);
104
105   aHBox->setMargin(0);
106   aHBox->setSpacing(SPACING);
107
108   mySMESHGUI     = SMESHGUI::GetSMESHGUI();
109   mySelectionMgr = SMESH::GetSelectionMgr( mySMESHGUI );
110   mySelectionActivated = false;
111   myParamValue = "";
112   myEmptyText = "";
113   myEmptyStyleSheet ="";
114
115   SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( mySMESHGUI );
116   QPixmap iconSlct ( mgr->loadPixmap("SMESH", tr("ICON_SELECT")));
117
118   mySelButton = new QPushButton(this);
119   mySelButton->setIcon(iconSlct);
120   mySelButton->setCheckable( true );
121
122   myObjNameLineEdit = new QLineEdit(this);
123   myObjNameLineEdit->setReadOnly(true);
124   myObjNameLineEdit->setStyleSheet(myEmptyStyleSheet);
125   myObjNameLineEdit->setMinimumWidth(150);
126
127   aHBox->addWidget( mySelButton );
128   aHBox->addWidget( myObjNameLineEdit );
129
130   connect( mySelButton, SIGNAL(clicked()), SLOT(activateSelection()));
131 }
132
133 //================================================================================
134 /*!
135  * \brief SLOT: Installs selection filter that is not done automatically
136  */
137 //================================================================================
138
139 void StdMeshersGUI_ObjectReferenceParamWdg::activateSelection()
140 {
141   if ( !mySelectionActivated && mySelectionMgr )
142   {
143     mySelectionActivated = true;
144     mySelectionMgr->clearFilters();
145     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
146       aViewWindow->SetSelectionMode(ActorSelection);
147     if ( myFilter )
148       mySelectionMgr->installFilter( myFilter );
149     connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
150   }
151   emit selectionActivated();
152   onSelectionDone();
153
154   mySelButton->setChecked( mySelectionActivated );
155 }
156
157 //================================================================================
158 /*!
159  * \brief SLOT: stop treating selection changes
160  */
161 //================================================================================
162
163 void StdMeshersGUI_ObjectReferenceParamWdg::deactivateSelection()
164 {
165   mySelectionActivated = false;
166   disconnect(mySelectionMgr, 0, this, 0 );
167   mySelectionMgr->removeFilter( myFilter );
168
169   mySelButton->setChecked( mySelectionActivated );
170 }
171
172 //================================================================================
173 /*!
174  * \brief Connect selection slots
175   * \param other - another StdMeshersGUI_ObjectReferenceParamWdg
176  */
177 //================================================================================
178
179 void StdMeshersGUI_ObjectReferenceParamWdg::AvoidSimultaneousSelection
180                                    ( StdMeshersGUI_ObjectReferenceParamWdg* other)
181 {
182   connect(other, SIGNAL(selectionActivated()), this, SLOT(deactivateSelection()));
183   connect(this, SIGNAL(selectionActivated()), other, SLOT(deactivateSelection()));
184 }
185
186 //================================================================================
187 /*!
188  * \brief Initialize selected object name
189  * \param obj - the current object
190  */
191 //================================================================================
192
193 void StdMeshersGUI_ObjectReferenceParamWdg::SetObject(CORBA::Object_ptr obj)
194 {
195   myObjects.clear();
196   myObjNameLineEdit->setText( myEmptyText );
197   myObjNameLineEdit->setStyleSheet(myEmptyStyleSheet);
198   myParamValue = "";
199
200   _PTR(SObject) sobj;
201   if ( !CORBA::is_nil( obj ))
202     sobj = SMESH::FindSObject (obj);
203   if ( sobj ) {
204     std::string name = sobj->GetName();
205     myObjNameLineEdit->setText( QString( name.c_str() ).trimmed() );
206     myObjNameLineEdit->setStyleSheet("");
207     myObjects.push_back( CORBA::Object::_duplicate( obj ));
208     myParamValue = sobj->GetID().c_str();
209   }
210   emit contentModified();
211 }
212
213 //================================================================================
214 /*!
215  * \brief Initialize selected objects
216  * \param objects - entries of objects
217  */
218 //================================================================================
219
220 void StdMeshersGUI_ObjectReferenceParamWdg::SetObjects(SMESH::string_array_var& objects)
221 {
222   myObjects.clear();
223   myObjNameLineEdit->setText( myEmptyText );
224   myObjNameLineEdit->setStyleSheet(myEmptyStyleSheet);
225   myParamValue = "";
226   bool selChanged = false;
227   
228   for ( unsigned i = 0; i < objects->length(); ++i )
229   {
230     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
231     _PTR(SObject) aSObj = aStudy->FindObjectID(objects[i].in());
232     CORBA::Object_var anObj = SMESH::SObjectToObject(aSObj,aStudy);
233     if ( !CORBA::is_nil( anObj )) {
234       std::string name = aSObj->GetName();
235       QString text = myObjNameLineEdit->text();
236       if ( text != myEmptyText )
237         text += " ";
238       else
239         text = "";
240       text += QString( name.c_str() ).trimmed();
241       myObjNameLineEdit->setText( text );
242       myObjNameLineEdit->setStyleSheet("");
243       myObjects.push_back( anObj );
244       myParamValue += " ";
245       myParamValue += objects[i];
246       selChanged = true;
247     }
248   }
249   if (selChanged)
250     emit contentModified();
251 }
252
253 //================================================================================
254 /*!
255  * \brief Takes selected object
256  */
257 //================================================================================
258
259 void StdMeshersGUI_ObjectReferenceParamWdg::onSelectionDone()
260 {
261   if ( mySelectionActivated ) {
262     CORBA::Object_var obj;
263     SALOME_ListIO aList;
264     mySelectionMgr->selectedObjects(aList);
265     if (aList.Extent() == 1)
266     {
267       obj = SMESH::IObjectToObject( aList.First() );
268       SetObject( obj.in() );
269     }
270     else if (myMultiSelection)
271     {
272       SMESH::string_array_var objIds = new SMESH::string_array;
273       objIds->length( aList.Extent());
274       SALOME_ListIteratorOfListIO io( aList );
275       int i = 0;
276       for ( ; io.More(); io.Next(), ++i )
277       {
278         Handle(SALOME_InteractiveObject) anIO = io.Value();
279         if ( anIO->hasEntry() )
280           objIds[i] = anIO->getEntry();
281         else
282           i--;
283       }
284       objIds->length(i);
285       SetObjects( objIds );
286     }
287   }
288 }
289
290 void StdMeshersGUI_ObjectReferenceParamWdg::SetDefaultText(QString defaultText, QString styleSheet)
291 {
292   myEmptyText = defaultText;
293   myEmptyStyleSheet = styleSheet;
294   myObjNameLineEdit->setText( myEmptyText );
295   myObjNameLineEdit->setStyleSheet( myEmptyStyleSheet);
296 }