Salome HOME
Porting SMESH module to Qt 4
[modules/smesh.git] / src / StdMeshersGUI / StdMeshersGUI_ObjectReferenceParamWdg.cxx
1 // Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 //
4 // This library is free software; you can redistribute it and/or 
5 // modify it under the terms of the GNU Lesser General Public 
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License. 
8 //
9 // This library is distributed in the hope that it will be useful, 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details. 
13 //
14 // You should have received a copy of the GNU Lesser General Public 
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 // File   : StdMeshersGUI_ObjectReferenceParamWdg.cxx
21 // Author : Open CASCADE S.A.S.
22 //
23
24 // SMESH includes
25 #include "StdMeshersGUI_ObjectReferenceParamWdg.h"
26
27 #include <SMESHGUI.h>
28 #include <SMESHGUI_VTKUtils.h>
29 #include <SMESH_TypeFilter.hxx>
30
31 // SALOME GUI includes
32 #include <SUIT_ResourceMgr.h>
33 #include <LightApp_SelectionMgr.h>
34 #include <SVTK_ViewWindow.h>
35 #include <SALOME_ListIO.hxx>
36
37 // SALOME KERNEL incldues
38 #include <SALOMEDSClient_SObject.hxx>
39
40 // Qt includes
41 #include <QPushButton>
42 #include <QLineEdit>
43 #include <QHBoxLayout>
44
45 #define SPACING 6
46
47 //================================================================================
48 /*!
49  * \brief Constructor initialized by filter
50   * \param f - object filter
51  */
52 //================================================================================
53
54 StdMeshersGUI_ObjectReferenceParamWdg::StdMeshersGUI_ObjectReferenceParamWdg
55 ( SUIT_SelectionFilter* f, QWidget* parent)
56   : QWidget( parent )
57 {
58   myFilter = f;
59   init();
60 }
61
62 //================================================================================
63 /*!
64  * \brief Constructor initialized by object type
65   * \param MeshObjectType - type of object to select
66  */
67 //================================================================================
68
69 StdMeshersGUI_ObjectReferenceParamWdg::StdMeshersGUI_ObjectReferenceParamWdg
70 ( MeshObjectType objType, QWidget* parent )
71   : QWidget( parent )
72 {
73   myFilter = new SMESH_TypeFilter( objType );
74   init();
75 }
76 //================================================================================
77 /*!
78  * \brief Destructor
79  */
80 //================================================================================
81
82 StdMeshersGUI_ObjectReferenceParamWdg::~StdMeshersGUI_ObjectReferenceParamWdg()
83 {
84   if ( myFilter )
85     delete myFilter;
86 }
87
88
89 //================================================================================
90 /*!
91  * \brief Create a leayout, initialize fields
92  */
93 //================================================================================
94
95 void StdMeshersGUI_ObjectReferenceParamWdg::init()
96 {
97   QHBoxLayout* aHBox = new QHBoxLayout(this);
98
99   aHBox->setMargin(0);
100   aHBox->setSpacing(SPACING);
101
102   mySMESHGUI     = SMESHGUI::GetSMESHGUI();
103   mySelectionMgr = SMESH::GetSelectionMgr( mySMESHGUI );
104   mySelectionActivated = false;
105   myParamValue = "";
106
107   SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( mySMESHGUI );
108   QPixmap iconSlct ( mgr->loadPixmap("SMESH", tr("ICON_SELECT")));
109
110   mySelButton = new QPushButton(this);
111   mySelButton->setIcon(iconSlct);
112   mySelButton->setCheckable( true );
113
114   myObjNameLineEdit = new QLineEdit(this);
115   myObjNameLineEdit->setReadOnly(true);
116
117   aHBox->addWidget( mySelButton );
118   aHBox->addWidget( myObjNameLineEdit );
119   aHBox->addStretch();
120
121   connect( mySelButton, SIGNAL(clicked()), SLOT(activateSelection()));
122 }
123
124 //================================================================================
125 /*!
126  * \brief SLOT: Installs selection filter that is not done automatically
127  */
128 //================================================================================
129
130 void StdMeshersGUI_ObjectReferenceParamWdg::activateSelection()
131 {
132   if ( !mySelectionActivated && mySelectionMgr )
133   {
134     mySelectionActivated = true;
135     mySelectionMgr->clearFilters();
136     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
137       aViewWindow->SetSelectionMode(ActorSelection);
138     if ( myFilter )
139       mySelectionMgr->installFilter( myFilter );
140     connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
141   }
142   emit selectionActivated();
143
144   mySelButton->setChecked( mySelectionActivated );
145 }
146
147 //================================================================================
148 /*!
149  * \brief SLOT: stop treating selection changes
150  */
151 //================================================================================
152
153 void StdMeshersGUI_ObjectReferenceParamWdg::deactivateSelection()
154 {
155   mySelectionActivated = false;
156   disconnect(mySelectionMgr, 0, this, 0 );
157   mySelectionMgr->removeFilter( myFilter );
158
159   mySelButton->setChecked( mySelectionActivated );
160 }
161
162 //================================================================================
163 /*!
164  * \brief Connect selection slots
165   * \param other - another StdMeshersGUI_ObjectReferenceParamWdg
166  */
167 //================================================================================
168
169 void StdMeshersGUI_ObjectReferenceParamWdg::AvoidSimultaneousSelection
170                                    ( StdMeshersGUI_ObjectReferenceParamWdg* other)
171 {
172   connect(other, SIGNAL(selectionActivated()), this, SLOT(deactivateSelection()));
173   connect(this, SIGNAL(selectionActivated()), other, SLOT(deactivateSelection()));
174 }
175
176 //================================================================================
177 /*!
178  * \brief Initialize selected object name
179  * \param obj - the current object
180  */
181 //================================================================================
182
183 void StdMeshersGUI_ObjectReferenceParamWdg::SetObject(CORBA::Object_ptr obj)
184 {
185   myObject = CORBA::Object::_nil();
186   myObjNameLineEdit->setText( "" );
187   myParamValue = "";
188
189   _PTR(SObject) sobj;
190   if ( !CORBA::is_nil( obj ))
191     sobj = SMESH::FindSObject (obj);
192   if ( sobj ) {
193     string name = sobj->GetName();
194     myObjNameLineEdit->setText( name.c_str() );
195     myObject = CORBA::Object::_duplicate( obj );
196     myParamValue = sobj->GetID().c_str();
197   }
198 }
199
200 //================================================================================
201 /*!
202  * \brief Takes selected object
203  */
204 //================================================================================
205
206 void StdMeshersGUI_ObjectReferenceParamWdg::onSelectionDone()
207 {
208   if ( mySelectionActivated ) {
209     CORBA::Object_var obj;
210     SALOME_ListIO aList;
211     mySelectionMgr->selectedObjects(aList);
212     if (aList.Extent() == 1)
213       obj = SMESH::IObjectToObject( aList.First() );
214     SetObject( obj.in() );
215   }
216 }