2 // File : SALOMEGUI_TableDlg.cxx
3 // Created : Wed Dec 25 12:00:00 2002
4 // Descr : Dialog box for displaying/editing of table
6 // Author : Vadim SANDLER
7 // Project : SALOME for ALLIANCES
9 // Copyright : Open CASCADE
12 #include "SALOMEGUI_TableDlg.h"
13 #include "QAD_Tools.h"
14 #include "QAD_MessageBox.h"
16 #include <qvalidator.h>
18 #include <qtabwidget.h>
19 #include <qvaluelist.h>
20 #include <qmemarray.h>
21 #include <qinputdialog.h>
23 #include "utilities.h"
25 #define MARGIN_SIZE 11
26 #define SPACING_SIZE 6
28 #define MIN_TABLE_WIDTH 200
29 #define MIN_TABLE_HEIGHT 200
32 class SALOMEGUI_Table : public QTable {
34 SALOMEGUI_Table( Orientation orient, QWidget* parent = 0, const char* name = 0 )
35 : QTable( parent, name ), myValidator( 0 ), myOrientation( orient ) {}
36 SALOMEGUI_Table( Orientation orient, int numRows, int numCols, QWidget* parent = 0, const char* name = 0 )
37 : QTable( numRows, numCols, parent, name ), myValidator( 0 ), myOrientation( orient ) {}
39 void setValidator( QValidator* v = 0 ) { myValidator = v; }
40 bool isEditing() const { return QTable::isEditing(); }
43 QWidget* createEditor ( int row, int col, bool initFromCell ) const
45 bool testUnits = ( myOrientation == Horizontal && col == 0 ) || ( myOrientation == Vertical && row == 0 );
46 QWidget* wg = QTable::createEditor( row, col, initFromCell );
47 if ( wg && wg->inherits("QLineEdit") && myValidator && !testUnits )
48 (( QLineEdit*)wg)->setValidator( myValidator );
53 QValidator* myValidator;
54 Orientation myOrientation;
60 SALOMEGUI_TableDlg::SALOMEGUI_TableDlg( QWidget* parent,
61 SALOMEDS::SObject_var obj,
65 bool showColumnTitles )
66 : QDialog( parent, "", false, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose),
67 myIntTable( 0 ), myRealTable( 0 )
69 setCaption( edit ? tr( "EDIT_TABLE_TLT" ) : tr( "VIEW_TABLE_TLT" ) );
70 setSizeGripEnabled( true );
72 myObject = SALOMEDS::SObject::_duplicate( obj );
73 bool bHasIntTable = false;
74 bool bHasRealTable = false;
75 if ( !myObject->_is_nil() ) {
76 SALOMEDS::GenericAttribute_var anAttr;
77 bHasIntTable = myObject->FindAttribute( anAttr, "AttributeTableOfInteger");
78 bHasRealTable = myObject->FindAttribute( anAttr, "AttributeTableOfReal");
81 QVBoxLayout* mainLayout = new QVBoxLayout( this );
82 mainLayout->setMargin( MARGIN_SIZE );
83 mainLayout->setSpacing( SPACING_SIZE );
85 bool bDoInt = which == ttInt || which == ttBoth || which == ttAuto && bHasIntTable;
86 bool bDoReal = which == ttReal || which == ttBoth || which == ttAuto && bHasRealTable;
90 if ( bDoInt && bDoReal ) {
91 top = new QTabWidget( this, "TabWidget" );
92 ( ( QTabWidget* ) top) ->setMargin( MARGIN_SIZE );
95 top = new QWidget( this, "DumbWidget" );
96 tl = new QVBoxLayout( top ); tl->setMargin( 0 ); tl->setSpacing( SPACING_SIZE );
100 myIntTable = new SALOMEGUI_TableWidget( top, "myIntTable", edit, orient, showColumnTitles );
101 myIntTable->getTable()->setValidator( new QIntValidator( this ) );
102 if ( bDoInt && bDoReal )
103 ( ( QTabWidget* )top )->addTab( myIntTable, tr( "TABLE_OF_INTEGER_TLT" ) );
105 tl->addWidget( myIntTable );
108 myRealTable = new SALOMEGUI_TableWidget( top, "myRealTable", edit, orient, showColumnTitles );
109 myRealTable->getTable()->setValidator( new QDoubleValidator( this ) );
110 if ( bDoInt && bDoReal )
111 ( ( QTabWidget* )top )->addTab( myRealTable, tr( "TABLE_OF_REAL_TLT" ) );
113 tl->addWidget( myRealTable );
115 if ( !bDoInt && !bDoReal ) {
116 QLabel *dumbLabel = new QLabel( tr( "ERR_TABLE_NOT_AVAILABLE" ), top, "DumbLabel" );
117 dumbLabel->setAlignment( AlignCenter );
118 tl->addWidget( dumbLabel );
121 QHBoxLayout* btnLayout = new QHBoxLayout;
122 btnLayout->setMargin( 0 ); btnLayout->setSpacing( SPACING_SIZE );
124 myOKBtn = new QPushButton( tr( "BUT_OK" ), this );
126 myCancelBtn = new QPushButton( tr( "BUT_CANCEL" ), this );
127 btnLayout->addWidget( myOKBtn );
128 btnLayout->addItem( new QSpacerItem( SPACER_SIZE, SPACER_SIZE, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
129 btnLayout->addWidget( myCancelBtn );
130 connect( myOKBtn, SIGNAL( clicked() ), this, SLOT( onOK() ) );
131 connect( myCancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
134 btnLayout->addItem( new QSpacerItem( SPACER_SIZE, SPACER_SIZE, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
135 btnLayout->addWidget( myOKBtn );
136 btnLayout->addItem( new QSpacerItem( SPACER_SIZE, SPACER_SIZE, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
137 connect( myOKBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
140 mainLayout->addWidget( top );
141 mainLayout->addLayout( btnLayout );
145 QAD_Tools::centerWidget( this, parent );
151 SALOMEGUI_TableDlg::~SALOMEGUI_TableDlg()
156 <OK> button slot, saves table(s)
157 Called only in create/edit mode ( <edit> parameter for constructor is true )
159 void SALOMEGUI_TableDlg::onOK()
161 myOKBtn->setFocus(); // accept possible changes
164 if ( !myObject->_is_nil() ) {
165 SALOMEDS::Study_var study = myObject->GetStudy();
166 SALOMEDS::GenericAttribute_var anAttr;
167 SALOMEDS::AttributeTableOfInteger_var tblIntAttr;
168 SALOMEDS::AttributeTableOfReal_var tblRealAttr;
170 if ( !study->_is_nil() ) {
171 SALOMEDS::StudyBuilder_var builder = study->NewBuilder();
172 builder->NewCommand(); // start transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
175 builder->RemoveAttribute( myObject, "AttributeTableOfInteger" );
176 tblIntAttr = SALOMEDS::AttributeTableOfInteger::_narrow(
177 builder->FindOrCreateAttribute( myObject, "AttributeTableOfInteger" ) );
180 int nbRows = myIntTable->getNumRows();
181 int nbCols = myIntTable->getNumCols();
182 QString tlt = myIntTable->getTableTitle();
183 QStringList rowTitles, colTitles, units;
184 myIntTable->getRowTitles( rowTitles );
185 myIntTable->getColTitles( colTitles );
186 myIntTable->getUnits( units );
191 tblIntAttr->SetNbColumns( nbCols );
192 for ( i = 0; i < nbRows; i++ ) {
194 myIntTable->getRowData( i, data );
195 bool bEmptyRow = true;
196 for ( int j = 0; j < data.count(); j++ ) {
197 if ( !data[ j ].isNull() ) {
198 tblIntAttr->PutValue( data[ j ].toInt(), nRow+1, j+1 );
202 if ( !bEmptyRow ) { // Skip rows with no data !!!
204 tblIntAttr->SetRowTitle( nRow+1, rowTitles[ i ].isNull() ? strdup( "" ) : strdup( rowTitles[ i ] ) );
206 tblIntAttr->SetRowUnit( nRow+1, units[ i ].isNull() ? strdup( "" ) : strdup( units[ i ] ) );
210 if ( nRow > 0 ) { // Set columns only if table is not empty, otherwise exception is raised !!!
212 for ( i = 0; i < colTitles.count(); i++ )
213 tblIntAttr->SetColumnTitle( i+1, colTitles[ i ].isNull() ? strdup( "" ) : strdup( colTitles[ i ] ) );
217 tblIntAttr->SetTitle( myIntTable->getTableTitle().latin1() );
220 builder->RemoveAttribute( myObject, "AttributeTableOfReal" );
221 tblRealAttr = SALOMEDS::AttributeTableOfReal::_narrow(
222 builder->FindOrCreateAttribute( myObject, "AttributeTableOfReal" ) );
225 int nbRows = myRealTable->getNumRows();
226 int nbCols = myRealTable->getNumCols();
227 QString tlt = myRealTable->getTableTitle();
228 QStringList rowTitles, colTitles, units;
229 myRealTable->getRowTitles( rowTitles );
230 myRealTable->getColTitles( colTitles );
231 myRealTable->getUnits( units );
236 tblRealAttr->SetNbColumns( nbCols );
237 for ( i = 0; i < nbRows; i++ ) {
239 myRealTable->getRowData( i, data );
240 bool bEmptyRow = true;
241 for ( int j = 0; j < data.count(); j++ ) {
242 if ( !data[ j ].isNull() ) {
243 tblRealAttr->PutValue( data[ j ].toDouble(), nRow+1, j+1 );
247 if ( !bEmptyRow ) { // Skip rows with no data !!!
249 tblRealAttr->SetRowTitle( nRow+1, rowTitles[ i ].isNull() ? strdup( "" ) : strdup( rowTitles[ i ] ) );
251 tblRealAttr->SetRowUnit( nRow+1, units[ i ].isNull() ? strdup( "" ) : strdup( units[ i ] ) );
255 if ( nRow > 0 ) { // Set columns only if table is not empty, otherwise exception is raised !!!
257 for ( i = 0; i < colTitles.count(); i++ )
258 tblRealAttr->SetColumnTitle( i+1, colTitles[ i ].isNull() ? strdup( "" ) : strdup( colTitles[ i ] ) );
262 tblRealAttr->SetTitle( myRealTable->getTableTitle().latin1() );
264 if ( myIntTable || myRealTable)
265 builder->CommitCommand(); // commit transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!
267 builder->AbortCommand(); // abort transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!
270 MESSAGE("SALOMEGUI_TableDlg::onOK : Exception has been caught !!!");
271 builder->AbortCommand(); // abort transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!
273 QAD_MessageBox::error1 ( this, tr("ERR_ERROR"), tr("ERR_APP_EXCEPTION"), tr ("BUT_OK") );
282 Populates table with data
284 void SALOMEGUI_TableDlg::initDlg()
287 if ( !myObject->_is_nil() ) {
288 SALOMEDS::GenericAttribute_var anAttr;
289 SALOMEDS::AttributeTableOfInteger_var tblIntAttr;
290 SALOMEDS::AttributeTableOfReal_var tblRealAttr;
291 if ( myObject->FindAttribute( anAttr, "AttributeTableOfInteger") ) {
292 tblIntAttr = SALOMEDS::AttributeTableOfInteger::_narrow( anAttr );
294 if ( myObject->FindAttribute( anAttr, "AttributeTableOfReal") ) {
295 tblRealAttr = SALOMEDS::AttributeTableOfReal::_narrow( anAttr );
298 if ( !tblIntAttr->_is_nil() && myIntTable ) {
300 SALOMEGUI_Table* tbl = myIntTable->getTable();
302 myIntTable->setTableTitle( strdup( tblIntAttr->GetTitle() ) );
304 int nbRows = tblIntAttr->GetNbRows() ;
305 int nbCols = tblIntAttr->GetNbColumns();
306 myIntTable->setNumRows( nbRows );
307 myIntTable->setNumCols( nbCols );
310 SALOMEDS::StringSeq_var rowTitles = tblIntAttr->GetRowTitles();
311 for ( i = 0; i < nbRows; i++ ) {
312 if ( rowTitles->length() > 0 )
313 strlist.append( strdup( rowTitles[i] ) );
315 strlist.append( "" );
317 myIntTable->setRowTitles( strlist );
320 SALOMEDS::StringSeq_var colTitles = tblIntAttr->GetColumnTitles();
321 for ( i = 0; i < nbCols; i++ ) {
322 if ( colTitles->length() > 0 )
323 strlist.append( strdup( colTitles[i] ) );
325 strlist.append( "" );
327 myIntTable->setColTitles( strlist );
330 SALOMEDS::StringSeq_var rowUnits = tblIntAttr->GetRowUnits();
331 if ( rowUnits->length() > 0 ) {
332 for ( i = 0; i < nbRows; i++ )
333 strlist.append( strdup( rowUnits[i] ) );
334 myIntTable->setUnits( strlist );
337 for ( i = 1; i <= nbRows; i++ ) {
339 for ( j = 1; j <= nbCols; j++ ) {
340 if ( tblIntAttr->HasValue( i, j ) )
341 strlist.append( QString::number( tblIntAttr->GetValue( i, j ) ) );
343 strlist.append( QString::null );
345 myIntTable->setRowData( i-1, strlist );
347 myIntTable->adjustTable();
350 MESSAGE("SALOMEGUI_TableDlg::initDlg : Exception has been caught !!!");
354 if ( !tblRealAttr->_is_nil() && myRealTable ) {
356 SALOMEGUI_Table* tbl = myRealTable->getTable();
358 myRealTable->setTableTitle( strdup( tblRealAttr->GetTitle() ) );
360 int nbRows = tblRealAttr->GetNbRows() ;
361 int nbCols = tblRealAttr->GetNbColumns();
362 myRealTable->setNumRows( nbRows );
363 myRealTable->setNumCols( nbCols );
366 SALOMEDS::StringSeq_var rowTitles = tblRealAttr->GetRowTitles();
367 for ( i = 0; i < nbRows; i++ ) {
368 if ( rowTitles->length() > 0 )
369 strlist.append( strdup( rowTitles[i] ) );
371 strlist.append( "" );
373 myRealTable->setRowTitles( strlist );
376 SALOMEDS::StringSeq_var colTitles = tblRealAttr->GetColumnTitles();
377 for ( i = 0; i < nbCols; i++ ) {
378 if ( colTitles->length() > 0 )
379 strlist.append( strdup( colTitles[i] ) );
381 strlist.append( "" );
383 myRealTable->setColTitles( strlist );
386 SALOMEDS::StringSeq_var rowUnits = tblRealAttr->GetRowUnits();
387 if ( rowUnits->length() > 0 ) {
388 for ( i = 0; i < nbRows; i++ )
389 strlist.append( strdup( rowUnits[i] ) );
390 myRealTable->setUnits( strlist );
393 for ( i = 1; i <= nbRows; i++ ) {
395 for ( j = 1; j <= nbCols; j++ ) {
396 if ( tblRealAttr->HasValue( i, j ) )
397 strlist.append( QString::number( tblRealAttr->GetValue( i, j ) ) );
399 strlist.append( QString::null );
401 myRealTable->setRowData( i-1, strlist );
403 myRealTable->adjustTable();
406 MESSAGE("SALOMEGUI_TableDlg::initDlg : Exception has been caught !!!");
415 SALOMEGUI_TableWidget::SALOMEGUI_TableWidget( QWidget* parent,
419 bool showColumnTitles )
420 : QWidget( parent, name ), myOrientation( orient )
422 QGridLayout* mainLayout = new QGridLayout( this );
423 mainLayout->setMargin( 0 );
424 mainLayout->setSpacing( SPACING_SIZE );
426 myTitleEdit = new QLineEdit( this, "TitleEdit" );
427 myTitleEdit->setAlignment( AlignCenter );
428 myTitleEdit->setReadOnly( !edit );
429 QFont fnt = myTitleEdit->font();
431 myTitleEdit->setFont( fnt );
433 myTable = new SALOMEGUI_Table( orient, this, "Table" );
434 myTable->setNumRows( 5 );
435 myTable->setNumCols( 5 );
436 myTable->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
437 myTable->setMinimumSize( MIN_TABLE_WIDTH, MIN_TABLE_HEIGHT );
438 myTable->setSelectionMode( QTable::Single );
439 myTable->setShowGrid( true );
440 myTable->setColumnMovingEnabled( false );
441 myTable->setRowMovingEnabled( false );
442 myTable->setReadOnly( !edit );
443 myTable->setDragEnabled( false );
444 setUnitsTitle( tr( "UNITS_TLT" ) );
446 if ( !showColumnTitles ) {
447 if ( myOrientation == Horizontal ) {
448 myTable->horizontalHeader()->hide();
449 myTable->setTopMargin( 0 );
452 myTable->verticalHeader()->hide();
453 myTable->setLeftMargin( 0 );
457 mainLayout->addWidget( myTitleEdit, 0, 0 );
458 mainLayout->addWidget( myTable, 1, 0 );
461 myAddRowBtn = new QPushButton( tr( "ADD_ROW_BTN" ), this, "AddRowBtn" );
462 myDelRowBtn = new QPushButton( tr( "REMOVE_ROW_BTN" ), this, "DelRowBtn" );
463 myAddColBtn = new QPushButton( tr( "ADD_COLUMN_BTN" ), this, "AddColBtn" );
464 myDelColBtn = new QPushButton( tr( "REMOVE_COLUMN_BTN" ), this, "DelColBtn" );
465 myAdjustBtn = new QPushButton( tr( "ADJUST_CELLS_BTN" ), this, "AdjustBtn" );
466 mySelectAllBtn = new QPushButton( tr( "SELECT_ALL_BTN" ), this, "SelectAllBtn" );
467 myClearBtn = new QPushButton( tr( "CLEAR_BTN"), this, "ClearBtn" );
468 QVBoxLayout* btnLayout = new QVBoxLayout; btnLayout->setMargin( 0 ); btnLayout->setSpacing( SPACING_SIZE );
469 btnLayout->addWidget( myAddRowBtn );
470 btnLayout->addWidget( myDelRowBtn );
471 btnLayout->addWidget( myAddColBtn );
472 btnLayout->addWidget( myDelColBtn );
473 btnLayout->addStretch();
474 btnLayout->addWidget( myAdjustBtn );
475 btnLayout->addStretch();
476 btnLayout->addWidget( mySelectAllBtn );
477 btnLayout->addWidget( myClearBtn );
478 mainLayout->addLayout( btnLayout, 1, 1 );
479 connect( myTable, SIGNAL( selectionChanged() ), this, SLOT( updateButtonsState() ) );
480 connect( myTable, SIGNAL( currentChanged( int, int) ), this, SLOT( updateButtonsState() ) );
481 connect( myAddRowBtn, SIGNAL( clicked() ), this, SLOT( addRow() ) );
482 connect( myAddColBtn, SIGNAL( clicked() ), this, SLOT( addCol() ) );
483 connect( myDelRowBtn, SIGNAL( clicked() ), this, SLOT( delRow() ) );
484 connect( myDelColBtn, SIGNAL( clicked() ), this, SLOT( delCol() ) );
485 connect( myAdjustBtn, SIGNAL( clicked() ), this, SLOT( adjustTable() ) );
486 connect( mySelectAllBtn, SIGNAL( clicked() ), this, SLOT( selectAll() ) );
487 connect( myClearBtn, SIGNAL( clicked() ), this, SLOT( clearTable() ) );
488 myTable->horizontalHeader()->installEventFilter( this );
489 myTable->verticalHeader()->installEventFilter( this );
490 myTable->installEventFilter( this );
492 updateButtonsState();
497 SALOMEGUI_TableWidget::~SALOMEGUI_TableWidget()
503 void SALOMEGUI_TableWidget::setTableTitle( const QString& title )
505 myTitleEdit->setText( title );
510 QString SALOMEGUI_TableWidget::getTableTitle()
512 return myTitleEdit->text();
515 Sets total number of rows
517 void SALOMEGUI_TableWidget::setNumRows( const int num )
519 myOrientation == Horizontal ? myTable->setNumRows( num ) : myTable->setNumCols( num );
522 Gets total number of rows
524 int SALOMEGUI_TableWidget::getNumRows()
526 return myOrientation == Horizontal ? myTable->numRows() : myTable->numCols();
529 Sets total number of columns
531 void SALOMEGUI_TableWidget::setNumCols( const int num )
533 // !!! first column contains units !!!
534 myOrientation == Horizontal ? myTable->setNumCols( num+1 ) : myTable->setNumRows( num+1 );
535 // myOrientation == Horizontal ? myTable->setColumnReadOnly( 0, true ) : myTable->setRowReadOnly( 0, true );
538 Gets total number of columns
540 int SALOMEGUI_TableWidget::getNumCols()
542 // !!! first column contains units !!!
543 return myOrientation == Horizontal ? myTable->numCols()-1 : myTable->numRows()-1;
548 void SALOMEGUI_TableWidget::setRowTitles( QStringList& tlts )
550 for ( int i = 0; i < tlts.count(); i++ ) {
551 myOrientation == Horizontal ?
552 myTable->verticalHeader()->setLabel( i, tlts[i] ) :
553 myTable->horizontalHeader()->setLabel( i, tlts[i] );
559 void SALOMEGUI_TableWidget::getRowTitles( QStringList& tlts )
562 if ( myOrientation == Horizontal ) {
563 for ( int i = 0; i < myTable->numRows(); i++ ) {
564 tlts.append( myTable->verticalHeader()->label( i ) );
568 for ( int i = 0; i < myTable->numCols(); i++ ) {
569 tlts.append( myTable->horizontalHeader()->label( i ) );
576 void SALOMEGUI_TableWidget::setColTitles( QStringList& tlts )
578 // !!! first column contains units !!!
579 for ( int i = 0; i < tlts.count(); i++ ) {
580 myOrientation == Horizontal ?
581 myTable->horizontalHeader()->setLabel( i+1, tlts[i] ) :
582 myTable->verticalHeader()->setLabel( i+1, tlts[i] );
588 void SALOMEGUI_TableWidget::getColTitles( QStringList& tlts )
590 // !!! first column contains units !!!
592 if ( myOrientation == Horizontal ) {
593 for ( int i = 1; i < myTable->numCols(); i++ ) {
594 tlts.append( myTable->horizontalHeader()->label( i ) );
598 for ( int i = 1; i < myTable->numRows(); i++ ) {
599 tlts.append( myTable->verticalHeader()->label( i ) );
606 void SALOMEGUI_TableWidget::setUnitsTitle( const QString& tlt ) {
607 // !!! first column contains units !!!
608 myOrientation == Horizontal ? myTable->horizontalHeader()->setLabel( 0, tlt ) : myTable->verticalHeader()->setLabel( 0, tlt );
613 void SALOMEGUI_TableWidget::setUnits( QStringList& units )
615 for ( int i = 0; i < units.count(); i++ ) {
616 myOrientation == Horizontal ? myTable->setText( i, 0, units[i] ) : myTable->setText( 0, i, units[i] );
622 void SALOMEGUI_TableWidget::getUnits( QStringList& units )
625 if ( myOrientation == Horizontal ) {
626 for ( int i = 0; i < myTable->numRows(); i++ )
627 units.append( myTable->text( i, 0 ).isNull() ? QString("") : myTable->text( i, 0 ) );
630 for ( int i = 0; i < myTable->numCols(); i++ )
631 units.append( myTable->text( 0, i ).isNull() ? QString("") : myTable->text( 0, i ) );
637 void SALOMEGUI_TableWidget::setRowData( int row, QStringList& data )
639 if ( row >= 0 && row < getNumRows() ) {
640 for ( int i = 0; i < data.count(); i++ ) {
641 if ( data[i].isNull() ) {
642 myOrientation == Horizontal ? myTable->clearCell( row, i+1 ) :
643 myTable->clearCell( i+1, row );
646 myOrientation == Horizontal ? myTable->setText( row, i+1, data[i] ) :
647 myTable->setText( i+1, row, data[i] );
655 void SALOMEGUI_TableWidget::getRowData( int row, QStringList& data )
658 if ( row >= 0 && row < getNumRows() ) {
659 if ( myOrientation == Horizontal ) {
660 for ( int i = 1; i < myTable->numCols(); i++ )
661 data.append( myTable->text( row, i ) );
664 for ( int i = 1; i < myTable->numRows(); i++ )
665 data.append( myTable->text( i, row ) );
670 Adjusts table cell to see contents, <Adjust Cells> button slot
672 void SALOMEGUI_TableWidget::adjustTable()
675 for ( i = 0; i < myTable->numRows(); i++ )
676 myTable->adjustRow( i );
677 for ( i = 0; i < myTable->numCols(); i++ )
678 myTable->adjustColumn( i );
681 Called when selection changed in table
683 void SALOMEGUI_TableWidget::updateButtonsState()
685 if ( myTable->isReadOnly() )
687 bool bDR = false; // <Delete Row(s)>
688 bool bDC = false; // <Delete Column(s)>
689 bool bSA = false; // <Select All>
690 bool bCT = false; // <Clear>
692 int c = myOrientation == Horizontal ? 0 : 1;
693 for ( i = c; i < myTable->numRows(); i++ ) {
694 if ( myTable->isRowSelected( i, true ) )
699 c = myOrientation == Horizontal ? 1 : 0;
700 for ( i = c; i < myTable->numCols(); i++ ) {
701 if ( myTable->isColumnSelected( i, true ) )
706 int nbSel = myTable->numSelections();
707 for ( i = 0; i < nbSel; i++ ) {
708 QTableSelection ts = myTable->selection( i );
709 for ( int j = ts.topRow(); j < ts.bottomRow()+1; j++) {
710 for ( int k = ts.leftCol(); k < ts.rightCol()+1; k++) {
711 if ( myTable->item( j, k ) )
716 if ( myTable->item( myTable->currentRow(), myTable->currentColumn() ) )
718 myDelRowBtn->setEnabled( bDR );
719 myDelColBtn->setEnabled( bDC );
720 mySelectAllBtn->setEnabled( bSA );
721 myClearBtn->setEnabled( bCT );
724 <Add row> button slot
726 void SALOMEGUI_TableWidget::addRow()
728 myTable->insertRows( myTable->numRows(), 1 );
729 updateButtonsState();
732 <Add column> button slot
734 void SALOMEGUI_TableWidget::addCol()
736 myTable->insertColumns( myTable->numCols(), 1 );
737 updateButtonsState();
740 <Delete row(s)> button slot
742 void SALOMEGUI_TableWidget::delRow()
744 int c = myOrientation == Horizontal ? 0 : 1;
747 for ( i = c; i < myTable->numRows(); i++ )
748 if ( myTable->isRowSelected( i, true ) )
750 if ( il.count() > 0 ) {
751 QMemArray<int> ildel( il.count() );
752 for ( i = 0; i < il.count(); i++ )
753 ildel[ i ] = il[ i ];
754 myTable->removeRows( ildel );
756 updateButtonsState();
759 <Delete column(s)> button slot
761 void SALOMEGUI_TableWidget::delCol()
763 int c = myOrientation == Horizontal ? 1 : 0;
766 for ( i = c; i < myTable->numCols(); i++ )
767 if ( myTable->isColumnSelected( i, true ) )
769 if ( il.count() > 0 ) {
770 QMemArray<int> ildel( il.count() );
771 for ( i = 0; i < il.count(); i++ )
772 ildel[ i ] = il[ i ];
773 myTable->removeColumns( ildel );
775 updateButtonsState();
778 <Select All> button slot
780 void SALOMEGUI_TableWidget::selectAll()
782 myTable->clearSelection();
784 ts.init( 0, 0 ); ts.expandTo( myTable->numRows()-1, myTable->numCols()-1 );
785 myTable->addSelection( ts );
786 updateButtonsState();
791 void SALOMEGUI_TableWidget::clearTable()
793 int nbSel = myTable->numSelections();
794 for ( int i = 0; i < nbSel; i++ ) {
795 QTableSelection ts = myTable->selection( i );
796 for ( int j = ts.topRow(); j < ts.bottomRow()+1; j++) {
797 if ( myOrientation == Vertical && j == 0 ) {
798 // continue; // UNITS
800 for ( int k = ts.leftCol(); k < ts.rightCol()+1; k++) {
801 if ( myOrientation == Horizontal && k == 0 ) {
802 // continue; // UNITS
804 myTable->clearCell( j, k );
809 myTable->clearCell( myTable->currentRow(), myTable->currentColumn() );
810 myTable->clearSelection();
811 updateButtonsState();
814 Event filter - handles titles editing
816 bool SALOMEGUI_TableWidget::eventFilter( QObject* o, QEvent* e )
818 if ( e->type() == QEvent::MouseButtonDblClick) {
819 QMouseEvent* me = ( QMouseEvent* )e;
820 if ( me->button() == LeftButton && !myTable->isReadOnly() ) {
821 if ( o == myTable->horizontalHeader() ) {
822 for ( int i = 0; i < myTable->horizontalHeader()->count(); i++ ) {
823 QRect rect = myTable->horizontalHeader()->sectionRect( i );
824 rect.addCoords( 1, 1, -1, -1 );
825 if ( rect.contains( myTable->horizontalHeader()->mapFromGlobal( me->globalPos() ) ) ) {
826 if ( myOrientation == Vertical || i != 0 ) {
828 QString tlt = QInputDialog::getText( tr( "SET_TITLE_TLT" ),
831 myTable->horizontalHeader()->label( i ),
834 if ( bOk && !tlt.isNull() )
835 myTable->horizontalHeader()->setLabel( i, tlt );
841 if ( o == myTable->verticalHeader() ) {
842 for ( int i = 0; i < myTable->verticalHeader()->count(); i++ ) {
843 QRect rect = myTable->verticalHeader()->sectionRect( i );
844 rect.addCoords( 1, 1, -1, -1 );
845 if ( rect.contains( myTable->verticalHeader()->mapFromGlobal( me->globalPos() ) ) ) {
846 if ( myOrientation == Horizontal || i != 0 ) {
848 QString tlt = QInputDialog::getText( tr( "SET_TITLE_TLT" ),
851 myTable->verticalHeader()->label( i ),
854 if ( bOk && !tlt.isNull() )
855 myTable->verticalHeader()->setLabel( i, tlt );
863 else if ( e->type() == QEvent::KeyRelease && o == myTable ) {
864 QKeyEvent* ke = (QKeyEvent*)e;
865 if ( ke->key() == Key_Delete && !myTable->isEditing() ) {
868 else if ( ke->key() == Key_Backspace && !myTable->isEditing() ) {
870 int i = myTable->currentRow();
871 int j = myTable->currentColumn() - 1;
872 if ( j < 0 ) { j = myTable->numCols()-1; i--; }
873 if ( i >= 0 && j >= 0 )
874 myTable->setCurrentCell( i, j );
877 return QWidget::eventFilter( o, e );