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