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