Salome HOME
f01212a3d6b55c4c1ed0e1a45c774246e2fee189
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MeshDlg.cxx
1 // SMESH SMESHGUI : GUI for SMESH component
2 //
3 // Copyright (C) 2003  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 // File   : SMESHGUI_MeshDlg.cxx
23 // Author : Sergey LITONIN, Open CASCADE S.A.S.
24 //
25
26 // SMESH includes
27 #include "SMESHGUI_MeshDlg.h"
28
29 // SALOME GUI includes
30 #include <SUIT_Session.h>
31 #include <SUIT_ResourceMgr.h>
32
33 // Qt includes
34 #include <QVBoxLayout>
35 #include <QGridLayout>
36 #include <QLabel>
37 #include <QTabWidget>
38 #include <QGroupBox>
39 #include <QToolButton>
40 #include <QComboBox>
41 #include <QMenu>
42 #include <QCursor>
43 #include <QPushButton>
44
45 #define SPACING 6
46 #define MARGIN  11
47
48 /*!
49  * \brief Tab for tab widget containing controls for definition of 
50  * algorithms and hypotheses
51 */ 
52
53 //================================================================================
54 /*!
55  * \brief Constructor
56   * \param theParent - Parent widget for this tab
57  * 
58  * Makes tab's look and feel
59  */
60 //================================================================================ 
61 SMESHGUI_MeshTab::SMESHGUI_MeshTab( QWidget* theParent )
62   : QFrame( theParent )
63 {
64   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
65   QIcon aCreateIcon( aResMgr->loadPixmap( "SMESH", tr( "ICON_HYPO" ) ) );
66   QIcon aEditIcon( aResMgr->loadPixmap( "SMESH", tr( "ICON_HYPO_EDIT" ) ) );
67   
68   // Algorifm
69   QLabel* anAlgoLbl = new QLabel( tr( "ALGORITHM" ), this );
70   myHyp[ Algo ] = new QComboBox( this );
71   
72   // Hypothesis
73   QLabel* aHypLbl = new QLabel( tr( "HYPOTHESIS" ), this );
74   myHyp[ MainHyp ] = new QComboBox( this );
75   myHyp[ MainHyp ]->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
76   myCreateHyp[ MainHyp ] = new QToolButton( this );
77   myCreateHyp[ MainHyp ]->setIcon( aCreateIcon );
78   myEditHyp[ MainHyp ] = new QToolButton( this );
79   myEditHyp[ MainHyp ]->setIcon( aEditIcon );
80   
81   // Line
82   QFrame* aLine = new QFrame( this );
83   aLine->setFrameStyle( QFrame::HLine | QFrame::Sunken );
84   
85   // Add. hypothesis
86   QLabel* anAddHypLbl = new QLabel( tr( "ADD_HYPOTHESIS" ), this );
87   myHyp[ AddHyp ] = new QComboBox( this );
88   myHyp[ AddHyp ]->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
89   myCreateHyp[ AddHyp ] = new QToolButton( this );
90   myCreateHyp[ AddHyp ]->setIcon( aCreateIcon );
91   myEditHyp[ AddHyp ] = new QToolButton( this );
92   myEditHyp[ AddHyp ]->setIcon( aEditIcon );
93   
94   // Fill layout
95   QGridLayout* aLay = new QGridLayout( this );
96   aLay->setMargin( MARGIN );
97   aLay->setSpacing( SPACING );
98
99   aLay->addWidget( anAlgoLbl, 0, 0 );
100   aLay->addWidget( myHyp[ Algo ], 0, 1 );
101   aLay->addWidget( aHypLbl, 1, 0 );
102   aLay->addWidget( myHyp[ MainHyp ], 1, 1 );
103   aLay->addWidget( myCreateHyp[ MainHyp ], 1, 2 );
104   aLay->addWidget( myEditHyp[ MainHyp ], 1, 3 );
105   aLay->addWidget( aLine, 2, 0, 1, 4 );
106   aLay->addWidget( anAddHypLbl, 3, 0 );
107   aLay->addWidget( myHyp[ AddHyp ], 3, 1 );
108   aLay->addWidget( myCreateHyp[ AddHyp ], 3, 2 );
109   aLay->addWidget( myEditHyp[ AddHyp ], 3, 3 );
110   aLay->addItem( new QSpacerItem( 0, 0, QSizePolicy::Fixed, QSizePolicy::Expanding ), 4, 0 );
111     
112   // Connect signals and slots
113   for ( int i = MainHyp; i <= AddHyp; i++ )
114   {
115     connect( myCreateHyp[ i ], SIGNAL( clicked() ), SLOT( onCreateHyp() ) );
116     connect( myEditHyp[ i ], SIGNAL( clicked() ), SLOT( onEditHyp() ) );
117     connect( myHyp[ i ], SIGNAL( activated( int ) ), SLOT( onHyp( int ) ) );
118   }
119   connect( myHyp[ Algo ], SIGNAL( activated( int ) ), SLOT( onHyp( int ) ) );
120   
121   // Initialize controls
122   
123   setAvailableHyps( Algo, QStringList() );
124   setAvailableHyps( MainHyp, QStringList() );
125   setAvailableHyps( AddHyp, QStringList() );
126 }
127
128 //================================================================================
129 /*!
130  * \brief Destructor
131  */
132 //================================================================================ 
133 SMESHGUI_MeshTab::~SMESHGUI_MeshTab()
134 {
135 }
136
137 //================================================================================
138 /*!
139  * \brief Sets available hypothesis or algorithms
140   * \param theId - identifier of hypothesis (main or additional, see HypType enumeration)
141   * \param theHyps - list of available hypothesis names
142  * 
143  * Sets available main or additional hypothesis for this tab
144  */
145 //================================================================================
146 void SMESHGUI_MeshTab::setAvailableHyps( const int theId, const QStringList& theHyps )
147 {
148   myAvailableHyps[ theId ] = theHyps;
149
150   bool enable = ! theHyps.isEmpty();
151   if ( theId == Algo )
152   {
153     myHyp[ Algo ]->clear();
154     myHyp[ Algo ]->addItem( tr( "NONE" ) );
155     myHyp[ Algo ]->addItems( theHyps );
156     myHyp[ Algo ]->setCurrentIndex( 0 );
157   }
158   else {
159     myCreateHyp[ theId ]->setEnabled( enable );
160     myEditHyp[ theId ]->setEnabled( false );
161   }
162   myHyp[ theId ]->setEnabled( enable );
163 }
164
165 //================================================================================
166 /*!
167  * \brief Sets existing hypothesis
168   * \param theId - identifier of hypothesis (main or additional, see HypType enumeration)
169   * \param theHyps - list of available hypothesis names
170  * 
171  * Sets existing main or additional hypothesis for this tab
172  */
173 //================================================================================
174 void SMESHGUI_MeshTab::setExistingHyps( const int theId, const QStringList& theHyps )
175 {
176   if ( theId != Algo )
177   {
178     myHyp[ theId ]->clear();
179     myHyp[ theId ]->addItem( tr( "NONE" ) );
180     myHyp[ theId ]->addItems( theHyps );
181     myHyp[ theId ]->setCurrentIndex( 0 );
182     myHyp[ theId ]->setEnabled( !theHyps.isEmpty() );
183     myEditHyp[ theId ]->setEnabled( false );
184   }
185 }
186
187 //================================================================================
188 /*!
189  * \brief Adds hypothesis in combo box of available ones
190   * \param theId - identifier of hypothesis (main or additional, see HypType enumeration)
191   * \param theHyp - name of hypothesis to be added
192  * 
193  * Adds hypothesis in combo box of available ones. This method is called by operation
194  * after creation of new hypothesis.
195  */
196 //================================================================================
197 void SMESHGUI_MeshTab::addHyp( const int theId, const QString& theHyp )
198 {
199   myHyp[ theId ]->addItem( theHyp );
200   myHyp[ theId ]->setCurrentIndex( myHyp[ theId ]->count() - 1 );
201   myEditHyp[ theId ]->setEnabled( true );
202   myHyp[ theId ]->setEnabled( true );
203 }
204
205 //================================================================================
206 /*!
207  * \brief Renames hypothesis
208   * \param theId - identifier of hypothesis (main or additional, see HypType enumeration)
209   * \param theIndex - index of hypothesis to be renamed
210   * \param theNewName - new name of hypothesis to be renamed
211  * 
212  * Renames hypothesis
213  */
214 //================================================================================
215 void SMESHGUI_MeshTab::renameHyp( const int theId, 
216                                   const int theIndex, 
217                                   const QString& theNewName )
218 {
219   if ( theIndex > 0 && theIndex < myHyp[ theId ]->count() )
220     myHyp[ theId ]->setItemText( theIndex, theNewName );
221 }                                  
222
223 //================================================================================
224 /*!
225  * \brief Sets current hypothesis 
226   * \param theId - identifier of hypothesis (main or additional, see HypType enumeration)
227   * \param theIndex - index of hypothesis to be set as current
228  * 
229  * Sets current hypothesis 
230  */
231 //================================================================================
232 void SMESHGUI_MeshTab::setCurrentHyp( const int theId, const int theIndex )
233 {
234   if ( theIndex >= 0 && theIndex < myHyp[ theId ]->count() )
235   {
236     myHyp[ theId ]->setCurrentIndex( theIndex );
237     if ( myEditHyp[ theId ] )
238       myEditHyp[ theId ]->setEnabled( theIndex > 0 );
239   }
240 }
241
242 //================================================================================
243 /*!
244  * \brief Gets current hypothesis
245   * \param theId - identifier of hypothesis (main or additional, see HypType enumeration)
246   * \retval int - index of current hypothesis
247  * 
248  * Gets current hypothesis
249  */
250 //================================================================================
251 int SMESHGUI_MeshTab::currentHyp( const int theId ) const
252 {
253   return myHyp[ theId ]->currentIndex();
254 }
255
256 //================================================================================
257 /*!
258  * \brief Emits createHyp( const int ) signal
259  * 
260  * SLOT called when "Create hypothesis" button clicked specifies sender and emits
261  * createHyp( const int ) signal
262  */
263 //================================================================================
264 void SMESHGUI_MeshTab::onCreateHyp()
265 {
266   bool isMainHyp = sender() == myCreateHyp[ MainHyp ];
267
268   QMenu aPopup( this );
269   
270   QStringList aHypNames = isMainHyp ? 
271     myAvailableHyps[ MainHyp ] : aHypNames = myAvailableHyps[ AddHyp ];
272
273   QList<QAction*> actions;
274   for ( int i = 0, n = aHypNames.count(); i < n; i++ )
275     actions.append( aPopup.addAction( aHypNames[ i ] ) );
276
277   QAction* a = aPopup.exec( QCursor::pos() );
278   if ( a )
279     emit createHyp( isMainHyp ? MainHyp : AddHyp, actions.indexOf( a ) );
280 }
281
282 //================================================================================
283 /*!
284  * \brief Emits editHyp( const int ) signal
285  * 
286  * SLOT called when "Edit hypothesis" button clicked specifies sender and emits
287  * editHyp( const int ) signal
288  */
289 //================================================================================
290 void SMESHGUI_MeshTab::onEditHyp()
291 {
292   const QObject* aSender = sender();
293   int aHypType = aSender == myEditHyp[ MainHyp ] ? MainHyp : AddHyp;
294   emit editHyp( aHypType, myHyp[ aHypType ]->currentIndex() - 1 );  // - 1 because there is NONE on the top
295 }
296
297 //================================================================================
298 /*!
299  * \brief Updates "Edit hypothesis" button state
300  * 
301  * SLOT called when current hypothesis changed. Disables "Edit hypothesis" button
302  * if current hypothesis is <None>, enables otherwise.
303  * If an algorithm changed, emits selectAlgo( theIndex ) signal
304  */
305 //================================================================================
306 void SMESHGUI_MeshTab::onHyp( int theIndex )
307 {
308   const QObject* aSender = sender();
309   if ( aSender == myHyp[ Algo ] )
310     emit selectAlgo( theIndex - 1 ); // - 1 because there is NONE on the top
311   else {
312     int anIndex = aSender == myHyp[ MainHyp ] ? MainHyp : AddHyp;
313     myEditHyp[ anIndex ]->setEnabled( theIndex > 0 );
314   }
315 }
316
317 //================================================================================
318 /*!
319  * \brief Resets all tab fields
320  *
321  * Resets all tab fields
322  */
323 //================================================================================  
324 void SMESHGUI_MeshTab::reset()
325 {
326   for ( int i = Algo; i <= AddHyp; i++ )
327   {
328     myHyp[ i ]->setCurrentIndex( 0 );
329     if ( myEditHyp[ i ] )
330       myEditHyp[ i ]->setEnabled( false );
331   }
332 }
333
334 /*!
335  * \brief Dialog for mech creation or editing
336  *
337  *  This dialog is used for mech creation or editing. 
338 */
339
340 //================================================================================
341 /*!
342  * \brief Constructor
343   * \param theToCreate - if this parameter is true then dialog is used for creation,
344   * for editing otherwise
345   * \param theIsMesh - if this parameter is true then dialog is used for mesh,
346   * for sub-mesh otherwise
347  * 
348  * Makes dialog's look and feel
349  */
350 //================================================================================
351 SMESHGUI_MeshDlg::SMESHGUI_MeshDlg( const bool theToCreate, const bool theIsMesh )
352 : SMESHGUI_Dialog( 0, false, true )
353 {
354   // Create top controls
355
356   // name 
357   createObject( tr( "NAME" ), mainFrame(), Obj );
358   setNameIndication( Obj, OneName );
359   setReadOnly( Obj, false );
360   // mesh
361   createObject( tr( "MESH" ), mainFrame(), Mesh );
362   // geometry
363   createObject( tr( "GEOMETRY" ), mainFrame(), Geom );
364   myGeomPopup = 0;
365   
366   // Create tab widget
367   
368   myTabWg = new QTabWidget( mainFrame() );
369   myTabs[ Dim0D ] = new SMESHGUI_MeshTab( myTabWg );
370   myTabs[ Dim1D ] = new SMESHGUI_MeshTab( myTabWg );
371   myTabs[ Dim2D ] = new SMESHGUI_MeshTab( myTabWg );
372   myTabs[ Dim3D ] = new SMESHGUI_MeshTab( myTabWg );
373   myTabWg->addTab( myTabs[ Dim3D ], tr( "DIM_3D" ) );
374   myTabWg->addTab( myTabs[ Dim2D ], tr( "DIM_2D" ) );
375   myTabWg->addTab( myTabs[ Dim1D ], tr( "DIM_1D" ) );
376   myTabWg->addTab( myTabs[ Dim0D ], tr( "DIM_0D" ) );
377
378   // Hypotheses Sets
379   myHypoSetButton = new QToolButton( mainFrame() );
380   myHypoSetButton->setText( tr( "HYPOTHESES_SETS" ) );
381   myHypoSetButton->setEnabled( false );
382   
383   // Fill layout
384   QGridLayout* aLay = new QGridLayout( mainFrame() );
385   aLay->setMargin( 0 );
386   aLay->setSpacing( SPACING );
387
388   aLay->addWidget( objectWg( Obj,  Label ),   0, 0 );
389   aLay->addWidget( objectWg( Obj,  Btn ),     0, 1 );
390   aLay->addWidget( objectWg( Obj,  Control ), 0, 2 );
391   aLay->addWidget( objectWg( Mesh, Label ),   1, 0 );
392   aLay->addWidget( objectWg( Mesh, Btn ),     1, 1 );
393   aLay->addWidget( objectWg( Mesh, Control ), 1, 2 );
394   aLay->addWidget( objectWg( Geom, Label ),   2, 0 );
395   aLay->addWidget( objectWg( Geom, Btn ),     2, 1 );
396   aLay->addWidget( objectWg( Geom, Control ), 2, 2 );
397   aLay->addWidget( myTabWg,                   4, 0, 1, 3 );
398   aLay->addWidget( myHypoSetButton,           5, 0, 1, 3 );
399   aLay->setRowMinimumHeight( 3, 20 );
400
401   // Disable controls if necessary
402   setObjectShown( Mesh, false );
403   if ( theToCreate )
404   {
405     setWindowTitle( tr( "CREATE_MESH" ) );
406     objectWg( Obj, Btn )->hide();
407     if ( theIsMesh )
408       setWindowTitle( tr( "CREATE_MESH" ) );
409     else
410     {
411       setWindowTitle( tr( "CREATE_SUBMESH" ) );
412       setObjectShown( Mesh, true );
413     }
414   }
415   else
416   {
417     setWindowTitle( tr( "EDIT_MESH_SUBMESH" ) );
418     objectWg( Mesh, Btn )->hide();
419     objectWg( Geom, Btn )->hide();
420   }
421 }
422
423 SMESHGUI_MeshDlg::~SMESHGUI_MeshDlg()
424 {
425 }
426
427 //================================================================================
428 /*!
429  * \brief Gets tab with given id
430   * \param theId - Tab identifier. Possible values are in "Dimensions" enumeration
431   * \retval SMESHGUI_MeshTab* - pointer to the tab or null if given parameter is 
432   * invalid
433  * 
434  * Gets tab containing controls for definition of algorithms and AddHypotheses
435  */
436 //================================================================================
437 SMESHGUI_MeshTab* SMESHGUI_MeshDlg::tab( const int theId ) const
438 {
439   return ( theId >= Dim0D && theId <= Dim3D ? myTabs[ theId ] : 0 );
440 }
441
442 //================================================================================
443 /*!
444  * \brief Resets all dialog fields
445  */
446 //================================================================================  
447 void SMESHGUI_MeshDlg::reset()
448 {
449   clearSelection();
450   myTabs[ Dim0D ]->reset();
451   myTabs[ Dim1D ]->reset();
452   myTabs[ Dim2D ]->reset();
453   myTabs[ Dim3D ]->reset();
454 }
455
456 //================================================================================
457 /*!
458  * \brief Sets curent tab
459  */
460 //================================================================================    
461 void SMESHGUI_MeshDlg::setCurrentTab( const int theId  )
462 {
463   myTabWg->setCurrentIndex( Dim3D - theId );
464 }
465
466 //================================================================================
467 /*!
468  * \brief Enable/disable tabs
469   * \param int - maximum possible dimention
470  */
471 //================================================================================
472
473 void SMESHGUI_MeshDlg::setMaxHypoDim( const int maxDim )
474 {
475   const int DIM = maxDim;
476   for ( int dim = Dim0D; dim <= Dim3D; ++dim ) {
477     bool enable = ( dim <= DIM );
478     if ( !enable )
479       myTabs[ dim ]->reset();
480     myTabWg->setTabEnabled( myTabWg->indexOf( myTabs[ dim ] ), enable );
481   }
482   // deselect desabled tab
483   if ( !myTabWg->isTabEnabled( myTabWg->currentIndex() ) )
484     setCurrentTab( DIM - 1 );
485 }
486
487 //================================================================================
488 /*!
489  * \brief Sets list of available Sets of Hypotheses
490  */
491 //================================================================================
492
493 void SMESHGUI_MeshDlg::setHypoSets( const QStringList& theSets )
494 {
495   QMenu* aHypoSetPopup = myHypoSetButton->menu();
496   if ( !aHypoSetPopup ) {
497     aHypoSetPopup = new QMenu( myHypoSetButton );
498     connect( aHypoSetPopup, SIGNAL( triggered( QAction* ) ), SLOT( onHypoSetPopup( QAction* ) ) );
499     myHypoSetButton->setMenu( aHypoSetPopup );
500     myHypoSetButton->setPopupMode( QToolButton::InstantPopup );
501   }
502   aHypoSetPopup->clear();
503   for ( int i = 0, n = theSets.count(); i < n; i++ ) {
504     aHypoSetPopup->addAction( theSets[ i ] );
505   }
506   myHypoSetButton->setEnabled( !aHypoSetPopup->isEmpty() );
507 }
508
509 //================================================================================
510 /*!
511  * \brief Emits hypoSet signal
512  * 
513  * SLOT is called when a hypotheses set is selected. Emits hypoSet
514  * signal to notify operation about this event
515  */
516 //================================================================================
517
518 void SMESHGUI_MeshDlg::onHypoSetPopup( QAction* a )
519 {
520   emit hypoSet( a->text() );
521 }
522   
523 //================================================================================
524 /*!
525  * \brief Enable showing of the popup when Geometry selection btn is clicked
526   * \param enable - true to enable
527  */
528 //================================================================================
529
530 enum { DIRECT_GEOM_INDEX = 0, GEOM_BY_MESH_INDEX };
531
532 void SMESHGUI_MeshDlg::setGeomPopupEnabled( const bool enable )
533 {
534   if ( QToolButton* selBtn = qobject_cast<QToolButton*>( objectWg( Geom, Btn )))
535   {
536     disconnect( selBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
537     if ( enable ) {
538       if ( ! myGeomPopup ) {
539         myGeomPopup = new QMenu();
540         myGeomPopup->addAction( tr("DIRECT_GEOM_SELECTION") )->setData( DIRECT_GEOM_INDEX );
541         myGeomPopup->addAction( tr("GEOM_BY_MESH_ELEM_SELECTION") )->setData( GEOM_BY_MESH_INDEX );
542         connect( myGeomPopup, SIGNAL( triggered( QAction* ) ), SLOT( onGeomPopup( QAction* ) ) );
543       }
544       connect( selBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
545     }
546   }
547 }
548
549
550 //================================================================================
551 /*!
552  * \brief Disable tab
553  * \param int - tab ID
554  */
555 //================================================================================
556 void SMESHGUI_MeshDlg::disableTab(const int theTabId) {
557   myTabWg->setTabEnabled( myTabWg->indexOf( myTabs[ theTabId ] ), false );
558 }
559
560 //================================================================================
561 /*!
562  * \brief Enable tabs
563  * \param int - tab ID
564  */
565 //================================================================================
566 void SMESHGUI_MeshDlg::enableTab(const int theTabId) {
567   myTabWg->setTabEnabled( myTabWg->indexOf( myTabs[ theTabId ] ), true );
568 }
569
570 void SMESHGUI_MeshDlg::onGeomSelectionButton(bool isBtnOn)
571 {
572   if ( myGeomPopup && isBtnOn )
573     myGeomPopup->exec( QCursor::pos() );
574 }
575
576 void SMESHGUI_MeshDlg::onGeomPopup( QAction* a )
577 {
578   emit geomSelectionByMesh( a->data().toInt() == GEOM_BY_MESH_INDEX );
579 }