Salome HOME
\ No newline at end of file
[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   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   aHBox->addStretch();
130
131   connect( mySelButton, SIGNAL(clicked()), SLOT(activateSelection()));
132 }
133
134 //================================================================================
135 /*!
136  * \brief SLOT: Installs selection filter that is not done automatically
137  */
138 //================================================================================
139
140 void StdMeshersGUI_ObjectReferenceParamWdg::activateSelection()
141 {
142   if ( !mySelectionActivated && mySelectionMgr )
143   {
144     mySelectionActivated = true;
145     mySelectionMgr->clearFilters();
146     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
147       aViewWindow->SetSelectionMode(ActorSelection);
148     if ( myFilter )
149       mySelectionMgr->installFilter( myFilter );
150     connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
151   }
152   emit selectionActivated();
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( name.c_str() );
206     myObjNameLineEdit->setStyleSheet("");
207     myObjects.push_back( CORBA::Object::_duplicate( obj ));
208     myParamValue = sobj->GetID().c_str();
209     emit contentModified();
210   }
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 += name.c_str();
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 }