1 // VISU VISUGUI : GUI of VISU component
3 // Copyright (C) 2003 CEA/DEN, EDF R&D
7 // File : VisuGUI_TableDlg.cxx
8 // Author : Vadim SANDLER
11 #include "VisuGUI_TableDlg.h"
13 #include "SUIT_Tools.h"
14 #include "SUIT_MessageBox.h"
16 #include "SALOMEDSClient_Study.hxx"
17 #include "SALOMEDSClient_GenericAttribute.hxx"
18 #include "SALOMEDSClient_AttributeTableOfInteger.hxx"
19 #include "SALOMEDSClient_AttributeTableOfReal.hxx"
20 #include "SALOMEDSClient_StudyBuilder.hxx"
23 #include <qvalidator.h>
25 #include <qtabwidget.h>
26 #include <qvaluelist.h>
27 #include <qmemarray.h>
28 #include <qinputdialog.h>
30 #include "utilities.h"
33 #define MARGIN_SIZE 11
34 #define SPACING_SIZE 6
36 #define MIN_TABLE_WIDTH 200
37 #define MIN_TABLE_HEIGHT 200
40 class VisuGUI_Table : public QTable {
42 VisuGUI_Table( Orientation orient, QWidget* parent = 0, const char* name = 0 )
43 : QTable( parent, name ), myValidator( 0 ), myOrientation( orient ) {}
44 VisuGUI_Table( Orientation orient, int numRows, int numCols, QWidget* parent = 0, const char* name = 0 )
45 : QTable( numRows, numCols, parent, name ), myValidator( 0 ), myOrientation( orient ) {}
47 void setValidator( QValidator* v = 0 ) { myValidator = v; }
48 bool isEditing() const { return QTable::isEditing(); }
51 QWidget* createEditor ( int row, int col, bool initFromCell ) const
53 bool testUnits = ( myOrientation == Horizontal && col == 0 ) || ( myOrientation == Vertical && row == 0 );
54 QWidget* wg = QTable::createEditor( row, col, initFromCell );
55 if ( wg && wg->inherits("QLineEdit") && myValidator && !testUnits )
56 (( QLineEdit*)wg)->setValidator( myValidator );
61 QValidator* myValidator;
62 Orientation myOrientation;
68 VisuGUI_TableDlg::VisuGUI_TableDlg( QWidget* parent,
73 bool showColumnTitles )
74 : QDialog( parent, "", false, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose),
75 myIntTable( 0 ), myRealTable( 0 )
77 setCaption( edit ? tr( "EDIT_TABLE_TLT" ) : tr( "VIEW_TABLE_TLT" ) );
78 setSizeGripEnabled( true );
81 bool bHasIntTable = false;
82 bool bHasRealTable = false;
84 _PTR(GenericAttribute) anAttr;
85 bHasIntTable = myObject->FindAttribute( anAttr, "AttributeTableOfInteger");
86 bHasRealTable = myObject->FindAttribute( anAttr, "AttributeTableOfReal");
89 QVBoxLayout* mainLayout = new QVBoxLayout( this );
90 mainLayout->setMargin( MARGIN_SIZE );
91 mainLayout->setSpacing( SPACING_SIZE );
93 bool bDoInt = which == ttInt || which == ttBoth || which == ttAuto && bHasIntTable;
94 bool bDoReal = which == ttReal || which == ttBoth || which == ttAuto && bHasRealTable;
98 if ( bDoInt && bDoReal ) {
99 top = new QTabWidget( this, "TabWidget" );
100 ( ( QTabWidget* ) top) ->setMargin( MARGIN_SIZE );
103 top = new QWidget( this, "DumbWidget" );
104 tl = new QVBoxLayout( top ); tl->setMargin( 0 ); tl->setSpacing( SPACING_SIZE );
108 myIntTable = new VisuGUI_TableWidget( top, "myIntTable", edit, orient, showColumnTitles );
109 myIntTable->getTable()->setValidator( new QIntValidator( this ) );
110 if ( bDoInt && bDoReal )
111 ( ( QTabWidget* )top )->addTab( myIntTable, tr( "TABLE_OF_INTEGER_TLT" ) );
113 tl->addWidget( myIntTable );
116 myRealTable = new VisuGUI_TableWidget( top, "myRealTable", edit, orient, showColumnTitles );
117 myRealTable->getTable()->setValidator( new QDoubleValidator( this ) );
118 if ( bDoInt && bDoReal )
119 ( ( QTabWidget* )top )->addTab( myRealTable, tr( "TABLE_OF_REAL_TLT" ) );
121 tl->addWidget( myRealTable );
123 if ( !bDoInt && !bDoReal ) {
124 QLabel *dumbLabel = new QLabel( tr( "ERR_TABLE_NOT_AVAILABLE" ), top, "DumbLabel" );
125 dumbLabel->setAlignment( AlignCenter );
126 tl->addWidget( dumbLabel );
129 QHBoxLayout* btnLayout = new QHBoxLayout;
130 btnLayout->setMargin( 0 ); btnLayout->setSpacing( SPACING_SIZE );
132 myOKBtn = new QPushButton( tr( "BUT_OK" ), this );
134 myCancelBtn = new QPushButton( tr( "BUT_CANCEL" ), this );
135 btnLayout->addWidget( myOKBtn );
136 btnLayout->addItem( new QSpacerItem( SPACER_SIZE, SPACER_SIZE, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
137 btnLayout->addWidget( myCancelBtn );
138 connect( myOKBtn, SIGNAL( clicked() ), this, SLOT( onOK() ) );
139 connect( myCancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
142 btnLayout->addItem( new QSpacerItem( SPACER_SIZE, SPACER_SIZE, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
143 btnLayout->addWidget( myOKBtn );
144 btnLayout->addItem( new QSpacerItem( SPACER_SIZE, SPACER_SIZE, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
145 connect( myOKBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
148 mainLayout->addWidget( top );
149 mainLayout->addLayout( btnLayout );
153 SUIT_Tools::centerWidget( this, parent );
159 VisuGUI_TableDlg::~VisuGUI_TableDlg()
164 <OK> button slot, saves table(s)
165 Called only in create/edit mode ( <edit> parameter for constructor is true )
167 void VisuGUI_TableDlg::onOK()
169 myOKBtn->setFocus(); // accept possible changes
173 _PTR(Study) study = myObject->GetStudy();
174 _PTR(AttributeTableOfInteger) tblIntAttr;
175 _PTR(AttributeTableOfReal) tblRealAttr;
178 _PTR(StudyBuilder) builder = study->NewBuilder();
179 builder->NewCommand(); // start transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
182 builder->RemoveAttribute( myObject, "AttributeTableOfInteger" );
183 tblIntAttr = builder->FindOrCreateAttribute( myObject, "AttributeTableOfInteger" );
186 int nbRows = myIntTable->getNumRows();
187 int nbCols = myIntTable->getNumCols();
188 QString tlt = myIntTable->getTableTitle();
189 QStringList rowTitles, colTitles, units;
190 myIntTable->getRowTitles( rowTitles );
191 myIntTable->getColTitles( colTitles );
192 myIntTable->getUnits( units );
197 tblIntAttr->SetNbColumns( nbCols );
198 for ( i = 0; i < nbRows; i++ ) {
200 myIntTable->getRowData( i, data );
201 bool bEmptyRow = true;
202 for ( int j = 0; j < data.count(); j++ ) {
203 if ( !data[ j ].isNull() ) {
204 tblIntAttr->PutValue( data[ j ].toInt(), nRow+1, j+1 );
208 if ( !bEmptyRow ) { // Skip rows with no data !!!
210 tblIntAttr->SetRowTitle( nRow+1, rowTitles[ i ].isNull() ? QString( "" ) : QString( rowTitles[ i ] ) );
212 tblIntAttr->SetRowUnit( nRow+1, units[ i ].isNull() ? QString( "" ) : QString( units[ i ] ) );
216 if ( nRow > 0 ) { // Set columns only if table is not empty, otherwise exception is raised !!!
218 for ( i = 0; i < colTitles.count(); i++ )
219 tblIntAttr->SetColumnTitle( i+1, colTitles[ i ].isNull() ? QString( "" ) : QString( colTitles[ i ] ) );
223 tblIntAttr->SetTitle( myIntTable->getTableTitle().latin1() );
226 builder->RemoveAttribute( myObject, "AttributeTableOfReal" );
227 tblRealAttr = builder->FindOrCreateAttribute( myObject, "AttributeTableOfReal" );
230 int nbRows = myRealTable->getNumRows();
231 int nbCols = myRealTable->getNumCols();
232 QString tlt = myRealTable->getTableTitle();
233 QStringList rowTitles, colTitles, units;
234 myRealTable->getRowTitles( rowTitles );
235 myRealTable->getColTitles( colTitles );
236 myRealTable->getUnits( units );
241 tblRealAttr->SetNbColumns( nbCols );
242 for ( i = 0; i < nbRows; i++ ) {
244 myRealTable->getRowData( i, data );
245 bool bEmptyRow = true;
246 for ( int j = 0; j < data.count(); j++ ) {
247 if ( !data[ j ].isNull() ) {
248 tblRealAttr->PutValue( data[ j ].toDouble(), nRow+1, j+1 );
252 if ( !bEmptyRow ) { // Skip rows with no data !!!
254 tblRealAttr->SetRowTitle( nRow+1, rowTitles[ i ].isNull() ? QString( "" ) : QString( rowTitles[ i ] ) );
256 tblRealAttr->SetRowUnit( nRow+1, units[ i ].isNull() ? QString( "" ) : QString( units[ i ] ) );
260 if ( nRow > 0 ) { // Set columns only if table is not empty, otherwise exception is raised !!!
262 for ( i = 0; i < colTitles.count(); i++ )
263 tblRealAttr->SetColumnTitle( i+1, colTitles[ i ].isNull() ? QString( "" ) : QString( colTitles[ i ] ) );
267 tblRealAttr->SetTitle( myRealTable->getTableTitle().latin1() );
269 if ( myIntTable || myRealTable)
270 builder->CommitCommand(); // commit transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!
272 builder->AbortCommand(); // abort transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!
275 MESSAGE("VisuGUI_TableDlg::onOK : Exception has been caught !!!");
276 builder->AbortCommand(); // abort transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!
278 SUIT_MessageBox::error1 ( this, tr("ERR_ERROR"), tr("ERR_APP_EXCEPTION"), tr ("BUT_OK") );
287 Populates table with data
289 void VisuGUI_TableDlg::initDlg()
293 _PTR(GenericAttribute) anAttr;
294 _PTR(AttributeTableOfInteger) tblIntAttr;
295 _PTR(AttributeTableOfReal) tblRealAttr;
296 if ( myObject->FindAttribute( anAttr, "AttributeTableOfInteger") ) {
299 if ( myObject->FindAttribute( anAttr, "AttributeTableOfReal") ) {
300 tblRealAttr = anAttr;
303 if ( tblIntAttr && myIntTable ) {
305 VisuGUI_Table* tbl = myIntTable->getTable();
307 myIntTable->setTableTitle( tblIntAttr->GetTitle().c_str() );
309 int nbRows = tblIntAttr->GetNbRows() ;
310 int nbCols = tblIntAttr->GetNbColumns();
311 myIntTable->setNumRows( nbRows );
312 myIntTable->setNumCols( nbCols );
315 vector<string> rowTitles = tblIntAttr->GetRowTitles();
316 for ( i = 0; i < nbRows; i++ ) {
317 if ( rowTitles.size() > 0 )
318 strlist.append( rowTitles[i].c_str() );
320 strlist.append( "" );
322 myIntTable->setRowTitles( strlist );
325 vector<string> colTitles = tblIntAttr->GetColumnTitles();
326 for ( i = 0; i < nbCols; i++ ) {
327 if ( colTitles.size() > 0 )
328 strlist.append( colTitles[i].c_str() );
330 strlist.append( "" );
332 myIntTable->setColTitles( strlist );
335 vector<string> rowUnits = tblIntAttr->GetRowUnits();
336 if ( rowUnits.size() > 0 ) {
337 for ( i = 0; i < nbRows; i++ )
338 strlist.append( rowUnits[i] );
339 myIntTable->setUnits( strlist );
342 for ( i = 1; i <= nbRows; i++ ) {
344 for ( j = 1; j <= nbCols; j++ ) {
345 if ( tblIntAttr->HasValue( i, j ) )
346 strlist.append( QString::number( tblIntAttr->GetValue( i, j ) ) );
348 strlist.append( QString::null );
350 myIntTable->setRowData( i-1, strlist );
352 myIntTable->adjustTable();
355 MESSAGE("VisuGUI_TableDlg::initDlg : Exception has been caught !!!");
359 if ( tblRealAttr && myRealTable ) {
361 VisuGUI_Table* tbl = myRealTable->getTable();
363 myRealTable->setTableTitle( tblRealAttr->GetTitle().c_str() );
365 int nbRows = tblRealAttr->GetNbRows() ;
366 int nbCols = tblRealAttr->GetNbColumns();
367 myRealTable->setNumRows( nbRows );
368 myRealTable->setNumCols( nbCols );
371 vector<string> rowTitles = tblRealAttr->GetRowTitles();
372 for ( i = 0; i < nbRows; i++ ) {
373 if ( rowTitles.size() > 0 )
374 strlist.append( rowTitles[i].c_str() );
376 strlist.append( "" );
378 myRealTable->setRowTitles( strlist );
381 vector<string> colTitles = tblRealAttr->GetColumnTitles();
382 for ( i = 0; i < nbCols; i++ ) {
383 if ( colTitles.size() > 0 )
384 strlist.append( colTitles[i].c_str() );
386 strlist.append( "" );
388 myRealTable->setColTitles( strlist );
391 vector<string> rowUnits = tblRealAttr->GetRowUnits();
392 if ( rowUnits.size() > 0 ) {
393 for ( i = 0; i < nbRows; i++ )
394 strlist.append( rowUnits[i].c_str() );
395 myRealTable->setUnits( strlist );
398 for ( i = 1; i <= nbRows; i++ ) {
400 for ( j = 1; j <= nbCols; j++ ) {
401 if ( tblRealAttr->HasValue( i, j ) )
402 strlist.append( QString::number( tblRealAttr->GetValue( i, j ) ) );
404 strlist.append( QString::null );
406 myRealTable->setRowData( i-1, strlist );
408 myRealTable->adjustTable();
411 MESSAGE("VisuGUI_TableDlg::initDlg : Exception has been caught !!!");
420 VisuGUI_TableWidget::VisuGUI_TableWidget( QWidget* parent,
424 bool showColumnTitles )
425 : QWidget( parent, name ), myOrientation( orient )
427 QGridLayout* mainLayout = new QGridLayout( this );
428 mainLayout->setMargin( 0 );
429 mainLayout->setSpacing( SPACING_SIZE );
431 myTitleEdit = new QLineEdit( this, "TitleEdit" );
432 myTitleEdit->setAlignment( AlignCenter );
433 myTitleEdit->setReadOnly( !edit );
434 QFont fnt = myTitleEdit->font();
436 myTitleEdit->setFont( fnt );
438 myTable = new VisuGUI_Table( orient, this, "Table" );
439 myTable->setNumRows( 5 );
440 myTable->setNumCols( 5 );
441 myTable->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
442 myTable->setMinimumSize( MIN_TABLE_WIDTH, MIN_TABLE_HEIGHT );
443 myTable->setSelectionMode( QTable::Single );
444 myTable->setShowGrid( true );
445 myTable->setColumnMovingEnabled( false );
446 myTable->setRowMovingEnabled( false );
447 myTable->setReadOnly( !edit );
448 myTable->setDragEnabled( false );
449 setUnitsTitle( tr( "UNITS_TLT" ) );
451 if ( !showColumnTitles ) {
452 if ( myOrientation == Horizontal ) {
453 myTable->horizontalHeader()->hide();
454 myTable->setTopMargin( 0 );
457 myTable->verticalHeader()->hide();
458 myTable->setLeftMargin( 0 );
462 mainLayout->addWidget( myTitleEdit, 0, 0 );
463 mainLayout->addWidget( myTable, 1, 0 );
466 myAddRowBtn = new QPushButton( tr( "ADD_ROW_BTN" ), this, "AddRowBtn" );
467 myDelRowBtn = new QPushButton( tr( "REMOVE_ROW_BTN" ), this, "DelRowBtn" );
468 myAddColBtn = new QPushButton( tr( "ADD_COLUMN_BTN" ), this, "AddColBtn" );
469 myDelColBtn = new QPushButton( tr( "REMOVE_COLUMN_BTN" ), this, "DelColBtn" );
470 myAdjustBtn = new QPushButton( tr( "ADJUST_CELLS_BTN" ), this, "AdjustBtn" );
471 mySelectAllBtn = new QPushButton( tr( "SELECT_ALL_BTN" ), this, "SelectAllBtn" );
472 myClearBtn = new QPushButton( tr( "CLEAR_BTN"), this, "ClearBtn" );
473 QVBoxLayout* btnLayout = new QVBoxLayout; btnLayout->setMargin( 0 ); btnLayout->setSpacing( SPACING_SIZE );
474 btnLayout->addWidget( myAddRowBtn );
475 btnLayout->addWidget( myDelRowBtn );
476 btnLayout->addWidget( myAddColBtn );
477 btnLayout->addWidget( myDelColBtn );
478 btnLayout->addStretch();
479 btnLayout->addWidget( myAdjustBtn );
480 btnLayout->addStretch();
481 btnLayout->addWidget( mySelectAllBtn );
482 btnLayout->addWidget( myClearBtn );
483 mainLayout->addLayout( btnLayout, 1, 1 );
484 connect( myTable, SIGNAL( selectionChanged() ), this, SLOT( updateButtonsState() ) );
485 connect( myTable, SIGNAL( currentChanged( int, int) ), this, SLOT( updateButtonsState() ) );
486 connect( myAddRowBtn, SIGNAL( clicked() ), this, SLOT( addRow() ) );
487 connect( myAddColBtn, SIGNAL( clicked() ), this, SLOT( addCol() ) );
488 connect( myDelRowBtn, SIGNAL( clicked() ), this, SLOT( delRow() ) );
489 connect( myDelColBtn, SIGNAL( clicked() ), this, SLOT( delCol() ) );
490 connect( myAdjustBtn, SIGNAL( clicked() ), this, SLOT( adjustTable() ) );
491 connect( mySelectAllBtn, SIGNAL( clicked() ), this, SLOT( selectAll() ) );
492 connect( myClearBtn, SIGNAL( clicked() ), this, SLOT( clearTable() ) );
493 myTable->horizontalHeader()->installEventFilter( this );
494 myTable->verticalHeader()->installEventFilter( this );
495 myTable->installEventFilter( this );
497 updateButtonsState();
502 VisuGUI_TableWidget::~VisuGUI_TableWidget()
508 void VisuGUI_TableWidget::setTableTitle( const QString& title )
510 myTitleEdit->setText( title );
515 QString VisuGUI_TableWidget::getTableTitle()
517 return myTitleEdit->text();
520 Sets total number of rows
522 void VisuGUI_TableWidget::setNumRows( const int num )
524 myOrientation == Horizontal ? myTable->setNumRows( num ) : myTable->setNumCols( num );
527 Gets total number of rows
529 int VisuGUI_TableWidget::getNumRows()
531 return myOrientation == Horizontal ? myTable->numRows() : myTable->numCols();
534 Sets total number of columns
536 void VisuGUI_TableWidget::setNumCols( const int num )
538 // !!! first column contains units !!!
539 myOrientation == Horizontal ? myTable->setNumCols( num+1 ) : myTable->setNumRows( num+1 );
540 // myOrientation == Horizontal ? myTable->setColumnReadOnly( 0, true ) : myTable->setRowReadOnly( 0, true );
543 Gets total number of columns
545 int VisuGUI_TableWidget::getNumCols()
547 // !!! first column contains units !!!
548 return myOrientation == Horizontal ? myTable->numCols()-1 : myTable->numRows()-1;
553 void VisuGUI_TableWidget::setRowTitles( QStringList& tlts )
555 for ( int i = 0; i < tlts.count(); i++ ) {
556 myOrientation == Horizontal ?
557 myTable->verticalHeader()->setLabel( i, tlts[i] ) :
558 myTable->horizontalHeader()->setLabel( i, tlts[i] );
564 void VisuGUI_TableWidget::getRowTitles( QStringList& tlts )
567 if ( myOrientation == Horizontal ) {
568 for ( int i = 0; i < myTable->numRows(); i++ ) {
569 tlts.append( myTable->verticalHeader()->label( i ) );
573 for ( int i = 0; i < myTable->numCols(); i++ ) {
574 tlts.append( myTable->horizontalHeader()->label( i ) );
581 void VisuGUI_TableWidget::setColTitles( QStringList& tlts )
583 // !!! first column contains units !!!
584 for ( int i = 0; i < tlts.count(); i++ ) {
585 myOrientation == Horizontal ?
586 myTable->horizontalHeader()->setLabel( i+1, tlts[i].isNull() ? "" : tlts[i] ) :
587 myTable->verticalHeader()->setLabel( i+1, tlts[i].isNull() ? "" : tlts[i] );
589 setUnitsTitle( tr( "UNITS_TLT" ) );
594 void VisuGUI_TableWidget::getColTitles( QStringList& tlts )
596 // !!! first column contains units !!!
598 if ( myOrientation == Horizontal ) {
599 for ( int i = 1; i < myTable->numCols(); i++ ) {
600 tlts.append( myTable->horizontalHeader()->label( i ) );
604 for ( int i = 1; i < myTable->numRows(); i++ ) {
605 tlts.append( myTable->verticalHeader()->label( i ) );
612 void VisuGUI_TableWidget::setUnitsTitle( const QString& tlt ) {
613 // !!! first column contains units !!!
614 myOrientation == Horizontal ? myTable->horizontalHeader()->setLabel( 0, tlt.isNull() ? "" : tlt ) : myTable->verticalHeader()->setLabel( 0, tlt.isNull() ? "" : tlt );
619 void VisuGUI_TableWidget::setUnits( QStringList& units )
621 for ( int i = 0; i < units.count(); i++ ) {
622 myOrientation == Horizontal ? myTable->setText( i, 0, units[i].isNull() ? "" : units[i] ) : myTable->setText( 0, i, units[i].isNull() ? "" : units[i] );
628 void VisuGUI_TableWidget::getUnits( QStringList& units )
631 if ( myOrientation == Horizontal ) {
632 for ( int i = 0; i < myTable->numRows(); i++ )
633 units.append( myTable->text( i, 0 ).isNull() ? QString("") : myTable->text( i, 0 ) );
636 for ( int i = 0; i < myTable->numCols(); i++ )
637 units.append( myTable->text( 0, i ).isNull() ? QString("") : myTable->text( 0, i ) );
643 void VisuGUI_TableWidget::setRowData( int row, QStringList& data )
645 if ( row >= 0 && row < getNumRows() ) {
646 for ( int i = 0; i < data.count(); i++ ) {
647 if ( data[i].isNull() ) {
648 myOrientation == Horizontal ? myTable->clearCell( row, i+1 ) :
649 myTable->clearCell( i+1, row );
652 myOrientation == Horizontal ? myTable->setText( row, i+1, data[i] ) :
653 myTable->setText( i+1, row, data[i] );
661 void VisuGUI_TableWidget::getRowData( int row, QStringList& data )
664 if ( row >= 0 && row < getNumRows() ) {
665 if ( myOrientation == Horizontal ) {
666 for ( int i = 1; i < myTable->numCols(); i++ )
667 data.append( myTable->text( row, i ) );
670 for ( int i = 1; i < myTable->numRows(); i++ )
671 data.append( myTable->text( i, row ) );
676 Adjusts table cell to see contents, <Adjust Cells> button slot
678 void VisuGUI_TableWidget::adjustTable()
681 for ( i = 0; i < myTable->numRows(); i++ )
682 myTable->adjustRow( i );
683 for ( i = 0; i < myTable->numCols(); i++ )
684 myTable->adjustColumn( i );
687 Called when selection changed in table
689 void VisuGUI_TableWidget::updateButtonsState()
691 if ( myTable->isReadOnly() )
693 bool bDR = false; // <Delete Row(s)>
694 bool bDC = false; // <Delete Column(s)>
695 bool bSA = false; // <Select All>
696 bool bCT = false; // <Clear>
698 int c = myOrientation == Horizontal ? 0 : 1;
699 for ( i = c; i < myTable->numRows(); i++ ) {
700 if ( myTable->isRowSelected( i, true ) )
705 c = myOrientation == Horizontal ? 1 : 0;
706 for ( i = c; i < myTable->numCols(); i++ ) {
707 if ( myTable->isColumnSelected( i, true ) )
712 int nbSel = myTable->numSelections();
713 for ( i = 0; i < nbSel; i++ ) {
714 QTableSelection ts = myTable->selection( i );
715 for ( int j = ts.topRow(); j < ts.bottomRow()+1; j++) {
716 for ( int k = ts.leftCol(); k < ts.rightCol()+1; k++) {
717 if ( myTable->item( j, k ) )
722 if ( myTable->item( myTable->currentRow(), myTable->currentColumn() ) )
724 myDelRowBtn->setEnabled( bDR );
725 myDelColBtn->setEnabled( bDC );
726 mySelectAllBtn->setEnabled( bSA );
727 myClearBtn->setEnabled( bCT );
730 <Add row> button slot
732 void VisuGUI_TableWidget::addRow()
734 myTable->insertRows( myTable->numRows(), 1 );
735 updateButtonsState();
738 <Add column> button slot
740 void VisuGUI_TableWidget::addCol()
742 myTable->insertColumns( myTable->numCols(), 1 );
743 updateButtonsState();
746 <Delete row(s)> button slot
748 void VisuGUI_TableWidget::delRow()
750 int c = myOrientation == Horizontal ? 0 : 1;
753 for ( i = c; i < myTable->numRows(); i++ )
754 if ( myTable->isRowSelected( i, true ) )
756 if ( il.count() > 0 ) {
757 QMemArray<int> ildel( il.count() );
758 for ( i = 0; i < il.count(); i++ )
759 ildel[ i ] = il[ i ];
760 myTable->removeRows( ildel );
762 updateButtonsState();
765 <Delete column(s)> button slot
767 void VisuGUI_TableWidget::delCol()
769 int c = myOrientation == Horizontal ? 1 : 0;
772 for ( i = c; i < myTable->numCols(); i++ )
773 if ( myTable->isColumnSelected( i, true ) )
775 if ( il.count() > 0 ) {
776 QMemArray<int> ildel( il.count() );
777 for ( i = 0; i < il.count(); i++ )
778 ildel[ i ] = il[ i ];
779 myTable->removeColumns( ildel );
781 updateButtonsState();
784 <Select All> button slot
786 void VisuGUI_TableWidget::selectAll()
788 myTable->clearSelection();
790 ts.init( 0, 0 ); ts.expandTo( myTable->numRows()-1, myTable->numCols()-1 );
791 myTable->addSelection( ts );
792 updateButtonsState();
797 void VisuGUI_TableWidget::clearTable()
799 int nbSel = myTable->numSelections();
800 for ( int i = 0; i < nbSel; i++ ) {
801 QTableSelection ts = myTable->selection( i );
802 for ( int j = ts.topRow(); j < ts.bottomRow()+1; j++) {
803 if ( myOrientation == Vertical && j == 0 ) {
804 // continue; // UNITS
806 for ( int k = ts.leftCol(); k < ts.rightCol()+1; k++) {
807 if ( myOrientation == Horizontal && k == 0 ) {
808 // continue; // UNITS
810 myTable->clearCell( j, k );
815 myTable->clearCell( myTable->currentRow(), myTable->currentColumn() );
816 myTable->clearSelection();
817 updateButtonsState();
820 Event filter - handles titles editing
822 bool VisuGUI_TableWidget::eventFilter( QObject* o, QEvent* e )
824 if ( e->type() == QEvent::MouseButtonDblClick) {
825 QMouseEvent* me = ( QMouseEvent* )e;
826 if ( me->button() == LeftButton && !myTable->isReadOnly() ) {
827 if ( o == myTable->horizontalHeader() ) {
828 for ( int i = 0; i < myTable->horizontalHeader()->count(); i++ ) {
829 QRect rect = myTable->horizontalHeader()->sectionRect( i );
830 rect.addCoords( 1, 1, -1, -1 );
831 if ( rect.contains( myTable->horizontalHeader()->mapFromGlobal( me->globalPos() ) ) ) {
832 if ( myOrientation == Vertical || i != 0 ) {
834 QString tlt = QInputDialog::getText( tr( "SET_TITLE_TLT" ),
837 myTable->horizontalHeader()->label( i ),
840 if ( bOk && !tlt.isNull() )
841 myTable->horizontalHeader()->setLabel( i, tlt );
847 if ( o == myTable->verticalHeader() ) {
848 for ( int i = 0; i < myTable->verticalHeader()->count(); i++ ) {
849 QRect rect = myTable->verticalHeader()->sectionRect( i );
850 rect.addCoords( 1, 1, -1, -1 );
851 if ( rect.contains( myTable->verticalHeader()->mapFromGlobal( me->globalPos() ) ) ) {
852 if ( myOrientation == Horizontal || i != 0 ) {
854 QString tlt = QInputDialog::getText( tr( "SET_TITLE_TLT" ),
857 myTable->verticalHeader()->label( i ),
860 if ( bOk && !tlt.isNull() )
861 myTable->verticalHeader()->setLabel( i, tlt );
869 else if ( e->type() == QEvent::KeyRelease && o == myTable ) {
870 QKeyEvent* ke = (QKeyEvent*)e;
871 if ( ke->key() == Key_Delete && !myTable->isEditing() ) {
874 else if ( ke->key() == Key_Backspace && !myTable->isEditing() ) {
876 int i = myTable->currentRow();
877 int j = myTable->currentColumn() - 1;
878 if ( j < 0 ) { j = myTable->numCols()-1; i--; }
879 if ( i >= 0 && j >= 0 )
880 myTable->setCurrentCell( i, j );
883 return QWidget::eventFilter( o, e );