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