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