Salome HOME
IPAL21120 SIGSEGV on Meshing attached Compound with Automatic Hexadralization
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_aParameter.h
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 //  SMESH SMESHGUI : GUI for SMESH component
23 //  File   : SMESHGUI_aParameter.h
24 //  Module : SMESH
25 //  $Header$
26 //
27 #ifndef SMESHGUI_aParameter_H
28 #define SMESHGUI_aParameter_H
29
30 #include <boost/shared_ptr.hpp>
31 #include <qstringlist.h>
32 #include <qmap.h>
33 #include <qtable.h>
34
35 #include <SALOMEconfig.h>
36 #include CORBA_SERVER_HEADER(SMESH_Mesh)
37
38 class QWidget;
39 class SMESHGUI_aParameter;
40
41 typedef boost::shared_ptr<SMESHGUI_aParameter> SMESHGUI_aParameterPtr;
42
43 /*!
44  *  \brief This class is the base class of all parameters
45  */
46 class SMESHGUI_aParameter
47
48 public:
49   typedef bool (*VALIDATION_FUNC)( SMESHGUI_aParameter* );
50
51   SMESHGUI_aParameter(const QString& label, const bool = false );
52   virtual ~SMESHGUI_aParameter();
53
54   enum Type { INT, DOUBLE, STRING, ENUM, BOOL, TABLE };
55   virtual Type GetType() const = 0;
56   virtual bool GetNewInt( int & Value ) const = 0;
57   virtual bool GetNewDouble( double & Value ) const = 0;
58   virtual bool GetNewText( QString & Value ) const = 0;
59   virtual void TakeValue( QWidget* ) = 0;
60   virtual QWidget* CreateWidget( QWidget* ) const = 0;
61   virtual void InitializeWidget( QWidget* ) const = 0;
62
63   bool needPreview() const;
64
65   /*!
66    *  \brief Returns string representation of signal emitted when value in corrsponding widget is changed
67    */
68   virtual QString sigValueChanged() const;
69
70   QString & Label();
71   
72 protected:
73   QString _label;
74   bool    _needPreview;
75 };
76
77 /*!
78  *  \brief This class provides parameter with integer value
79  */
80 class SMESHGUI_intParameter: public SMESHGUI_aParameter
81
82 public:
83   SMESHGUI_intParameter(const int      initValue = 0,
84                         const QString& label     = QString::null,
85                         const int      bottom    = 0,
86                         const int      top       = 1000,
87                         const bool = false );
88   int & InitValue() { return _initValue; }
89   int & Top()       { return _top; }
90   int & Bottom()    { return _bottom; }
91   virtual Type GetType() const;
92   virtual bool GetNewInt( int & Value ) const;
93   virtual bool GetNewDouble( double & Value ) const;
94   virtual bool GetNewText( QString & Value ) const;
95   virtual void TakeValue( QWidget* );
96   virtual QWidget* CreateWidget( QWidget* ) const;
97   virtual void InitializeWidget( QWidget* ) const;
98
99   virtual QString sigValueChanged() const;
100   
101 protected:
102   int _top, _bottom;
103   int _initValue, _newValue;
104 };
105
106 /*!
107  *  \brief This class provides parameter with double value
108  */
109 class SMESHGUI_doubleParameter: public SMESHGUI_aParameter
110
111 public:
112   SMESHGUI_doubleParameter(const double   initValue = 0.0,
113                            const QString& label     = QString::null,
114                            const double   bottom    = -1E6,
115                            const double   top       = +1E6,
116                            const double   step      = 1.0,
117                            const int      decimals  = 3,
118                            const bool = false);
119   double & InitValue() { return _initValue; }
120   double & Top()       { return _top; }
121   double & Bottom()    { return _bottom; }
122   double & Step()      { return _step; }
123   int    & Decimals()  { return _decimals; }
124   virtual Type GetType() const;
125   virtual bool GetNewInt( int & Value ) const;
126   virtual bool GetNewDouble( double & Value ) const;
127   virtual bool GetNewText( QString & Value ) const;
128   virtual QWidget* CreateWidget( QWidget* ) const;
129   virtual void InitializeWidget( QWidget* ) const;
130   virtual void TakeValue( QWidget* );
131
132   virtual QString sigValueChanged() const;
133   
134 protected:
135   double _top, _bottom, _step;
136   double _initValue, _newValue;
137   int _decimals;
138 };
139
140 /*!
141  *  \brief This class provides parameter with string value
142  */
143 class SMESHGUI_strParameter: public SMESHGUI_aParameter
144
145 public:
146   SMESHGUI_strParameter( const QString& initValue = "",
147                          const QString& label     = QString::null,
148                          const bool = false );
149   QString& InitValue() { return _initValue; }
150   virtual Type GetType() const;
151   virtual bool GetNewInt( int & Value ) const;
152   virtual bool GetNewDouble( double & Value ) const;
153   virtual bool GetNewText( QString & Value ) const;
154   virtual QWidget* CreateWidget( QWidget* ) const;
155   virtual void InitializeWidget( QWidget* ) const;
156   virtual void TakeValue( QWidget* );
157
158   virtual QString sigValueChanged() const;
159   
160 protected:
161   QString _initValue, _newValue;
162 };
163
164
165 /*!
166  *  \brief This class represents the base parameter which contains dependency of
167  *  shown state of other parameters on value of current
168  */
169 class SMESHGUI_dependParameter: public SMESHGUI_aParameter
170 {
171 public:
172   /*!
173    *  \brief This map describes what parameters must be shown when this parameter has value as key
174    *  The list contains some indices of parameters (for example, order in some list)
175    *  Value is integer based 0. If map is empty, it means that there is no dependencies.
176    */
177   typedef QValueList< int > IntList;
178   typedef QMap< int, IntList >  ShownMap;
179
180 public:
181   SMESHGUI_dependParameter( const QString& = QString::null, const bool = false );
182
183   const ShownMap&    shownMap() const;
184   ShownMap&          shownMap();
185   
186 private:
187   ShownMap     myShownMap;
188 };
189
190 /*!
191  *  \brief This class represents parameter which can have value from fixed set
192  */
193 class SMESHGUI_enumParameter: public SMESHGUI_dependParameter
194 {
195 public:
196   /*!
197    *  \brief Creates parameter with set of values 'values', default value 'init' and title 'label'
198    *  Every value can be described both by integer based 0 or by string value
199    */
200   SMESHGUI_enumParameter( const QStringList& values,
201                           const int init = 0,
202                           const QString& label = QString::null,
203                           const bool = false );
204   virtual ~SMESHGUI_enumParameter();
205
206   /*!
207    *  \brief Returns count of possible values
208    */
209   int            Count() const;
210
211   int& InitValue() { return myInitValue; }
212   virtual Type GetType() const;
213   virtual bool GetNewInt( int& ) const;
214   virtual bool GetNewDouble( double& ) const;
215   virtual bool GetNewText( QString& ) const;
216   virtual QWidget* CreateWidget( QWidget* ) const;
217   virtual void InitializeWidget( QWidget* ) const;
218   virtual void TakeValue( QWidget* );
219
220   virtual QString sigValueChanged() const;
221     
222 protected:
223   int         myInitValue, myValue;
224   QStringList myValues;
225 };
226
227
228 /*!
229  *  \brief This class represents parameter which can have value true or false
230  */
231 class SMESHGUI_boolParameter: public SMESHGUI_dependParameter
232 {
233 public:
234   SMESHGUI_boolParameter( const bool = false,
235                           const QString& = QString::null,
236                           const bool = false );
237   virtual ~SMESHGUI_boolParameter();
238
239   bool& InitValue() { return myInitValue; }
240   virtual Type GetType() const;
241   virtual bool GetNewInt( int& ) const;
242   virtual bool GetNewDouble( double& ) const;
243   virtual bool GetNewText( QString& ) const;
244   virtual QWidget* CreateWidget( QWidget* ) const;
245   virtual void InitializeWidget( QWidget* ) const;
246   virtual void TakeValue( QWidget* );
247
248   virtual QString sigValueChanged() const;
249   
250 protected:
251   bool myInitValue, myValue;
252 };
253
254
255 class QButton;
256 class SMESHGUI_tableParameter;
257
258
259 /*!
260  *  \brief This class represents custom table. It has only double values and
261     editor for every cell has validator
262  */
263 class SMESHGUI_Table : public QTable
264 {
265   Q_OBJECT
266   
267 public:
268   SMESHGUI_Table( const SMESHGUI_tableParameter*, int numRows, int numCols, QWidget* = 0, const char* = 0 );
269   virtual ~SMESHGUI_Table();
270
271 /*!
272  *  \brief Hides current editor of cell
273  */
274   void stopEditing();
275   
276   virtual QSize sizeHint() const;
277
278 /*!
279  *  \brief Returns parameters of double validator corresponding to cell (row,col)
280  */
281   void validator( const int row, const int col, double&, double&, int& );
282   
283 /*!
284  *  \brief Sets the double validator parameters to every cell in row range [rmin,rmax]
285  *         and column range [cmin,cmax].
286  *         If rmin=-1 then rmin is set to 0, if rmax=-1 then rmax = last row.
287  *         Analogically cmin and cmax are set
288  */
289   void setValidator( const double, const double, const int,
290                      const int rmin = -1, const int rmax = -1,
291                      const int cmin = -1, const int cmax = -1 );  
292
293 protected:
294   virtual void keyPressEvent( QKeyEvent* );
295   virtual bool eventFilter( QObject*, QEvent* );
296   virtual QWidget* createEditor( int, int, bool ) const;
297
298 private:
299   SMESHGUI_tableParameter*   myParam;
300 };
301
302
303 /*!
304  *  \brief This class represents frame for table and buttons
305  */
306 class SMESHGUI_TableFrame : public QFrame
307 {
308   Q_OBJECT
309   
310 public:
311 /*!
312  *  \brief Values corresponding to buttons for table resize
313  */
314   typedef enum { ADD_COLUMN, REMOVE_COLUMN, ADD_ROW, REMOVE_ROW } Button;
315
316 public:
317   SMESHGUI_TableFrame( const SMESHGUI_tableParameter*, QWidget* );
318   ~SMESHGUI_TableFrame();
319
320   SMESHGUI_Table* table() const;
321
322 /*!
323  *  \brief Changes shown state of some button for table resize
324  */   
325   void setShown( const Button, const bool );
326
327 /*!
328  *  \brief Returns shown state of some button for table resize
329  */
330   bool isShown( const Button ) const;
331   
332 private:
333   QButton* button( const Button ) const;
334
335 private slots:
336   void onButtonClicked();
337   
338 signals:
339 /*!
340  *  \brief This signal is emitted if some of button for table resize is clicked
341  *         Second parameter is current column for ADD_COLUMN, REMOVE_COLUMN buttons
342  *         and current row for ADD_ROW, REMOVE_ROW buttons. Take into account that
343  *         this object resize table ( returned by table() ) automatically
344  */
345   void toEdit( SMESHGUI_TableFrame::Button, int );
346   void valueChanged( int, int );
347
348 private:
349   QButton *myAddColumn, *myRemoveColumn, *myAddRow, *myRemoveRow;
350   SMESHGUI_Table*  myTable;
351 };
352
353
354 /*!
355  *  \brief This class represents parameter which can have two-dimensional array of values
356  */
357 class SMESHGUI_tableParameter: public QObject, public SMESHGUI_aParameter
358 {
359   Q_OBJECT
360   
361 public:
362 /*!
363  *  \brief Creates table parameter with default value 'init' and title 'label'.
364  *         The default value means that by default the table is filled with default value
365  *         and if new column or row is added then it is filled with default value
366  */
367   SMESHGUI_tableParameter( const double init = 0.0,
368                            const QString& label = QString::null,
369                            const bool preview = false );
370   virtual ~SMESHGUI_tableParameter();
371
372   virtual Type GetType() const;
373   virtual bool GetNewInt( int& ) const;
374   virtual bool GetNewDouble( double& ) const;
375   virtual bool GetNewText( QString& ) const;
376   virtual QWidget* CreateWidget( QWidget* ) const;
377   virtual void InitializeWidget( QWidget* ) const;
378   virtual void TakeValue( QWidget* );
379
380   static void sortData( SMESH::double_array& );
381
382 /*!
383  *  \brief Updates look of widget in accordance with all parameters of this object
384  */
385   void update( QWidget* ) const;
386   
387 /*!
388  *  \brief Returns data taken from widget. Please don't forget to call TakeValue before.
389  */
390   void data( SMESH::double_array& ) const;
391
392 /*!
393  *  \brief Sets data. The InitializeWidget must be called in order to change values in widget
394  */
395   void setData( const SMESH::double_array& );
396
397 /*!
398  *  \brief Sets count of columns and updates widget
399  */
400   void setColCount( const int, QWidget* = 0 );
401
402 /*!
403  *  \brief Sets count of rows and updates widget
404  */
405   void setRowCount( const int, QWidget* = 0 );
406   
407 /*!
408  *  \brief Binds count of columns to some parameter and updates widget. Take into account that
409  *         if this parameter is changed, the update() must be called to resize table
410  */
411   void setColCount( const SMESHGUI_aParameterPtr, QWidget* = 0 );
412
413 /*!
414  *  \brief Binds count of rows to some parameter and updates widget. Take into account that
415  *         if this parameter is changed, the update() must be called to resize table
416  */
417   void setRowCount( const SMESHGUI_aParameterPtr, QWidget* = 0 );
418   
419 /*!
420  *  \brief Enables or disables to change count of columns by buttons
421  */
422   void setEditCols( const bool );
423
424 /*!
425  *  \brief Enables or disables to change count of rows by buttons
426  */
427   void setEditRows( const bool );  
428
429   virtual QString sigValueChanged() const;
430   
431   void setValidator( const int col, const double, const double, const int );
432   void validator( const int col, double&, double&, int& ) const;
433
434 /*!
435  *  \brief These methods allow to read and change name of column
436  */
437   void    setColName( const int, const QString& );
438   QString colName( const int ) const;
439
440 private slots:
441   void onEdit( SMESHGUI_TableFrame::Button, int );
442   void onEdit( SMESHGUI_Table*, SMESHGUI_TableFrame::Button, int );
443
444 private:
445   void setItems( QWidget*, int = -1, int = -1, int = -1, int = -1 ) const;
446
447 private:
448   typedef struct
449   {
450     double myMin, myMax;
451     int    myDecimals;
452   } ValidatorInfo;
453
454   typedef QMap<int, ValidatorInfo>  ValidatorsMap;
455   
456 private:
457   int                      myColsInt, myRowsInt;
458   SMESHGUI_aParameterPtr   myCols, myRows;
459   double                   myInitValue;
460   SMESH::double_array      myData;
461   ValidatorsMap            myValidators;
462   bool                     myEditCols, myEditRows;
463   QMap< int, QString >     myColNames;
464
465   friend class SMESHGUI_Table;
466 };
467
468 #endif // SMESHGUI_aParameter.h
469