]> SALOME platform Git repositories - modules/yacs.git/blob - src/gui/YACSGui_Table.h
Salome HOME
merge from branch DEV tag mergeto_trunk_04apr08
[modules/yacs.git] / src / gui / YACSGui_Table.h
1 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 // 
4 //  This library is free software; you can redistribute it and/or 
5 //  modify it under the terms of the GNU Lesser General Public 
6 //  License as published by the Free Software Foundation; either 
7 //  version 2.1 of the License. 
8 // 
9 //  This library is distributed in the hope that it will be useful, 
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 //  Lesser General Public License for more details. 
13 // 
14 //  You should have received a copy of the GNU Lesser General Public 
15 //  License along with this library; if not, write to the Free Software 
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17 // 
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21 #ifndef YACSGui_TABLE_HEADER
22 #define YACSGui_TABLE_HEADER
23
24 #include <QtxTable.h>
25 #include <qvaluevector.h>
26 #include <qtoolbutton.h>
27
28 #include <list>
29
30 typedef std::list<int> IntListVal;
31 typedef std::list<double> DoubleListVal;
32
33 /*!
34  * Class, intended to create table with different types of values entered to columns. 
35  * The following types are possible: int, double (created as spinboxes), 
36  * string (created as lineedit) and it's possible to specify combobox.
37  */
38 class YACSGui_Table : public QtxTable
39 {
40   Q_OBJECT
41
42 public:
43   /*enum, defines types of columns
44    */
45   typedef enum { String, Int, Double, Combo, ComboSelect } CellType;
46
47 public:
48   YACSGui_Table( const int numRows, const int numCols, QWidget*, CellType = Int );
49   virtual ~YACSGui_Table();
50
51   /*!
52    * Returns type assigned to all cells of the table by default
53    */
54   CellType          defCellType() const;
55
56   /*!
57    * Set type of cells, which will be set to table by default
58    */
59   void             setDefCellType( CellType );
60
61   /*!
62    * Returns type of given cell, if cell type is not set 
63    * evidently then returns type of cell which is set by default.
64    * Parameters: int - row number
65    *             int - column number
66    *             bool - returns true if it is possible to determine column type, otherwise - false
67    */
68   CellType          cellType( const int, const int, bool& ) const;
69
70   /*!
71    * Set type for cell at the table
72    * Parameters: int - row number, if row < 0 ( i.e. row is not defined),
73    *                   set the same types for all rows in the given column
74    *             int - column number
75    *             CellType - cell type 
76    */
77   void             setCellType( const int, const int, CellType );
78
79
80   /*!
81    * Returns value by default for given type
82    */
83   QString          defValue( CellType ) const;
84
85   /*!
86    * Sets value by default for given type
87    */
88   void             setDefValue( CellType, const QString& );
89
90   /*!
91    * Returns value by default for given row and column (i.e. cell)
92    */
93   QString          defValue( const int, const int ) const;
94
95   /*!
96    * Sets value by default for given row and column (i.e. cell)
97    */
98   void             setDefValue( const int, const int, const QString& );
99
100
101   /*!
102    * This method finishes ediitng the cell contents in a cell specified by currEditRow() and currEditCol(). 
103    * Modifications are accepted or rejected depending on <accept> parameter value
104    */
105   void             stopEdit( const bool );
106
107
108   /*!
109    * Returns list of double values for given column. If value is not defined 
110    * in row of the table then 0 will be added to the list 
111    * and parameter bool will return false
112    * Paramrters: first int - column number
113    *             second int - It is an index of string starting from which 
114    *                          all following strings are ignored, i.e. their 
115    *                          values are not added to the list 
116    *             ok - returns true if all values from the strings 
117    *                  had been sucessfully casted to double.
118    */
119   DoubleListVal    doubles( const int, const int = 0, bool* ok = 0 ) const;
120
121   /*!
122    * Fill given column with values, given at list. If size of list is lesser that number 
123    * of rows in the table then the rest rows are filled by values set by parameter def.
124    * Parameters: col - column number
125    *             DoubleListVal- list of values
126    *             def- value for rows which are not given at list
127    *             Resize - determines if need to change number of rows in the table 
128    *                      according to given list size
129    */
130   void             setDoubles( const int, const DoubleListVal&, const double, const bool = false );
131
132   /*!
133    * Returns list of integer values for given column. If value is not defined 
134    * in row of the table then 0 will be added to the list 
135    * and parameter bool will return false
136    * Paramrters: first int - column number
137    *             ok - returns true if all values from the strings 
138    *                  had been sucessfully casted to double.
139    */
140   IntListVal       integers( const int, bool* ok = 0 ) const;
141
142   /*!
143    * Fill given column with values, given at list. If size of list is lesser that number 
144    * of rows in the table then the rest rows are filled by values set by parameter def.
145    * Parameters: col - column number
146    *             DoubleListVal- list of values
147    *             def- value for string which are not given at list
148    *             Resize - determines if need to change number of rows in the table 
149    *                      according to given list size
150    */
151   void             setIntegers( const int, const IntListVal&, const int, const bool = false );
152
153   /*!
154    * Returns list of string values for given column into QStringList. If value is not defined 
155    * in row of the table then QString::null will be added to the list 
156    * Paramrters: first int - column number
157    *             QStringList - list of string values
158    */
159   void             strings( const int, QStringList& ) const;
160
161   /*!
162    * Fill given column with values, given at list.
163    * Parameters: col - column number
164    *             DoubleListVal- list of values
165    *             Resize - determines if need to change number of rows in the table 
166    *                      according to given list size
167    */
168   void             setStrings( const int, const QStringList&, const bool = false );
169
170   /*!
171    * Returns list of integer values for given column. If value is not defined 
172    * in row of the table then -1 will be added to the list 
173    * Paramrters: int - column number
174    */
175   IntListVal       valueComboBox( const int ) const;
176
177   /*!
178    * Returns values of integer of item for given row and column
179    */
180   int              intValue( const int, const int, bool* ok = 0 ) const;
181
182   /*!
183    * Returns values of double of item for given row and column
184    */
185   double           doubleValue( const int, const int, bool* ok = 0 ) const;
186
187   /*!
188    * Returns values of integer of item for given row and column
189    */
190   int              intValueCombo( const int, const int ) const;
191
192   /*!
193    * Allow getting value of special table field: when this field is true, 
194    * the contents of the cell being edited are synchroinized with the contents of 
195    * the cell editor widget, and valueChanged() signal is emitted each time the 
196    * editor contents change - for instance, each time the user types a character 
197    * in a line edit that is used as a cell editor.
198    */
199   bool             isEditorSync() const;
200
201   /*!
202    * Allow setting value of special table field: when this field is true, 
203    * the contents of the cell being edited are synchroinized with the contents of 
204    * the cell editor widget, and valueChanged() signal is emitted each time the 
205    * eidtor contents change - for instance, eahc time the user types a character 
206    * in a line edit that is used as a cell editor.
207    */
208   void             setEditorSync( const bool );
209
210   /*!
211    * Returns values(true/false) of item for given row and column
212    * Parameters: first int - row - row number
213    *             second int - col - column number
214    */
215   bool             readOnly( const int, const int ) const;
216
217   /*!
218    * Sets flag read only for given row and column.
219    * Parameters: first int - row - row number, if row < 0 ( i.e. row is not defined),
220    *                               set the read only flag for all rows in the given column
221    *             second int - col - column number
222    */
223   void             setReadOnly( const int, const int, const bool );
224
225   /*!
226    * Rebuilds table items if necessary, for instance when the number of rows 
227    * or columns changes.
228    */
229   void             update();
230
231   /*!
232    * Generates list of strings for given row and column (i.e. cell) which will be used at combobox
233    * Parameters: int - row - row number, if row < 0 ( i.e. row is not defined),
234    *                   set the same parameters for all rows in the given column
235    *             int - col - column number
236    *             Text - values for combobox for given column separated by ";"
237    */
238   void             setParams( const int, const int, const QString& );
239
240   /*!
241    * Returns list of strings for indexes of row and column (i.e. cell) which defines it's combobox contents
242    * Parameters: int - row - row number
243    *             int - col - column number
244    */
245   QStringList      Params( int, int ) const;
246
247   /*!
248    * Returns number of selected row
249    */
250   int              selectedRow() const; 
251   
252   virtual QRect    cellGeometry ( int row, int col ) const;
253
254 signals:
255   void             itemCreated( const int, const int );
256   void             selectButtonClicked( const int, const int );
257   
258 public slots:
259   /*!
260    * Sets the number of the columns at the table. called method update()
261    */
262   virtual void setNumCols( int c );
263
264   /*!
265    * Sets the number of the rows at the table. called method update()
266    */
267   virtual void setNumRows( int r );
268
269   /*!
270    * Inserts "count" empty rows at row "row"
271    */
272   virtual void insertRows( int row, int count = 1 );
273
274   /*!
275    * Inserts "count" empty columns at column "col"
276    */
277   virtual void insertColumns( int col, int count = 1 );
278
279   /*!
280    * Redefine this slot to show/hide the cell's selection control
281    */ 
282   virtual void setCurrentCell ( int row, int col );
283
284   void onSelectButton();
285
286 protected:
287   /*!
288    * creates new item for tables with type,
289    * defined by CellType and by value, defined by default at row row and column col
290    */
291   virtual QTableItem*  createItem( const int, const int, CellType ) const;
292   /*!
293    * Returns type of the table item 
294    */
295   virtual CellType   itemType( QTableItem* ) const;
296
297   virtual void endEdit( int row, int col, bool accept, bool replace );
298
299 protected slots:
300   virtual void columnWidthChanged( int col );
301
302   virtual void rowHeightChanged( int row );
303
304 private slots:
305   /*!
306    * slot which is called when syncronization is enabled - it 
307    * receives new value from editor and saves it ro cell and emits valueChanged()
308    */
309   void onSync();
310
311 private:
312   /*!
313    * change number of strings according to count when resize == true, 
314    * otherwise retur miniman value from count or numRows
315    */
316   int    updateSize( const int, const bool ); 
317
318 private:
319   CellType                    myDefType;
320   bool                       myIsSync;
321   QMap<CellType, QString>     myTDefValues;
322   
323   QMap<int, QMap<int, QString> > myRCDefValues; // row <-> ( column <-> DefValue )
324
325   QMap<int, QMap<int, QStringList> > myRCComboBoxParam; // row <-> ( column <-> Params )
326   
327   QValueVector<CellType>      myColTypes;
328   QMap<int, QMap<int, CellType> > myCellTypes; //column <-> ( row <-> cellType )
329
330 };
331
332 /*!
333  * The YACSGui_TableItem class provides the cell content for YACSGui_Table cells.
334  */
335 class YACSGui_TableItem : public QTableItem
336 {
337 public:
338   YACSGui_TableItem( QTable*, YACSGui_Table::CellType );
339   virtual ~YACSGui_TableItem();
340
341   /*!
342    * Returns type of item 
343    */
344   YACSGui_Table::CellType  type() const;
345
346   /*!
347    * Sets text for current item
348    */
349   virtual void     setText( const QString& );
350
351   /*!
352    * This virtual function creates an editor which the user can interact with to edit the cell's contents. 
353    */
354   virtual QWidget* createEditor() const;
355
356   /*!
357    * Updates cell contents after cell editing has been finished. Retrieves values from the editor widget.
358    */
359   virtual void     setContentFromEditor( QWidget* );
360
361   /*!
362    * Returns flag read only for item.
363    */
364   bool readOnly() const;
365
366   /*!
367    * Sets flag read only for item.
368    */
369   void setReadOnly( const bool );
370
371 protected:
372   bool                       myIsReadOnly;
373   YACSGui_Table::CellType     myType;
374
375 private:
376 };
377
378 /*!
379  * The YACSGui_TableComboSelectItem class provides the cell content with combo box and selection control for YACSGui_Table cells.
380  */
381 class YACSGui_TableComboSelectItem : public YACSGui_TableItem
382 {
383 public:
384   YACSGui_TableComboSelectItem( QTable*, bool );
385   virtual ~YACSGui_TableComboSelectItem();
386
387   /*!
388    * Sets text for current item
389    */
390   virtual void     setText( const QString& );
391
392   /*!
393    * This virtual function creates an editor which the user can interact with to edit the cell's contents. 
394    */
395   virtual QWidget* createEditor() const;
396
397   /*!
398    * Updates cell contents after cell editing has been finished. Retrieves values from the editor widget.
399    */
400   virtual void     setContentFromEditor( QWidget* );
401
402   virtual void     showEditor( bool theIsEditing = false );
403   virtual void     hideEditor();
404
405   QToolButton*     selectButton() const { return mySelectBtn; }
406
407   void             showText( QString theText );
408   
409 private:
410   void             createSelectButton();
411   void             placeEditor( bool theIsEditing );
412   void             updateSelectButtonState();
413
414   QToolButton*     mySelectBtn;
415   bool             myEditable;
416 };
417
418 #endif