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