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