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