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