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