Salome HOME
Merge branch V7_3_1_BR
[modules/smesh.git] / src / StdMeshersGUI / StdMeshersGUI_QuadrangleParamWdg.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 //  File   : StdMeshersGUI_QuadrangleParamWdg.cxx
20 //  Author : Open CASCADE S.A.S. (jfa)
21 //  SMESH includes
22
23 #include "StdMeshersGUI_QuadrangleParamWdg.h"
24
25 #include "SMESHGUI.h"
26 #include "SMESHGUI_SpinBox.h"
27 #include "StdMeshersGUI_SubShapeSelectorWdg.h"
28
29 #include <GEOMBase.h>
30 #include <LightApp_SelectionMgr.h>
31 #include <SALOME_ListIO.hxx>
32 #include <SALOME_ListIteratorOfListIO.hxx>
33 #include <SUIT_ResourceMgr.h>
34
35 // Qt includes
36 #include <QButtonGroup>
37 #include <QFrame>
38 #include <QGridLayout>
39 #include <QGroupBox>
40 #include <QLabel>
41 #include <QLineEdit>
42 #include <QListWidget>
43 #include <QListWidgetItem>
44 #include <QPushButton>
45 #include <QRadioButton>
46 #include <QTreeWidget>
47 #include <QVBoxLayout>
48
49 // IDL includes
50 #include <SALOMEconfig.h>
51 #include CORBA_CLIENT_HEADER(SMESH_BasicHypothesis)
52 #include CORBA_CLIENT_HEADER(GEOM_Gen)
53
54
55 #define SPACING 6
56 #define MARGIN 11
57
58 enum { TAB_TRANSITION, TAB_VERTEX, TAB_ENF_POINTS };
59
60 //================================================================================
61 // function : Constructor
62 // purpose  :
63 //================================================================================
64
65 StdMeshersGUI_QuadrangleParamCreator::StdMeshersGUI_QuadrangleParamCreator(const QString& aHypType)
66   : StdMeshersGUI_StdHypothesisCreator( aHypType )
67 {
68 }
69
70 //=======================================================================
71 //function : helpPage
72 //purpose  : 
73 //=======================================================================
74
75 QString  StdMeshersGUI_QuadrangleParamCreator::helpPage() const
76 {
77   return "a2d_meshing_hypo_page.html#hypo_quad_params_anchor";
78 }
79
80 //=======================================================================
81 //function : buildFrame
82 //purpose  :
83 //=======================================================================
84
85 QFrame*  StdMeshersGUI_QuadrangleParamCreator::buildFrame()
86 {
87   QFrame* fr = new QFrame();
88
89   QGridLayout* lay = new QGridLayout( fr );
90   lay->setMargin( MARGIN );
91   lay->setSpacing( SPACING );
92   int row = 0;
93
94   myName = 0;
95   if ( isCreation() )
96   {
97     myName = new QLineEdit( fr );
98     QLabel* nameLab = new QLabel( tr("SMESH_NAME"));
99     lay->addWidget( nameLab, row, 0 );
100     lay->addWidget( myName,  row, 1 );
101     ++row;
102   }
103
104   // Transition type
105
106   myTypeWdg = new StdMeshersGUI_QuadrangleParamWdg( fr );
107
108   // Vertexes
109
110   myVertexSelWdg = new StdMeshersGUI_SubShapeSelectorWdg( fr, TopAbs_VERTEX );
111   myVertexSelWdg->layout()->setMargin( MARGIN );
112
113   // Enforced Points
114
115   QWidget* pointsFrame = new QWidget( fr );
116   QVBoxLayout* pointsLay = new QVBoxLayout( pointsFrame );
117   pointsLay->setMargin(MARGIN);
118   pointsLay->setSpacing(SPACING);
119
120   // shapes
121   QGroupBox* shapesGroup = new QGroupBox( tr("SHAPES"), pointsFrame );
122   myShapesList = new QListWidget( shapesGroup );
123   myAddShapeBut = new QPushButton( tr("SMESH_BUT_ADD"), shapesGroup );
124   QPushButton* remShapeBut = new QPushButton( tr("SMESH_BUT_REMOVE"), shapesGroup );
125   //
126   QGridLayout* shapesLay = new QGridLayout( shapesGroup );
127   shapesLay->setMargin(MARGIN);
128   shapesLay->setSpacing(SPACING);
129   shapesLay->addWidget( myShapesList,  0, 0, 3, 2 );
130   shapesLay->addWidget( myAddShapeBut, 0, 2 );
131   shapesLay->addWidget( remShapeBut,   1, 2 );
132   shapesLay->setColumnStretch( 0, 1 );
133   shapesLay->setRowStretch   ( 2, 1 );
134
135   // coords
136   QGroupBox* coordsGroup = new QGroupBox( tr("POINTS"), pointsFrame );
137   myCoordsTreeWdg = new QTreeWidget( coordsGroup );
138   myCoordsTreeWdg->setColumnCount ( 3 );
139   myCoordsTreeWdg->setHeaderLabels( QStringList() << "X" << "Y" << "Z" );
140   myCoordsTreeWdg->setItemDelegate( new ItemDelegate( myCoordsTreeWdg ));
141   QPushButton* addCoordBut = new QPushButton( tr("SMESH_BUT_ADD"), coordsGroup );
142   QPushButton* remCoordBut = new QPushButton( tr("SMESH_BUT_REMOVE"), coordsGroup );
143   //
144   QGridLayout* coordsLay = new QGridLayout( coordsGroup );
145   coordsLay->setMargin(MARGIN);
146   coordsLay->setSpacing(SPACING);
147   coordsLay->addWidget( myCoordsTreeWdg, 0, 0, 3, 2 );
148   coordsLay->addWidget( addCoordBut,     0, 2 );
149   coordsLay->addWidget( remCoordBut,     1, 2 );
150   coordsLay->setColumnStretch( 0, 1 );
151   coordsLay->setRowStretch   ( 2, 1 );
152
153   pointsLay->addWidget( shapesGroup );
154   pointsLay->addWidget( coordsGroup );
155
156   // Tabs
157   myTabs = new QTabWidget( fr );
158   myTabs->addTab( myTypeWdg,      tr("TRANSITION"));
159   myTabs->addTab( myVertexSelWdg, tr("SMESH_BASE_VERTEX"));
160   myTabs->addTab( pointsFrame,    tr("ENF_NODES"));
161
162   lay->addWidget( myTabs, row, 0, 2, 3 );
163
164   // signals
165   connect( myTypeWdg,     SIGNAL( typeChanged(int)),   SLOT( onTypeChanged(int)));
166   connect( myAddShapeBut, SIGNAL( clicked()),          SLOT( onAddShape() ));
167   connect( remShapeBut,   SIGNAL( clicked()),          SLOT( onRemoveShape() ));
168   connect( addCoordBut,   SIGNAL( clicked()),          SLOT( onAddPoint() ));
169   connect( remCoordBut,   SIGNAL( clicked()),          SLOT( onRemovePoint() ));
170   connect( myTabs,        SIGNAL( currentChanged(int)),SLOT( onTabChanged(int)));
171
172   LightApp_SelectionMgr* selMgr = SMESHGUI::GetSMESHGUI()->selectionMgr();
173   connect( selMgr, SIGNAL(currentSelectionChanged()), SLOT( onSelectionChanged()));
174
175   return fr;
176 }
177
178 //=======================================================================
179 //function : retrieveParams
180 //purpose  :
181 //=======================================================================
182
183 void StdMeshersGUI_QuadrangleParamCreator::retrieveParams() const
184 {
185   StdMeshers::StdMeshers_QuadrangleParams_var h =
186     StdMeshers::StdMeshers_QuadrangleParams::_narrow( initParamsHypothesis() );
187
188   // name
189   if( myName )
190     myName->setText( hypName() );
191
192   // main shape
193   myVertexSelWdg->SetMaxSize(1);
194   QString anEntry    = SMESHGUI_GenericHypothesisCreator::getShapeEntry();
195   QString aMainEntry = SMESHGUI_GenericHypothesisCreator::getMainShapeEntry();
196   if ( anEntry.isEmpty() )
197     anEntry = h->GetObjectEntry();
198   myVertexSelWdg->SetGeomShapeEntry(anEntry);
199   myVertexSelWdg->SetMainShapeEntry(aMainEntry);
200
201   if ( !isCreation())
202   {
203     // type
204     myTypeWdg->SetType(int(h->GetQuadType()));
205
206     // vertex
207     int vertID = h->GetTriaVertex();
208     if (vertID > 0) {
209       SMESH::long_array_var aVec = new SMESH::long_array;
210       aVec->length(1);
211       aVec[0] = vertID;
212       myVertexSelWdg->SetListOfIDs(aVec);
213     }
214
215     // enforced nodes
216     GEOM::ListOfGO_var     shapes;
217     SMESH::nodes_array_var points;
218     h->GetEnforcedNodes( shapes, points );
219     for ( int i = 0; i < shapes->length(); ++i )
220     {
221       CORBA::String_var name  = shapes[i]->GetName();
222       CORBA::String_var entry = shapes[i]->GetStudyEntry();
223       QListWidgetItem* item = new QListWidgetItem( name.in() );
224       item->setData( Qt::UserRole, entry.in() );
225       myShapesList->addItem( item );
226     }
227     for ( int i = 0; i < points->length(); ++i )
228     {
229       QTreeWidgetItem* item = new QTreeWidgetItem
230         ( QStringList()
231           << QString::number( points[i].x )
232           << QString::number( points[i].y )
233           << QString::number( points[i].z ));
234       item->setFlags( item->flags() | Qt::ItemIsEditable );
235       myCoordsTreeWdg->addTopLevelItem( item );
236     }
237   }
238   ((StdMeshersGUI_QuadrangleParamCreator*) this)->onSelectionChanged();
239 }
240
241 //=======================================================================
242 //function : storeParams
243 //purpose  :
244 //=======================================================================
245
246 QString  StdMeshersGUI_QuadrangleParamCreator::storeParams() const
247 {
248   StdMeshers::StdMeshers_QuadrangleParams_var h =
249     StdMeshers::StdMeshers_QuadrangleParams::_narrow( hypothesis() );
250
251   // name
252   if( myName )
253     SMESH::SetName( SMESH::FindSObject( h ), myName->text().toLatin1().constData() );
254
255   // transition
256   h->SetQuadType( StdMeshers::QuadType( myTypeWdg->GetType()) );
257
258   // vertex
259   if ( myVertexSelWdg->GetListSize() > 0 )
260   {
261     h->SetTriaVertex( myVertexSelWdg->GetListOfIDs()[0] ); // getlist must be called once
262     h->SetObjectEntry( myVertexSelWdg->GetMainShapeEntry() );
263   }
264   else
265   {
266     h->SetTriaVertex( -1 );
267   }
268
269   // enfored nodes
270
271   GEOM::ListOfGO_var goList = new GEOM::ListOfGO;
272   int nbShapes = 0;
273   goList->length( myShapesList->count() );
274   for ( int i = 0; i < myShapesList->count(); ++i )
275   {
276     QListWidgetItem* item = myShapesList->item(i);
277     QString         entry = item->data( Qt::UserRole ).toString();
278     Handle(SALOME_InteractiveObject) io =
279       new SALOME_InteractiveObject( entry.toStdString().c_str(), "GEOM" );
280     GEOM::GEOM_Object_var go = GEOMBase::ConvertIOinGEOMObject( io );
281     if ( !go->_is_nil() )
282       goList[ nbShapes++ ] = go;
283   }
284   goList->length( nbShapes );
285   
286   SMESH::nodes_array_var points = new SMESH::nodes_array;
287   points->length( myCoordsTreeWdg->topLevelItemCount() );
288   for ( int i = 0; i < myCoordsTreeWdg->topLevelItemCount(); ++i )
289   {
290     QTreeWidgetItem* item = myCoordsTreeWdg->topLevelItem( i );
291     points[i].x = item->text(0).toInt();
292     points[i].y = item->text(1).toInt();
293     points[i].z = item->text(2).toInt();
294   }
295   h->SetEnforcedNodes( goList, points );
296
297   return "";
298 }
299
300 //=======================================================================
301 //function : onTypeChanged
302 //purpose  :
303 //=======================================================================
304
305 void StdMeshersGUI_QuadrangleParamCreator::onTypeChanged(int type)
306 {
307   myTabs->setTabEnabled( TAB_ENF_POINTS, ( type != StdMeshers::QUAD_REDUCED ));
308 }
309
310 //=======================================================================
311 //function : onAddShape
312 //purpose  : 
313 //=======================================================================
314
315 void StdMeshersGUI_QuadrangleParamCreator::onAddShape()
316 {
317   if ( !mySelectedShapeIO.IsNull() )
318   {
319     QListWidgetItem* item = new QListWidgetItem( mySelectedShapeIO->getName() );
320     item->setData( Qt::UserRole, mySelectedShapeIO->getEntry() );
321     myShapesList->addItem( item );
322     mySelectedShapeIO.Nullify();
323     myAddShapeBut->setEnabled( false );
324   }
325 }
326
327 //=======================================================================
328 //function : onRemoveShape
329 //purpose  : 
330 //=======================================================================
331
332 void StdMeshersGUI_QuadrangleParamCreator::onRemoveShape()
333 {
334   if ( QListWidgetItem * item = myShapesList->currentItem() )
335     delete item;
336   onSelectionChanged();
337 }
338
339 //=======================================================================
340 //function : onAddPoint
341 //purpose  : 
342 //=======================================================================
343
344 void StdMeshersGUI_QuadrangleParamCreator::onAddPoint()
345 {
346   QTreeWidgetItem* item = new QTreeWidgetItem( QStringList() << "0" << "0" << "0" );
347   item->setFlags( item->flags() | Qt::ItemIsEditable );
348   myCoordsTreeWdg->addTopLevelItem( item );
349 }
350
351 //=======================================================================
352 //function : onRemovePoint
353 //purpose  :
354 //=======================================================================
355
356 void StdMeshersGUI_QuadrangleParamCreator::onRemovePoint()
357 {
358   if ( myCoordsTreeWdg->topLevelItemCount() )
359     delete myCoordsTreeWdg->currentItem();
360 }
361
362 //=======================================================================
363 //function : onSelectionChanged
364 //purpose  : 
365 //=======================================================================
366
367 void StdMeshersGUI_QuadrangleParamCreator::onSelectionChanged()
368 {
369   mySelectedShapeIO.Nullify();
370
371   // find a sole selected geometry
372   LightApp_SelectionMgr* selMgr = SMESHGUI::GetSMESHGUI()->selectionMgr();
373   SALOME_ListIO          selList;
374   selMgr->selectedObjects( selList );
375   SALOME_ListIteratorOfListIO selIt( selList );
376   for ( ; selIt.More(); selIt.Next() )
377   {
378     GEOM::GEOM_Object_var go = GEOMBase::ConvertIOinGEOMObject( selIt.Value() );
379     if ( !go->_is_nil() )
380     {
381       if ( !mySelectedShapeIO.IsNull() )
382       {
383         mySelectedShapeIO.Nullify();
384         break;
385       }
386       mySelectedShapeIO = selIt.Value();
387       if ( !mySelectedShapeIO->getName() || !mySelectedShapeIO->getName()[0] )
388         mySelectedShapeIO.Nullify();
389     }
390   }
391   // check if a selected geometry is not already in myShapesList
392   if ( !mySelectedShapeIO.IsNull() )
393   {
394     for ( int i = 0; i < myShapesList->count(); ++i )
395       if ( myShapesList->item(i)->data( Qt::UserRole ) == mySelectedShapeIO->getEntry() )
396       {
397         mySelectedShapeIO.Nullify();
398         break;
399       }
400   }
401   myAddShapeBut->setEnabled( !mySelectedShapeIO.IsNull() );
402 }
403
404 //=======================================================================
405 //function : onTabChanged
406 //purpose  : 
407 //=======================================================================
408
409 void StdMeshersGUI_QuadrangleParamCreator::onTabChanged(int i)
410 {
411   myVertexSelWdg->showPreview( i == TAB_VERTEX );
412 }
413
414 //================================================================================
415 // function : Constructor
416 // purpose  :
417 //================================================================================
418
419 StdMeshersGUI_QuadrangleParamWdg::StdMeshersGUI_QuadrangleParamWdg (QWidget * parent)
420   : QWidget(parent), myType(0)
421 {
422   myType = new QButtonGroup (this);
423
424   QGridLayout* typeLay = new QGridLayout( this );
425
426   typeLay->setMargin(MARGIN);
427   typeLay->setSpacing(SPACING);
428
429   QString aTypeKey ("SMESH_QUAD_TYPE_%1");
430   QString aPictKey ("ICON_StdMeshers_Quadrangle_Params_%1");
431
432   int itype = 0;
433   for (; itype < int(StdMeshers::QUAD_NB_TYPES); itype++) {
434     QRadioButton* rbi = new QRadioButton (tr(aTypeKey.arg(itype).toLatin1()), this);
435     QPixmap pmi (SMESHGUI::resourceMgr()->loadPixmap("SMESH", tr(aPictKey.arg(itype).toLatin1())));
436     QLabel* pli = new QLabel (this);
437     pli->setPixmap(pmi);
438     typeLay->addWidget(rbi, itype, 0, 1, 1);
439     typeLay->addWidget(pli, itype, 1, 1, 1);
440     myType->addButton(rbi, itype);
441   }
442   myType->button(0)->setChecked(true);
443
444   setLayout(typeLay);
445   setMinimumWidth(300);
446
447   connect( myType, SIGNAL( buttonClicked(int)), this, SIGNAL( typeChanged(int)));
448 }
449
450 //================================================================================
451 // function : Destructor
452 // purpose  :
453 //================================================================================
454 StdMeshersGUI_QuadrangleParamWdg::~StdMeshersGUI_QuadrangleParamWdg()
455 {
456 }
457
458 //=================================================================================
459 // function : SetType
460 // purpose  :
461 //=================================================================================
462 void StdMeshersGUI_QuadrangleParamWdg::SetType (int theType)
463 {
464   myType->button(theType)->setChecked(true);
465 }
466
467 //=================================================================================
468 // function : GetType
469 // purpose  :
470 //=================================================================================
471 int StdMeshersGUI_QuadrangleParamWdg::GetType()
472 {
473   return myType->checkedId();
474 }
475
476 //================================================================================
477 /*!
478   \brief Constructor
479 */
480 StdMeshersGUI_QuadrangleParamCreator::
481 ItemDelegate::ItemDelegate( QObject* parent ) : QItemDelegate( parent )
482 {
483 }
484 //================================================================================
485 /*!
486   \brief Create item editor widget
487 */
488 QWidget* StdMeshersGUI_QuadrangleParamCreator::
489 ItemDelegate::createEditor( QWidget*                    parent,
490                             const QStyleOptionViewItem& option,
491                             const QModelIndex&          index ) const
492 {
493   SMESHGUI_SpinBox* sb = new SMESHGUI_SpinBox( parent );
494   sb->RangeStepAndValidator( COORD_MIN, COORD_MAX, 10 );
495   return sb;
496 }