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