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