1 // SALOME SALOMEGUI : implementation of desktop and GUI kernel
3 // Copyright (C) 2003 CEA/DEN, EDF R&D
7 // File : SALOMEGUI_TableDlg.cxx
8 // Author : Vadim SANDLER
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"
26 #define MARGIN_SIZE 11
27 #define SPACING_SIZE 6
29 #define MIN_TABLE_WIDTH 200
30 #define MIN_TABLE_HEIGHT 200
33 class SALOMEGUI_Table : public QTable {
35 SALOMEGUI_Table( Orientation orient, QWidget* parent = 0, const char* name = 0 )
36 : QTable( parent, name ), myValidator( 0 ), myOrientation( orient ) {}
37 SALOMEGUI_Table( Orientation orient, int numRows, int numCols, QWidget* parent = 0, const char* name = 0 )
38 : QTable( numRows, numCols, parent, name ), myValidator( 0 ), myOrientation( orient ) {}
40 void setValidator( QValidator* v = 0 ) { myValidator = v; }
41 bool isEditing() const { return QTable::isEditing(); }
44 QWidget* createEditor ( int row, int col, bool initFromCell ) const
46 bool testUnits = ( myOrientation == Horizontal && col == 0 ) || ( myOrientation == Vertical && row == 0 );
47 QWidget* wg = QTable::createEditor( row, col, initFromCell );
48 if ( wg && wg->inherits("QLineEdit") && myValidator && !testUnits )
49 (( QLineEdit*)wg)->setValidator( myValidator );
54 QValidator* myValidator;
55 Orientation myOrientation;
61 SALOMEGUI_TableDlg::SALOMEGUI_TableDlg( QWidget* parent,
62 SALOMEDS::SObject_var obj,
66 bool showColumnTitles )
67 : QDialog( parent, "", false, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose),
68 myIntTable( 0 ), myRealTable( 0 )
70 setCaption( edit ? tr( "EDIT_TABLE_TLT" ) : tr( "VIEW_TABLE_TLT" ) );
71 setSizeGripEnabled( true );
73 myObject = SALOMEDS::SObject::_duplicate( obj );
74 bool bHasIntTable = false;
75 bool bHasRealTable = false;
76 if ( !myObject->_is_nil() ) {
77 SALOMEDS::GenericAttribute_var anAttr;
78 bHasIntTable = myObject->FindAttribute( anAttr, "AttributeTableOfInteger");
79 bHasRealTable = myObject->FindAttribute( anAttr, "AttributeTableOfReal");
82 QVBoxLayout* mainLayout = new QVBoxLayout( this );
83 mainLayout->setMargin( MARGIN_SIZE );
84 mainLayout->setSpacing( SPACING_SIZE );
86 bool bDoInt = which == ttInt || which == ttBoth || which == ttAuto && bHasIntTable;
87 bool bDoReal = which == ttReal || which == ttBoth || which == ttAuto && bHasRealTable;
91 if ( bDoInt && bDoReal ) {
92 top = new QTabWidget( this, "TabWidget" );
93 ( ( QTabWidget* ) top) ->setMargin( MARGIN_SIZE );
96 top = new QWidget( this, "DumbWidget" );
97 tl = new QVBoxLayout( top ); tl->setMargin( 0 ); tl->setSpacing( SPACING_SIZE );
101 myIntTable = new SALOMEGUI_TableWidget( top, "myIntTable", edit, orient, showColumnTitles );
102 myIntTable->getTable()->setValidator( new QIntValidator( this ) );
103 if ( bDoInt && bDoReal )
104 ( ( QTabWidget* )top )->addTab( myIntTable, tr( "TABLE_OF_INTEGER_TLT" ) );
106 tl->addWidget( myIntTable );
109 myRealTable = new SALOMEGUI_TableWidget( top, "myRealTable", edit, orient, showColumnTitles );
110 myRealTable->getTable()->setValidator( new QDoubleValidator( this ) );
111 if ( bDoInt && bDoReal )
112 ( ( QTabWidget* )top )->addTab( myRealTable, tr( "TABLE_OF_REAL_TLT" ) );
114 tl->addWidget( myRealTable );
116 if ( !bDoInt && !bDoReal ) {
117 QLabel *dumbLabel = new QLabel( tr( "ERR_TABLE_NOT_AVAILABLE" ), top, "DumbLabel" );
118 dumbLabel->setAlignment( AlignCenter );
119 tl->addWidget( dumbLabel );
122 QHBoxLayout* btnLayout = new QHBoxLayout;
123 btnLayout->setMargin( 0 ); btnLayout->setSpacing( SPACING_SIZE );
125 myOKBtn = new QPushButton( tr( "BUT_OK" ), this );
127 myCancelBtn = new QPushButton( tr( "BUT_CANCEL" ), this );
128 btnLayout->addWidget( myOKBtn );
129 btnLayout->addItem( new QSpacerItem( SPACER_SIZE, SPACER_SIZE, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
130 btnLayout->addWidget( myCancelBtn );
131 connect( myOKBtn, SIGNAL( clicked() ), this, SLOT( onOK() ) );
132 connect( myCancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
135 btnLayout->addItem( new QSpacerItem( SPACER_SIZE, SPACER_SIZE, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
136 btnLayout->addWidget( myOKBtn );
137 btnLayout->addItem( new QSpacerItem( SPACER_SIZE, SPACER_SIZE, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
138 connect( myOKBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
141 mainLayout->addWidget( top );
142 mainLayout->addLayout( btnLayout );
146 QAD_Tools::centerWidget( this, parent );
152 SALOMEGUI_TableDlg::~SALOMEGUI_TableDlg()
157 <OK> button slot, saves table(s)
158 Called only in create/edit mode ( <edit> parameter for constructor is true )
160 void SALOMEGUI_TableDlg::onOK()
162 myOKBtn->setFocus(); // accept possible changes
165 if ( !myObject->_is_nil() ) {
166 SALOMEDS::Study_var study = myObject->GetStudy();
167 SALOMEDS::GenericAttribute_var anAttr;
168 SALOMEDS::AttributeTableOfInteger_var tblIntAttr;
169 SALOMEDS::AttributeTableOfReal_var tblRealAttr;
171 if ( !study->_is_nil() ) {
172 SALOMEDS::StudyBuilder_var builder = study->NewBuilder();
173 builder->NewCommand(); // start transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
176 builder->RemoveAttribute( myObject, "AttributeTableOfInteger" );
177 tblIntAttr = SALOMEDS::AttributeTableOfInteger::_narrow(
178 builder->FindOrCreateAttribute( myObject, "AttributeTableOfInteger" ) );
181 int nbRows = myIntTable->getNumRows();
182 int nbCols = myIntTable->getNumCols();
183 QString tlt = myIntTable->getTableTitle();
184 QStringList rowTitles, colTitles, units;
185 myIntTable->getRowTitles( rowTitles );
186 myIntTable->getColTitles( colTitles );
187 myIntTable->getUnits( units );
192 tblIntAttr->SetNbColumns( nbCols );
193 for ( i = 0; i < nbRows; i++ ) {
195 myIntTable->getRowData( i, data );
196 bool bEmptyRow = true;
197 for ( int j = 0; j < data.count(); j++ ) {
198 if ( !data[ j ].isNull() ) {
199 tblIntAttr->PutValue( data[ j ].toInt(), nRow+1, j+1 );
203 if ( !bEmptyRow ) { // Skip rows with no data !!!
205 tblIntAttr->SetRowTitle( nRow+1, rowTitles[ i ].isNull() ? strdup( "" ) : strdup( rowTitles[ i ] ) );
207 tblIntAttr->SetRowUnit( nRow+1, units[ i ].isNull() ? strdup( "" ) : strdup( units[ i ] ) );
211 if ( nRow > 0 ) { // Set columns only if table is not empty, otherwise exception is raised !!!
213 for ( i = 0; i < colTitles.count(); i++ )
214 tblIntAttr->SetColumnTitle( i+1, colTitles[ i ].isNull() ? strdup( "" ) : strdup( colTitles[ i ] ) );
218 tblIntAttr->SetTitle( myIntTable->getTableTitle().latin1() );
221 builder->RemoveAttribute( myObject, "AttributeTableOfReal" );
222 tblRealAttr = SALOMEDS::AttributeTableOfReal::_narrow(
223 builder->FindOrCreateAttribute( myObject, "AttributeTableOfReal" ) );
226 int nbRows = myRealTable->getNumRows();
227 int nbCols = myRealTable->getNumCols();
228 QString tlt = myRealTable->getTableTitle();
229 QStringList rowTitles, colTitles, units;
230 myRealTable->getRowTitles( rowTitles );
231 myRealTable->getColTitles( colTitles );
232 myRealTable->getUnits( units );
237 tblRealAttr->SetNbColumns( nbCols );
238 for ( i = 0; i < nbRows; i++ ) {
240 myRealTable->getRowData( i, data );
241 bool bEmptyRow = true;
242 for ( int j = 0; j < data.count(); j++ ) {
243 if ( !data[ j ].isNull() ) {
244 tblRealAttr->PutValue( data[ j ].toDouble(), nRow+1, j+1 );
248 if ( !bEmptyRow ) { // Skip rows with no data !!!
250 tblRealAttr->SetRowTitle( nRow+1, rowTitles[ i ].isNull() ? strdup( "" ) : strdup( rowTitles[ i ] ) );
252 tblRealAttr->SetRowUnit( nRow+1, units[ i ].isNull() ? strdup( "" ) : strdup( units[ i ] ) );
256 if ( nRow > 0 ) { // Set columns only if table is not empty, otherwise exception is raised !!!
258 for ( i = 0; i < colTitles.count(); i++ )
259 tblRealAttr->SetColumnTitle( i+1, colTitles[ i ].isNull() ? strdup( "" ) : strdup( colTitles[ i ] ) );
263 tblRealAttr->SetTitle( myRealTable->getTableTitle().latin1() );
265 if ( myIntTable || myRealTable)
266 builder->CommitCommand(); // commit transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!
268 builder->AbortCommand(); // abort transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!
271 MESSAGE("SALOMEGUI_TableDlg::onOK : Exception has been caught !!!");
272 builder->AbortCommand(); // abort transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!
274 QAD_MessageBox::error1 ( this, tr("ERR_ERROR"), tr("ERR_APP_EXCEPTION"), tr ("BUT_OK") );
283 Populates table with data
285 void SALOMEGUI_TableDlg::initDlg()
288 if ( !myObject->_is_nil() ) {
289 SALOMEDS::GenericAttribute_var anAttr;
290 SALOMEDS::AttributeTableOfInteger_var tblIntAttr;
291 SALOMEDS::AttributeTableOfReal_var tblRealAttr;
292 if ( myObject->FindAttribute( anAttr, "AttributeTableOfInteger") ) {
293 tblIntAttr = SALOMEDS::AttributeTableOfInteger::_narrow( anAttr );
295 if ( myObject->FindAttribute( anAttr, "AttributeTableOfReal") ) {
296 tblRealAttr = SALOMEDS::AttributeTableOfReal::_narrow( anAttr );
299 if ( !tblIntAttr->_is_nil() && myIntTable ) {
301 SALOMEGUI_Table* tbl = myIntTable->getTable();
303 myIntTable->setTableTitle( strdup( tblIntAttr->GetTitle() ) );
305 int nbRows = tblIntAttr->GetNbRows() ;
306 int nbCols = tblIntAttr->GetNbColumns();
307 myIntTable->setNumRows( nbRows );
308 myIntTable->setNumCols( nbCols );
311 SALOMEDS::StringSeq_var rowTitles = tblIntAttr->GetRowTitles();
312 for ( i = 0; i < nbRows; i++ ) {
313 if ( rowTitles->length() > 0 )
314 strlist.append( strdup( rowTitles[i] ) );
316 strlist.append( "" );
318 myIntTable->setRowTitles( strlist );
321 SALOMEDS::StringSeq_var colTitles = tblIntAttr->GetColumnTitles();
322 for ( i = 0; i < nbCols; i++ ) {
323 if ( colTitles->length() > 0 )
324 strlist.append( strdup( colTitles[i] ) );
326 strlist.append( "" );
328 myIntTable->setColTitles( strlist );
331 SALOMEDS::StringSeq_var rowUnits = tblIntAttr->GetRowUnits();
332 if ( rowUnits->length() > 0 ) {
333 for ( i = 0; i < nbRows; i++ )
334 strlist.append( strdup( rowUnits[i] ) );
335 myIntTable->setUnits( strlist );
338 for ( i = 1; i <= nbRows; i++ ) {
340 for ( j = 1; j <= nbCols; j++ ) {
341 if ( tblIntAttr->HasValue( i, j ) )
342 strlist.append( QString::number( tblIntAttr->GetValue( i, j ) ) );
344 strlist.append( QString::null );
346 myIntTable->setRowData( i-1, strlist );
348 myIntTable->adjustTable();
351 MESSAGE("SALOMEGUI_TableDlg::initDlg : Exception has been caught !!!");
355 if ( !tblRealAttr->_is_nil() && myRealTable ) {
357 SALOMEGUI_Table* tbl = myRealTable->getTable();
359 myRealTable->setTableTitle( strdup( tblRealAttr->GetTitle() ) );
361 int nbRows = tblRealAttr->GetNbRows() ;
362 int nbCols = tblRealAttr->GetNbColumns();
363 myRealTable->setNumRows( nbRows );
364 myRealTable->setNumCols( nbCols );
367 SALOMEDS::StringSeq_var rowTitles = tblRealAttr->GetRowTitles();
368 for ( i = 0; i < nbRows; i++ ) {
369 if ( rowTitles->length() > 0 )
370 strlist.append( strdup( rowTitles[i] ) );
372 strlist.append( "" );
374 myRealTable->setRowTitles( strlist );
377 SALOMEDS::StringSeq_var colTitles = tblRealAttr->GetColumnTitles();
378 for ( i = 0; i < nbCols; i++ ) {
379 if ( colTitles->length() > 0 )
380 strlist.append( strdup( colTitles[i] ) );
382 strlist.append( "" );
384 myRealTable->setColTitles( strlist );
387 SALOMEDS::StringSeq_var rowUnits = tblRealAttr->GetRowUnits();
388 if ( rowUnits->length() > 0 ) {
389 for ( i = 0; i < nbRows; i++ )
390 strlist.append( strdup( rowUnits[i] ) );
391 myRealTable->setUnits( strlist );
394 for ( i = 1; i <= nbRows; i++ ) {
396 for ( j = 1; j <= nbCols; j++ ) {
397 if ( tblRealAttr->HasValue( i, j ) )
398 strlist.append( QString::number( tblRealAttr->GetValue( i, j ) ) );
400 strlist.append( QString::null );
402 myRealTable->setRowData( i-1, strlist );
404 myRealTable->adjustTable();
407 MESSAGE("SALOMEGUI_TableDlg::initDlg : Exception has been caught !!!");
416 SALOMEGUI_TableWidget::SALOMEGUI_TableWidget( QWidget* parent,
420 bool showColumnTitles )
421 : QWidget( parent, name ), myOrientation( orient )
423 QGridLayout* mainLayout = new QGridLayout( this );
424 mainLayout->setMargin( 0 );
425 mainLayout->setSpacing( SPACING_SIZE );
427 myTitleEdit = new QLineEdit( this, "TitleEdit" );
428 myTitleEdit->setAlignment( AlignCenter );
429 myTitleEdit->setReadOnly( !edit );
430 QFont fnt = myTitleEdit->font();
432 myTitleEdit->setFont( fnt );
434 myTable = new SALOMEGUI_Table( orient, this, "Table" );
435 myTable->setNumRows( 5 );
436 myTable->setNumCols( 5 );
437 myTable->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
438 myTable->setMinimumSize( MIN_TABLE_WIDTH, MIN_TABLE_HEIGHT );
439 myTable->setSelectionMode( QTable::Single );
440 myTable->setShowGrid( true );
441 myTable->setColumnMovingEnabled( false );
442 myTable->setRowMovingEnabled( false );
443 myTable->setReadOnly( !edit );
444 myTable->setDragEnabled( false );
445 setUnitsTitle( tr( "UNITS_TLT" ) );
447 if ( !showColumnTitles ) {
448 if ( myOrientation == Horizontal ) {
449 myTable->horizontalHeader()->hide();
450 myTable->setTopMargin( 0 );
453 myTable->verticalHeader()->hide();
454 myTable->setLeftMargin( 0 );
458 mainLayout->addWidget( myTitleEdit, 0, 0 );
459 mainLayout->addWidget( myTable, 1, 0 );
462 myAddRowBtn = new QPushButton( tr( "ADD_ROW_BTN" ), this, "AddRowBtn" );
463 myDelRowBtn = new QPushButton( tr( "REMOVE_ROW_BTN" ), this, "DelRowBtn" );
464 myAddColBtn = new QPushButton( tr( "ADD_COLUMN_BTN" ), this, "AddColBtn" );
465 myDelColBtn = new QPushButton( tr( "REMOVE_COLUMN_BTN" ), this, "DelColBtn" );
466 myAdjustBtn = new QPushButton( tr( "ADJUST_CELLS_BTN" ), this, "AdjustBtn" );
467 mySelectAllBtn = new QPushButton( tr( "SELECT_ALL_BTN" ), this, "SelectAllBtn" );
468 myClearBtn = new QPushButton( tr( "CLEAR_BTN"), this, "ClearBtn" );
469 QVBoxLayout* btnLayout = new QVBoxLayout; btnLayout->setMargin( 0 ); btnLayout->setSpacing( SPACING_SIZE );
470 btnLayout->addWidget( myAddRowBtn );
471 btnLayout->addWidget( myDelRowBtn );
472 btnLayout->addWidget( myAddColBtn );
473 btnLayout->addWidget( myDelColBtn );
474 btnLayout->addStretch();
475 btnLayout->addWidget( myAdjustBtn );
476 btnLayout->addStretch();
477 btnLayout->addWidget( mySelectAllBtn );
478 btnLayout->addWidget( myClearBtn );
479 mainLayout->addLayout( btnLayout, 1, 1 );
480 connect( myTable, SIGNAL( selectionChanged() ), this, SLOT( updateButtonsState() ) );
481 connect( myTable, SIGNAL( currentChanged( int, int) ), this, SLOT( updateButtonsState() ) );
482 connect( myAddRowBtn, SIGNAL( clicked() ), this, SLOT( addRow() ) );
483 connect( myAddColBtn, SIGNAL( clicked() ), this, SLOT( addCol() ) );
484 connect( myDelRowBtn, SIGNAL( clicked() ), this, SLOT( delRow() ) );
485 connect( myDelColBtn, SIGNAL( clicked() ), this, SLOT( delCol() ) );
486 connect( myAdjustBtn, SIGNAL( clicked() ), this, SLOT( adjustTable() ) );
487 connect( mySelectAllBtn, SIGNAL( clicked() ), this, SLOT( selectAll() ) );
488 connect( myClearBtn, SIGNAL( clicked() ), this, SLOT( clearTable() ) );
489 myTable->horizontalHeader()->installEventFilter( this );
490 myTable->verticalHeader()->installEventFilter( this );
491 myTable->installEventFilter( this );
493 updateButtonsState();
498 SALOMEGUI_TableWidget::~SALOMEGUI_TableWidget()
504 void SALOMEGUI_TableWidget::setTableTitle( const QString& title )
506 myTitleEdit->setText( title );
511 QString SALOMEGUI_TableWidget::getTableTitle()
513 return myTitleEdit->text();
516 Sets total number of rows
518 void SALOMEGUI_TableWidget::setNumRows( const int num )
520 myOrientation == Horizontal ? myTable->setNumRows( num ) : myTable->setNumCols( num );
523 Gets total number of rows
525 int SALOMEGUI_TableWidget::getNumRows()
527 return myOrientation == Horizontal ? myTable->numRows() : myTable->numCols();
530 Sets total number of columns
532 void SALOMEGUI_TableWidget::setNumCols( const int num )
534 // !!! first column contains units !!!
535 myOrientation == Horizontal ? myTable->setNumCols( num+1 ) : myTable->setNumRows( num+1 );
536 // myOrientation == Horizontal ? myTable->setColumnReadOnly( 0, true ) : myTable->setRowReadOnly( 0, true );
539 Gets total number of columns
541 int SALOMEGUI_TableWidget::getNumCols()
543 // !!! first column contains units !!!
544 return myOrientation == Horizontal ? myTable->numCols()-1 : myTable->numRows()-1;
549 void SALOMEGUI_TableWidget::setRowTitles( QStringList& tlts )
551 for ( int i = 0; i < tlts.count(); i++ ) {
552 myOrientation == Horizontal ?
553 myTable->verticalHeader()->setLabel( i, tlts[i] ) :
554 myTable->horizontalHeader()->setLabel( i, tlts[i] );
560 void SALOMEGUI_TableWidget::getRowTitles( QStringList& tlts )
563 if ( myOrientation == Horizontal ) {
564 for ( int i = 0; i < myTable->numRows(); i++ ) {
565 tlts.append( myTable->verticalHeader()->label( i ) );
569 for ( int i = 0; i < myTable->numCols(); i++ ) {
570 tlts.append( myTable->horizontalHeader()->label( i ) );
577 void SALOMEGUI_TableWidget::setColTitles( QStringList& tlts )
579 // !!! first column contains units !!!
580 for ( int i = 0; i < tlts.count(); i++ ) {
581 myOrientation == Horizontal ?
582 myTable->horizontalHeader()->setLabel( i+1, tlts[i] ) :
583 myTable->verticalHeader()->setLabel( i+1, tlts[i] );
589 void SALOMEGUI_TableWidget::getColTitles( QStringList& tlts )
591 // !!! first column contains units !!!
593 if ( myOrientation == Horizontal ) {
594 for ( int i = 1; i < myTable->numCols(); i++ ) {
595 tlts.append( myTable->horizontalHeader()->label( i ) );
599 for ( int i = 1; i < myTable->numRows(); i++ ) {
600 tlts.append( myTable->verticalHeader()->label( i ) );
607 void SALOMEGUI_TableWidget::setUnitsTitle( const QString& tlt ) {
608 // !!! first column contains units !!!
609 myOrientation == Horizontal ? myTable->horizontalHeader()->setLabel( 0, tlt ) : myTable->verticalHeader()->setLabel( 0, tlt );
614 void SALOMEGUI_TableWidget::setUnits( QStringList& units )
616 for ( int i = 0; i < units.count(); i++ ) {
617 myOrientation == Horizontal ? myTable->setText( i, 0, units[i] ) : myTable->setText( 0, i, units[i] );
623 void SALOMEGUI_TableWidget::getUnits( QStringList& units )
626 if ( myOrientation == Horizontal ) {
627 for ( int i = 0; i < myTable->numRows(); i++ )
628 units.append( myTable->text( i, 0 ).isNull() ? QString("") : myTable->text( i, 0 ) );
631 for ( int i = 0; i < myTable->numCols(); i++ )
632 units.append( myTable->text( 0, i ).isNull() ? QString("") : myTable->text( 0, i ) );
638 void SALOMEGUI_TableWidget::setRowData( int row, QStringList& data )
640 if ( row >= 0 && row < getNumRows() ) {
641 for ( int i = 0; i < data.count(); i++ ) {
642 if ( data[i].isNull() ) {
643 myOrientation == Horizontal ? myTable->clearCell( row, i+1 ) :
644 myTable->clearCell( i+1, row );
647 myOrientation == Horizontal ? myTable->setText( row, i+1, data[i] ) :
648 myTable->setText( i+1, row, data[i] );
656 void SALOMEGUI_TableWidget::getRowData( int row, QStringList& data )
659 if ( row >= 0 && row < getNumRows() ) {
660 if ( myOrientation == Horizontal ) {
661 for ( int i = 1; i < myTable->numCols(); i++ )
662 data.append( myTable->text( row, i ) );
665 for ( int i = 1; i < myTable->numRows(); i++ )
666 data.append( myTable->text( i, row ) );
671 Adjusts table cell to see contents, <Adjust Cells> button slot
673 void SALOMEGUI_TableWidget::adjustTable()
676 for ( i = 0; i < myTable->numRows(); i++ )
677 myTable->adjustRow( i );
678 for ( i = 0; i < myTable->numCols(); i++ )
679 myTable->adjustColumn( i );
682 Called when selection changed in table
684 void SALOMEGUI_TableWidget::updateButtonsState()
686 if ( myTable->isReadOnly() )
688 bool bDR = false; // <Delete Row(s)>
689 bool bDC = false; // <Delete Column(s)>
690 bool bSA = false; // <Select All>
691 bool bCT = false; // <Clear>
693 int c = myOrientation == Horizontal ? 0 : 1;
694 for ( i = c; i < myTable->numRows(); i++ ) {
695 if ( myTable->isRowSelected( i, true ) )
700 c = myOrientation == Horizontal ? 1 : 0;
701 for ( i = c; i < myTable->numCols(); i++ ) {
702 if ( myTable->isColumnSelected( i, true ) )
707 int nbSel = myTable->numSelections();
708 for ( i = 0; i < nbSel; i++ ) {
709 QTableSelection ts = myTable->selection( i );
710 for ( int j = ts.topRow(); j < ts.bottomRow()+1; j++) {
711 for ( int k = ts.leftCol(); k < ts.rightCol()+1; k++) {
712 if ( myTable->item( j, k ) )
717 if ( myTable->item( myTable->currentRow(), myTable->currentColumn() ) )
719 myDelRowBtn->setEnabled( bDR );
720 myDelColBtn->setEnabled( bDC );
721 mySelectAllBtn->setEnabled( bSA );
722 myClearBtn->setEnabled( bCT );
725 <Add row> button slot
727 void SALOMEGUI_TableWidget::addRow()
729 myTable->insertRows( myTable->numRows(), 1 );
730 updateButtonsState();
733 <Add column> button slot
735 void SALOMEGUI_TableWidget::addCol()
737 myTable->insertColumns( myTable->numCols(), 1 );
738 updateButtonsState();
741 <Delete row(s)> button slot
743 void SALOMEGUI_TableWidget::delRow()
745 int c = myOrientation == Horizontal ? 0 : 1;
748 for ( i = c; i < myTable->numRows(); i++ )
749 if ( myTable->isRowSelected( i, true ) )
751 if ( il.count() > 0 ) {
752 QMemArray<int> ildel( il.count() );
753 for ( i = 0; i < il.count(); i++ )
754 ildel[ i ] = il[ i ];
755 myTable->removeRows( ildel );
757 updateButtonsState();
760 <Delete column(s)> button slot
762 void SALOMEGUI_TableWidget::delCol()
764 int c = myOrientation == Horizontal ? 1 : 0;
767 for ( i = c; i < myTable->numCols(); i++ )
768 if ( myTable->isColumnSelected( i, true ) )
770 if ( il.count() > 0 ) {
771 QMemArray<int> ildel( il.count() );
772 for ( i = 0; i < il.count(); i++ )
773 ildel[ i ] = il[ i ];
774 myTable->removeColumns( ildel );
776 updateButtonsState();
779 <Select All> button slot
781 void SALOMEGUI_TableWidget::selectAll()
783 myTable->clearSelection();
785 ts.init( 0, 0 ); ts.expandTo( myTable->numRows()-1, myTable->numCols()-1 );
786 myTable->addSelection( ts );
787 updateButtonsState();
792 void SALOMEGUI_TableWidget::clearTable()
794 int nbSel = myTable->numSelections();
795 for ( int i = 0; i < nbSel; i++ ) {
796 QTableSelection ts = myTable->selection( i );
797 for ( int j = ts.topRow(); j < ts.bottomRow()+1; j++) {
798 if ( myOrientation == Vertical && j == 0 ) {
799 // continue; // UNITS
801 for ( int k = ts.leftCol(); k < ts.rightCol()+1; k++) {
802 if ( myOrientation == Horizontal && k == 0 ) {
803 // continue; // UNITS
805 myTable->clearCell( j, k );
810 myTable->clearCell( myTable->currentRow(), myTable->currentColumn() );
811 myTable->clearSelection();
812 updateButtonsState();
815 Event filter - handles titles editing
817 bool SALOMEGUI_TableWidget::eventFilter( QObject* o, QEvent* e )
819 if ( e->type() == QEvent::MouseButtonDblClick) {
820 QMouseEvent* me = ( QMouseEvent* )e;
821 if ( me->button() == LeftButton && !myTable->isReadOnly() ) {
822 if ( o == myTable->horizontalHeader() ) {
823 for ( int i = 0; i < myTable->horizontalHeader()->count(); i++ ) {
824 QRect rect = myTable->horizontalHeader()->sectionRect( i );
825 rect.addCoords( 1, 1, -1, -1 );
826 if ( rect.contains( myTable->horizontalHeader()->mapFromGlobal( me->globalPos() ) ) ) {
827 if ( myOrientation == Vertical || i != 0 ) {
829 QString tlt = QInputDialog::getText( tr( "SET_TITLE_TLT" ),
832 myTable->horizontalHeader()->label( i ),
835 if ( bOk && !tlt.isNull() )
836 myTable->horizontalHeader()->setLabel( i, tlt );
842 if ( o == myTable->verticalHeader() ) {
843 for ( int i = 0; i < myTable->verticalHeader()->count(); i++ ) {
844 QRect rect = myTable->verticalHeader()->sectionRect( i );
845 rect.addCoords( 1, 1, -1, -1 );
846 if ( rect.contains( myTable->verticalHeader()->mapFromGlobal( me->globalPos() ) ) ) {
847 if ( myOrientation == Horizontal || i != 0 ) {
849 QString tlt = QInputDialog::getText( tr( "SET_TITLE_TLT" ),
852 myTable->verticalHeader()->label( i ),
855 if ( bOk && !tlt.isNull() )
856 myTable->verticalHeader()->setLabel( i, tlt );
864 else if ( e->type() == QEvent::KeyRelease && o == myTable ) {
865 QKeyEvent* ke = (QKeyEvent*)e;
866 if ( ke->key() == Key_Delete && !myTable->isEditing() ) {
869 else if ( ke->key() == Key_Backspace && !myTable->isEditing() ) {
871 int i = myTable->currentRow();
872 int j = myTable->currentColumn() - 1;
873 if ( j < 0 ) { j = myTable->numCols()-1; i--; }
874 if ( i >= 0 && j >= 0 )
875 myTable->setCurrentCell( i, j );
878 return QWidget::eventFilter( o, e );