Salome HOME
Additional fix for the '23049: [CEA 1475] Increase the size of some dialog boxes...
[modules/smesh.git] / src / StdMeshersGUI / StdMeshersGUI_ObjectReferenceParamWdg.cxx
1 // Copyright (C) 2007-2015  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, or (at your option) any later version.
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 #include <SALOMEDSClient_Study.hxx>
42
43 // Qt includes
44 #include <QPushButton>
45 #include <QLineEdit>
46 #include <QHBoxLayout>
47
48 #define SPACING 6
49
50 //================================================================================
51 /*!
52  * \brief Constructor initialized by filter
53   * \param f - object filter
54  */
55 //================================================================================
56
57 StdMeshersGUI_ObjectReferenceParamWdg::StdMeshersGUI_ObjectReferenceParamWdg
58 ( SUIT_SelectionFilter* f, QWidget* parent, bool multiSelection 
59   /*, bool stretch */)
60   : QWidget( parent ), myMultiSelection( multiSelection )
61 {
62   myFilter = f;
63   // myStretchActivated = stretch;
64   init();
65 }
66
67 //================================================================================
68 /*!
69  * \brief Constructor initialized by object type
70   * \param MeshObjectType - type of object to select
71  */
72 //================================================================================
73
74 StdMeshersGUI_ObjectReferenceParamWdg::StdMeshersGUI_ObjectReferenceParamWdg
75 ( SMESH::MeshObjectType objType, QWidget* parent, bool multiSelection )
76   : QWidget( parent ), myMultiSelection( multiSelection )
77 {
78   myFilter = new SMESH_TypeFilter( objType );
79   init();
80 }
81 //================================================================================
82 /*!
83  * \brief Destructor
84  */
85 //================================================================================
86
87 StdMeshersGUI_ObjectReferenceParamWdg::~StdMeshersGUI_ObjectReferenceParamWdg()
88 {
89   if ( myFilter )
90   {
91     mySelectionMgr->removeFilter( myFilter );
92     delete myFilter;
93   }
94 }
95
96
97 //================================================================================
98 /*!
99  * \brief Create a leayout, initialize fields
100  */
101 //================================================================================
102
103 void StdMeshersGUI_ObjectReferenceParamWdg::init()
104 {
105   QHBoxLayout* aHBox = new QHBoxLayout(this);
106
107   aHBox->setMargin(0);
108   aHBox->setSpacing(SPACING);
109
110   mySMESHGUI     = SMESHGUI::GetSMESHGUI();
111   mySelectionMgr = SMESH::GetSelectionMgr( mySMESHGUI );
112   mySelectionActivated = false;
113   myParamValue = "";
114   myEmptyText = "";
115   myEmptyStyleSheet ="";
116
117   SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( mySMESHGUI );
118   QPixmap iconSlct ( mgr->loadPixmap("SMESH", tr("ICON_SELECT")));
119
120   mySelButton = new QPushButton(this);
121   mySelButton->setIcon(iconSlct);
122   mySelButton->setCheckable( true );
123
124   myObjNameLineEdit = new QLineEdit(this);
125   myObjNameLineEdit->setReadOnly(true);
126   myObjNameLineEdit->setStyleSheet(myEmptyStyleSheet);
127   myObjNameLineEdit->setMinimumWidth(150);
128
129   aHBox->addWidget( mySelButton );
130   aHBox->addWidget( myObjNameLineEdit );
131   //if (myStretchActivated){
132   //  aHBox->addStretch();
133   //}
134
135   connect( mySelButton, SIGNAL(clicked()), SLOT(activateSelection()));
136 }
137
138 //================================================================================
139 /*!
140  * \brief SLOT: Installs selection filter that is not done automatically
141  */
142 //================================================================================
143
144 void StdMeshersGUI_ObjectReferenceParamWdg::activateSelection()
145 {
146   if ( !mySelectionActivated && mySelectionMgr )
147   {
148     mySelectionActivated = true;
149     mySelectionMgr->clearFilters();
150     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
151       aViewWindow->SetSelectionMode(ActorSelection);
152     if ( myFilter )
153       mySelectionMgr->installFilter( myFilter );
154     connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
155   }
156   emit selectionActivated();
157   onSelectionDone();
158
159   mySelButton->setChecked( mySelectionActivated );
160 }
161
162 //================================================================================
163 /*!
164  * \brief SLOT: stop treating selection changes
165  */
166 //================================================================================
167
168 void StdMeshersGUI_ObjectReferenceParamWdg::deactivateSelection()
169 {
170   mySelectionActivated = false;
171   disconnect(mySelectionMgr, 0, this, 0 );
172   mySelectionMgr->removeFilter( myFilter );
173
174   mySelButton->setChecked( mySelectionActivated );
175 }
176
177 //================================================================================
178 /*!
179  * \brief Connect selection slots
180   * \param other - another StdMeshersGUI_ObjectReferenceParamWdg
181  */
182 //================================================================================
183
184 void StdMeshersGUI_ObjectReferenceParamWdg::AvoidSimultaneousSelection
185                                    ( StdMeshersGUI_ObjectReferenceParamWdg* other)
186 {
187   connect(other, SIGNAL(selectionActivated()), this, SLOT(deactivateSelection()));
188   connect(this, SIGNAL(selectionActivated()), other, SLOT(deactivateSelection()));
189 }
190
191 //================================================================================
192 /*!
193  * \brief Initialize selected object name
194  * \param obj - the current object
195  */
196 //================================================================================
197
198 void StdMeshersGUI_ObjectReferenceParamWdg::SetObject(CORBA::Object_ptr obj)
199 {
200   myObjects.clear();
201   myObjNameLineEdit->setText( myEmptyText );
202   myObjNameLineEdit->setStyleSheet(myEmptyStyleSheet);
203   myParamValue = "";
204
205   _PTR(SObject) sobj;
206   if ( !CORBA::is_nil( obj ))
207     sobj = SMESH::FindSObject (obj);
208   if ( sobj ) {
209     std::string name = sobj->GetName();
210     myObjNameLineEdit->setText( QString( name.c_str() ).trimmed() );
211     myObjNameLineEdit->setStyleSheet("");
212     myObjects.push_back( CORBA::Object::_duplicate( obj ));
213     myParamValue = sobj->GetID().c_str();
214   }
215   emit contentModified();
216 }
217
218 //================================================================================
219 /*!
220  * \brief Initialize selected objects
221  * \param objects - entries of objects
222  */
223 //================================================================================
224
225 void StdMeshersGUI_ObjectReferenceParamWdg::SetObjects(SMESH::string_array_var& objects)
226 {
227   myObjects.clear();
228   myObjNameLineEdit->setText( myEmptyText );
229   myObjNameLineEdit->setStyleSheet(myEmptyStyleSheet);
230   myParamValue = "";
231   bool selChanged = false;
232   
233   for ( unsigned i = 0; i < objects->length(); ++i )
234   {
235     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
236     _PTR(SObject) aSObj = aStudy->FindObjectID(objects[i].in());
237     CORBA::Object_var anObj = SMESH::SObjectToObject(aSObj,aStudy);
238     if ( !CORBA::is_nil( anObj )) {
239       std::string name = aSObj->GetName();
240       QString text = myObjNameLineEdit->text();
241       if ( text != myEmptyText )
242         text += " ";
243       else
244         text = "";
245       text += QString( name.c_str() ).trimmed();
246       myObjNameLineEdit->setText( text );
247       myObjNameLineEdit->setStyleSheet("");
248       myObjects.push_back( anObj );
249       myParamValue += " ";
250       myParamValue += objects[i];
251       selChanged = true;
252     }
253   }
254   if (selChanged)
255     emit contentModified();
256 }
257
258 //================================================================================
259 /*!
260  * \brief Takes selected object
261  */
262 //================================================================================
263
264 void StdMeshersGUI_ObjectReferenceParamWdg::onSelectionDone()
265 {
266   if ( mySelectionActivated ) {
267     CORBA::Object_var obj;
268     SALOME_ListIO aList;
269     mySelectionMgr->selectedObjects(aList);
270     if (aList.Extent() == 1)
271     {
272       obj = SMESH::IObjectToObject( aList.First() );
273       SetObject( obj.in() );
274     }
275     else if (myMultiSelection)
276     {
277       SMESH::string_array_var objIds = new SMESH::string_array;
278       objIds->length( aList.Extent());
279       SALOME_ListIteratorOfListIO io( aList );
280       int i = 0;
281       for ( ; io.More(); io.Next(), ++i )
282       {
283         Handle(SALOME_InteractiveObject) anIO = io.Value();
284         if ( anIO->hasEntry() )
285           objIds[i] = anIO->getEntry();
286         else
287           i--;
288       }
289       objIds->length(i);
290       SetObjects( objIds );
291     }
292   }
293 }
294
295 void StdMeshersGUI_ObjectReferenceParamWdg::SetDefaultText(QString defaultText, QString styleSheet)
296 {
297   myEmptyText = defaultText;
298   myEmptyStyleSheet = styleSheet;
299   myObjNameLineEdit->setText( myEmptyText );
300   myObjNameLineEdit->setStyleSheet( myEmptyStyleSheet);
301 }