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