Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[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/ or email : webmaster.salome@opencascade.com
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     myHyp[ theId ]->setEnabled( !theHyps.isEmpty() );
179     myEditHyp[ theId ]->setEnabled( false );
180   }
181 }
182
183 //================================================================================
184 /*!
185  * \brief Adds hypothesis in combo box of available ones
186   * \param theId - identifier of hypothesis (main or additional, see HypType enumeration)
187   * \param theHyp - name of hypothesis to be added
188  * 
189  * Adds hypothesis in combo box of available ones. This method is called by operation
190  * after creation of new hypothesis.
191  */
192 //================================================================================
193 void SMESHGUI_MeshTab::addHyp( const int theId, const QString& theHyp )
194 {
195   myHyp[ theId ]->insertItem( theHyp );
196   myHyp[ theId ]->setCurrentItem( myHyp[ theId ]->count() - 1 );
197   myEditHyp[ theId ]->setEnabled( true );
198   myHyp[ theId ]->setEnabled( true );
199 }
200
201 //================================================================================
202 /*!
203  * \brief Renames hypothesis
204   * \param theId - identifier of hypothesis (main or additional, see HypType enumeration)
205   * \param theIndex - index of hypothesis to be renamed
206   * \param theNewName - new name of hypothesis to be renamed
207  * 
208  * Renames hypothesis
209  */
210 //================================================================================
211 void SMESHGUI_MeshTab::renameHyp( const int theId, 
212                                   const int theIndex, 
213                                   const QString& theNewName )
214 {
215   if ( theIndex > 0 && theIndex < myHyp[ theId ]->count() )
216     myHyp[ theId ]->changeItem( theNewName, theIndex );
217 }                                  
218
219 //================================================================================
220 /*!
221  * \brief Sets current hypothesis 
222   * \param theId - identifier of hypothesis (main or additional, see HypType enumeration)
223   * \param theIndex - index of hypothesis to be set as current
224  * 
225  * Sets current hypothesis 
226  */
227 //================================================================================
228 void SMESHGUI_MeshTab::setCurrentHyp( const int theId, const int theIndex )
229 {
230   if ( theIndex >= 0 && theIndex < myHyp[ theId ]->count() )
231   {
232     myHyp[ theId ]->setCurrentItem( theIndex );
233     if ( myEditHyp[ theId ] )
234       myEditHyp[ theId ]->setEnabled( theIndex > 0 );
235   }
236 }
237
238 //================================================================================
239 /*!
240  * \brief Gets current hypothesis
241   * \param theId - identifier of hypothesis (main or additional, see HypType enumeration)
242   * \retval int - index of current hypothesis
243  * 
244  * Gets current hypothesis
245  */
246 //================================================================================
247 int SMESHGUI_MeshTab::currentHyp( const int theId ) const
248 {
249   return myHyp[ theId ]->currentItem();
250 }
251
252 //================================================================================
253 /*!
254  * \brief Emits createHyp( const int ) signal
255  * 
256  * SLOT called when "Create hypothesis" button clicked specifies sender and emits
257  * createHyp( const int ) signal
258  */
259 //================================================================================
260 void SMESHGUI_MeshTab::onCreateHyp()
261 {
262   const QObject* aSender = sender();
263     
264   if ( !myPopup )
265   {
266     myPopup = new QPopupMenu( 0 );
267     connect( myPopup, SIGNAL( activated( int ) ), SLOT( onPopupItem( int ) ) );
268   }
269   
270   QStringList aHypNames;
271   if ( aSender == myCreateHyp[ MainHyp ] )
272   {
273     aHypNames = myAvailableHyps[ MainHyp ];
274     myPopup->setName( "MainHypPopup" );
275   }
276   else
277   {
278     aHypNames = myAvailableHyps[ AddHyp ];
279     myPopup->setName( "AddHypPopup" );
280   }
281  
282   myPopup->clear();
283   for ( int i = 0, n = aHypNames.count(); i < n; i++ )
284     myPopup->insertItem( aHypNames[ i ], i );
285
286   myPopup->exec( QCursor::pos() );
287 }
288
289 //================================================================================
290 /*!
291  * \brief Emits editHyp( const int ) signal
292  * 
293  * SLOT called when "Edit hypothesis" button clicked specifies sender and emits
294  * editHyp( const int ) signal
295  */
296 //================================================================================
297 void SMESHGUI_MeshTab::onEditHyp()
298 {
299   const QObject* aSender = sender();
300   int aHypType = aSender == myEditHyp[ MainHyp ] ? MainHyp : AddHyp;
301   emit editHyp( aHypType, myHyp[ aHypType ]->currentItem() - 1 );  // - 1 because there is NONE on the top
302 }
303
304 //================================================================================
305 /*!
306  * \brief Updates "Edit hypothesis" button state
307  * 
308  * SLOT called when current hypothesis changed. Disables "Edit hypothesis" button
309  * if current hypothesis is <None>, enables otherwise.
310  * If an algorithm changed, emits selectAlgo( theIndex ) signal
311  */
312 //================================================================================
313 void SMESHGUI_MeshTab::onHyp( int theIndex )
314 {
315   const QObject* aSender = sender();
316   if ( aSender == myHyp[ Algo ] )
317     emit selectAlgo( theIndex - 1 ); // - 1 because there is NONE on the top
318   else {
319     int anIndex = aSender == myHyp[ MainHyp ] ? MainHyp : AddHyp;
320     myEditHyp[ anIndex ]->setEnabled( theIndex > 0 );
321   }
322 }
323
324 //================================================================================
325 /*!
326  * \brief Emits createHyp signal
327  * 
328  * SLOT called when item of popup for hypothesis creation is activated. Emits 
329  * createHyp signal to notify operation obout this event
330  */
331 //================================================================================
332 void SMESHGUI_MeshTab::onPopupItem( int theId )
333 {
334   const QObject* aSender = sender();
335   if ( aSender )
336     emit createHyp( strcmp( aSender->name(),  "MainHypPopup" ) == 0 ? MainHyp : AddHyp, theId );
337 }
338
339 //================================================================================
340 /*!
341  * \brief Resets all tab fields
342  *
343  * Resets all tab fields
344  */
345 //================================================================================  
346 void SMESHGUI_MeshTab::reset()
347 {
348   for ( int i = Algo; i <= AddHyp; i++ )
349   {
350     myHyp[ i ]->setCurrentItem( 0 );
351     if ( myEditHyp[ i ] )
352       myEditHyp[ i ]->setEnabled( false );
353   }
354 }
355
356 /*!
357  * \brief Dialog for mech creation or editing
358  *
359  *  This dialog is used for mech creation or editing. 
360 */
361
362 //================================================================================
363 /*!
364  * \brief Constructor
365   * \param theToCreate - if this parameter is true then dialog is used for creation,
366   * for editing otherwise
367   * \param theIsMesh - if this parameter is true then dialog is used for mesh,
368   * for sub-mesh otherwise
369  * 
370  * Makes dialog's look and feel
371  */
372 //================================================================================
373 SMESHGUI_MeshDlg::SMESHGUI_MeshDlg( const bool theToCreate, const bool theIsMesh )
374 : SMESHGUI_Dialog( 0, false, true )
375 {
376   // Create top controls
377   
378   QGroupBox* aGrp = new QGroupBox( 3, Qt::Horizontal, mainFrame() );
379   aGrp->setFrameStyle( QFrame::NoFrame );
380   aGrp->setInsideMargin( 0 );
381   // name 
382   createObject( tr( "NAME" ), aGrp, Obj );
383   setNameIndication( Obj, OneName );
384   setReadOnly( Obj, false );
385   // mesh
386   createObject( tr( "MESH" ), aGrp, Mesh );
387   // geometry
388   createObject( tr( "GEOMETRY" ), aGrp, Geom );
389   myGeomPopup = 0;
390   
391   // Create tab widget
392   
393   myTabWg = new QTabWidget( mainFrame() );
394   myTabs[ Dim0D ] = new SMESHGUI_MeshTab( myTabWg );
395   myTabs[ Dim1D ] = new SMESHGUI_MeshTab( myTabWg );
396   myTabs[ Dim2D ] = new SMESHGUI_MeshTab( myTabWg );
397   myTabs[ Dim3D ] = new SMESHGUI_MeshTab( myTabWg );
398   myTabWg->addTab( myTabs[ Dim3D ], tr( "DIM_3D" ) );
399   myTabWg->addTab( myTabs[ Dim2D ], tr( "DIM_2D" ) );
400   myTabWg->addTab( myTabs[ Dim1D ], tr( "DIM_1D" ) );
401   myTabWg->addTab( myTabs[ Dim0D ], tr( "DIM_0D" ) );
402
403   // Hypotheses Sets
404   myHypoSetPopup = new QPopupMenu();
405   QButton* aHypoSetButton = new QPushButton( mainFrame(), "aHypoSetButton");
406   aHypoSetButton->setText( tr( "HYPOTHESES_SETS" ) );
407   
408   // Fill layout
409   QVBoxLayout* aLay = new QVBoxLayout( mainFrame(), 0, 5 );
410   aLay->addWidget( aGrp );
411   aLay->addItem( new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum) );
412   aLay->addWidget( myTabWg );
413   aLay->addWidget( aHypoSetButton );
414
415   // Disable controls if necessary
416   setObjectShown( Mesh, false );
417   if ( theToCreate )
418   {
419     setCaption( tr( "CREATE_MESH" ) );
420     objectWg( Obj, Btn )->hide();
421     if ( theIsMesh )
422       setCaption( tr( "CREATE_MESH" ) );
423     else
424     {
425       setCaption( tr( "CREATE_SUBMESH" ) );
426       setObjectShown( Mesh, true );
427     }
428   }
429   else
430   {
431     setCaption( tr( "EDIT_MESH_SUBMESH" ) );
432     objectWg( Mesh, Btn )->hide();
433     objectWg( Geom, Btn )->hide();
434   }
435
436   // Connect signals and slots
437   connect( aHypoSetButton, SIGNAL( clicked() ), SLOT( onHypoSetButton() ) );
438   connect( myHypoSetPopup, SIGNAL( activated( int ) ), SLOT( onHypoSetPopup( int ) ) );
439 }
440
441 SMESHGUI_MeshDlg::~SMESHGUI_MeshDlg()
442 {
443   if ( myHypoSetPopup )
444     delete myHypoSetPopup;
445 }
446
447 //================================================================================
448 /*!
449  * \brief Gets tab with given id
450   * \param theId - Tab identifier. Possible values are in "Dimensions" enumeration
451   * \retval SMESHGUI_MeshTab* - pointer to the tab or null if given parameter is 
452   * invalid
453  * 
454  * Gets tab containing controls for definition of algorithms and AddHypotheses
455  */
456 //================================================================================
457 SMESHGUI_MeshTab* SMESHGUI_MeshDlg::tab( const int theId ) const
458 {
459   return ( theId >= Dim0D && theId <= Dim3D ? myTabs[ theId ] : 0 );
460 }
461
462 //================================================================================
463 /*!
464  * \brief Resets all dialog fields
465  */
466 //================================================================================  
467 void SMESHGUI_MeshDlg::reset()
468 {
469   clearSelection();
470   myTabs[ Dim0D ]->reset();
471   myTabs[ Dim1D ]->reset();
472   myTabs[ Dim2D ]->reset();
473   myTabs[ Dim3D ]->reset();
474 }
475
476 //================================================================================
477 /*!
478  * \brief Sets curent tab
479  */
480 //================================================================================    
481 void SMESHGUI_MeshDlg::setCurrentTab( const int theId  )
482 {
483   myTabWg->setCurrentPage( Dim3D - theId );
484 }
485
486 //================================================================================
487 /*!
488  * \brief Enable/disable tabs
489   * \param int - maximum possible dimention
490  */
491 //================================================================================
492
493 void SMESHGUI_MeshDlg::setMaxHypoDim( const int maxDim )
494 {
495   const int DIM = maxDim;
496   for ( int dim = Dim0D; dim <= Dim3D; ++dim ) {
497     bool enable = ( dim <= DIM );
498     if ( !enable )
499       myTabs[ dim ]->reset();
500     myTabWg->setTabEnabled( myTabs[ dim ], enable );
501   }
502   // deselect desabled tab
503   if ( !myTabWg->isTabEnabled( myTabWg->currentPage() ))
504     setCurrentTab( DIM - 1 );
505 }
506
507 //================================================================================
508 /*!
509  * \brief Sets list of available Sets of Hypotheses
510  */
511 //================================================================================
512
513 void SMESHGUI_MeshDlg::setHypoSets( const QStringList& theSets )
514 {
515   myHypoSetPopup->clear();
516   for ( int i = 0, n = theSets.count(); i < n; i++ ) {
517     myHypoSetPopup->insertItem( theSets[ i ], i );
518   }
519 }
520
521 //================================================================================
522 /*!
523  * \brief Emits hypoSet signal
524  * 
525  * SLOT is called when a hypotheses set is selected. Emits hypoSet
526  * signal to notify operation about this event
527  */
528 //================================================================================
529
530 void SMESHGUI_MeshDlg::onHypoSetPopup( int theIndex )
531 {
532   emit hypoSet( myHypoSetPopup->text( theIndex ));
533 }
534   
535 //================================================================================
536 /*!
537  * \brief Shows myHypoSetPopup
538  */
539 //================================================================================
540
541 void SMESHGUI_MeshDlg::onHypoSetButton()
542 {
543   myHypoSetPopup->exec( QCursor::pos() );
544 }
545
546 //================================================================================
547 /*!
548  * \brief Enable showing of the popup when Geometry selection btn is clicked
549   * \param enable - true to enable
550  */
551 //================================================================================
552
553 enum { DIRECT_GEOM_INDEX = 0, GEOM_BY_MESH_INDEX };
554
555 void SMESHGUI_MeshDlg::setGeomPopupEnabled( const bool enable )
556 {
557   if ( QButton* selBtn = dynamic_cast<QButton*>( objectWg( Geom, Btn )))
558   {
559     disconnect( selBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
560     if ( enable ) {
561       if ( ! myGeomPopup ) {
562         myGeomPopup = new QPopupMenu();
563         myGeomPopup->insertItem( tr("DIRECT_GEOM_SELECTION"), DIRECT_GEOM_INDEX );
564         myGeomPopup->insertItem( tr("GEOM_BY_MESH_ELEM_SELECTION"), GEOM_BY_MESH_INDEX );
565         connect( myGeomPopup, SIGNAL( activated( int ) ), SLOT( onGeomPopup( int ) ) );
566       }
567       connect( selBtn, SIGNAL( toggled(bool) ), this, SLOT( onGeomSelectionButton(bool) ));
568     }
569   }
570 }
571
572
573 //================================================================================
574 /*!
575  * \brief Disable tab
576  * \param int - tab ID
577  */
578 //================================================================================
579 void SMESHGUI_MeshDlg::disableTab(const int theTabId) {
580   myTabWg->setTabEnabled( myTabs[ theTabId ], false );
581 }
582
583 //================================================================================
584 /*!
585  * \brief Enable tabs
586  * \param int - tab ID
587  */
588 //================================================================================
589 void SMESHGUI_MeshDlg::enableTab(const int theTabId) {
590   myTabWg->setTabEnabled( myTabs[ theTabId ], true );
591 }
592
593 void SMESHGUI_MeshDlg::onGeomSelectionButton(bool isBtnOn)
594 {
595   if ( myGeomPopup && isBtnOn )
596     myGeomPopup->exec( QCursor::pos() );
597 }
598
599 void SMESHGUI_MeshDlg::onGeomPopup( int index )
600 {
601   emit geomSelectionByMesh( index == GEOM_BY_MESH_INDEX );
602 }