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