Salome HOME
correct previous integration (Porting to Python 2.6)
[modules/smesh.git] / src / StdMeshersGUI / StdMeshersGUI_ObjectReferenceParamWdg.cxx
1 //  Copyright (C) 2007-2008  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 // File   : StdMeshersGUI_ObjectReferenceParamWdg.cxx
23 // Author : Open CASCADE S.A.S.
24 // SMESH includes
25 //
26 #include "StdMeshersGUI_ObjectReferenceParamWdg.h"
27
28 #include <SMESHGUI.h>
29 #include <SMESHGUI_VTKUtils.h>
30 #include <SMESH_TypeFilter.hxx>
31
32 // SALOME GUI includes
33 #include <SUIT_ResourceMgr.h>
34 #include <LightApp_SelectionMgr.h>
35 #include <SVTK_ViewWindow.h>
36 #include <SALOME_ListIO.hxx>
37
38 // SALOME KERNEL incldues
39 #include <SALOMEDSClient_SObject.hxx>
40
41 // Qt includes
42 #include <QPushButton>
43 #include <QLineEdit>
44 #include <QHBoxLayout>
45
46 #define SPACING 6
47
48 //================================================================================
49 /*!
50  * \brief Constructor initialized by filter
51   * \param f - object filter
52  */
53 //================================================================================
54
55 StdMeshersGUI_ObjectReferenceParamWdg::StdMeshersGUI_ObjectReferenceParamWdg
56 ( SUIT_SelectionFilter* f, QWidget* parent)
57   : QWidget( parent )
58 {
59   myFilter = f;
60   init();
61 }
62
63 //================================================================================
64 /*!
65  * \brief Constructor initialized by object type
66   * \param MeshObjectType - type of object to select
67  */
68 //================================================================================
69
70 StdMeshersGUI_ObjectReferenceParamWdg::StdMeshersGUI_ObjectReferenceParamWdg
71 ( MeshObjectType objType, QWidget* parent )
72   : QWidget( parent )
73 {
74   myFilter = new SMESH_TypeFilter( objType );
75   init();
76 }
77 //================================================================================
78 /*!
79  * \brief Destructor
80  */
81 //================================================================================
82
83 StdMeshersGUI_ObjectReferenceParamWdg::~StdMeshersGUI_ObjectReferenceParamWdg()
84 {
85   if ( myFilter )
86     delete myFilter;
87 }
88
89
90 //================================================================================
91 /*!
92  * \brief Create a leayout, initialize fields
93  */
94 //================================================================================
95
96 void StdMeshersGUI_ObjectReferenceParamWdg::init()
97 {
98   QHBoxLayout* aHBox = new QHBoxLayout(this);
99
100   aHBox->setMargin(0);
101   aHBox->setSpacing(SPACING);
102
103   mySMESHGUI     = SMESHGUI::GetSMESHGUI();
104   mySelectionMgr = SMESH::GetSelectionMgr( mySMESHGUI );
105   mySelectionActivated = false;
106   myParamValue = "";
107
108   SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( mySMESHGUI );
109   QPixmap iconSlct ( mgr->loadPixmap("SMESH", tr("ICON_SELECT")));
110
111   mySelButton = new QPushButton(this);
112   mySelButton->setIcon(iconSlct);
113   mySelButton->setCheckable( true );
114
115   myObjNameLineEdit = new QLineEdit(this);
116   myObjNameLineEdit->setReadOnly(true);
117
118   aHBox->addWidget( mySelButton );
119   aHBox->addWidget( myObjNameLineEdit );
120   aHBox->addStretch();
121
122   connect( mySelButton, SIGNAL(clicked()), SLOT(activateSelection()));
123 }
124
125 //================================================================================
126 /*!
127  * \brief SLOT: Installs selection filter that is not done automatically
128  */
129 //================================================================================
130
131 void StdMeshersGUI_ObjectReferenceParamWdg::activateSelection()
132 {
133   if ( !mySelectionActivated && mySelectionMgr )
134   {
135     mySelectionActivated = true;
136     mySelectionMgr->clearFilters();
137     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
138       aViewWindow->SetSelectionMode(ActorSelection);
139     if ( myFilter )
140       mySelectionMgr->installFilter( myFilter );
141     connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
142   }
143   emit selectionActivated();
144
145   mySelButton->setChecked( mySelectionActivated );
146 }
147
148 //================================================================================
149 /*!
150  * \brief SLOT: stop treating selection changes
151  */
152 //================================================================================
153
154 void StdMeshersGUI_ObjectReferenceParamWdg::deactivateSelection()
155 {
156   mySelectionActivated = false;
157   disconnect(mySelectionMgr, 0, this, 0 );
158   mySelectionMgr->removeFilter( myFilter );
159
160   mySelButton->setChecked( mySelectionActivated );
161 }
162
163 //================================================================================
164 /*!
165  * \brief Connect selection slots
166   * \param other - another StdMeshersGUI_ObjectReferenceParamWdg
167  */
168 //================================================================================
169
170 void StdMeshersGUI_ObjectReferenceParamWdg::AvoidSimultaneousSelection
171                                    ( StdMeshersGUI_ObjectReferenceParamWdg* other)
172 {
173   connect(other, SIGNAL(selectionActivated()), this, SLOT(deactivateSelection()));
174   connect(this, SIGNAL(selectionActivated()), other, SLOT(deactivateSelection()));
175 }
176
177 //================================================================================
178 /*!
179  * \brief Initialize selected object name
180  * \param obj - the current object
181  */
182 //================================================================================
183
184 void StdMeshersGUI_ObjectReferenceParamWdg::SetObject(CORBA::Object_ptr obj)
185 {
186   myObject = CORBA::Object::_nil();
187   myObjNameLineEdit->setText( "" );
188   myParamValue = "";
189
190   _PTR(SObject) sobj;
191   if ( !CORBA::is_nil( obj ))
192     sobj = SMESH::FindSObject (obj);
193   if ( sobj ) {
194     std::string name = sobj->GetName();
195     myObjNameLineEdit->setText( name.c_str() );
196     myObject = CORBA::Object::_duplicate( obj );
197     myParamValue = sobj->GetID().c_str();
198   }
199 }
200
201 //================================================================================
202 /*!
203  * \brief Takes selected object
204  */
205 //================================================================================
206
207 void StdMeshersGUI_ObjectReferenceParamWdg::onSelectionDone()
208 {
209   if ( mySelectionActivated ) {
210     CORBA::Object_var obj;
211     SALOME_ListIO aList;
212     mySelectionMgr->selectedObjects(aList);
213     if (aList.Extent() == 1)
214       obj = SMESH::IObjectToObject( aList.First() );
215     SetObject( obj.in() );
216   }
217 }