]> SALOME platform Git repositories - modules/visu.git/blob - src/VISUGUI/VisuGUI_TableDlg.cxx
Salome HOME
1d211460aca846d4494b43d5ac601823ddebb1a8
[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() ? QString( "" ) : QString( rowTitles[ i ] ) ); 
211                 // set row unit
212                 tblIntAttr->SetRowUnit( nRow+1, units[ i ].isNull() ? QString( "" ) : QString( units[ i ] ) ); 
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() ? QString( "" ) : QString( colTitles[ i ] ) );
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() ? QString( "" ) : QString( rowTitles[ i ] ) ); 
255                 // set row unit
256                 tblRealAttr->SetRowUnit( nRow+1, units[ i ].isNull() ? QString( "" ) : QString( units[ i ] ) ); 
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() ? QString( "" ) : QString( colTitles[ i ] ) );
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         VisuGUI_Table* tbl = myIntTable->getTable();
306         // title
307         myIntTable->setTableTitle( tblIntAttr->GetTitle().c_str() );
308         // nb of rows & cols
309         int nbRows = tblIntAttr->GetNbRows() ; 
310         int nbCols = tblIntAttr->GetNbColumns();
311         myIntTable->setNumRows( nbRows );
312         myIntTable->setNumCols( nbCols );
313         // rows titles
314         QStringList strlist;
315         vector<string> rowTitles = tblIntAttr->GetRowTitles();
316         for ( i = 0; i < nbRows; i++ ) {
317           if ( rowTitles.size() > 0 )
318             strlist.append( rowTitles[i].c_str() );
319           else
320             strlist.append( "" );
321         }
322         myIntTable->setRowTitles( strlist );
323         // columns titles
324         strlist.clear();
325         vector<string> colTitles = tblIntAttr->GetColumnTitles();
326         for ( i = 0; i < nbCols; i++ ) {
327           if ( colTitles.size() > 0 )
328             strlist.append( colTitles[i].c_str() );
329           else
330             strlist.append( "" );
331         }
332         myIntTable->setColTitles( strlist );
333         // units
334         strlist.clear();
335         vector<string> rowUnits = tblIntAttr->GetRowUnits();
336         if ( rowUnits.size() > 0 ) {
337           for ( i = 0; i < nbRows; i++ )
338             strlist.append( rowUnits[i] );
339           myIntTable->setUnits( strlist );
340         }
341         // data
342         for ( i = 1; i <= nbRows; i++ ) {
343           strlist.clear();
344           for ( j = 1; j <= nbCols; j++ ) {
345             if ( tblIntAttr->HasValue( i, j ) )
346               strlist.append( QString::number( tblIntAttr->GetValue( i, j ) ) );
347             else
348               strlist.append( QString::null );
349           }
350           myIntTable->setRowData( i-1, strlist );
351         }
352         myIntTable->adjustTable();
353       }
354       catch( ... ) {
355         MESSAGE("VisuGUI_TableDlg::initDlg : Exception has been caught !!!");
356       }
357     } 
358     // Table of real
359     if ( tblRealAttr && myRealTable ) {
360       try {
361         VisuGUI_Table* tbl = myRealTable->getTable();
362         // title
363         myRealTable->setTableTitle( tblRealAttr->GetTitle().c_str() );
364         // nb of rows & cols
365         int nbRows = tblRealAttr->GetNbRows() ; 
366         int nbCols = tblRealAttr->GetNbColumns();
367         myRealTable->setNumRows( nbRows );
368         myRealTable->setNumCols( nbCols );
369         // rows titles
370         QStringList strlist;
371         vector<string> rowTitles = tblRealAttr->GetRowTitles();
372         for ( i = 0; i < nbRows; i++ ) {
373           if ( rowTitles.size() > 0 )
374             strlist.append( rowTitles[i].c_str() );
375           else
376             strlist.append( "" );
377         }
378         myRealTable->setRowTitles( strlist );
379         // columns titles
380         strlist.clear();
381         vector<string> colTitles = tblRealAttr->GetColumnTitles();
382         for ( i = 0; i < nbCols; i++ ) {
383           if ( colTitles.size() > 0 )
384             strlist.append( colTitles[i].c_str() );
385           else
386             strlist.append( "" );
387         }
388         myRealTable->setColTitles( strlist );
389         // units
390         strlist.clear();
391         vector<string> rowUnits = tblRealAttr->GetRowUnits();
392         if ( rowUnits.size() > 0 ) {
393           for ( i = 0; i < nbRows; i++ )
394             strlist.append( rowUnits[i].c_str() );
395           myRealTable->setUnits( strlist );
396         }
397         // data
398         for ( i = 1; i <= nbRows; i++ ) {
399           strlist.clear();
400           for ( j = 1; j <= nbCols; j++ ) {
401             if ( tblRealAttr->HasValue( i, j ) )
402               strlist.append( QString::number( tblRealAttr->GetValue( i, j ) ) );
403             else
404               strlist.append( QString::null );
405           }
406           myRealTable->setRowData( i-1, strlist );
407         }
408         myRealTable->adjustTable();
409       }
410       catch( ... ) {
411         MESSAGE("VisuGUI_TableDlg::initDlg : Exception has been caught !!!");
412       }
413     } 
414   }
415 }
416
417 /*!
418   Constructor
419 */
420 VisuGUI_TableWidget::VisuGUI_TableWidget( QWidget* parent, 
421                                               const char* name, 
422                                               bool edit, 
423                                               Orientation orient,
424                                               bool showColumnTitles )
425      : QWidget( parent, name ), myOrientation( orient )
426 {
427   QGridLayout* mainLayout = new QGridLayout( this );
428   mainLayout->setMargin( 0 );
429   mainLayout->setSpacing( SPACING_SIZE );
430
431   myTitleEdit = new QLineEdit( this, "TitleEdit" );
432   myTitleEdit->setAlignment( AlignCenter );
433   myTitleEdit->setReadOnly( !edit );
434   QFont fnt = myTitleEdit->font();
435   fnt.setBold( true ); 
436   myTitleEdit->setFont( fnt );
437
438   myTable = new VisuGUI_Table( orient, this, "Table" );
439   myTable->setNumRows( 5 );
440   myTable->setNumCols( 5 );
441   myTable->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
442   myTable->setMinimumSize( MIN_TABLE_WIDTH, MIN_TABLE_HEIGHT );
443   myTable->setSelectionMode( QTable::Single );
444   myTable->setShowGrid( true );
445   myTable->setColumnMovingEnabled( false );
446   myTable->setRowMovingEnabled( false );
447   myTable->setReadOnly( !edit );
448   myTable->setDragEnabled( false );
449   setUnitsTitle( tr( "UNITS_TLT" ) );
450
451   if ( !showColumnTitles ) {
452     if ( myOrientation == Horizontal ) {
453       myTable->horizontalHeader()->hide();
454       myTable->setTopMargin( 0 );
455     }
456     else {
457       myTable->verticalHeader()->hide();
458       myTable->setLeftMargin( 0 );
459     }
460   }
461
462   mainLayout->addWidget( myTitleEdit, 0, 0 );
463   mainLayout->addWidget( myTable, 1, 0 );
464
465   if ( edit ) {
466     myAddRowBtn    = new QPushButton( tr( "ADD_ROW_BTN" ), this, "AddRowBtn" );
467     myDelRowBtn    = new QPushButton( tr( "REMOVE_ROW_BTN" ), this, "DelRowBtn" );
468     myAddColBtn    = new QPushButton( tr( "ADD_COLUMN_BTN" ), this, "AddColBtn" );
469     myDelColBtn    = new QPushButton( tr( "REMOVE_COLUMN_BTN" ), this, "DelColBtn" );
470     myAdjustBtn    = new QPushButton( tr( "ADJUST_CELLS_BTN" ), this, "AdjustBtn" );
471     mySelectAllBtn = new QPushButton( tr( "SELECT_ALL_BTN" ), this, "SelectAllBtn" );
472     myClearBtn     = new QPushButton( tr( "CLEAR_BTN"), this, "ClearBtn" );
473     QVBoxLayout* btnLayout = new QVBoxLayout; btnLayout->setMargin( 0 ); btnLayout->setSpacing( SPACING_SIZE );
474     btnLayout->addWidget( myAddRowBtn );
475     btnLayout->addWidget( myDelRowBtn );
476     btnLayout->addWidget( myAddColBtn );
477     btnLayout->addWidget( myDelColBtn );
478     btnLayout->addStretch();
479     btnLayout->addWidget( myAdjustBtn );
480     btnLayout->addStretch();
481     btnLayout->addWidget( mySelectAllBtn );
482     btnLayout->addWidget( myClearBtn );
483     mainLayout->addLayout( btnLayout, 1, 1 );
484     connect( myTable, SIGNAL( selectionChanged() ),        this, SLOT( updateButtonsState() ) );
485     connect( myTable, SIGNAL( currentChanged( int, int) ), this, SLOT( updateButtonsState() ) );
486     connect( myAddRowBtn,    SIGNAL( clicked() ),   this, SLOT( addRow() ) );
487     connect( myAddColBtn,    SIGNAL( clicked() ),   this, SLOT( addCol() ) );
488     connect( myDelRowBtn,    SIGNAL( clicked() ),   this, SLOT( delRow() ) );
489     connect( myDelColBtn,    SIGNAL( clicked() ),   this, SLOT( delCol() ) );
490     connect( myAdjustBtn,    SIGNAL( clicked() ),   this, SLOT( adjustTable() ) );
491     connect( mySelectAllBtn, SIGNAL( clicked() ),   this, SLOT( selectAll() ) );
492     connect( myClearBtn,     SIGNAL( clicked() ),   this, SLOT( clearTable() ) );
493     myTable->horizontalHeader()->installEventFilter( this );
494     myTable->verticalHeader()->installEventFilter( this );
495     myTable->installEventFilter( this );
496   }
497   updateButtonsState();
498 }
499 /*!
500   Destructor
501 */
502 VisuGUI_TableWidget::~VisuGUI_TableWidget()
503 {
504 }
505 /*!
506   Sets table title
507 */
508 void VisuGUI_TableWidget::setTableTitle( const QString& title )
509 {
510   myTitleEdit->setText( title );
511 }
512 /*!
513   Gets table title
514 */
515 QString VisuGUI_TableWidget::getTableTitle()
516 {
517   return myTitleEdit->text();
518 }
519 /*!
520   Sets total number of rows
521 */
522 void VisuGUI_TableWidget::setNumRows( const int num )
523 {
524   myOrientation == Horizontal ? myTable->setNumRows( num ) : myTable->setNumCols( num );
525 }
526 /*!
527   Gets total number of rows
528 */
529 int VisuGUI_TableWidget::getNumRows()
530 {
531   return myOrientation == Horizontal ? myTable->numRows() : myTable->numCols();
532 }
533 /*!
534   Sets total number of columns
535 */
536 void VisuGUI_TableWidget::setNumCols( const int num )
537 {
538   // !!! first column contains units !!!
539   myOrientation == Horizontal ? myTable->setNumCols( num+1 ) : myTable->setNumRows( num+1 );
540 //  myOrientation == Horizontal ? myTable->setColumnReadOnly( 0, true ) : myTable->setRowReadOnly( 0, true );
541 }
542 /*!
543   Gets total number of columns
544 */
545 int VisuGUI_TableWidget::getNumCols()
546 {
547   // !!! first column contains units !!!
548   return myOrientation == Horizontal ? myTable->numCols()-1 : myTable->numRows()-1;
549 }
550 /*!
551   Sets rows titles
552 */
553 void VisuGUI_TableWidget::setRowTitles( QStringList& tlts )
554 {
555   for ( int i = 0; i < tlts.count(); i++ ) {
556     myOrientation == Horizontal ? 
557       myTable->verticalHeader()->setLabel( i, tlts[i] ) : 
558       myTable->horizontalHeader()->setLabel( i, tlts[i] );
559   }
560 }
561 /*!
562   Gets rows titles
563 */
564 void VisuGUI_TableWidget::getRowTitles( QStringList& tlts )
565 {
566   tlts.clear();
567   if ( myOrientation == Horizontal ) {
568     for ( int i = 0; i < myTable->numRows(); i++ ) {
569       tlts.append( myTable->verticalHeader()->label( i ) );
570     }
571   }
572   else {
573     for ( int i = 0; i < myTable->numCols(); i++ ) {
574       tlts.append( myTable->horizontalHeader()->label( i ) );
575     }
576   }
577 }
578 /*!
579   Sets columns titles
580 */
581 void VisuGUI_TableWidget::setColTitles( QStringList& tlts )
582 {
583   // !!! first column contains units !!!
584   for ( int i = 0; i < tlts.count(); i++ ) {
585     myOrientation == Horizontal ? 
586       myTable->horizontalHeader()->setLabel( i+1, tlts[i].isNull() ? "" : tlts[i] ) :
587       myTable->verticalHeader()->setLabel( i+1, tlts[i].isNull() ? "" : tlts[i] );
588   }
589   setUnitsTitle( tr( "UNITS_TLT" ) );
590 }
591 /*!
592   Sets columns titles
593 */
594 void VisuGUI_TableWidget::getColTitles( QStringList& tlts )
595 {
596   // !!! first column contains units !!!
597   tlts.clear();
598   if ( myOrientation == Horizontal ) {
599     for ( int i = 1; i < myTable->numCols(); i++ ) {
600       tlts.append( myTable->horizontalHeader()->label( i ) );
601     }
602   }
603   else {
604     for ( int i = 1; i < myTable->numRows(); i++ ) {
605       tlts.append( myTable->verticalHeader()->label( i ) );
606     }
607   }
608 }
609 /*!
610   Sets units title
611 */
612 void VisuGUI_TableWidget::setUnitsTitle( const QString& tlt ) {
613   // !!! first column contains units !!!
614   myOrientation == Horizontal ? myTable->horizontalHeader()->setLabel( 0, tlt.isNull() ? "" : tlt ) : myTable->verticalHeader()->setLabel( 0, tlt.isNull() ? "" : tlt );
615 }
616 /*!
617   Sets units
618 */
619 void VisuGUI_TableWidget::setUnits( QStringList& units )
620 {
621   for ( int i = 0; i < units.count(); i++ ) {
622     myOrientation == Horizontal ? myTable->setText( i, 0, units[i].isNull() ? "" : units[i] ) : myTable->setText( 0, i, units[i].isNull() ? "" : units[i] );
623   }
624 }
625 /*!
626   Gets units
627 */
628 void VisuGUI_TableWidget::getUnits( QStringList& units )
629 {
630   units.clear();
631   if ( myOrientation == Horizontal ) {
632     for ( int i = 0; i < myTable->numRows(); i++ )
633       units.append( myTable->text( i, 0 ).isNull() ? QString("") : myTable->text( i, 0 ) );
634   }
635   else {
636     for ( int i = 0; i < myTable->numCols(); i++ )
637       units.append( myTable->text( 0, i ).isNull() ? QString("") : myTable->text( 0, i ) );
638   }
639 }
640 /*!
641   Sets row data
642 */
643 void VisuGUI_TableWidget::setRowData( int row, QStringList& data )
644 {
645   if ( row >= 0 && row < getNumRows() ) {
646     for ( int i = 0; i < data.count(); i++ ) {
647       if ( data[i].isNull() ) {
648         myOrientation == Horizontal ? myTable->clearCell( row, i+1 ) :
649                                       myTable->clearCell( i+1, row );
650       }
651       else {
652         myOrientation == Horizontal ? myTable->setText( row, i+1, data[i] ) :
653                                       myTable->setText( i+1, row, data[i] );
654       }
655     }
656   }
657 }
658 /*!
659   Gets row data
660 */
661 void VisuGUI_TableWidget::getRowData( int row, QStringList& data )
662 {
663   data.clear();
664   if ( row >= 0 && row < getNumRows() ) {
665     if ( myOrientation == Horizontal ) {
666       for ( int i = 1; i < myTable->numCols(); i++ )
667         data.append( myTable->text( row, i ) );
668     }
669     else {
670       for ( int i = 1; i < myTable->numRows(); i++ )
671         data.append( myTable->text( i, row ) );
672     }
673   }
674 }
675 /*!
676   Adjusts table cell to see contents, <Adjust Cells> button slot
677 */
678 void VisuGUI_TableWidget::adjustTable()
679 {
680   int i;
681   for ( i = 0; i < myTable->numRows(); i++ )
682     myTable->adjustRow( i );
683   for ( i = 0; i < myTable->numCols(); i++ )
684     myTable->adjustColumn( i );
685 }
686 /*!
687   Called when selection changed in table
688 */
689 void VisuGUI_TableWidget::updateButtonsState()
690 {
691   if ( myTable->isReadOnly() )
692     return;
693   bool bDR = false; // <Delete Row(s)>
694   bool bDC = false; // <Delete Column(s)>
695   bool bSA = false; // <Select All>
696   bool bCT = false; // <Clear>
697   int i;
698   int c = myOrientation == Horizontal ? 0 : 1;
699   for ( i = c; i < myTable->numRows(); i++ ) {
700     if ( myTable->isRowSelected( i, true ) )
701       bDR = true;
702     else 
703       bSA = true;
704   }
705   c = myOrientation == Horizontal ? 1 : 0;
706   for ( i = c; i < myTable->numCols(); i++ ) {
707     if ( myTable->isColumnSelected( i, true ) )
708       bDC = true;
709     else 
710       bSA = true;
711   }
712   int nbSel = myTable->numSelections();
713   for ( i = 0; i < nbSel; i++ ) {
714     QTableSelection ts = myTable->selection( i );
715     for ( int j = ts.topRow(); j < ts.bottomRow()+1; j++) {
716       for ( int k = ts.leftCol(); k < ts.rightCol()+1; k++) {
717         if ( myTable->item( j, k ) )
718           bCT = true;
719       }
720     }
721   }
722   if ( myTable->item( myTable->currentRow(), myTable->currentColumn() ) )
723     bCT = true;
724   myDelRowBtn->setEnabled( bDR );
725   myDelColBtn->setEnabled( bDC );
726   mySelectAllBtn->setEnabled( bSA );
727   myClearBtn->setEnabled( bCT );
728 }
729 /*!
730   <Add row> button slot
731 */
732 void VisuGUI_TableWidget::addRow()
733 {
734   myTable->insertRows( myTable->numRows(), 1 );
735   updateButtonsState();
736 }
737 /*!
738   <Add column> button slot
739 */
740 void VisuGUI_TableWidget::addCol()
741 {
742   myTable->insertColumns( myTable->numCols(), 1 );
743   updateButtonsState();
744 }
745 /*!
746   <Delete row(s)> button slot
747 */
748 void VisuGUI_TableWidget::delRow()
749 {
750   int c = myOrientation == Horizontal ? 0 : 1;
751   QValueList<int> il;
752   int i;
753   for ( i = c; i < myTable->numRows(); i++ )
754     if ( myTable->isRowSelected( i, true ) )
755       il.append( i );
756   if ( il.count() > 0 ) {
757     QMemArray<int> ildel( il.count() );
758     for ( i = 0; i < il.count(); i++ )
759       ildel[ i ] = il[ i ];
760     myTable->removeRows( ildel );
761   }
762   updateButtonsState();
763 }
764 /*!
765   <Delete column(s)> button slot
766 */
767 void VisuGUI_TableWidget::delCol()
768 {
769   int c = myOrientation == Horizontal ? 1 : 0;
770   QValueList<int> il;
771   int i;
772   for ( i = c; i < myTable->numCols(); i++ )
773     if ( myTable->isColumnSelected( i, true ) )
774       il.append( i );
775   if ( il.count() > 0 ) {
776     QMemArray<int> ildel( il.count() );
777     for ( i = 0; i < il.count(); i++ )
778       ildel[ i ] = il[ i ];
779     myTable->removeColumns( ildel );
780   }
781   updateButtonsState();
782 }
783 /*!
784   <Select All> button slot
785 */
786 void VisuGUI_TableWidget::selectAll()
787 {
788   myTable->clearSelection();
789   QTableSelection ts;
790   ts.init( 0, 0 ); ts.expandTo( myTable->numRows()-1, myTable->numCols()-1 );
791   myTable->addSelection( ts );
792   updateButtonsState();
793 }
794 /*!
795   <Clear> button slot
796 */
797 void VisuGUI_TableWidget::clearTable()
798 {
799   int nbSel = myTable->numSelections();
800   for ( int i = 0; i < nbSel; i++ ) {
801     QTableSelection ts = myTable->selection( i );
802     for ( int j = ts.topRow(); j < ts.bottomRow()+1; j++) {
803       if ( myOrientation == Vertical && j == 0 ) {
804 //      continue;      // UNITS
805       }
806       for ( int k = ts.leftCol(); k < ts.rightCol()+1; k++) {
807         if ( myOrientation == Horizontal && k == 0 ) {
808 //        continue;   // UNITS
809         }
810         myTable->clearCell( j, k );
811       }
812     }
813   }
814   if ( nbSel == 0 )
815     myTable->clearCell( myTable->currentRow(), myTable->currentColumn() );
816   myTable->clearSelection();
817   updateButtonsState();
818 }
819 /*!
820   Event filter - handles titles editing
821 */
822 bool VisuGUI_TableWidget::eventFilter( QObject* o, QEvent* e )
823 {
824   if ( e->type() == QEvent::MouseButtonDblClick) {
825     QMouseEvent* me = ( QMouseEvent* )e;
826     if ( me->button() == LeftButton && !myTable->isReadOnly() ) {
827       if ( o == myTable->horizontalHeader() ) {
828         for ( int i = 0; i < myTable->horizontalHeader()->count(); i++ ) {
829           QRect rect = myTable->horizontalHeader()->sectionRect( i );
830           rect.addCoords( 1, 1, -1, -1 );
831           if ( rect.contains( myTable->horizontalHeader()->mapFromGlobal( me->globalPos() ) ) ) {
832             if ( myOrientation == Vertical || i != 0 ) {
833               bool bOk;
834               QString tlt = QInputDialog::getText( tr( "SET_TITLE_TLT" ), 
835                                                    tr( "TITLE_LBL" ),
836                                                    QLineEdit::Normal,
837                                                    myTable->horizontalHeader()->label( i ),
838                                                    &bOk,
839                                                    this );
840               if ( bOk && !tlt.isNull() )
841                 myTable->horizontalHeader()->setLabel( i, tlt );
842               break;
843             }
844           }
845         }
846       }
847       if ( o == myTable->verticalHeader() ) {
848         for ( int i = 0; i < myTable->verticalHeader()->count(); i++ ) {
849           QRect rect = myTable->verticalHeader()->sectionRect( i );
850           rect.addCoords( 1, 1, -1, -1 );
851           if ( rect.contains( myTable->verticalHeader()->mapFromGlobal( me->globalPos() ) ) ) {
852             if ( myOrientation == Horizontal || i != 0 ) {
853               bool bOk;
854               QString tlt = QInputDialog::getText( tr( "SET_TITLE_TLT" ), 
855                                                    tr( "TITLE_LBL" ),
856                                                    QLineEdit::Normal,
857                                                    myTable->verticalHeader()->label( i ),
858                                                    &bOk,
859                                                    this );
860               if ( bOk && !tlt.isNull() )
861                 myTable->verticalHeader()->setLabel( i, tlt );
862               break;
863             }
864           }
865         }
866       }
867     }    
868   }
869   else if ( e->type() == QEvent::KeyRelease && o == myTable ) {
870     QKeyEvent* ke = (QKeyEvent*)e;
871     if ( ke->key() == Key_Delete && !myTable->isEditing() ) {
872       clearTable();
873     }
874     else if ( ke->key() == Key_Backspace && !myTable->isEditing() ) {
875       clearTable();
876       int i = myTable->currentRow();
877       int j = myTable->currentColumn() - 1;
878       if ( j < 0 ) { j = myTable->numCols()-1; i--; }
879       if ( i >= 0 && j >= 0 )
880         myTable->setCurrentCell( i, j );
881     }
882   }
883   return QWidget::eventFilter( o, e );
884 }
885
886
887