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);
316 SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
317 QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
318 arg(app->resourceMgr()->stringValue("ExternalBrowser", "application")).arg(aHelpFileName),
319 QObject::tr("BUT_OK"));
324 Populates table with data
326 void VisuGUI_TableDlg::initDlg()
330 _PTR(GenericAttribute) anAttr;
331 _PTR(AttributeTableOfInteger) tblIntAttr;
332 _PTR(AttributeTableOfReal) tblRealAttr;
333 if ( myObject->FindAttribute( anAttr, "AttributeTableOfInteger") ) {
336 if ( myObject->FindAttribute( anAttr, "AttributeTableOfReal") ) {
337 tblRealAttr = anAttr;
340 if ( tblIntAttr && myIntTable ) {
343 myIntTable->setTableTitle( tblIntAttr->GetTitle().c_str() );
345 int nbRows = tblIntAttr->GetNbRows() ;
346 int nbCols = tblIntAttr->GetNbColumns();
347 myIntTable->setNumRows( nbRows );
348 myIntTable->setNumCols( nbCols );
351 vector<string> rowTitles = tblIntAttr->GetRowTitles();
352 for ( i = 0; i < nbRows; i++ ) {
353 if ( rowTitles.size() > 0 )
354 strlist.append( rowTitles[i].c_str() );
356 strlist.append( "" );
358 myIntTable->setRowTitles( strlist );
361 vector<string> colTitles = tblIntAttr->GetColumnTitles();
362 for ( i = 0; i < nbCols; i++ ) {
363 if ( colTitles.size() > 0 )
364 strlist.append( colTitles[i].c_str() );
366 strlist.append( "" );
368 myIntTable->setColTitles( strlist );
371 vector<string> rowUnits = tblIntAttr->GetRowUnits();
372 if ( rowUnits.size() > 0 ) {
373 for ( i = 0; i < nbRows; i++ )
374 strlist.append( rowUnits[i].c_str() );
375 myIntTable->setUnits( strlist );
378 for ( i = 1; i <= nbRows; i++ ) {
380 for ( j = 1; j <= nbCols; j++ ) {
381 if ( tblIntAttr->HasValue( i, j ) )
382 strlist.append( QString::number( tblIntAttr->GetValue( i, j ) ) );
384 strlist.append( QString::null );
386 myIntTable->setRowData( i-1, strlist );
388 myIntTable->adjustTable();
391 MESSAGE("VisuGUI_TableDlg::initDlg : Exception has been caught !!!");
395 if ( tblRealAttr && myRealTable ) {
398 myRealTable->setTableTitle( tblRealAttr->GetTitle().c_str() );
400 int nbRows = tblRealAttr->GetNbRows() ;
401 int nbCols = tblRealAttr->GetNbColumns();
402 myRealTable->setNumRows( nbRows );
403 myRealTable->setNumCols( nbCols );
406 vector<string> rowTitles = tblRealAttr->GetRowTitles();
407 for ( i = 0; i < nbRows; i++ ) {
408 if ( rowTitles.size() > 0 )
409 strlist.append( rowTitles[i].c_str() );
411 strlist.append( "" );
413 myRealTable->setRowTitles( strlist );
416 vector<string> colTitles = tblRealAttr->GetColumnTitles();
417 for ( i = 0; i < nbCols; i++ ) {
418 if ( colTitles.size() > 0 )
419 strlist.append( colTitles[i].c_str() );
421 strlist.append( "" );
423 myRealTable->setColTitles( strlist );
426 vector<string> rowUnits = tblRealAttr->GetRowUnits();
427 if ( rowUnits.size() > 0 ) {
428 for ( i = 0; i < nbRows; i++ )
429 strlist.append( rowUnits[i].c_str() );
430 myRealTable->setUnits( strlist );
433 for ( i = 1; i <= nbRows; i++ ) {
435 for ( j = 1; j <= nbCols; j++ ) {
436 if ( tblRealAttr->HasValue( i, j ) )
437 strlist.append( QString::number( tblRealAttr->GetValue( i, j ) ) );
439 strlist.append( QString::null );
441 myRealTable->setRowData( i-1, strlist );
443 myRealTable->adjustTable();
446 MESSAGE("VisuGUI_TableDlg::initDlg : Exception has been caught !!!");
455 VisuGUI_TableWidget::VisuGUI_TableWidget( QWidget* parent,
459 bool showColumnTitles )
460 : QWidget( parent, name ), myOrientation( orient )
462 QGridLayout* mainLayout = new QGridLayout( this );
463 mainLayout->setMargin( 0 );
464 mainLayout->setSpacing( SPACING_SIZE );
466 myTitleEdit = new QLineEdit( this, "TitleEdit" );
467 myTitleEdit->setAlignment( AlignCenter );
468 myTitleEdit->setReadOnly( !edit );
469 QFont fnt = myTitleEdit->font();
471 myTitleEdit->setFont( fnt );
473 myTable = new VisuGUI_Table( orient, this, "Table" );
474 myTable->setNumRows( 5 );
475 myTable->setNumCols( 5 );
476 myTable->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
477 myTable->setMinimumSize( MIN_TABLE_WIDTH, MIN_TABLE_HEIGHT );
478 myTable->setSelectionMode( QTable::Single );
479 myTable->setShowGrid( true );
480 myTable->setColumnMovingEnabled( false );
481 myTable->setRowMovingEnabled( false );
482 myTable->setReadOnly( !edit );
483 myTable->setDragEnabled( false );
484 setUnitsTitle( tr( "UNITS_TLT" ) );
486 if ( !showColumnTitles ) {
487 if ( myOrientation == Horizontal ) {
488 myTable->horizontalHeader()->hide();
489 myTable->setTopMargin( 0 );
492 myTable->verticalHeader()->hide();
493 myTable->setLeftMargin( 0 );
497 mainLayout->addWidget( myTitleEdit, 0, 0 );
498 mainLayout->addWidget( myTable, 1, 0 );
501 myAddRowBtn = new QPushButton( tr( "ADD_ROW_BTN" ), this, "AddRowBtn" );
502 myDelRowBtn = new QPushButton( tr( "REMOVE_ROW_BTN" ), this, "DelRowBtn" );
503 myAddColBtn = new QPushButton( tr( "ADD_COLUMN_BTN" ), this, "AddColBtn" );
504 myDelColBtn = new QPushButton( tr( "REMOVE_COLUMN_BTN" ), this, "DelColBtn" );
505 myAdjustBtn = new QPushButton( tr( "ADJUST_CELLS_BTN" ), this, "AdjustBtn" );
506 mySelectAllBtn = new QPushButton( tr( "SELECT_ALL_BTN" ), this, "SelectAllBtn" );
507 myClearBtn = new QPushButton( tr( "CLEAR_BTN"), this, "ClearBtn" );
508 QVBoxLayout* btnLayout = new QVBoxLayout; btnLayout->setMargin( 0 ); btnLayout->setSpacing( SPACING_SIZE );
509 btnLayout->addWidget( myAddRowBtn );
510 btnLayout->addWidget( myDelRowBtn );
511 btnLayout->addWidget( myAddColBtn );
512 btnLayout->addWidget( myDelColBtn );
513 btnLayout->addStretch();
514 btnLayout->addWidget( myAdjustBtn );
515 btnLayout->addStretch();
516 btnLayout->addWidget( mySelectAllBtn );
517 btnLayout->addWidget( myClearBtn );
518 mainLayout->addLayout( btnLayout, 1, 1 );
519 connect( myTable, SIGNAL( selectionChanged() ), this, SLOT( updateButtonsState() ) );
520 connect( myTable, SIGNAL( currentChanged( int, int) ), this, SLOT( updateButtonsState() ) );
521 connect( myAddRowBtn, SIGNAL( clicked() ), this, SLOT( addRow() ) );
522 connect( myAddColBtn, SIGNAL( clicked() ), this, SLOT( addCol() ) );
523 connect( myDelRowBtn, SIGNAL( clicked() ), this, SLOT( delRow() ) );
524 connect( myDelColBtn, SIGNAL( clicked() ), this, SLOT( delCol() ) );
525 connect( myAdjustBtn, SIGNAL( clicked() ), this, SLOT( adjustTable() ) );
526 connect( mySelectAllBtn, SIGNAL( clicked() ), this, SLOT( selectAll() ) );
527 connect( myClearBtn, SIGNAL( clicked() ), this, SLOT( clearTable() ) );
528 myTable->horizontalHeader()->installEventFilter( this );
529 myTable->verticalHeader()->installEventFilter( this );
530 myTable->installEventFilter( this );
532 updateButtonsState();
537 VisuGUI_TableWidget::~VisuGUI_TableWidget()
543 void VisuGUI_TableWidget::setTableTitle( const QString& title )
545 myTitleEdit->setText( title );
550 QString VisuGUI_TableWidget::getTableTitle()
552 return myTitleEdit->text();
555 Sets total number of rows
557 void VisuGUI_TableWidget::setNumRows( const int num )
559 myOrientation == Horizontal ? myTable->setNumRows( num ) : myTable->setNumCols( num );
562 Gets total number of rows
564 int VisuGUI_TableWidget::getNumRows()
566 return myOrientation == Horizontal ? myTable->numRows() : myTable->numCols();
569 Sets total number of columns
571 void VisuGUI_TableWidget::setNumCols( const int num )
573 // !!! first column contains units !!!
574 myOrientation == Horizontal ? myTable->setNumCols( num+1 ) : myTable->setNumRows( num+1 );
575 // myOrientation == Horizontal ? myTable->setColumnReadOnly( 0, true ) : myTable->setRowReadOnly( 0, true );
578 Gets total number of columns
580 int VisuGUI_TableWidget::getNumCols()
582 // !!! first column contains units !!!
583 return myOrientation == Horizontal ? myTable->numCols()-1 : myTable->numRows()-1;
588 void VisuGUI_TableWidget::setRowTitles( QStringList& tlts )
590 for ( int i = 0; i < tlts.count(); i++ ) {
591 myOrientation == Horizontal ?
592 myTable->verticalHeader()->setLabel( i, tlts[i] ) :
593 myTable->horizontalHeader()->setLabel( i, tlts[i] );
599 void VisuGUI_TableWidget::getRowTitles( QStringList& tlts )
602 if ( myOrientation == Horizontal ) {
603 for ( int i = 0; i < myTable->numRows(); i++ ) {
604 tlts.append( myTable->verticalHeader()->label( i ) );
608 for ( int i = 0; i < myTable->numCols(); i++ ) {
609 tlts.append( myTable->horizontalHeader()->label( i ) );
616 void VisuGUI_TableWidget::setColTitles( QStringList& tlts )
618 // !!! first column contains units !!!
619 for ( int i = 0; i < tlts.count(); i++ ) {
620 myOrientation == Horizontal ?
621 myTable->horizontalHeader()->setLabel( i+1, tlts[i].isNull() ? "" : tlts[i] ) :
622 myTable->verticalHeader()->setLabel( i+1, tlts[i].isNull() ? "" : tlts[i] );
624 setUnitsTitle( tr( "UNITS_TLT" ) );
629 void VisuGUI_TableWidget::getColTitles( QStringList& tlts )
631 // !!! first column contains units !!!
633 if ( myOrientation == Horizontal ) {
634 for ( int i = 1; i < myTable->numCols(); i++ ) {
635 tlts.append( myTable->horizontalHeader()->label( i ) );
639 for ( int i = 1; i < myTable->numRows(); i++ ) {
640 tlts.append( myTable->verticalHeader()->label( i ) );
647 void VisuGUI_TableWidget::setUnitsTitle( const QString& tlt ) {
648 // !!! first column contains units !!!
649 myOrientation == Horizontal ? myTable->horizontalHeader()->setLabel( 0, tlt.isNull() ? "" : tlt ) : myTable->verticalHeader()->setLabel( 0, tlt.isNull() ? "" : tlt );
654 void VisuGUI_TableWidget::setUnits( QStringList& units )
656 for ( int i = 0; i < units.count(); i++ ) {
657 myOrientation == Horizontal ? myTable->setText( i, 0, units[i].isNull() ? "" : units[i] ) : myTable->setText( 0, i, units[i].isNull() ? "" : units[i] );
663 void VisuGUI_TableWidget::getUnits( QStringList& units )
666 if ( myOrientation == Horizontal ) {
667 for ( int i = 0; i < myTable->numRows(); i++ )
668 units.append( myTable->text( i, 0 ).isNull() ? QString("") : myTable->text( i, 0 ) );
671 for ( int i = 0; i < myTable->numCols(); i++ )
672 units.append( myTable->text( 0, i ).isNull() ? QString("") : myTable->text( 0, i ) );
678 void VisuGUI_TableWidget::setRowData( int row, QStringList& data )
680 if ( row >= 0 && row < getNumRows() ) {
681 for ( int i = 0; i < data.count(); i++ ) {
682 if ( data[i].isNull() ) {
683 myOrientation == Horizontal ? myTable->clearCell( row, i+1 ) :
684 myTable->clearCell( i+1, row );
687 myOrientation == Horizontal ? myTable->setText( row, i+1, data[i] ) :
688 myTable->setText( i+1, row, data[i] );
696 void VisuGUI_TableWidget::getRowData( int row, QStringList& data )
699 if ( row >= 0 && row < getNumRows() ) {
700 if ( myOrientation == Horizontal ) {
701 for ( int i = 1; i < myTable->numCols(); i++ )
702 data.append( myTable->text( row, i ) );
705 for ( int i = 1; i < myTable->numRows(); i++ )
706 data.append( myTable->text( i, row ) );
711 Adjusts table cell to see contents, <Adjust Cells> button slot
713 void VisuGUI_TableWidget::adjustTable()
716 for ( i = 0; i < myTable->numRows(); i++ )
717 myTable->adjustRow( i );
718 for ( i = 0; i < myTable->numCols(); i++ )
719 myTable->adjustColumn( i );
722 Called when selection changed in table
724 void VisuGUI_TableWidget::updateButtonsState()
726 if ( myTable->isReadOnly() )
728 bool bDR = false; // <Delete Row(s)>
729 bool bDC = false; // <Delete Column(s)>
730 bool bSA = false; // <Select All>
731 bool bCT = false; // <Clear>
733 int c = myOrientation == Horizontal ? 0 : 1;
734 for ( i = c; i < myTable->numRows(); i++ ) {
735 if ( myTable->isRowSelected( i, true ) )
740 c = myOrientation == Horizontal ? 1 : 0;
741 for ( i = c; i < myTable->numCols(); i++ ) {
742 if ( myTable->isColumnSelected( i, true ) )
747 int nbSel = myTable->numSelections();
748 for ( i = 0; i < nbSel; i++ ) {
749 QTableSelection ts = myTable->selection( i );
750 for ( int j = ts.topRow(); j < ts.bottomRow()+1; j++) {
751 for ( int k = ts.leftCol(); k < ts.rightCol()+1; k++) {
752 if ( myTable->item( j, k ) )
757 if ( myTable->item( myTable->currentRow(), myTable->currentColumn() ) )
759 myDelRowBtn->setEnabled( bDR );
760 myDelColBtn->setEnabled( bDC );
761 mySelectAllBtn->setEnabled( bSA );
762 myClearBtn->setEnabled( bCT );
765 <Add row> button slot
767 void VisuGUI_TableWidget::addRow()
769 myTable->insertRows( myTable->numRows(), 1 );
770 updateButtonsState();
773 <Add column> button slot
775 void VisuGUI_TableWidget::addCol()
777 myTable->insertColumns( myTable->numCols(), 1 );
778 updateButtonsState();
781 <Delete row(s)> button slot
783 void VisuGUI_TableWidget::delRow()
785 int c = myOrientation == Horizontal ? 0 : 1;
788 for ( i = c; i < myTable->numRows(); i++ )
789 if ( myTable->isRowSelected( i, true ) )
791 if ( il.count() > 0 ) {
792 QMemArray<int> ildel( il.count() );
793 for ( i = 0; i < il.count(); i++ )
794 ildel[ i ] = il[ i ];
795 myTable->removeRows( ildel );
797 updateButtonsState();
800 <Delete column(s)> button slot
802 void VisuGUI_TableWidget::delCol()
804 int c = myOrientation == Horizontal ? 1 : 0;
807 for ( i = c; i < myTable->numCols(); i++ )
808 if ( myTable->isColumnSelected( i, true ) )
810 if ( il.count() > 0 ) {
811 QMemArray<int> ildel( il.count() );
812 for ( i = 0; i < il.count(); i++ )
813 ildel[ i ] = il[ i ];
814 myTable->removeColumns( ildel );
816 updateButtonsState();
819 <Select All> button slot
821 void VisuGUI_TableWidget::selectAll()
823 myTable->clearSelection();
825 ts.init( 0, 0 ); ts.expandTo( myTable->numRows()-1, myTable->numCols()-1 );
826 myTable->addSelection( ts );
827 updateButtonsState();
832 void VisuGUI_TableWidget::clearTable()
834 int nbSel = myTable->numSelections();
835 for ( int i = 0; i < nbSel; i++ ) {
836 QTableSelection ts = myTable->selection( i );
837 for ( int j = ts.topRow(); j < ts.bottomRow()+1; j++) {
838 if ( myOrientation == Vertical && j == 0 ) {
839 // continue; // UNITS
841 for ( int k = ts.leftCol(); k < ts.rightCol()+1; k++) {
842 if ( myOrientation == Horizontal && k == 0 ) {
843 // continue; // UNITS
845 myTable->clearCell( j, k );
850 myTable->clearCell( myTable->currentRow(), myTable->currentColumn() );
851 myTable->clearSelection();
852 updateButtonsState();
855 Event filter - handles titles editing
857 bool VisuGUI_TableWidget::eventFilter( QObject* o, QEvent* e )
859 if ( e->type() == QEvent::MouseButtonDblClick) {
860 QMouseEvent* me = ( QMouseEvent* )e;
861 if ( me->button() == LeftButton && !myTable->isReadOnly() ) {
862 if ( o == myTable->horizontalHeader() ) {
863 for ( int i = 0; i < myTable->horizontalHeader()->count(); i++ ) {
864 QRect rect = myTable->horizontalHeader()->sectionRect( i );
865 rect.addCoords( 1, 1, -1, -1 );
866 if ( rect.contains( myTable->horizontalHeader()->mapFromGlobal( me->globalPos() ) ) ) {
867 if ( myOrientation == Vertical || i != 0 ) {
869 QString tlt = QInputDialog::getText( tr( "SET_TITLE_TLT" ),
872 myTable->horizontalHeader()->label( i ),
875 if ( bOk && !tlt.isNull() )
876 myTable->horizontalHeader()->setLabel( i, tlt );
882 if ( o == myTable->verticalHeader() ) {
883 for ( int i = 0; i < myTable->verticalHeader()->count(); i++ ) {
884 QRect rect = myTable->verticalHeader()->sectionRect( i );
885 rect.addCoords( 1, 1, -1, -1 );
886 if ( rect.contains( myTable->verticalHeader()->mapFromGlobal( me->globalPos() ) ) ) {
887 if ( myOrientation == Horizontal || i != 0 ) {
889 QString tlt = QInputDialog::getText( tr( "SET_TITLE_TLT" ),
892 myTable->verticalHeader()->label( i ),
895 if ( bOk && !tlt.isNull() )
896 myTable->verticalHeader()->setLabel( i, tlt );
904 else if ( e->type() == QEvent::KeyRelease && o == myTable ) {
905 QKeyEvent* ke = (QKeyEvent*)e;
906 if ( ke->key() == Key_Delete && !myTable->isEditing() ) {
909 else if ( ke->key() == Key_Backspace && !myTable->isEditing() ) {
911 int i = myTable->currentRow();
912 int j = myTable->currentColumn() - 1;
913 if ( j < 0 ) { j = myTable->numCols()-1; i--; }
914 if ( i >= 0 && j >= 0 )
915 myTable->setCurrentCell( i, j );
918 return QWidget::eventFilter( o, e );