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