1 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : VisuGUI_TableDlg.cxx
21 // Author : Vadim SANDLER
24 #include "VisuGUI_TableDlg.h"
26 #include "SUIT_Tools.h"
27 #include "SUIT_MessageBox.h"
28 #include "SUIT_ResourceMgr.h"
29 #include "SUIT_Session.h"
31 #include "LightApp_Application.h"
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"
40 #include <qvalidator.h>
42 #include <qtabwidget.h>
43 #include <qvaluelist.h>
44 #include <qmemarray.h>
45 #include <qinputdialog.h>
47 #include "utilities.h"
50 #define MARGIN_SIZE 11
51 #define SPACING_SIZE 6
53 #define MIN_TABLE_WIDTH 200
54 #define MIN_TABLE_HEIGHT 200
57 class VisuGUI_Table : public QTable {
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 ) {}
64 void setValidator( QValidator* v = 0 ) { myValidator = v; }
65 bool isEditing() const { return QTable::isEditing(); }
68 QWidget* createEditor ( int row, int col, bool initFromCell ) const
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 );
78 QValidator* myValidator;
79 Orientation myOrientation;
85 VisuGUI_TableDlg::VisuGUI_TableDlg( QWidget* parent,
90 bool showColumnTitles )
91 : QDialog( parent, "", false, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose),
92 myIntTable( 0 ), myRealTable( 0 )
94 setCaption( edit ? tr( "EDIT_TABLE_TLT" ) : tr( "VIEW_TABLE_TLT" ) );
95 setSizeGripEnabled( true );
98 bool bHasIntTable = false;
99 bool bHasRealTable = false;
101 _PTR(GenericAttribute) anAttr;
102 bHasIntTable = myObject->FindAttribute( anAttr, "AttributeTableOfInteger");
103 bHasRealTable = myObject->FindAttribute( anAttr, "AttributeTableOfReal");
106 QVBoxLayout* mainLayout = new QVBoxLayout( this );
107 mainLayout->setMargin( MARGIN_SIZE );
108 mainLayout->setSpacing( SPACING_SIZE );
110 bool bDoInt = which == ttInt || which == ttBoth || which == ttAuto && bHasIntTable;
111 bool bDoReal = which == ttReal || which == ttBoth || which == ttAuto && bHasRealTable;
115 if ( bDoInt && bDoReal ) {
116 top = new QTabWidget( this, "TabWidget" );
117 ( ( QTabWidget* ) top) ->setMargin( MARGIN_SIZE );
120 top = new QWidget( this, "DumbWidget" );
121 tl = new QVBoxLayout( top ); tl->setMargin( 0 ); tl->setSpacing( SPACING_SIZE );
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" ) );
130 tl->addWidget( myIntTable );
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" ) );
138 tl->addWidget( myRealTable );
140 if ( !bDoInt && !bDoReal ) {
141 QLabel *dumbLabel = new QLabel( tr( "ERR_TABLE_NOT_AVAILABLE" ), top, "DumbLabel" );
142 dumbLabel->setAlignment( AlignCenter );
143 tl->addWidget( dumbLabel );
146 QHBoxLayout* btnLayout = new QHBoxLayout;
147 btnLayout->setMargin( 0 ); btnLayout->setSpacing( SPACING_SIZE );
149 myOKBtn = new QPushButton( tr( "BUT_OK" ), this );
150 myHelpBtn = new QPushButton( tr( "BUT_HELP" ), this );
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() ) );
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() ) );
166 connect( myHelpBtn, SIGNAL( clicked() ), this, SLOT( onHelp() ) );
168 mainLayout->addWidget( top );
169 mainLayout->addLayout( btnLayout );
173 SUIT_Tools::centerWidget( this, parent );
179 VisuGUI_TableDlg::~VisuGUI_TableDlg()
184 <OK> button slot, saves table(s)
185 Called only in create/edit mode ( <edit> parameter for constructor is true )
187 void VisuGUI_TableDlg::onOK()
189 myOKBtn->setFocus(); // accept possible changes
193 _PTR(Study) study = myObject->GetStudy();
194 _PTR(AttributeTableOfInteger) tblIntAttr;
195 _PTR(AttributeTableOfReal) tblRealAttr;
198 _PTR(StudyBuilder) builder = study->NewBuilder();
199 builder->NewCommand(); // start transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
202 builder->RemoveAttribute( myObject, "AttributeTableOfInteger" );
203 tblIntAttr = builder->FindOrCreateAttribute( myObject, "AttributeTableOfInteger" );
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 );
217 tblIntAttr->SetNbColumns( nbCols );
218 for ( i = 0; i < nbRows; i++ ) {
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 );
228 if ( !bEmptyRow ) { // Skip rows with no data !!!
230 tblIntAttr->SetRowTitle( nRow+1, rowTitles[ i ].isNull() ? "" : rowTitles[ i ].latin1() );
232 tblIntAttr->SetRowUnit( nRow+1, units[ i ].isNull() ? "" : units[ i ].latin1() );
236 if ( nRow > 0 ) { // Set columns only if table is not empty, otherwise exception is raised !!!
238 for ( i = 0; i < colTitles.count(); i++ )
239 tblIntAttr->SetColumnTitle( i+1, colTitles[ i ].isNull() ? "" : colTitles[ i ].latin1() );
243 tblIntAttr->SetTitle( myIntTable->getTableTitle().latin1() );
246 builder->RemoveAttribute( myObject, "AttributeTableOfReal" );
247 tblRealAttr = builder->FindOrCreateAttribute( myObject, "AttributeTableOfReal" );
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 );
261 tblRealAttr->SetNbColumns( nbCols );
262 for ( i = 0; i < nbRows; i++ ) {
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 );
272 if ( !bEmptyRow ) { // Skip rows with no data !!!
274 tblRealAttr->SetRowTitle( nRow+1, rowTitles[ i ].isNull() ? "" : rowTitles[ i ].latin1() );
276 tblRealAttr->SetRowUnit( nRow+1, units[ i ].isNull() ? "" : units[ i ].latin1() );
280 if ( nRow > 0 ) { // Set columns only if table is not empty, otherwise exception is raised !!!
282 for ( i = 0; i < colTitles.count(); i++ )
283 tblRealAttr->SetColumnTitle( i+1, colTitles[ i ].isNull() ? "" : colTitles[ i ].latin1() );
287 tblRealAttr->SetTitle( myRealTable->getTableTitle().latin1() );
289 if ( myIntTable || myRealTable)
290 builder->CommitCommand(); // commit transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!
292 builder->AbortCommand(); // abort transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!
295 MESSAGE("VisuGUI_TableDlg::onOK : Exception has been caught !!!");
296 builder->AbortCommand(); // abort transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!
298 SUIT_MessageBox::error1 ( this, tr("ERR_ERROR"), tr("ERR_APP_EXCEPTION"), tr ("BUT_OK") );
307 <Help> button slot, shows corresponding help page
309 void VisuGUI_TableDlg::onHelp()
311 QString aHelpFileName = "/files/displaying_tables.htm";
312 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
314 app->onHelpContextModule(app->activeModule() ? app->moduleName(app->activeModule()->moduleName()) : QString(""), aHelpFileName);
318 platform = "winapplication";
320 platform = "application";
322 SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
323 QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
324 arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(aHelpFileName),
325 QObject::tr("BUT_OK"));
330 Populates table with data
332 void VisuGUI_TableDlg::initDlg()
336 _PTR(GenericAttribute) anAttr;
337 _PTR(AttributeTableOfInteger) tblIntAttr;
338 _PTR(AttributeTableOfReal) tblRealAttr;
339 if ( myObject->FindAttribute( anAttr, "AttributeTableOfInteger") ) {
342 if ( myObject->FindAttribute( anAttr, "AttributeTableOfReal") ) {
343 tblRealAttr = anAttr;
346 if ( tblIntAttr && myIntTable ) {
349 myIntTable->setTableTitle( tblIntAttr->GetTitle().c_str() );
351 int nbRows = tblIntAttr->GetNbRows() ;
352 int nbCols = tblIntAttr->GetNbColumns();
353 myIntTable->setNumRows( nbRows );
354 myIntTable->setNumCols( nbCols );
357 vector<string> rowTitles = tblIntAttr->GetRowTitles();
358 for ( i = 0; i < nbRows; i++ ) {
359 if ( rowTitles.size() > 0 )
360 strlist.append( rowTitles[i].c_str() );
362 strlist.append( "" );
364 myIntTable->setRowTitles( strlist );
367 vector<string> colTitles = tblIntAttr->GetColumnTitles();
368 for ( i = 0; i < nbCols; i++ ) {
369 if ( colTitles.size() > 0 )
370 strlist.append( colTitles[i].c_str() );
372 strlist.append( "" );
374 myIntTable->setColTitles( strlist );
377 vector<string> rowUnits = tblIntAttr->GetRowUnits();
378 if ( rowUnits.size() > 0 ) {
379 for ( i = 0; i < nbRows; i++ )
380 strlist.append( rowUnits[i].c_str() );
381 myIntTable->setUnits( strlist );
384 for ( i = 1; i <= nbRows; i++ ) {
386 for ( j = 1; j <= nbCols; j++ ) {
387 if ( tblIntAttr->HasValue( i, j ) )
388 strlist.append( QString::number( tblIntAttr->GetValue( i, j ) ) );
390 strlist.append( QString::null );
392 myIntTable->setRowData( i-1, strlist );
394 myIntTable->adjustTable();
397 MESSAGE("VisuGUI_TableDlg::initDlg : Exception has been caught !!!");
401 if ( tblRealAttr && myRealTable ) {
404 myRealTable->setTableTitle( tblRealAttr->GetTitle().c_str() );
406 int nbRows = tblRealAttr->GetNbRows() ;
407 int nbCols = tblRealAttr->GetNbColumns();
408 myRealTable->setNumRows( nbRows );
409 myRealTable->setNumCols( nbCols );
412 vector<string> rowTitles = tblRealAttr->GetRowTitles();
413 for ( i = 0; i < nbRows; i++ ) {
414 if ( rowTitles.size() > 0 )
415 strlist.append( rowTitles[i].c_str() );
417 strlist.append( "" );
419 myRealTable->setRowTitles( strlist );
422 vector<string> colTitles = tblRealAttr->GetColumnTitles();
423 for ( i = 0; i < nbCols; i++ ) {
424 if ( colTitles.size() > 0 )
425 strlist.append( colTitles[i].c_str() );
427 strlist.append( "" );
429 myRealTable->setColTitles( strlist );
432 vector<string> rowUnits = tblRealAttr->GetRowUnits();
433 if ( rowUnits.size() > 0 ) {
434 for ( i = 0; i < nbRows; i++ )
435 strlist.append( rowUnits[i].c_str() );
436 myRealTable->setUnits( strlist );
439 for ( i = 1; i <= nbRows; i++ ) {
441 for ( j = 1; j <= nbCols; j++ ) {
442 if ( tblRealAttr->HasValue( i, j ) )
443 strlist.append( QString::number( tblRealAttr->GetValue( i, j ) ) );
445 strlist.append( QString::null );
447 myRealTable->setRowData( i-1, strlist );
449 myRealTable->adjustTable();
452 MESSAGE("VisuGUI_TableDlg::initDlg : Exception has been caught !!!");
459 Provides help on F1 button click
461 void VisuGUI_TableDlg::keyPressEvent( QKeyEvent* e )
463 QDialog::keyPressEvent( e );
464 if ( e->isAccepted() )
467 if ( e->key() == Key_F1 )
477 VisuGUI_TableWidget::VisuGUI_TableWidget( QWidget* parent,
481 bool showColumnTitles )
482 : QWidget( parent, name ), myOrientation( orient )
484 QGridLayout* mainLayout = new QGridLayout( this );
485 mainLayout->setMargin( 0 );
486 mainLayout->setSpacing( SPACING_SIZE );
488 myTitleEdit = new QLineEdit( this, "TitleEdit" );
489 myTitleEdit->setAlignment( AlignCenter );
490 myTitleEdit->setReadOnly( !edit );
491 QFont fnt = myTitleEdit->font();
493 myTitleEdit->setFont( fnt );
495 myTable = new VisuGUI_Table( orient, this, "Table" );
496 myTable->setNumRows( 5 );
497 myTable->setNumCols( 5 );
498 myTable->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
499 myTable->setMinimumSize( MIN_TABLE_WIDTH, MIN_TABLE_HEIGHT );
500 myTable->setSelectionMode( QTable::Single );
501 myTable->setShowGrid( true );
502 myTable->setColumnMovingEnabled( false );
503 myTable->setRowMovingEnabled( false );
504 myTable->setReadOnly( !edit );
505 myTable->setDragEnabled( false );
506 setUnitsTitle( tr( "UNITS_TLT" ) );
508 if ( !showColumnTitles ) {
509 if ( myOrientation == Horizontal ) {
510 myTable->horizontalHeader()->hide();
511 myTable->setTopMargin( 0 );
514 myTable->verticalHeader()->hide();
515 myTable->setLeftMargin( 0 );
519 mainLayout->addWidget( myTitleEdit, 0, 0 );
520 mainLayout->addWidget( myTable, 1, 0 );
523 myAddRowBtn = new QPushButton( tr( "ADD_ROW_BTN" ), this, "AddRowBtn" );
524 myDelRowBtn = new QPushButton( tr( "REMOVE_ROW_BTN" ), this, "DelRowBtn" );
525 myAddColBtn = new QPushButton( tr( "ADD_COLUMN_BTN" ), this, "AddColBtn" );
526 myDelColBtn = new QPushButton( tr( "REMOVE_COLUMN_BTN" ), this, "DelColBtn" );
527 myAdjustBtn = new QPushButton( tr( "ADJUST_CELLS_BTN" ), this, "AdjustBtn" );
528 mySelectAllBtn = new QPushButton( tr( "SELECT_ALL_BTN" ), this, "SelectAllBtn" );
529 myClearBtn = new QPushButton( tr( "CLEAR_BTN"), this, "ClearBtn" );
530 QVBoxLayout* btnLayout = new QVBoxLayout; btnLayout->setMargin( 0 ); btnLayout->setSpacing( SPACING_SIZE );
531 btnLayout->addWidget( myAddRowBtn );
532 btnLayout->addWidget( myDelRowBtn );
533 btnLayout->addWidget( myAddColBtn );
534 btnLayout->addWidget( myDelColBtn );
535 btnLayout->addStretch();
536 btnLayout->addWidget( myAdjustBtn );
537 btnLayout->addStretch();
538 btnLayout->addWidget( mySelectAllBtn );
539 btnLayout->addWidget( myClearBtn );
540 mainLayout->addLayout( btnLayout, 1, 1 );
541 connect( myTable, SIGNAL( selectionChanged() ), this, SLOT( updateButtonsState() ) );
542 connect( myTable, SIGNAL( currentChanged( int, int) ), this, SLOT( updateButtonsState() ) );
543 connect( myAddRowBtn, SIGNAL( clicked() ), this, SLOT( addRow() ) );
544 connect( myAddColBtn, SIGNAL( clicked() ), this, SLOT( addCol() ) );
545 connect( myDelRowBtn, SIGNAL( clicked() ), this, SLOT( delRow() ) );
546 connect( myDelColBtn, SIGNAL( clicked() ), this, SLOT( delCol() ) );
547 connect( myAdjustBtn, SIGNAL( clicked() ), this, SLOT( adjustTable() ) );
548 connect( mySelectAllBtn, SIGNAL( clicked() ), this, SLOT( selectAll() ) );
549 connect( myClearBtn, SIGNAL( clicked() ), this, SLOT( clearTable() ) );
550 myTable->horizontalHeader()->installEventFilter( this );
551 myTable->verticalHeader()->installEventFilter( this );
552 myTable->installEventFilter( this );
554 updateButtonsState();
559 VisuGUI_TableWidget::~VisuGUI_TableWidget()
565 void VisuGUI_TableWidget::setTableTitle( const QString& title )
567 myTitleEdit->setText( title );
572 QString VisuGUI_TableWidget::getTableTitle()
574 return myTitleEdit->text();
577 Sets total number of rows
579 void VisuGUI_TableWidget::setNumRows( const int num )
581 myOrientation == Horizontal ? myTable->setNumRows( num ) : myTable->setNumCols( num );
584 Gets total number of rows
586 int VisuGUI_TableWidget::getNumRows()
588 return myOrientation == Horizontal ? myTable->numRows() : myTable->numCols();
591 Sets total number of columns
593 void VisuGUI_TableWidget::setNumCols( const int num )
595 // !!! first column contains units !!!
596 myOrientation == Horizontal ? myTable->setNumCols( num+1 ) : myTable->setNumRows( num+1 );
597 // myOrientation == Horizontal ? myTable->setColumnReadOnly( 0, true ) : myTable->setRowReadOnly( 0, true );
600 Gets total number of columns
602 int VisuGUI_TableWidget::getNumCols()
604 // !!! first column contains units !!!
605 return myOrientation == Horizontal ? myTable->numCols()-1 : myTable->numRows()-1;
610 void VisuGUI_TableWidget::setRowTitles( QStringList& tlts )
612 for ( int i = 0; i < tlts.count(); i++ ) {
613 myOrientation == Horizontal ?
614 myTable->verticalHeader()->setLabel( i, tlts[i] ) :
615 myTable->horizontalHeader()->setLabel( i, tlts[i] );
621 void VisuGUI_TableWidget::getRowTitles( QStringList& tlts )
624 if ( myOrientation == Horizontal ) {
625 for ( int i = 0; i < myTable->numRows(); i++ ) {
626 tlts.append( myTable->verticalHeader()->label( i ) );
630 for ( int i = 0; i < myTable->numCols(); i++ ) {
631 tlts.append( myTable->horizontalHeader()->label( i ) );
638 void VisuGUI_TableWidget::setColTitles( QStringList& tlts )
640 // !!! first column contains units !!!
641 for ( int i = 0; i < tlts.count(); i++ ) {
642 myOrientation == Horizontal ?
643 myTable->horizontalHeader()->setLabel( i+1, tlts[i].isNull() ? "" : tlts[i] ) :
644 myTable->verticalHeader()->setLabel( i+1, tlts[i].isNull() ? "" : tlts[i] );
646 setUnitsTitle( tr( "UNITS_TLT" ) );
651 void VisuGUI_TableWidget::getColTitles( QStringList& tlts )
653 // !!! first column contains units !!!
655 if ( myOrientation == Horizontal ) {
656 for ( int i = 1; i < myTable->numCols(); i++ ) {
657 tlts.append( myTable->horizontalHeader()->label( i ) );
661 for ( int i = 1; i < myTable->numRows(); i++ ) {
662 tlts.append( myTable->verticalHeader()->label( i ) );
669 void VisuGUI_TableWidget::setUnitsTitle( const QString& tlt ) {
670 // !!! first column contains units !!!
671 myOrientation == Horizontal ? myTable->horizontalHeader()->setLabel( 0, tlt.isNull() ? "" : tlt ) : myTable->verticalHeader()->setLabel( 0, tlt.isNull() ? "" : tlt );
676 void VisuGUI_TableWidget::setUnits( QStringList& units )
678 for ( int i = 0; i < units.count(); i++ ) {
679 myOrientation == Horizontal ? myTable->setText( i, 0, units[i].isNull() ? "" : units[i] ) : myTable->setText( 0, i, units[i].isNull() ? "" : units[i] );
685 void VisuGUI_TableWidget::getUnits( QStringList& units )
688 if ( myOrientation == Horizontal ) {
689 for ( int i = 0; i < myTable->numRows(); i++ )
690 units.append( myTable->text( i, 0 ).isNull() ? QString("") : myTable->text( i, 0 ) );
693 for ( int i = 0; i < myTable->numCols(); i++ )
694 units.append( myTable->text( 0, i ).isNull() ? QString("") : myTable->text( 0, i ) );
700 void VisuGUI_TableWidget::setRowData( int row, QStringList& data )
702 if ( row >= 0 && row < getNumRows() ) {
703 for ( int i = 0; i < data.count(); i++ ) {
704 if ( data[i].isNull() ) {
705 myOrientation == Horizontal ? myTable->clearCell( row, i+1 ) :
706 myTable->clearCell( i+1, row );
709 myOrientation == Horizontal ? myTable->setText( row, i+1, data[i] ) :
710 myTable->setText( i+1, row, data[i] );
718 void VisuGUI_TableWidget::getRowData( int row, QStringList& data )
721 if ( row >= 0 && row < getNumRows() ) {
722 if ( myOrientation == Horizontal ) {
723 for ( int i = 1; i < myTable->numCols(); i++ )
724 data.append( myTable->text( row, i ) );
727 for ( int i = 1; i < myTable->numRows(); i++ )
728 data.append( myTable->text( i, row ) );
733 Adjusts table cell to see contents, <Adjust Cells> button slot
735 void VisuGUI_TableWidget::adjustTable()
738 for ( i = 0; i < myTable->numRows(); i++ )
739 myTable->adjustRow( i );
740 for ( i = 0; i < myTable->numCols(); i++ )
741 myTable->adjustColumn( i );
744 Called when selection changed in table
746 void VisuGUI_TableWidget::updateButtonsState()
748 if ( myTable->isReadOnly() )
750 bool bDR = false; // <Delete Row(s)>
751 bool bDC = false; // <Delete Column(s)>
752 bool bSA = false; // <Select All>
753 bool bCT = false; // <Clear>
755 int c = myOrientation == Horizontal ? 0 : 1;
756 for ( i = c; i < myTable->numRows(); i++ ) {
757 if ( myTable->isRowSelected( i, true ) )
762 c = myOrientation == Horizontal ? 1 : 0;
763 for ( i = c; i < myTable->numCols(); i++ ) {
764 if ( myTable->isColumnSelected( i, true ) )
769 int nbSel = myTable->numSelections();
770 for ( i = 0; i < nbSel; i++ ) {
771 QTableSelection ts = myTable->selection( i );
772 for ( int j = ts.topRow(); j < ts.bottomRow()+1; j++) {
773 for ( int k = ts.leftCol(); k < ts.rightCol()+1; k++) {
774 if ( myTable->item( j, k ) )
779 if ( myTable->item( myTable->currentRow(), myTable->currentColumn() ) )
781 myDelRowBtn->setEnabled( bDR );
782 myDelColBtn->setEnabled( bDC );
783 mySelectAllBtn->setEnabled( bSA );
784 myClearBtn->setEnabled( bCT );
787 <Add row> button slot
789 void VisuGUI_TableWidget::addRow()
791 myTable->insertRows( myTable->numRows(), 1 );
792 updateButtonsState();
795 <Add column> button slot
797 void VisuGUI_TableWidget::addCol()
799 myTable->insertColumns( myTable->numCols(), 1 );
800 updateButtonsState();
803 <Delete row(s)> button slot
805 void VisuGUI_TableWidget::delRow()
807 int c = myOrientation == Horizontal ? 0 : 1;
810 for ( i = c; i < myTable->numRows(); i++ )
811 if ( myTable->isRowSelected( i, true ) )
813 if ( il.count() > 0 ) {
814 QMemArray<int> ildel( il.count() );
815 for ( i = 0; i < il.count(); i++ )
816 ildel[ i ] = il[ i ];
817 myTable->removeRows( ildel );
819 updateButtonsState();
822 <Delete column(s)> button slot
824 void VisuGUI_TableWidget::delCol()
826 int c = myOrientation == Horizontal ? 1 : 0;
829 for ( i = c; i < myTable->numCols(); i++ )
830 if ( myTable->isColumnSelected( i, true ) )
832 if ( il.count() > 0 ) {
833 QMemArray<int> ildel( il.count() );
834 for ( i = 0; i < il.count(); i++ )
835 ildel[ i ] = il[ i ];
836 myTable->removeColumns( ildel );
838 updateButtonsState();
841 <Select All> button slot
843 void VisuGUI_TableWidget::selectAll()
845 myTable->clearSelection();
847 ts.init( 0, 0 ); ts.expandTo( myTable->numRows()-1, myTable->numCols()-1 );
848 myTable->addSelection( ts );
849 updateButtonsState();
854 void VisuGUI_TableWidget::clearTable()
856 int nbSel = myTable->numSelections();
857 for ( int i = 0; i < nbSel; i++ ) {
858 QTableSelection ts = myTable->selection( i );
859 for ( int j = ts.topRow(); j < ts.bottomRow()+1; j++) {
860 if ( myOrientation == Vertical && j == 0 ) {
861 // continue; // UNITS
863 for ( int k = ts.leftCol(); k < ts.rightCol()+1; k++) {
864 if ( myOrientation == Horizontal && k == 0 ) {
865 // continue; // UNITS
867 myTable->clearCell( j, k );
872 myTable->clearCell( myTable->currentRow(), myTable->currentColumn() );
873 myTable->clearSelection();
874 updateButtonsState();
877 Event filter - handles titles editing
879 bool VisuGUI_TableWidget::eventFilter( QObject* o, QEvent* e )
881 if ( e->type() == QEvent::MouseButtonDblClick) {
882 QMouseEvent* me = ( QMouseEvent* )e;
883 if ( me->button() == LeftButton && !myTable->isReadOnly() ) {
884 if ( o == myTable->horizontalHeader() ) {
885 for ( int i = 0; i < myTable->horizontalHeader()->count(); i++ ) {
886 QRect rect = myTable->horizontalHeader()->sectionRect( i );
887 rect.addCoords( 1, 1, -1, -1 );
888 if ( rect.contains( myTable->horizontalHeader()->mapFromGlobal( me->globalPos() ) ) ) {
889 if ( myOrientation == Vertical || i != 0 ) {
891 QString tlt = QInputDialog::getText( tr( "SET_TITLE_TLT" ),
894 myTable->horizontalHeader()->label( i ),
897 if ( bOk && !tlt.isNull() )
898 myTable->horizontalHeader()->setLabel( i, tlt );
904 if ( o == myTable->verticalHeader() ) {
905 for ( int i = 0; i < myTable->verticalHeader()->count(); i++ ) {
906 QRect rect = myTable->verticalHeader()->sectionRect( i );
907 rect.addCoords( 1, 1, -1, -1 );
908 if ( rect.contains( myTable->verticalHeader()->mapFromGlobal( me->globalPos() ) ) ) {
909 if ( myOrientation == Horizontal || i != 0 ) {
911 QString tlt = QInputDialog::getText( tr( "SET_TITLE_TLT" ),
914 myTable->verticalHeader()->label( i ),
917 if ( bOk && !tlt.isNull() )
918 myTable->verticalHeader()->setLabel( i, tlt );
926 else if ( e->type() == QEvent::KeyRelease && o == myTable ) {
927 QKeyEvent* ke = (QKeyEvent*)e;
928 if ( ke->key() == Key_Delete && !myTable->isEditing() ) {
931 else if ( ke->key() == Key_Backspace && !myTable->isEditing() ) {
933 int i = myTable->currentRow();
934 int j = myTable->currentColumn() - 1;
935 if ( j < 0 ) { j = myTable->numCols()-1; i--; }
936 if ( i >= 0 && j >= 0 )
937 myTable->setCurrentCell( i, j );
940 return QWidget::eventFilter( o, e );