Salome HOME
Porting to Mandrake 10.1 and new products:
[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 #include "SALOMEGUI_TableDlg.h"
13 #include "QAD_Tools.h"
14 #include "QAD_MessageBox.h"
15 #include <qlayout.h>
16 #include <qvalidator.h>
17 #include <qtable.h>
18 #include <qtabwidget.h>
19 #include <qvaluelist.h>
20 #include <qmemarray.h>
21 #include <qinputdialog.h>
22 #include <qlabel.h>
23 #include "utilities.h"
24 using namespace std;
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].isNull() ? "" : tlts[i] ) :
583       myTable->verticalHeader()->setLabel( i+1, tlts[i].isNull() ? "" : tlts[i] );
584   }
585   setUnitsTitle( tr( "UNITS_TLT" ) );
586 }
587 /*!
588   Sets columns titles
589 */
590 void SALOMEGUI_TableWidget::getColTitles( QStringList& tlts )
591 {
592   // !!! first column contains units !!!
593   tlts.clear();
594   if ( myOrientation == Horizontal ) {
595     for ( int i = 1; i < myTable->numCols(); i++ ) {
596       tlts.append( myTable->horizontalHeader()->label( i ) );
597     }
598   }
599   else {
600     for ( int i = 1; i < myTable->numRows(); i++ ) {
601       tlts.append( myTable->verticalHeader()->label( i ) );
602     }
603   }
604 }
605 /*!
606   Sets units title
607 */
608 void SALOMEGUI_TableWidget::setUnitsTitle( const QString& tlt ) {
609   // !!! first column contains units !!!
610   myOrientation == Horizontal ? myTable->horizontalHeader()->setLabel( 0, tlt.isNull() ? "" : tlt ) : myTable->verticalHeader()->setLabel( 0, tlt.isNull() ? "" : tlt );
611 }
612 /*!
613   Sets units
614 */
615 void SALOMEGUI_TableWidget::setUnits( QStringList& units )
616 {
617   for ( int i = 0; i < units.count(); i++ ) {
618     myOrientation == Horizontal ? myTable->setText( i, 0, units[i].isNull() ? "" : units[i] ) : myTable->setText( 0, i, units[i].isNull() ? "" : units[i] );
619   }
620 }
621 /*!
622   Gets units
623 */
624 void SALOMEGUI_TableWidget::getUnits( QStringList& units )
625 {
626   units.clear();
627   if ( myOrientation == Horizontal ) {
628     for ( int i = 0; i < myTable->numRows(); i++ )
629       units.append( myTable->text( i, 0 ).isNull() ? QString("") : myTable->text( i, 0 ) );
630   }
631   else {
632     for ( int i = 0; i < myTable->numCols(); i++ )
633       units.append( myTable->text( 0, i ).isNull() ? QString("") : myTable->text( 0, i ) );
634   }
635 }
636 /*!
637   Sets row data
638 */
639 void SALOMEGUI_TableWidget::setRowData( int row, QStringList& data )
640 {
641   if ( row >= 0 && row < getNumRows() ) {
642     for ( int i = 0; i < data.count(); i++ ) {
643       if ( data[i].isNull() ) {
644         myOrientation == Horizontal ? myTable->clearCell( row, i+1 ) :
645                                       myTable->clearCell( i+1, row );
646       }
647       else {
648         myOrientation == Horizontal ? myTable->setText( row, i+1, data[i] ) :
649                                       myTable->setText( i+1, row, data[i] );
650       }
651     }
652   }
653 }
654 /*!
655   Gets row data
656 */
657 void SALOMEGUI_TableWidget::getRowData( int row, QStringList& data )
658 {
659   data.clear();
660   if ( row >= 0 && row < getNumRows() ) {
661     if ( myOrientation == Horizontal ) {
662       for ( int i = 1; i < myTable->numCols(); i++ )
663         data.append( myTable->text( row, i ) );
664     }
665     else {
666       for ( int i = 1; i < myTable->numRows(); i++ )
667         data.append( myTable->text( i, row ) );
668     }
669   }
670 }
671 /*!
672   Adjusts table cell to see contents, <Adjust Cells> button slot
673 */
674 void SALOMEGUI_TableWidget::adjustTable()
675 {
676   int i;
677   for ( i = 0; i < myTable->numRows(); i++ )
678     myTable->adjustRow( i );
679   for ( i = 0; i < myTable->numCols(); i++ )
680     myTable->adjustColumn( i );
681 }
682 /*!
683   Called when selection changed in table
684 */
685 void SALOMEGUI_TableWidget::updateButtonsState()
686 {
687   if ( myTable->isReadOnly() )
688     return;
689   bool bDR = false; // <Delete Row(s)>
690   bool bDC = false; // <Delete Column(s)>
691   bool bSA = false; // <Select All>
692   bool bCT = false; // <Clear>
693   int i;
694   int c = myOrientation == Horizontal ? 0 : 1;
695   for ( i = c; i < myTable->numRows(); i++ ) {
696     if ( myTable->isRowSelected( i, true ) )
697       bDR = true;
698     else 
699       bSA = true;
700   }
701   c = myOrientation == Horizontal ? 1 : 0;
702   for ( i = c; i < myTable->numCols(); i++ ) {
703     if ( myTable->isColumnSelected( i, true ) )
704       bDC = true;
705     else 
706       bSA = true;
707   }
708   int nbSel = myTable->numSelections();
709   for ( i = 0; i < nbSel; i++ ) {
710     QTableSelection ts = myTable->selection( i );
711     for ( int j = ts.topRow(); j < ts.bottomRow()+1; j++) {
712       for ( int k = ts.leftCol(); k < ts.rightCol()+1; k++) {
713         if ( myTable->item( j, k ) )
714           bCT = true;
715       }
716     }
717   }
718   if ( myTable->item( myTable->currentRow(), myTable->currentColumn() ) )
719     bCT = true;
720   myDelRowBtn->setEnabled( bDR );
721   myDelColBtn->setEnabled( bDC );
722   mySelectAllBtn->setEnabled( bSA );
723   myClearBtn->setEnabled( bCT );
724 }
725 /*!
726   <Add row> button slot
727 */
728 void SALOMEGUI_TableWidget::addRow()
729 {
730   myTable->insertRows( myTable->numRows(), 1 );
731   updateButtonsState();
732 }
733 /*!
734   <Add column> button slot
735 */
736 void SALOMEGUI_TableWidget::addCol()
737 {
738   myTable->insertColumns( myTable->numCols(), 1 );
739   updateButtonsState();
740 }
741 /*!
742   <Delete row(s)> button slot
743 */
744 void SALOMEGUI_TableWidget::delRow()
745 {
746   int c = myOrientation == Horizontal ? 0 : 1;
747   QValueList<int> il;
748   int i;
749   for ( i = c; i < myTable->numRows(); i++ )
750     if ( myTable->isRowSelected( i, true ) )
751       il.append( i );
752   if ( il.count() > 0 ) {
753     QMemArray<int> ildel( il.count() );
754     for ( i = 0; i < il.count(); i++ )
755       ildel[ i ] = il[ i ];
756     myTable->removeRows( ildel );
757   }
758   updateButtonsState();
759 }
760 /*!
761   <Delete column(s)> button slot
762 */
763 void SALOMEGUI_TableWidget::delCol()
764 {
765   int c = myOrientation == Horizontal ? 1 : 0;
766   QValueList<int> il;
767   int i;
768   for ( i = c; i < myTable->numCols(); i++ )
769     if ( myTable->isColumnSelected( i, true ) )
770       il.append( i );
771   if ( il.count() > 0 ) {
772     QMemArray<int> ildel( il.count() );
773     for ( i = 0; i < il.count(); i++ )
774       ildel[ i ] = il[ i ];
775     myTable->removeColumns( ildel );
776   }
777   updateButtonsState();
778 }
779 /*!
780   <Select All> button slot
781 */
782 void SALOMEGUI_TableWidget::selectAll()
783 {
784   myTable->clearSelection();
785   QTableSelection ts;
786   ts.init( 0, 0 ); ts.expandTo( myTable->numRows()-1, myTable->numCols()-1 );
787   myTable->addSelection( ts );
788   updateButtonsState();
789 }
790 /*!
791   <Clear> button slot
792 */
793 void SALOMEGUI_TableWidget::clearTable()
794 {
795   int nbSel = myTable->numSelections();
796   for ( int i = 0; i < nbSel; i++ ) {
797     QTableSelection ts = myTable->selection( i );
798     for ( int j = ts.topRow(); j < ts.bottomRow()+1; j++) {
799       if ( myOrientation == Vertical && j == 0 ) {
800 //      continue;      // UNITS
801       }
802       for ( int k = ts.leftCol(); k < ts.rightCol()+1; k++) {
803         if ( myOrientation == Horizontal && k == 0 ) {
804 //        continue;   // UNITS
805         }
806         myTable->clearCell( j, k );
807       }
808     }
809   }
810   if ( nbSel == 0 )
811     myTable->clearCell( myTable->currentRow(), myTable->currentColumn() );
812   myTable->clearSelection();
813   updateButtonsState();
814 }
815 /*!
816   Event filter - handles titles editing
817 */
818 bool SALOMEGUI_TableWidget::eventFilter( QObject* o, QEvent* e )
819 {
820   if ( e->type() == QEvent::MouseButtonDblClick) {
821     QMouseEvent* me = ( QMouseEvent* )e;
822     if ( me->button() == LeftButton && !myTable->isReadOnly() ) {
823       if ( o == myTable->horizontalHeader() ) {
824         for ( int i = 0; i < myTable->horizontalHeader()->count(); i++ ) {
825           QRect rect = myTable->horizontalHeader()->sectionRect( i );
826           rect.addCoords( 1, 1, -1, -1 );
827           if ( rect.contains( myTable->horizontalHeader()->mapFromGlobal( me->globalPos() ) ) ) {
828             if ( myOrientation == Vertical || i != 0 ) {
829               bool bOk;
830               QString tlt = QInputDialog::getText( tr( "SET_TITLE_TLT" ), 
831                                                    tr( "TITLE_LBL" ),
832                                                    QLineEdit::Normal,
833                                                    myTable->horizontalHeader()->label( i ),
834                                                    &bOk,
835                                                    this );
836               if ( bOk && !tlt.isNull() )
837                 myTable->horizontalHeader()->setLabel( i, tlt );
838               break;
839             }
840           }
841         }
842       }
843       if ( o == myTable->verticalHeader() ) {
844         for ( int i = 0; i < myTable->verticalHeader()->count(); i++ ) {
845           QRect rect = myTable->verticalHeader()->sectionRect( i );
846           rect.addCoords( 1, 1, -1, -1 );
847           if ( rect.contains( myTable->verticalHeader()->mapFromGlobal( me->globalPos() ) ) ) {
848             if ( myOrientation == Horizontal || i != 0 ) {
849               bool bOk;
850               QString tlt = QInputDialog::getText( tr( "SET_TITLE_TLT" ), 
851                                                    tr( "TITLE_LBL" ),
852                                                    QLineEdit::Normal,
853                                                    myTable->verticalHeader()->label( i ),
854                                                    &bOk,
855                                                    this );
856               if ( bOk && !tlt.isNull() )
857                 myTable->verticalHeader()->setLabel( i, tlt );
858               break;
859             }
860           }
861         }
862       }
863     }    
864   }
865   else if ( e->type() == QEvent::KeyRelease && o == myTable ) {
866     QKeyEvent* ke = (QKeyEvent*)e;
867     if ( ke->key() == Key_Delete && !myTable->isEditing() ) {
868       clearTable();
869     }
870     else if ( ke->key() == Key_Backspace && !myTable->isEditing() ) {
871       clearTable();
872       int i = myTable->currentRow();
873       int j = myTable->currentColumn() - 1;
874       if ( j < 0 ) { j = myTable->numCols()-1; i--; }
875       if ( i >= 0 && j >= 0 )
876         myTable->setCurrentCell( i, j );
877     }
878   }
879   return QWidget::eventFilter( o, e );
880 }
881
882
883