Salome HOME
Fixes for compilation errors.
[modules/visu.git] / src / VISUGUI / VisuGUI_TableDlg.cxx
1 //  VISU VISUGUI : GUI of VISU component
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : VisuGUI_TableDlg.cxx
8 //  Author : Vadim SANDLER
9 //  Module : SALOME
10
11 #include "VisuGUI_TableDlg.h"
12
13 #include "SUIT_Tools.h"
14 #include "SUIT_MessageBox.h"
15
16 #include "SALOMEDSClient_Study.hxx"
17 #include "SALOMEDSClient_GenericAttribute.hxx"
18 #include "SALOMEDSClient_AttributeTableOfInteger.hxx"
19 #include "SALOMEDSClient_AttributeTableOfReal.hxx"
20 #include "SALOMEDSClient_StudyBuilder.hxx"
21
22 #include <qlayout.h>
23 #include <qvalidator.h>
24 #include <qtable.h>
25 #include <qtabwidget.h>
26 #include <qvaluelist.h>
27 #include <qmemarray.h>
28 #include <qinputdialog.h>
29 #include <qlabel.h>
30 #include "utilities.h"
31 using namespace std;
32
33 #define MARGIN_SIZE       11
34 #define SPACING_SIZE      6
35 #define SPACER_SIZE       5
36 #define MIN_TABLE_WIDTH   200
37 #define MIN_TABLE_HEIGHT  200
38
39
40 class VisuGUI_Table : public QTable {
41 public:
42   VisuGUI_Table( Orientation orient, QWidget* parent = 0, const char* name = 0 ) 
43     : QTable( parent, name ), myValidator( 0 ), myOrientation( orient ) {}
44   VisuGUI_Table( Orientation orient, int numRows, int numCols, QWidget* parent = 0, const char* name = 0 )
45     : QTable( numRows, numCols, parent, name ), myValidator( 0 ), myOrientation( orient ) {}
46   
47   void setValidator( QValidator* v = 0 ) { myValidator = v;  }
48   bool isEditing() const { return QTable::isEditing(); }
49   
50 protected:
51   QWidget* createEditor ( int row, int col, bool initFromCell ) const
52     {
53       bool testUnits = ( myOrientation == Horizontal && col == 0 ) || ( myOrientation == Vertical && row == 0 );
54       QWidget* wg = QTable::createEditor( row, col, initFromCell );
55       if ( wg && wg->inherits("QLineEdit") && myValidator && !testUnits ) 
56         (( QLineEdit*)wg)->setValidator( myValidator );
57       return wg;
58     }
59
60 protected:
61   QValidator* myValidator;
62   Orientation myOrientation;
63 };
64
65 /*!
66   Constructor
67 */
68 VisuGUI_TableDlg::VisuGUI_TableDlg( QWidget* parent, 
69                                     _PTR(SObject) obj, 
70                                     bool edit,
71                                     int which,
72                                     Orientation orient,
73                                     bool showColumnTitles )
74      : QDialog( parent, "", false, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose),
75        myIntTable( 0 ), myRealTable( 0 )
76 {
77   setCaption( edit ? tr( "EDIT_TABLE_TLT" ) : tr( "VIEW_TABLE_TLT" ) );
78   setSizeGripEnabled( true );
79
80   myObject = obj;
81   bool bHasIntTable = false;
82   bool bHasRealTable = false;
83   if ( myObject ) {
84     _PTR(GenericAttribute) anAttr;
85     bHasIntTable  = myObject->FindAttribute( anAttr, "AttributeTableOfInteger");
86     bHasRealTable = myObject->FindAttribute( anAttr, "AttributeTableOfReal");
87   }
88   
89   QVBoxLayout* mainLayout = new QVBoxLayout( this );
90   mainLayout->setMargin( MARGIN_SIZE );
91   mainLayout->setSpacing( SPACING_SIZE );
92
93   bool bDoInt  = which == ttInt  || which == ttBoth || which == ttAuto && bHasIntTable;
94   bool bDoReal = which == ttReal || which == ttBoth || which == ttAuto && bHasRealTable;
95
96   QWidget* top;
97   QVBoxLayout* tl;
98   if ( bDoInt && bDoReal ) {
99     top = new QTabWidget( this, "TabWidget" );
100     ( ( QTabWidget* ) top) ->setMargin( MARGIN_SIZE );
101   }
102   else {
103     top = new QWidget( this, "DumbWidget" );
104     tl  = new QVBoxLayout( top ); tl->setMargin( 0 ); tl->setSpacing( SPACING_SIZE );
105   }
106
107   if ( bDoInt ) {
108     myIntTable = new VisuGUI_TableWidget( top, "myIntTable", edit, orient, showColumnTitles );
109     myIntTable->getTable()->setValidator( new QIntValidator( this ) );
110     if ( bDoInt && bDoReal )
111       ( ( QTabWidget* )top )->addTab( myIntTable, tr( "TABLE_OF_INTEGER_TLT" ) );
112     else
113       tl->addWidget( myIntTable );
114   }
115   if ( bDoReal ) {
116     myRealTable = new VisuGUI_TableWidget( top, "myRealTable", edit, orient, showColumnTitles );
117     myRealTable->getTable()->setValidator( new QDoubleValidator( this ) );
118     if ( bDoInt && bDoReal )
119       ( ( QTabWidget* )top )->addTab( myRealTable, tr( "TABLE_OF_REAL_TLT" ) );
120     else
121       tl->addWidget( myRealTable );
122   }
123   if ( !bDoInt && !bDoReal ) {
124     QLabel *dumbLabel = new QLabel( tr( "ERR_TABLE_NOT_AVAILABLE" ), top, "DumbLabel" );
125     dumbLabel->setAlignment( AlignCenter );
126     tl->addWidget( dumbLabel );
127   }
128
129   QHBoxLayout* btnLayout = new QHBoxLayout; 
130   btnLayout->setMargin( 0 ); btnLayout->setSpacing( SPACING_SIZE );
131   
132   myOKBtn = new QPushButton( tr( "BUT_OK" ), this );
133   if ( edit ) {
134     myCancelBtn = new QPushButton( tr( "BUT_CANCEL" ), this );
135     btnLayout->addWidget( myOKBtn );
136     btnLayout->addItem( new QSpacerItem( SPACER_SIZE, SPACER_SIZE, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
137     btnLayout->addWidget( myCancelBtn );
138     connect( myOKBtn,     SIGNAL( clicked() ), this, SLOT( onOK() ) );
139     connect( myCancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
140   }
141   else {
142     btnLayout->addItem( new QSpacerItem( SPACER_SIZE, SPACER_SIZE, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
143     btnLayout->addWidget( myOKBtn );
144     btnLayout->addItem( new QSpacerItem( SPACER_SIZE, SPACER_SIZE, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
145     connect( myOKBtn,     SIGNAL( clicked() ), this, SLOT( accept() ) );
146   }
147
148   mainLayout->addWidget( top );
149   mainLayout->addLayout( btnLayout );
150
151   initDlg();
152   resize( 500, 400 );
153   SUIT_Tools::centerWidget( this, parent );
154 }
155
156 /*!
157   Destructor
158 */
159 VisuGUI_TableDlg::~VisuGUI_TableDlg()
160 {
161 }
162
163 /*!
164   <OK> button slot, saves table(s)
165   Called only in create/edit mode ( <edit> parameter for constructor is true )
166 */
167 void VisuGUI_TableDlg::onOK()
168 {
169   myOKBtn->setFocus(); // accept possible changes
170   bool done = true;
171
172   if ( myObject ) {
173     _PTR(Study) study = myObject->GetStudy();
174     _PTR(AttributeTableOfInteger) tblIntAttr;
175     _PTR(AttributeTableOfReal)    tblRealAttr;
176
177     if ( study ) {
178       _PTR(StudyBuilder) builder = study->NewBuilder();
179       builder->NewCommand(); // start transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
180       try {
181         if ( myIntTable ) {
182           builder->RemoveAttribute( myObject, "AttributeTableOfInteger" );
183           tblIntAttr = builder->FindOrCreateAttribute( myObject, "AttributeTableOfInteger" );
184
185           int i;
186           int nbRows  = myIntTable->getNumRows();
187           int nbCols  = myIntTable->getNumCols();
188           QString tlt = myIntTable->getTableTitle();
189           QStringList rowTitles, colTitles, units;
190           myIntTable->getRowTitles( rowTitles );
191           myIntTable->getColTitles( colTitles );
192           myIntTable->getUnits( units );
193           
194           if ( nbRows > 0) {
195             // data
196             int nRow = 0;
197             tblIntAttr->SetNbColumns( nbCols );
198             for ( i = 0; i < nbRows; i++ ) {
199               QStringList data;
200               myIntTable->getRowData( i, data );
201               bool bEmptyRow = true;
202               for ( int j = 0; j < data.count(); j++ ) {
203                 if ( !data[ j ].isNull() ) {
204                   tblIntAttr->PutValue( data[ j ].toInt(), nRow+1, j+1 );
205                   bEmptyRow = false;
206                 }
207               }
208               if ( !bEmptyRow ) {  // Skip rows with no data !!!
209                 // set row title
210                 tblIntAttr->SetRowTitle( nRow+1, rowTitles[ i ].isNull() ? "" : rowTitles[ i ].latin1() ); 
211                 // set row unit
212                 tblIntAttr->SetRowUnit( nRow+1, units[ i ].isNull() ? "" : units[ i ].latin1() ); 
213                 nRow++;
214               }
215             }
216             if ( nRow > 0 ) { // Set columns only if table is not empty, otherwise exception is raised !!!
217               // column titles
218               for ( i = 0; i < colTitles.count(); i++ )
219                 tblIntAttr->SetColumnTitle( i+1, colTitles[ i ].isNull() ? "" : colTitles[ i ].latin1() );
220             }
221           }
222           // title
223           tblIntAttr->SetTitle( myIntTable->getTableTitle().latin1() );
224         }
225         if ( myRealTable ) {
226           builder->RemoveAttribute( myObject, "AttributeTableOfReal" );
227           tblRealAttr = builder->FindOrCreateAttribute( myObject, "AttributeTableOfReal" );
228
229           int i;
230           int nbRows  = myRealTable->getNumRows();
231           int nbCols  = myRealTable->getNumCols();
232           QString tlt = myRealTable->getTableTitle();
233           QStringList rowTitles, colTitles, units;
234           myRealTable->getRowTitles( rowTitles );
235           myRealTable->getColTitles( colTitles );
236           myRealTable->getUnits( units );
237           
238           if ( nbRows > 0) {
239             // data
240             int nRow = 0;
241             tblRealAttr->SetNbColumns( nbCols );
242             for ( i = 0; i < nbRows; i++ ) {
243               QStringList data;
244               myRealTable->getRowData( i, data );
245               bool bEmptyRow = true;
246               for ( int j = 0; j < data.count(); j++ ) {
247                 if ( !data[ j ].isNull() ) {
248                   tblRealAttr->PutValue( data[ j ].toDouble(), nRow+1, j+1 );
249                   bEmptyRow = false;
250                 }
251               }
252               if ( !bEmptyRow ) {  // Skip rows with no data !!!
253                 // set row title
254                 tblRealAttr->SetRowTitle( nRow+1, rowTitles[ i ].isNull() ? "" : rowTitles[ i ].latin1() ); 
255                 // set row unit
256                 tblRealAttr->SetRowUnit( nRow+1, units[ i ].isNull() ? "" : units[ i ].latin1() );
257                 nRow++;
258               }
259             }
260             if ( nRow > 0 ) { // Set columns only if table is not empty, otherwise exception is raised !!!
261               // column titles
262               for ( i = 0; i < colTitles.count(); i++ )
263                 tblRealAttr->SetColumnTitle( i+1, colTitles[ i ].isNull() ? "" : colTitles[ i ].latin1() );
264             }
265           }
266           // title
267           tblRealAttr->SetTitle( myRealTable->getTableTitle().latin1() );
268         }
269         if ( myIntTable || myRealTable)
270           builder->CommitCommand(); // commit transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!
271         else
272           builder->AbortCommand();  // abort transaction  !!!!!!!!!!!!!!!!!!!!!!!!!!!
273       }
274       catch( ... ) {
275         MESSAGE("VisuGUI_TableDlg::onOK : Exception has been caught !!!");
276         builder->AbortCommand();  // abort transaction  !!!!!!!!!!!!!!!!!!!!!!!!!!!
277         done = false;
278         SUIT_MessageBox::error1 ( this, tr("ERR_ERROR"), tr("ERR_APP_EXCEPTION"), tr ("BUT_OK") );
279       }
280     }
281   }
282   if ( done ) 
283     accept();
284 }
285
286 /*!
287    Populates table with data
288 */
289 void VisuGUI_TableDlg::initDlg()
290 {
291   int i, j;
292   if ( myObject ) {
293     _PTR(GenericAttribute) anAttr;
294     _PTR(AttributeTableOfInteger) tblIntAttr;
295     _PTR(AttributeTableOfReal)    tblRealAttr;
296     if ( myObject->FindAttribute( anAttr, "AttributeTableOfInteger") ) {
297       tblIntAttr = anAttr;
298     }
299     if ( myObject->FindAttribute( anAttr, "AttributeTableOfReal") ) {
300       tblRealAttr = anAttr;
301     }
302     // Table of integer
303     if ( tblIntAttr && myIntTable ) {
304       try {
305         // title
306         myIntTable->setTableTitle( tblIntAttr->GetTitle().c_str() );
307         // nb of rows & cols
308         int nbRows = tblIntAttr->GetNbRows() ; 
309         int nbCols = tblIntAttr->GetNbColumns();
310         myIntTable->setNumRows( nbRows );
311         myIntTable->setNumCols( nbCols );
312         // rows titles
313         QStringList strlist;
314         vector<string> rowTitles = tblIntAttr->GetRowTitles();
315         for ( i = 0; i < nbRows; i++ ) {
316           if ( rowTitles.size() > 0 )
317             strlist.append( rowTitles[i].c_str() );
318           else
319             strlist.append( "" );
320         }
321         myIntTable->setRowTitles( strlist );
322         // columns titles
323         strlist.clear();
324         vector<string> colTitles = tblIntAttr->GetColumnTitles();
325         for ( i = 0; i < nbCols; i++ ) {
326           if ( colTitles.size() > 0 )
327             strlist.append( colTitles[i].c_str() );
328           else
329             strlist.append( "" );
330         }
331         myIntTable->setColTitles( strlist );
332         // units
333         strlist.clear();
334         vector<string> rowUnits = tblIntAttr->GetRowUnits();
335         if ( rowUnits.size() > 0 ) {
336           for ( i = 0; i < nbRows; i++ )
337             strlist.append( rowUnits[i].c_str() );
338           myIntTable->setUnits( strlist );
339         }
340         // data
341         for ( i = 1; i <= nbRows; i++ ) {
342           strlist.clear();
343           for ( j = 1; j <= nbCols; j++ ) {
344             if ( tblIntAttr->HasValue( i, j ) )
345               strlist.append( QString::number( tblIntAttr->GetValue( i, j ) ) );
346             else
347               strlist.append( QString::null );
348           }
349           myIntTable->setRowData( i-1, strlist );
350         }
351         myIntTable->adjustTable();
352       }
353       catch( ... ) {
354         MESSAGE("VisuGUI_TableDlg::initDlg : Exception has been caught !!!");
355       }
356     } 
357     // Table of real
358     if ( tblRealAttr && myRealTable ) {
359       try {
360         // title
361         myRealTable->setTableTitle( tblRealAttr->GetTitle().c_str() );
362         // nb of rows & cols
363         int nbRows = tblRealAttr->GetNbRows() ; 
364         int nbCols = tblRealAttr->GetNbColumns();
365         myRealTable->setNumRows( nbRows );
366         myRealTable->setNumCols( nbCols );
367         // rows titles
368         QStringList strlist;
369         vector<string> rowTitles = tblRealAttr->GetRowTitles();
370         for ( i = 0; i < nbRows; i++ ) {
371           if ( rowTitles.size() > 0 )
372             strlist.append( rowTitles[i].c_str() );
373           else
374             strlist.append( "" );
375         }
376         myRealTable->setRowTitles( strlist );
377         // columns titles
378         strlist.clear();
379         vector<string> colTitles = tblRealAttr->GetColumnTitles();
380         for ( i = 0; i < nbCols; i++ ) {
381           if ( colTitles.size() > 0 )
382             strlist.append( colTitles[i].c_str() );
383           else
384             strlist.append( "" );
385         }
386         myRealTable->setColTitles( strlist );
387         // units
388         strlist.clear();
389         vector<string> rowUnits = tblRealAttr->GetRowUnits();
390         if ( rowUnits.size() > 0 ) {
391           for ( i = 0; i < nbRows; i++ )
392             strlist.append( rowUnits[i].c_str() );
393           myRealTable->setUnits( strlist );
394         }
395         // data
396         for ( i = 1; i <= nbRows; i++ ) {
397           strlist.clear();
398           for ( j = 1; j <= nbCols; j++ ) {
399             if ( tblRealAttr->HasValue( i, j ) )
400               strlist.append( QString::number( tblRealAttr->GetValue( i, j ) ) );
401             else
402               strlist.append( QString::null );
403           }
404           myRealTable->setRowData( i-1, strlist );
405         }
406         myRealTable->adjustTable();
407       }
408       catch( ... ) {
409         MESSAGE("VisuGUI_TableDlg::initDlg : Exception has been caught !!!");
410       }
411     } 
412   }
413 }
414
415 /*!
416   Constructor
417 */
418 VisuGUI_TableWidget::VisuGUI_TableWidget( QWidget* parent, 
419                                               const char* name, 
420                                               bool edit, 
421                                               Orientation orient,
422                                               bool showColumnTitles )
423      : QWidget( parent, name ), myOrientation( orient )
424 {
425   QGridLayout* mainLayout = new QGridLayout( this );
426   mainLayout->setMargin( 0 );
427   mainLayout->setSpacing( SPACING_SIZE );
428
429   myTitleEdit = new QLineEdit( this, "TitleEdit" );
430   myTitleEdit->setAlignment( AlignCenter );
431   myTitleEdit->setReadOnly( !edit );
432   QFont fnt = myTitleEdit->font();
433   fnt.setBold( true ); 
434   myTitleEdit->setFont( fnt );
435
436   myTable = new VisuGUI_Table( orient, this, "Table" );
437   myTable->setNumRows( 5 );
438   myTable->setNumCols( 5 );
439   myTable->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
440   myTable->setMinimumSize( MIN_TABLE_WIDTH, MIN_TABLE_HEIGHT );
441   myTable->setSelectionMode( QTable::Single );
442   myTable->setShowGrid( true );
443   myTable->setColumnMovingEnabled( false );
444   myTable->setRowMovingEnabled( false );
445   myTable->setReadOnly( !edit );
446   myTable->setDragEnabled( false );
447   setUnitsTitle( tr( "UNITS_TLT" ) );
448
449   if ( !showColumnTitles ) {
450     if ( myOrientation == Horizontal ) {
451       myTable->horizontalHeader()->hide();
452       myTable->setTopMargin( 0 );
453     }
454     else {
455       myTable->verticalHeader()->hide();
456       myTable->setLeftMargin( 0 );
457     }
458   }
459
460   mainLayout->addWidget( myTitleEdit, 0, 0 );
461   mainLayout->addWidget( myTable, 1, 0 );
462
463   if ( edit ) {
464     myAddRowBtn    = new QPushButton( tr( "ADD_ROW_BTN" ), this, "AddRowBtn" );
465     myDelRowBtn    = new QPushButton( tr( "REMOVE_ROW_BTN" ), this, "DelRowBtn" );
466     myAddColBtn    = new QPushButton( tr( "ADD_COLUMN_BTN" ), this, "AddColBtn" );
467     myDelColBtn    = new QPushButton( tr( "REMOVE_COLUMN_BTN" ), this, "DelColBtn" );
468     myAdjustBtn    = new QPushButton( tr( "ADJUST_CELLS_BTN" ), this, "AdjustBtn" );
469     mySelectAllBtn = new QPushButton( tr( "SELECT_ALL_BTN" ), this, "SelectAllBtn" );
470     myClearBtn     = new QPushButton( tr( "CLEAR_BTN"), this, "ClearBtn" );
471     QVBoxLayout* btnLayout = new QVBoxLayout; btnLayout->setMargin( 0 ); btnLayout->setSpacing( SPACING_SIZE );
472     btnLayout->addWidget( myAddRowBtn );
473     btnLayout->addWidget( myDelRowBtn );
474     btnLayout->addWidget( myAddColBtn );
475     btnLayout->addWidget( myDelColBtn );
476     btnLayout->addStretch();
477     btnLayout->addWidget( myAdjustBtn );
478     btnLayout->addStretch();
479     btnLayout->addWidget( mySelectAllBtn );
480     btnLayout->addWidget( myClearBtn );
481     mainLayout->addLayout( btnLayout, 1, 1 );
482     connect( myTable, SIGNAL( selectionChanged() ),        this, SLOT( updateButtonsState() ) );
483     connect( myTable, SIGNAL( currentChanged( int, int) ), this, SLOT( updateButtonsState() ) );
484     connect( myAddRowBtn,    SIGNAL( clicked() ),   this, SLOT( addRow() ) );
485     connect( myAddColBtn,    SIGNAL( clicked() ),   this, SLOT( addCol() ) );
486     connect( myDelRowBtn,    SIGNAL( clicked() ),   this, SLOT( delRow() ) );
487     connect( myDelColBtn,    SIGNAL( clicked() ),   this, SLOT( delCol() ) );
488     connect( myAdjustBtn,    SIGNAL( clicked() ),   this, SLOT( adjustTable() ) );
489     connect( mySelectAllBtn, SIGNAL( clicked() ),   this, SLOT( selectAll() ) );
490     connect( myClearBtn,     SIGNAL( clicked() ),   this, SLOT( clearTable() ) );
491     myTable->horizontalHeader()->installEventFilter( this );
492     myTable->verticalHeader()->installEventFilter( this );
493     myTable->installEventFilter( this );
494   }
495   updateButtonsState();
496 }
497 /*!
498   Destructor
499 */
500 VisuGUI_TableWidget::~VisuGUI_TableWidget()
501 {
502 }
503 /*!
504   Sets table title
505 */
506 void VisuGUI_TableWidget::setTableTitle( const QString& title )
507 {
508   myTitleEdit->setText( title );
509 }
510 /*!
511   Gets table title
512 */
513 QString VisuGUI_TableWidget::getTableTitle()
514 {
515   return myTitleEdit->text();
516 }
517 /*!
518   Sets total number of rows
519 */
520 void VisuGUI_TableWidget::setNumRows( const int num )
521 {
522   myOrientation == Horizontal ? myTable->setNumRows( num ) : myTable->setNumCols( num );
523 }
524 /*!
525   Gets total number of rows
526 */
527 int VisuGUI_TableWidget::getNumRows()
528 {
529   return myOrientation == Horizontal ? myTable->numRows() : myTable->numCols();
530 }
531 /*!
532   Sets total number of columns
533 */
534 void VisuGUI_TableWidget::setNumCols( const int num )
535 {
536   // !!! first column contains units !!!
537   myOrientation == Horizontal ? myTable->setNumCols( num+1 ) : myTable->setNumRows( num+1 );
538 //  myOrientation == Horizontal ? myTable->setColumnReadOnly( 0, true ) : myTable->setRowReadOnly( 0, true );
539 }
540 /*!
541   Gets total number of columns
542 */
543 int VisuGUI_TableWidget::getNumCols()
544 {
545   // !!! first column contains units !!!
546   return myOrientation == Horizontal ? myTable->numCols()-1 : myTable->numRows()-1;
547 }
548 /*!
549   Sets rows titles
550 */
551 void VisuGUI_TableWidget::setRowTitles( QStringList& tlts )
552 {
553   for ( int i = 0; i < tlts.count(); i++ ) {
554     myOrientation == Horizontal ? 
555       myTable->verticalHeader()->setLabel( i, tlts[i] ) : 
556       myTable->horizontalHeader()->setLabel( i, tlts[i] );
557   }
558 }
559 /*!
560   Gets rows titles
561 */
562 void VisuGUI_TableWidget::getRowTitles( QStringList& tlts )
563 {
564   tlts.clear();
565   if ( myOrientation == Horizontal ) {
566     for ( int i = 0; i < myTable->numRows(); i++ ) {
567       tlts.append( myTable->verticalHeader()->label( i ) );
568     }
569   }
570   else {
571     for ( int i = 0; i < myTable->numCols(); i++ ) {
572       tlts.append( myTable->horizontalHeader()->label( i ) );
573     }
574   }
575 }
576 /*!
577   Sets columns titles
578 */
579 void VisuGUI_TableWidget::setColTitles( QStringList& tlts )
580 {
581   // !!! first column contains units !!!
582   for ( int i = 0; i < tlts.count(); i++ ) {
583     myOrientation == Horizontal ? 
584       myTable->horizontalHeader()->setLabel( i+1, tlts[i].isNull() ? "" : tlts[i] ) :
585       myTable->verticalHeader()->setLabel( i+1, tlts[i].isNull() ? "" : tlts[i] );
586   }
587   setUnitsTitle( tr( "UNITS_TLT" ) );
588 }
589 /*!
590   Sets columns titles
591 */
592 void VisuGUI_TableWidget::getColTitles( QStringList& tlts )
593 {
594   // !!! first column contains units !!!
595   tlts.clear();
596   if ( myOrientation == Horizontal ) {
597     for ( int i = 1; i < myTable->numCols(); i++ ) {
598       tlts.append( myTable->horizontalHeader()->label( i ) );
599     }
600   }
601   else {
602     for ( int i = 1; i < myTable->numRows(); i++ ) {
603       tlts.append( myTable->verticalHeader()->label( i ) );
604     }
605   }
606 }
607 /*!
608   Sets units title
609 */
610 void VisuGUI_TableWidget::setUnitsTitle( const QString& tlt ) {
611   // !!! first column contains units !!!
612   myOrientation == Horizontal ? myTable->horizontalHeader()->setLabel( 0, tlt.isNull() ? "" : tlt ) : myTable->verticalHeader()->setLabel( 0, tlt.isNull() ? "" : tlt );
613 }
614 /*!
615   Sets units
616 */
617 void VisuGUI_TableWidget::setUnits( QStringList& units )
618 {
619   for ( int i = 0; i < units.count(); i++ ) {
620     myOrientation == Horizontal ? myTable->setText( i, 0, units[i].isNull() ? "" : units[i] ) : myTable->setText( 0, i, units[i].isNull() ? "" : units[i] );
621   }
622 }
623 /*!
624   Gets units
625 */
626 void VisuGUI_TableWidget::getUnits( QStringList& units )
627 {
628   units.clear();
629   if ( myOrientation == Horizontal ) {
630     for ( int i = 0; i < myTable->numRows(); i++ )
631       units.append( myTable->text( i, 0 ).isNull() ? QString("") : myTable->text( i, 0 ) );
632   }
633   else {
634     for ( int i = 0; i < myTable->numCols(); i++ )
635       units.append( myTable->text( 0, i ).isNull() ? QString("") : myTable->text( 0, i ) );
636   }
637 }
638 /*!
639   Sets row data
640 */
641 void VisuGUI_TableWidget::setRowData( int row, QStringList& data )
642 {
643   if ( row >= 0 && row < getNumRows() ) {
644     for ( int i = 0; i < data.count(); i++ ) {
645       if ( data[i].isNull() ) {
646         myOrientation == Horizontal ? myTable->clearCell( row, i+1 ) :
647                                       myTable->clearCell( i+1, row );
648       }
649       else {
650         myOrientation == Horizontal ? myTable->setText( row, i+1, data[i] ) :
651                                       myTable->setText( i+1, row, data[i] );
652       }
653     }
654   }
655 }
656 /*!
657   Gets row data
658 */
659 void VisuGUI_TableWidget::getRowData( int row, QStringList& data )
660 {
661   data.clear();
662   if ( row >= 0 && row < getNumRows() ) {
663     if ( myOrientation == Horizontal ) {
664       for ( int i = 1; i < myTable->numCols(); i++ )
665         data.append( myTable->text( row, i ) );
666     }
667     else {
668       for ( int i = 1; i < myTable->numRows(); i++ )
669         data.append( myTable->text( i, row ) );
670     }
671   }
672 }
673 /*!
674   Adjusts table cell to see contents, <Adjust Cells> button slot
675 */
676 void VisuGUI_TableWidget::adjustTable()
677 {
678   int i;
679   for ( i = 0; i < myTable->numRows(); i++ )
680     myTable->adjustRow( i );
681   for ( i = 0; i < myTable->numCols(); i++ )
682     myTable->adjustColumn( i );
683 }
684 /*!
685   Called when selection changed in table
686 */
687 void VisuGUI_TableWidget::updateButtonsState()
688 {
689   if ( myTable->isReadOnly() )
690     return;
691   bool bDR = false; // <Delete Row(s)>
692   bool bDC = false; // <Delete Column(s)>
693   bool bSA = false; // <Select All>
694   bool bCT = false; // <Clear>
695   int i;
696   int c = myOrientation == Horizontal ? 0 : 1;
697   for ( i = c; i < myTable->numRows(); i++ ) {
698     if ( myTable->isRowSelected( i, true ) )
699       bDR = true;
700     else 
701       bSA = true;
702   }
703   c = myOrientation == Horizontal ? 1 : 0;
704   for ( i = c; i < myTable->numCols(); i++ ) {
705     if ( myTable->isColumnSelected( i, true ) )
706       bDC = true;
707     else 
708       bSA = true;
709   }
710   int nbSel = myTable->numSelections();
711   for ( i = 0; i < nbSel; i++ ) {
712     QTableSelection ts = myTable->selection( i );
713     for ( int j = ts.topRow(); j < ts.bottomRow()+1; j++) {
714       for ( int k = ts.leftCol(); k < ts.rightCol()+1; k++) {
715         if ( myTable->item( j, k ) )
716           bCT = true;
717       }
718     }
719   }
720   if ( myTable->item( myTable->currentRow(), myTable->currentColumn() ) )
721     bCT = true;
722   myDelRowBtn->setEnabled( bDR );
723   myDelColBtn->setEnabled( bDC );
724   mySelectAllBtn->setEnabled( bSA );
725   myClearBtn->setEnabled( bCT );
726 }
727 /*!
728   <Add row> button slot
729 */
730 void VisuGUI_TableWidget::addRow()
731 {
732   myTable->insertRows( myTable->numRows(), 1 );
733   updateButtonsState();
734 }
735 /*!
736   <Add column> button slot
737 */
738 void VisuGUI_TableWidget::addCol()
739 {
740   myTable->insertColumns( myTable->numCols(), 1 );
741   updateButtonsState();
742 }
743 /*!
744   <Delete row(s)> button slot
745 */
746 void VisuGUI_TableWidget::delRow()
747 {
748   int c = myOrientation == Horizontal ? 0 : 1;
749   QValueList<int> il;
750   int i;
751   for ( i = c; i < myTable->numRows(); i++ )
752     if ( myTable->isRowSelected( i, true ) )
753       il.append( i );
754   if ( il.count() > 0 ) {
755     QMemArray<int> ildel( il.count() );
756     for ( i = 0; i < il.count(); i++ )
757       ildel[ i ] = il[ i ];
758     myTable->removeRows( ildel );
759   }
760   updateButtonsState();
761 }
762 /*!
763   <Delete column(s)> button slot
764 */
765 void VisuGUI_TableWidget::delCol()
766 {
767   int c = myOrientation == Horizontal ? 1 : 0;
768   QValueList<int> il;
769   int i;
770   for ( i = c; i < myTable->numCols(); i++ )
771     if ( myTable->isColumnSelected( i, true ) )
772       il.append( i );
773   if ( il.count() > 0 ) {
774     QMemArray<int> ildel( il.count() );
775     for ( i = 0; i < il.count(); i++ )
776       ildel[ i ] = il[ i ];
777     myTable->removeColumns( ildel );
778   }
779   updateButtonsState();
780 }
781 /*!
782   <Select All> button slot
783 */
784 void VisuGUI_TableWidget::selectAll()
785 {
786   myTable->clearSelection();
787   QTableSelection ts;
788   ts.init( 0, 0 ); ts.expandTo( myTable->numRows()-1, myTable->numCols()-1 );
789   myTable->addSelection( ts );
790   updateButtonsState();
791 }
792 /*!
793   <Clear> button slot
794 */
795 void VisuGUI_TableWidget::clearTable()
796 {
797   int nbSel = myTable->numSelections();
798   for ( int i = 0; i < nbSel; i++ ) {
799     QTableSelection ts = myTable->selection( i );
800     for ( int j = ts.topRow(); j < ts.bottomRow()+1; j++) {
801       if ( myOrientation == Vertical && j == 0 ) {
802 //      continue;      // UNITS
803       }
804       for ( int k = ts.leftCol(); k < ts.rightCol()+1; k++) {
805         if ( myOrientation == Horizontal && k == 0 ) {
806 //        continue;   // UNITS
807         }
808         myTable->clearCell( j, k );
809       }
810     }
811   }
812   if ( nbSel == 0 )
813     myTable->clearCell( myTable->currentRow(), myTable->currentColumn() );
814   myTable->clearSelection();
815   updateButtonsState();
816 }
817 /*!
818   Event filter - handles titles editing
819 */
820 bool VisuGUI_TableWidget::eventFilter( QObject* o, QEvent* e )
821 {
822   if ( e->type() == QEvent::MouseButtonDblClick) {
823     QMouseEvent* me = ( QMouseEvent* )e;
824     if ( me->button() == LeftButton && !myTable->isReadOnly() ) {
825       if ( o == myTable->horizontalHeader() ) {
826         for ( int i = 0; i < myTable->horizontalHeader()->count(); i++ ) {
827           QRect rect = myTable->horizontalHeader()->sectionRect( i );
828           rect.addCoords( 1, 1, -1, -1 );
829           if ( rect.contains( myTable->horizontalHeader()->mapFromGlobal( me->globalPos() ) ) ) {
830             if ( myOrientation == Vertical || i != 0 ) {
831               bool bOk;
832               QString tlt = QInputDialog::getText( tr( "SET_TITLE_TLT" ), 
833                                                    tr( "TITLE_LBL" ),
834                                                    QLineEdit::Normal,
835                                                    myTable->horizontalHeader()->label( i ),
836                                                    &bOk,
837                                                    this );
838               if ( bOk && !tlt.isNull() )
839                 myTable->horizontalHeader()->setLabel( i, tlt );
840               break;
841             }
842           }
843         }
844       }
845       if ( o == myTable->verticalHeader() ) {
846         for ( int i = 0; i < myTable->verticalHeader()->count(); i++ ) {
847           QRect rect = myTable->verticalHeader()->sectionRect( i );
848           rect.addCoords( 1, 1, -1, -1 );
849           if ( rect.contains( myTable->verticalHeader()->mapFromGlobal( me->globalPos() ) ) ) {
850             if ( myOrientation == Horizontal || i != 0 ) {
851               bool bOk;
852               QString tlt = QInputDialog::getText( tr( "SET_TITLE_TLT" ), 
853                                                    tr( "TITLE_LBL" ),
854                                                    QLineEdit::Normal,
855                                                    myTable->verticalHeader()->label( i ),
856                                                    &bOk,
857                                                    this );
858               if ( bOk && !tlt.isNull() )
859                 myTable->verticalHeader()->setLabel( i, tlt );
860               break;
861             }
862           }
863         }
864       }
865     }    
866   }
867   else if ( e->type() == QEvent::KeyRelease && o == myTable ) {
868     QKeyEvent* ke = (QKeyEvent*)e;
869     if ( ke->key() == Key_Delete && !myTable->isEditing() ) {
870       clearTable();
871     }
872     else if ( ke->key() == Key_Backspace && !myTable->isEditing() ) {
873       clearTable();
874       int i = myTable->currentRow();
875       int j = myTable->currentColumn() - 1;
876       if ( j < 0 ) { j = myTable->numCols()-1; i--; }
877       if ( i >= 0 && j >= 0 )
878         myTable->setCurrentCell( i, j );
879     }
880   }
881   return QWidget::eventFilter( o, e );
882 }
883
884
885