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