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