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