1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : VISU_TableDlg.cxx
21 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
23 #include "VISU_TableDlg.h"
25 #include "VISU_Table_i.hh"
27 #include "SUIT_Tools.h"
28 #include "SUIT_MessageBox.h"
29 #include "SUIT_ResourceMgr.h"
30 #include "SUIT_Session.h"
32 #include "LightApp_Application.h"
34 #include "CAM_Module.h"
36 #include "SALOMEDSClient_Study.hxx"
37 #include "SALOMEDSClient_GenericAttribute.hxx"
38 #include "SALOMEDSClient_AttributeTableOfInteger.hxx"
39 #include "SALOMEDSClient_AttributeTableOfReal.hxx"
40 #include "SALOMEDSClient_StudyBuilder.hxx"
43 #include <QTableWidget>
46 #include <QInputDialog>
48 #include <QIntValidator>
49 #include <QDoubleValidator>
51 #include <QHeaderView>
52 #include <QPushButton>
57 #include "utilities.h"
59 const int MARGIN_SIZE = 11;
60 const int SPACING_SIZE = 6;
61 const int MIN_TABLE_WIDTH = 200;
62 const int MIN_TABLE_HEIGHT = 200;
64 NumDelegateItem::NumDelegateItem( QObject* parent, NumValidator mode )
65 : QItemDelegate( parent ),
70 NumDelegateItem::~NumDelegateItem()
74 QWidget* NumDelegateItem::createEditor( QWidget* parent,
75 const QStyleOptionViewItem& option,
76 const QModelIndex& index ) const
78 QLineEdit* editor = new QLineEdit( parent );
82 editor->setValidator( new QIntValidator( editor ) );
85 editor->setValidator( new QDoubleValidator( editor ) );
93 void NumDelegateItem::setEditorData( QWidget* editor,
94 const QModelIndex& index ) const
96 QLineEdit* aLE = qobject_cast<QLineEdit*>( editor );
104 int value = index.model()->data( index, Qt::DisplayRole ).toInt();
105 aLE->setText( QString("%1").arg( value ) );
110 double value = index.model()->data(index, Qt::DisplayRole).toDouble();
111 aLE->setText( QString("%1").arg( value ) );
115 aLE->setText( index.model()->data( index, Qt::DisplayRole ).toString() );
120 class VISU_TableDlg::WidgetCointainer : public QWidget
123 WidgetCointainer( QWidget* parent, Qt::Orientation o, int lm, int tm, int rm, int bm, int s )
126 QLayout* l = o == Qt::Horizontal ? (QLayout*)( new QHBoxLayout( this ) ) : ( QLayout* )( new QVBoxLayout( this ) );
127 l->setContentsMargins( lm, tm, rm, bm );
131 void addWidget( QWidget* w )
134 QHBoxLayout* hl = qobject_cast<QHBoxLayout*>( layout() );
135 QVBoxLayout* wl = qobject_cast<QVBoxLayout*>( layout() );
136 if ( hl ) hl->addWidget( w );
137 else if ( wl ) wl->addWidget( w );
140 void addSpacing( int s )
143 QHBoxLayout* hl = qobject_cast<QHBoxLayout*>( layout() );
144 QVBoxLayout* wl = qobject_cast<QVBoxLayout*>( layout() );
145 if ( hl ) hl->addSpacing( s );
146 else if ( wl ) wl->addSpacing( s );
149 void addStretch( int s )
152 QHBoxLayout* hl = qobject_cast<QHBoxLayout*>( layout() );
153 QVBoxLayout* wl = qobject_cast<QVBoxLayout*>( layout() );
154 if ( hl ) hl->addStretch( s );
155 else if ( wl ) wl->addStretch( s );
160 /*class VISU_Table : public QTableWidget {
162 VISU_Table( Orientation orient, QWidget* parent = 0 )
163 : QTableWidget( parent ), myValidator( 0 ), myOrientation( orient ) {}
164 VISU_Table( Orientation orient, int numRows, int numCols, QWidget* parent = 0 )
165 : QTableWidget( numRows, numCols, parent ), myValidator( 0 ), myOrientation( orient ) {}
167 void setValidator( QValidator* v = 0 ) { myValidator = v; }
168 bool isEditing() const { return QTable::isEditing(); }
171 QWidget* createEditor ( int row, int col, bool initFromCell ) const
173 bool testUnits = ( myOrientation == Qt::Horizontal && col == 0 ) || ( myOrientation == Qt::Vertical && row == 0 );
174 QWidget* wg = QTable::createEditor( row, col, initFromCell );
175 if ( wg && wg->inherits("QLineEdit") && myValidator && !testUnits )
176 (( QLineEdit*)wg)->setValidator( myValidator );
181 QValidator* myValidator;
182 Orientation myOrientation;
185 QString VISU_TableDlg::tableTitle( int type )
189 case ttInt: tlt = tr( "TABLE_OF_INTEGER_TLT" ); break;
190 case ttReal: tlt = tr( "TABLE_OF_REAL_TLT" ); break;
191 default: tlt = tr( "TABLE_UNKNOWN_TLT" ); break;
198 VISU_TableDlg::VISU_TableDlg( QWidget* parent,
200 VISU::Table_i* table,
203 Qt::Orientation orient,
204 bool showColumnTitles )
205 : QDialog( parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint ),
206 myStudy( study ), myTable( table ), myEditCheck( 0 ), myUpdateCheck( 0 )
208 allowEdition = true; //////////////////////////////////
209 setWindowTitle( tr( "VIEW_TABLE_TLT" ) );
210 setSizeGripEnabled( true );
212 bool bHasIntTable = false;
213 bool bHasRealTable = false;
214 if ( myStudy && myTable ) {
215 _PTR(SObject) aSObject = myStudy->FindObjectID( myTable->GetObjectEntry() );
217 _PTR(GenericAttribute) anAttr;
218 bHasIntTable = aSObject->FindAttribute( anAttr, "AttributeTableOfInteger" );
219 bHasRealTable = aSObject->FindAttribute( anAttr, "AttributeTableOfReal" );
223 if ( allowEdition ) {
224 myEditCheck = new QCheckBox( tr( "ALLOW_EDIT_CHECK" ), this );
225 myUpdateCheck = new QCheckBox( tr( "DO_UPDATE_CHECK" ), this );
226 connect( myEditCheck, SIGNAL( toggled( bool ) ), myUpdateCheck, SLOT( setEnabled( bool ) ) );
227 myUpdateCheck->setEnabled( false );
228 myUpdateCheck->hide(); // the feature has been temporarily disabled
233 if ( which == ttInt || which == ttAll || which == ttAuto && bHasIntTable ) {
234 myTableMap[ ttInt ] = new TableWidget( this, orient );
235 myTableMap[ ttInt ]->initialize( myStudy, myTable, ttInt );
236 myTableMap[ ttInt ]->setEditEnabled( false );
237 myTableMap[ ttInt ]->showColumnTitles( showColumnTitles );
239 connect( myEditCheck, SIGNAL( toggled( bool ) ), myTableMap[ ttInt ], SLOT( setEditEnabled( bool ) ) );
241 if ( which == ttReal || which == ttAll || which == ttAuto && bHasRealTable ) {
242 myTableMap[ ttReal ] = new TableWidget( this, orient );
243 myTableMap[ ttReal ]->initialize( myStudy, myTable, ttReal );
244 myTableMap[ ttReal ]->setEditEnabled( false );
245 myTableMap[ ttReal ]->showColumnTitles( showColumnTitles );
247 connect( myEditCheck, SIGNAL( toggled( bool ) ), myTableMap[ ttReal ], SLOT( setEditEnabled( bool ) ) );
250 if ( myTableMap.count() > 1 ) {
251 QTabWidget* tw = new QTabWidget( this );
252 for ( int i = ttInt; i < ttAll; i++ ) {
253 if ( myTableMap.contains( i ) ) {
254 tw->addTab( myTableMap[ i ], tableTitle( i ) );
255 myTableMap[ i ]->layout()->setMargin( MARGIN_SIZE );
260 else if ( myTableMap.count() == 1 ) {
261 top = myTableMap[myTableMap.keys().first()];
264 QLabel* dumbLabel = new QLabel( tr( "ERR_TABLE_NOT_AVAILABLE" ), this );
265 dumbLabel->setAlignment( Qt::AlignCenter );
269 myOKBtn = new QPushButton( tr( "BUT_OK" ), this );
270 myHelpBtn = new QPushButton( tr( "BUT_HELP" ), this );
272 QHBoxLayout* btnLayout = new QHBoxLayout;
273 btnLayout->setMargin( 0 );
274 btnLayout->setSpacing( SPACING_SIZE );
276 btnLayout->addWidget( myOKBtn );
277 btnLayout->addStretch( 20 );
278 btnLayout->addWidget( myHelpBtn );
279 connect( myOKBtn, SIGNAL( clicked() ), this, SLOT( close() ) );
280 connect( myHelpBtn, SIGNAL( clicked() ), this, SLOT( help() ) );
282 QVBoxLayout* mainLayout = new QVBoxLayout( this );
283 mainLayout->setMargin( MARGIN_SIZE );
284 mainLayout->setSpacing( SPACING_SIZE );
287 QHBoxLayout* checkLayout = new QHBoxLayout;
288 checkLayout->setMargin( 0 );
289 checkLayout->setSpacing( SPACING_SIZE );
290 checkLayout->addWidget( myEditCheck );
291 checkLayout->addWidget( myUpdateCheck );
292 mainLayout->addLayout( checkLayout );
294 if ( LightApp_Application* app = ( LightApp_Application* )SUIT_Session::session()->activeApplication() ) {
295 int anEnableEditing = app->resourceMgr()->booleanValue( "VISU", "tables_enable_editing", false );
296 myEditCheck->setChecked( anEnableEditing );
299 mainLayout->addWidget( top );
300 mainLayout->addLayout( btnLayout );
303 SUIT_Tools::centerWidget( this, parent );
309 VISU_TableDlg::~VISU_TableDlg()
314 <OK> button slot, saves table(s)
315 Called only in create/edit mode ( <edit> parameter for constructor is true )
318 void VISU_TableDlg::onOK()
320 myOKBtn->setFocus(); // accept possible changes
324 _PTR(Study) study = myObject->GetStudy();
325 _PTR(AttributeTableOfInteger) tblIntAttr;
326 _PTR(AttributeTableOfReal) tblRealAttr;
329 _PTR(StudyBuilder) builder = study->NewBuilder();
330 builder->NewCommand(); // start transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
332 if ( myTableMap.contains( ttInt ) ) {
333 builder->RemoveAttribute( myObject, "AttributeTableOfInteger" );
334 tblIntAttr = builder->FindOrCreateAttribute( myObject, "AttributeTableOfInteger" );
337 int nbRows = myTableMap[ ttInt ]->getNumRows();
338 int nbCols = myTableMap[ ttInt ]->getNumCols();
339 QString tlt = myTableMap[ ttInt ]->getTableTitle();
340 QStringList rowTitles, colTitles, units;
341 myTableMap[ ttInt ]->getRowTitles( rowTitles );
342 myTableMap[ ttInt ]->getColTitles( colTitles );
343 myTableMap[ ttInt ]->getUnits( units );
348 tblIntAttr->SetNbColumns( nbCols );
349 for ( i = 0; i < nbRows; i++ ) {
351 myTableMap[ ttInt ]->getRowData( i, data );
352 bool bEmptyRow = true;
353 for ( int j = 0; j < data.count(); j++ ) {
354 if ( !data[ j ].isNull() ) {
355 tblIntAttr->PutValue( data[ j ].toInt(), nRow+1, j+1 );
359 if ( !bEmptyRow ) { // Skip rows with no data !!!
361 tblIntAttr->SetRowTitle( nRow+1, rowTitles[ i ].isNull() ? "" : (const char*)rowTitles[ i ].toLatin1() );
363 tblIntAttr->SetRowUnit( nRow+1, units[ i ].isNull() ? "" : (const char*)units[ i ].toLatin1() );
367 if ( nRow > 0 ) { // Set columns only if table is not empty, otherwise exception is raised !!!
369 for ( i = 0; i < colTitles.count(); i++ )
370 tblIntAttr->SetColumnTitle( i+1, colTitles[ i ].isNull() ? "" : (const char*)colTitles[ i ].toLatin1() );
374 tblIntAttr->SetTitle( (const char*)myTableMap[ ttInt ]->getTableTitle().toLatin1() );
376 if ( myTableMap.contains( ttReal ) ) {
377 builder->RemoveAttribute( myObject, "AttributeTableOfReal" );
378 tblRealAttr = builder->FindOrCreateAttribute( myObject, "AttributeTableOfReal" );
381 int nbRows = myTableMap[ ttReal ]->getNumRows();
382 int nbCols = myTableMap[ ttReal ]->getNumCols();
383 QString tlt = myTableMap[ ttReal ]->getTableTitle();
384 QStringList rowTitles, colTitles, units;
385 myTableMap[ ttReal ]->getRowTitles( rowTitles );
386 myTableMap[ ttReal ]->getColTitles( colTitles );
387 myTableMap[ ttReal ]->getUnits( units );
392 tblRealAttr->SetNbColumns( nbCols );
393 for ( i = 0; i < nbRows; i++ ) {
395 myTableMap[ ttReal ]->getRowData( i, data );
396 bool bEmptyRow = true;
397 for ( int j = 0; j < data.count(); j++ ) {
398 if ( !data[ j ].isNull() ) {
399 tblRealAttr->PutValue( data[ j ].toDouble(), nRow+1, j+1 );
403 if ( !bEmptyRow ) { // Skip rows with no data !!!
405 tblRealAttr->SetRowTitle( nRow+1, rowTitles[ i ].isNull() ? "" : (const char*)rowTitles[ i ].toLatin1() );
407 tblRealAttr->SetRowUnit( nRow+1, units[ i ].isNull() ? "" : (const char*)units[ i ].toLatin1() );
411 if ( nRow > 0 ) { // Set columns only if table is not empty, otherwise exception is raised !!!
413 for ( i = 0; i < colTitles.count(); i++ )
414 tblRealAttr->SetColumnTitle( i+1, colTitles[ i ].isNull() ? "" : (const char*)colTitles[ i ].toLatin1() );
418 tblRealAttr->SetTitle( (const char*)myTableMap[ ttReal ]->getTableTitle().toLatin1() );
420 if ( myTableMap.contains( ttInt ) || myTableMap.contains( ttReal ) )
421 builder->CommitCommand(); // commit transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!
423 builder->AbortCommand(); // abort transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!
426 MESSAGE("VISU_TableDlg::onOK : Exception has been caught !!!");
427 builder->AbortCommand(); // abort transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!
429 SUIT_MessageBox::critical ( this, tr("ERR_ERROR"), tr("ERR_APP_EXCEPTION") );
439 <Help> button slot, shows corresponding help page
441 void VISU_TableDlg::help()
443 QString aHelpFileName = "table_presentations_page.html";
444 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
446 app->onHelpContextModule(app->activeModule() ? app->moduleName(app->activeModule()->moduleName()) : QString(""), aHelpFileName);
450 platform = "winapplication";
452 platform = "application";
454 SUIT_MessageBox::warning( this,
455 QObject::tr("WRN_WARNING"),
456 QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
457 arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(aHelpFileName) );
462 Provides help on F1 button click
464 void VISU_TableDlg::keyPressEvent( QKeyEvent* e )
466 QDialog::keyPressEvent( e );
467 if ( e->isAccepted() )
470 if ( e->key() == Qt::Key_F1 ) {
479 VISU_TableDlg::TableWidget::TableWidget( QWidget* parent,
480 Qt::Orientation orientation )
481 : QWidget( parent ), myOrientation( orientation )
483 myTitleEdit = new QLineEdit( this );
485 myTable = new QTableWidget( 5, 5, this );
486 myTable->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
487 myTable->setMinimumSize( MIN_TABLE_WIDTH, MIN_TABLE_HEIGHT );
488 myTable->setSelectionMode( QAbstractItemView::SingleSelection );
489 myTable->setShowGrid( true );
490 myTable->horizontalHeader()->setMovable( false );
491 myTable->verticalHeader()->setMovable( false );
492 myTable->setDragEnabled( false );
494 myAdjustBtn = new QPushButton( VISU_TableDlg::tr( "ADJUST_CELLS_BTN" ), this );
495 myAddRowBtn = new QPushButton( VISU_TableDlg::tr( "ADD_ROW_BTN" ), this );
496 myDelRowBtn = new QPushButton( VISU_TableDlg::tr( "REMOVE_ROW_BTN" ), this );
497 myAddColBtn = new QPushButton( VISU_TableDlg::tr( "ADD_COLUMN_BTN" ), this );
498 myDelColBtn = new QPushButton( VISU_TableDlg::tr( "REMOVE_COLUMN_BTN" ), this );
499 mySelectAllBtn = new QPushButton( VISU_TableDlg::tr( "SELECT_ALL_BTN" ), this );
500 myClearBtn = new QPushButton( VISU_TableDlg::tr( "CLEAR_BTN" ), this );
502 mySortPolicyLabel = new QLabel( VISU_TableDlg::tr( "VISU_TABLES_SORT_POLICY" ), this );
503 mySortPolicyCombo = new QComboBox( this );
504 mySortPolicyCombo->insertItems( 0, QStringList() <<
505 VISU_TableDlg::tr( "VISU_TABLES_EMPTY_LOWEST" ) <<
506 VISU_TableDlg::tr( "VISU_TABLES_EMPTY_HIGHEST" ) <<
507 VISU_TableDlg::tr( "VISU_TABLES_EMPTY_FIRST" ) <<
508 VISU_TableDlg::tr( "VISU_TABLES_EMPTY_LAST" ) <<
509 VISU_TableDlg::tr( "VISU_TABLES_EMPTY_IGNORE" ) );
511 if ( LightApp_Application* app = ( LightApp_Application* )SUIT_Session::session()->activeApplication() ) {
512 int aSortPolicy = app->resourceMgr()->integerValue( "VISU", "tables_sort_policy", 3 );
513 mySortPolicyCombo->setCurrentIndex( aSortPolicy );
516 // the features has been temporarily disabled
521 mySelectAllBtn->hide();
524 QVBoxLayout* btnLayout = new QVBoxLayout;
525 btnLayout->setMargin( 0 );
526 btnLayout->setSpacing( SPACING_SIZE );
527 btnLayout->addWidget( myAdjustBtn );
528 btnLayout->addStretch( 20 );
529 btnLayout->addWidget( myAddRowBtn );
530 btnLayout->addWidget( myDelRowBtn );
531 btnLayout->addWidget( myAddColBtn );
532 btnLayout->addWidget( myDelColBtn );
533 btnLayout->addStretch( 20 );
534 btnLayout->addWidget( mySelectAllBtn );
535 btnLayout->addWidget( myClearBtn );
537 QHBoxLayout* sortLayout = new QHBoxLayout;
538 sortLayout->setMargin( 0 );
539 sortLayout->setSpacing( SPACING_SIZE );
540 sortLayout->addWidget( mySortPolicyLabel );
541 sortLayout->addWidget( mySortPolicyCombo );
542 sortLayout->addStretch( 1 );
544 QGridLayout* mainLayout = new QGridLayout( this );
545 mainLayout->setMargin( 0 );
546 mainLayout->setSpacing( SPACING_SIZE );
547 mainLayout->addWidget( myTitleEdit, 0, 0, 1, 2 );
548 mainLayout->addWidget( myTable, 1, 0 );
549 mainLayout->addLayout( btnLayout, 1, 1 );
550 mainLayout->addLayout( sortLayout, 2, 0, 1, 2 );
552 connect( myTable, SIGNAL( itemSelectionChanged() ),
553 this, SLOT( updateButtonsState() ) );
554 connect( myTable, SIGNAL( currentItemChanged( QTableWidgetItem*, QTableWidgetItem* ) ),
555 this, SLOT( updateButtonsState() ) );
556 connect( myAddRowBtn, SIGNAL( clicked() ), this, SLOT( addRow() ) );
557 connect( myAddColBtn, SIGNAL( clicked() ), this, SLOT( addCol() ) );
558 connect( myDelRowBtn, SIGNAL( clicked() ), this, SLOT( delRow() ) );
559 connect( myDelColBtn, SIGNAL( clicked() ), this, SLOT( delCol() ) );
560 connect( myAdjustBtn, SIGNAL( clicked() ), this, SLOT( adjustTable() ) );
561 connect( mySelectAllBtn, SIGNAL( clicked() ), this, SLOT( selectAll() ) );
562 connect( myClearBtn, SIGNAL( clicked() ), this, SLOT( clearTable() ) );
563 connect( myTable->horizontalHeader(), SIGNAL( sectionClicked( int ) ),
564 this, SLOT( columnClicked( int ) ) );
565 connect( myTable->verticalHeader(), SIGNAL( sectionClicked( int ) ),
566 this, SLOT( rowClicked( int ) ) );
567 myTable->horizontalHeader()->installEventFilter( this );
568 myTable->verticalHeader()->installEventFilter( this );
569 myTable->installEventFilter( this );
571 updateButtonsState();
577 VISU_TableDlg::TableWidget::~TableWidget()
584 void VISU_TableDlg::TableWidget::initialize( _PTR(Study) study, VISU::Table_i* table, int type )
590 setUnitsTitle( VISU_TableDlg::tr( "UNITS_TLT" ) );
591 setEditEnabled( false );
592 showColumnTitles( false );
594 updateTableFromServant( true );
595 updateButtonsState();
598 void VISU_TableDlg::TableWidget::updateTableFromServant( bool adjust )
600 _PTR(SObject) aSObject = myStudy->FindObjectID( myTableObj->GetObjectEntry() );
605 myTable->setItemDelegate( new NumDelegateItem( this, NumDelegateItem::NV_Int ) );
606 if ( aSObject->FindAttribute( myAttr, "AttributeTableOfInteger" ) ) {
607 _PTR(AttributeTableOfInteger) intAttr = myAttr;
610 setTableTitle( intAttr->GetTitle().c_str() );
612 int nbRows = intAttr->GetNbRows() ;
613 int nbCols = intAttr->GetNbColumns();
614 setNumRows( nbRows );
615 setNumCols( nbCols );
618 std::vector<std::string> titles = intAttr->GetRowTitles();
619 for ( i = 0; i < nbRows; i++ ) {
620 if ( titles.size() > 0 )
621 strlist.append( titles[i].c_str() );
623 strlist.append( "" );
625 setRowTitles( strlist );
628 titles = intAttr->GetColumnTitles();
629 for ( i = 0; i < nbCols; i++ ) {
630 if ( titles.size() > 0 )
631 strlist.append( titles[i].c_str() );
633 strlist.append( "" );
635 setColTitles( strlist );
638 titles = intAttr->GetRowUnits();
639 if ( titles.size() > 0 ) {
640 for ( i = 0; i < nbRows; i++ )
641 strlist.append( titles[i].c_str() );
645 for ( i = 1; i <= nbRows; i++ ) {
647 for ( j = 1; j <= nbCols; j++ ) {
648 if ( intAttr->HasValue( i, j ) )
649 strlist.append( QString::number( intAttr->GetValue( i, j ) ) );
651 strlist.append( QString::null );
653 setRowData( i-1, strlist );
655 if ( adjust ) adjustTable();
658 MESSAGE("VISU_TableDlg::TableWidget::initialize : Exception has been caught !!!");
663 myTable->setItemDelegate( new NumDelegateItem( this, NumDelegateItem::NV_Real ) );
664 if ( aSObject->FindAttribute( myAttr, "AttributeTableOfReal" ) ) {
665 _PTR(AttributeTableOfReal) realAttr = myAttr;
668 setTableTitle( realAttr->GetTitle().c_str() );
670 int nbRows = realAttr->GetNbRows() ;
671 int nbCols = realAttr->GetNbColumns();
672 setNumRows( nbRows );
673 setNumCols( nbCols );
676 std::vector<std::string> titles = realAttr->GetRowTitles();
677 for ( i = 0; i < nbRows; i++ ) {
678 if ( titles.size() > 0 )
679 strlist.append( titles[i].c_str() );
681 strlist.append( "" );
683 setRowTitles( strlist );
686 titles = realAttr->GetColumnTitles();
687 for ( i = 0; i < nbCols; i++ ) {
688 if ( titles.size() > 0 )
689 strlist.append( titles[i].c_str() );
691 strlist.append( "" );
693 setColTitles( strlist );
696 titles = realAttr->GetRowUnits();
697 if ( titles.size() > 0 ) {
698 for ( i = 0; i < nbRows; i++ )
699 strlist.append( titles[i].c_str() );
703 for ( i = 1; i <= nbRows; i++ ) {
705 for ( j = 1; j <= nbCols; j++ ) {
706 if ( realAttr->HasValue( i, j ) )
707 strlist.append( QString::number( realAttr->GetValue( i, j ) ) );
709 strlist.append( QString::null );
711 setRowData( i-1, strlist );
713 if ( adjust ) adjustTable();
716 MESSAGE("VISU_TableDlg::TableWidget::initialize : Exception has been caught !!!");
727 Enable / disable editing
729 void VISU_TableDlg::TableWidget::setEditEnabled( bool enable )
732 myTable->horizontalHeader()->setSortIndicatorShown( false );
733 myTable->verticalHeader()->setSortIndicatorShown( false );
737 mySortPolicyLabel->setEnabled( enable );
738 mySortPolicyCombo->setEnabled( enable );
740 // the rest features have been temporarily disabled
743 myTitleEdit->setReadOnly( !enable );
744 myTable->setEditTriggers( enable ?
745 QAbstractItemView::DoubleClicked |
746 QAbstractItemView::SelectedClicked |
747 QAbstractItemView::EditKeyPressed :
748 QAbstractItemView::NoEditTriggers );
749 myAddRowBtn->setVisible( enable );
750 myDelRowBtn->setVisible( enable );
751 myAddColBtn->setVisible( enable );
752 myDelColBtn->setVisible( enable );
753 mySelectAllBtn->setVisible( enable );
754 myClearBtn->setVisible( enable );
758 Show / hide column titles
760 void VISU_TableDlg::TableWidget::showColumnTitles( bool showTitles )
762 if ( myOrientation == Qt::Horizontal )
763 myTable->horizontalHeader()->setVisible( showTitles );
765 myTable->verticalHeader()->setVisible( showTitles );
771 void VISU_TableDlg::TableWidget::setTableTitle( const QString& title )
773 myTitleEdit->setText( title );
779 QString VISU_TableDlg::TableWidget::getTableTitle()
781 return myTitleEdit->text();
785 Sets total number of rows
787 void VISU_TableDlg::TableWidget::setNumRows( const int num )
789 myOrientation == Qt::Horizontal ? myTable->setRowCount( num ) : myTable->setColumnCount( num );
793 Gets total number of rows
795 int VISU_TableDlg::TableWidget::getNumRows()
797 return myOrientation == Qt::Horizontal ? myTable->rowCount() : myTable->columnCount();
801 Sets total number of columns
803 void VISU_TableDlg::TableWidget::setNumCols( const int num )
805 // !!! first column contains units !!!
806 myOrientation == Qt::Horizontal ? myTable->setColumnCount( num+1 ) : myTable->setRowCount( num+1 );
807 // myOrientation == Qt::Horizontal ? myTable->setColumnReadOnly( 0, true ) : myTable->setRowReadOnly( 0, true );
811 Gets total number of columns
813 int VISU_TableDlg::TableWidget::getNumCols()
815 // !!! first column contains units !!!
816 return myOrientation == Qt::Horizontal ? myTable->columnCount()-1 : myTable->rowCount()-1;
821 void VISU_TableDlg::TableWidget::setRowTitles( QStringList& tlts )
824 for ( int i = 0; i < tlts.count(); i++ )
825 tlts[i].isNull() ? aLabels.append("") : aLabels.append( tlts[i] );
827 myOrientation == Qt::Horizontal ?
828 myTable->setVerticalHeaderLabels( aLabels ) :
829 myTable->setHorizontalHeaderLabels( aLabels );
831 // myTable->verticalHeader()->setLabel( i, tlts[i] ) :
832 //myTable->horizontalHeader()->setLabel( i, tlts[i] );
838 void VISU_TableDlg::TableWidget::getRowTitles( QStringList& tlts )
841 if ( myOrientation == Qt::Horizontal ) {
842 for ( int i = 0; i < myTable->rowCount(); i++ ) {
843 tlts.append( myTable->verticalHeaderItem(i) ? myTable->verticalHeaderItem(i)->text() : "" );
847 for ( int i = 0; i < myTable->columnCount(); i++ ) {
848 tlts.append( myTable->horizontalHeaderItem(i) ? myTable->horizontalHeaderItem(i)->text() : "" );
855 void VISU_TableDlg::TableWidget::setColTitles( QStringList& tlts )
859 // !!! first column contains units !!!
860 aLabels.append(""); // it'll be initialized below - in setUnitsTitle() method
862 for ( int i = 0; i < tlts.count(); i++ )
863 tlts[i].isNull() ? aLabels.append("") : aLabels.append( tlts[i] );
865 myOrientation == Qt::Horizontal ?
866 myTable->setHorizontalHeaderLabels( aLabels ) :
867 myTable->setVerticalHeaderLabels( aLabels );
869 setUnitsTitle( VISU_TableDlg::tr( "UNITS_TLT" ) );
874 void VISU_TableDlg::TableWidget::getColTitles( QStringList& tlts )
876 // !!! first column contains units !!!
878 if ( myOrientation == Qt::Horizontal ) {
879 for ( int i = 1; i < myTable->columnCount(); i++ ) {
880 tlts.append( myTable->horizontalHeaderItem(i) ? myTable->horizontalHeaderItem(i)->text() : "" );
884 for ( int i = 1; i < myTable->rowCount(); i++ ) {
885 tlts.append( myTable->verticalHeaderItem(i) ? myTable->verticalHeaderItem(i)->text() : "" );
892 void VISU_TableDlg::TableWidget::setUnitsTitle( const QString& tlt ) {
893 // !!! first column contains units !!!
894 myTable->model()->setHeaderData( 0, myOrientation, QVariant(tlt.isNull() ? "" : tlt), Qt::DisplayRole );
899 void VISU_TableDlg::TableWidget::setUnits( QStringList& units )
901 QAbstractTableModel* aModel = qobject_cast<QAbstractTableModel*>( myTable->model() );
905 for ( int i = 0; i < units.count(); i++ )
907 myOrientation == Qt::Horizontal ?
908 anIndex = aModel->index( i, 0 ) :
909 anIndex = aModel->index( 0, i );
911 aModel->setData( anIndex, QVariant( units[i].isNull() ? "" : units[i] ) );
918 void VISU_TableDlg::TableWidget::getUnits( QStringList& units )
921 QAbstractTableModel* aModel = qobject_cast<QAbstractTableModel*>( myTable->model() );
924 if ( myOrientation == Qt::Horizontal )
926 for ( int i = 0; i < myTable->rowCount(); i++ )
927 units.append( aModel->index( i, 0 ).data().toString() );
930 for ( int i = 0; i < myTable->columnCount(); i++ )
931 units.append( aModel->index( 0, i ).data().toString() );
938 void VISU_TableDlg::TableWidget::setRowData( int row, QStringList& data )
940 QAbstractTableModel* aModel = qobject_cast<QAbstractTableModel*>( myTable->model() );
944 if ( row >= 0 && row < getNumRows() ) {
945 for ( int i = 0; i < data.count(); i++ )
947 myOrientation == Qt::Horizontal ? anIndex = aModel->index( row, i+1 ) :
948 anIndex = aModel->index( i+1, row );
949 aModel->setData( anIndex, QVariant( data[i] ) );
958 void VISU_TableDlg::TableWidget::getRowData( int row, QStringList& data )
961 QAbstractTableModel* aModel = qobject_cast<QAbstractTableModel*>( myTable->model() );
964 if ( row >= 0 && row < getNumRows() )
966 if ( myOrientation == Qt::Horizontal )
968 for ( int i = 1; i < myTable->columnCount(); i++ )
969 data.append( aModel->index( row, i ).data().toString() );
972 for ( int i = 1; i < myTable->rowCount(); i++ )
973 data.append( aModel->index( i, row ).data().toString() );
979 Adjusts table cell to see contents, <Adjust Cells> button slot
981 void VISU_TableDlg::TableWidget::adjustTable()
983 myTable->resizeRowsToContents();
984 myTable->resizeColumnsToContents();
987 Called when selection changed in table
989 void VISU_TableDlg::TableWidget::updateButtonsState()
991 if ( myTable->editTriggers() == QAbstractItemView::NoEditTriggers )
993 bool bDR = false; // <Delete Row(s)>
994 bool bDC = false; // <Delete Column(s)>
995 bool bSA = false; // <Select All>
996 bool bCT = false; // <Clear>
998 //TO DO column/row selection check
999 /*int c = myOrientation == Qt::Horizontal ? 0 : 1;
1000 for ( i = c; i < myTable->rowCount(); i++ ) {
1002 if ( myTable->isRowSelected( i, true ) )
1007 c = myOrientation == Qt::Horizontal ? 1 : 0;
1008 for ( i = c; i < myTable->columnCount(); i++ ) {
1009 if ( myTable->isColumnSelected( i, true ) )
1014 /*int nbSel = myTable->numSelections();
1015 for ( i = 0; i < nbSel; i++ ) {
1016 QTableSelection ts = myTable->selection( i );
1017 for ( int j = ts.topRow(); j < ts.bottomRow()+1; j++) {
1018 for ( int k = ts.leftCol(); k < ts.rightCol()+1; k++) {
1019 if ( myTable->item( j, k ) )
1024 QList<QTableWidgetItem*> aSelection = myTable->selectedItems();
1025 QList<QTableWidgetItem*>::ConstIterator anIt = aSelection.constBegin(),
1026 anEndIt = aSelection.constEnd();
1027 for ( ; anIt != anEndIt; anIt++ )
1036 if ( myTable->item( myTable->currentRow(), myTable->currentColumn() ) )
1038 myDelRowBtn->setEnabled( bDR );
1039 myDelColBtn->setEnabled( bDC );
1040 mySelectAllBtn->setEnabled( bSA );
1041 myClearBtn->setEnabled( bCT );
1044 <Add row> button slot
1046 void VISU_TableDlg::TableWidget::addRow()
1048 myTable->insertRow( myTable->rowCount() );
1049 updateButtonsState();
1052 <Add column> button slot
1054 void VISU_TableDlg::TableWidget::addCol()
1056 myTable->insertColumn( myTable->columnCount() );
1057 updateButtonsState();
1060 <Delete row(s)> button slot
1062 void VISU_TableDlg::TableWidget::delRow()
1065 /*int c = myOrientation == Qt::Horizontal ? 0 : 1;
1068 for ( i = c; i < myTable->rowCount(); i++ )
1069 if ( myTable->isRowSelected( i, true ) )
1071 if ( il.count() > 0 ) {
1072 QMemArray<int> ildel( il.count() );
1073 for ( i = 0; i < il.count(); i++ )
1074 ildel[ i ] = il[ i ];
1075 myTable->removeRows( ildel );
1078 updateButtonsState();
1081 <Delete column(s)> button slot
1083 void VISU_TableDlg::TableWidget::delCol()
1086 /*int c = myOrientation == Qt::Horizontal ? 1 : 0;
1089 for ( i = c; i < myTable->numCols(); i++ )
1090 if ( myTable->isColumnSelected( i, true ) )
1092 if ( il.count() > 0 ) {
1093 QMemArray<int> ildel( il.count() );
1094 for ( i = 0; i < il.count(); i++ )
1095 ildel[ i ] = il[ i ];
1096 myTable->removeColumns( ildel );
1098 updateButtonsState();
1101 <Select All> button slot
1103 void VISU_TableDlg::TableWidget::selectAll()
1105 /*myTable->clearSelection();
1107 ts.init( 0, 0 ); ts.expandTo( myTable->numRows()-1, myTable->numCols()-1 );
1108 myTable->addSelection( ts );*/
1109 myTable->selectAll();
1110 updateButtonsState();
1115 void VISU_TableDlg::TableWidget::clearTable()
1117 /*int nbSel = myTable->numSelections();
1118 for ( int i = 0; i < nbSel; i++ ) {
1119 QTableSelection ts = myTable->selection( i );
1120 for ( int j = ts.topRow(); j < ts.bottomRow()+1; j++) {
1121 if ( myOrientation == Qt::Vertical && j == 0 ) {
1122 // continue; // UNITS
1124 for ( int k = ts.leftCol(); k < ts.rightCol()+1; k++) {
1125 if ( myOrientation == Qt::Horizontal && k == 0 ) {
1126 // continue; // UNITS
1128 myTable->clearCell( j, k );
1133 myTable->clearCell( myTable->currentRow(), myTable->currentColumn() );
1134 myTable->clearSelection();*/
1135 myTable->clearContents();
1136 updateButtonsState();
1141 void VISU_TableDlg::TableWidget::columnClicked( int column )
1143 if ( myTableObj && mySortPolicyCombo->isEnabled() ) {
1144 myTableObj->SortByRow( column + 1,
1145 ( VISU::SortOrder )myTable->horizontalHeader()->sortIndicatorOrder(),
1146 ( VISU::SortPolicy )mySortPolicyCombo->currentIndex() );
1147 myTable->horizontalHeader()->setSortIndicatorShown( true );
1148 myTable->verticalHeader()->setSortIndicatorShown( false );
1149 updateTableFromServant( false );
1155 void VISU_TableDlg::TableWidget::rowClicked( int row )
1157 /* the feature has been temporarily disabled
1158 if ( myTableObj && mySortPolicyCombo->isEnabled() && row > 0 ) { // first row contains units
1159 myTableObj->SortByColumn( row,
1160 ( VISU::SortOrder )myTable->verticalHeader()->sortIndicatorOrder(),
1161 ( VISU::SortPolicy )mySortPolicyCombo->currentIndex() );
1162 myTable->horizontalHeader()->setSortIndicatorShown( false );
1163 myTable->verticalHeader()->setSortIndicatorShown( true );
1164 updateTableFromServant( false );
1169 Event filter - handles titles editing
1171 bool VISU_TableDlg::TableWidget::eventFilter( QObject* o, QEvent* e )
1173 if ( e->type() == QEvent::MouseButtonDblClick) {
1175 /*QMouseEvent* me = ( QMouseEvent* )e;
1176 if ( me->button() == Qt::LeftButton && (myTable->editTriggers() != QAbstractItemView::NoEditTriggers ) ) {
1177 if ( o == myTable->horizontalHeader() ) {
1178 for ( int i = 0; i < myTable->horizontalHeader()->count(); i++ ) {
1179 QRect rect = myTable->horizontalHeader()->sectionRect( i );
1180 rect.addCoords( 1, 1, -1, -1 );
1181 if ( rect.contains( myTable->horizontalHeader()->mapFromGlobal( me->globalPos() ) ) ) {
1182 if ( myOrientation == Qt::Vertical || i != 0 ) {
1184 QString tlt = QInputDialog::getText( tr( "SET_TITLE_TLT" ),
1187 myTable->horizontalHeader()->label( i ),
1190 if ( bOk && !tlt.isNull() )
1191 myTable->horizontalHeader()->setLabel( i, tlt );
1197 if ( o == myTable->verticalHeader() ) {
1198 for ( int i = 0; i < myTable->verticalHeader()->count(); i++ ) {
1199 QRect rect = myTable->verticalHeader()->sectionRect( i );
1200 rect.addCoords( 1, 1, -1, -1 );
1201 if ( rect.contains( myTable->verticalHeader()->mapFromGlobal( me->globalPos() ) ) ) {
1202 if ( myOrientation == Qt::Horizontal || i != 0 ) {
1204 QString tlt = QInputDialog::getText( tr( "SET_TITLE_TLT" ),
1207 myTable->verticalHeader()->label( i ),
1210 if ( bOk && !tlt.isNull() )
1211 myTable->verticalHeader()->setLabel( i, tlt );
1219 else if ( e->type() == QEvent::KeyRelease && o == myTable ) {
1220 QKeyEvent* ke = (QKeyEvent*)e;
1221 if ( ke->key() == Qt::Key_Delete && (myTable->editTriggers() != QAbstractItemView::NoEditTriggers) ) {
1224 else if ( ke->key() == Qt::Key_Backspace && (myTable->editTriggers() != QAbstractItemView::NoEditTriggers) ) {
1226 int i = myTable->currentRow();
1227 int j = myTable->currentColumn() - 1;
1228 if ( j < 0 ) { j = myTable->columnCount()-1; i--; }
1229 if ( i >= 0 && j >= 0 )
1230 myTable->setCurrentCell( i, j );
1233 return QWidget::eventFilter( o, e );