1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : VisuGUI_TableDlg.cxx
23 // Author : Vadim SANDLER
26 #include "VisuGUI_TableDlg.h"
28 #include "SUIT_Tools.h"
29 #include "SUIT_MessageBox.h"
30 #include "SUIT_ResourceMgr.h"
31 #include "SUIT_Session.h"
33 #include "LightApp_Application.h"
35 #include "CAM_Module.h"
37 #include "SALOMEDSClient_Study.hxx"
38 #include "SALOMEDSClient_GenericAttribute.hxx"
39 #include "SALOMEDSClient_AttributeTableOfInteger.hxx"
40 #include "SALOMEDSClient_AttributeTableOfReal.hxx"
41 #include "SALOMEDSClient_StudyBuilder.hxx"
45 #include <QTableWidget>
49 #include <QInputDialog>
51 #include <QIntValidator>
52 #include <QDoubleValidator>
54 #include <QHeaderView>
56 #include "utilities.h"
60 #define MARGIN_SIZE 11
61 #define SPACING_SIZE 6
63 #define MIN_TABLE_WIDTH 200
64 #define MIN_TABLE_HEIGHT 200
66 NumDelegateItem::NumDelegateItem( QObject* parent, NumValidator mode )
67 : QItemDelegate( parent ),
72 NumDelegateItem::~NumDelegateItem()
76 QWidget * NumDelegateItem::createEditor( QWidget * parent,
77 const QStyleOptionViewItem & option,
78 const QModelIndex & index ) const
80 QLineEdit *editor = new QLineEdit(parent);
84 editor->setValidator( new QIntValidator( editor ) );
87 editor->setValidator( new QDoubleValidator( editor ) );
90 editor->setText( "No validator!" );
96 void NumDelegateItem::setEditorData( QWidget * editor,
97 const QModelIndex & index ) const
99 QLineEdit *aLE = qobject_cast<QLineEdit*>(editor);
107 int value = index.model()->data(index, Qt::DisplayRole).toInt();
108 aLE->setText( QString("%1").arg( value ) );
113 double value = index.model()->data(index, Qt::DisplayRole).toDouble();
114 aLE->setText( QString("%1").arg( value ) );
118 aLE->setText( "No validator!!!" );
126 /*class VisuGUI_Table : public QTableWidget {
128 VisuGUI_Table( Orientation orient, QWidget* parent = 0 )
129 : QTableWidget( parent ), myValidator( 0 ), myOrientation( orient ) {}
130 VisuGUI_Table( Orientation orient, int numRows, int numCols, QWidget* parent = 0 )
131 : QTableWidget( numRows, numCols, parent ), myValidator( 0 ), myOrientation( orient ) {}
133 void setValidator( QValidator* v = 0 ) { myValidator = v; }
134 bool isEditing() const { return QTable::isEditing(); }
137 QWidget* createEditor ( int row, int col, bool initFromCell ) const
139 bool testUnits = ( myOrientation == Qt::Horizontal && col == 0 ) || ( myOrientation == Qt::Vertical && row == 0 );
140 QWidget* wg = QTable::createEditor( row, col, initFromCell );
141 if ( wg && wg->inherits("QLineEdit") && myValidator && !testUnits )
142 (( QLineEdit*)wg)->setValidator( myValidator );
147 QValidator* myValidator;
148 Orientation myOrientation;
154 VisuGUI_TableDlg::VisuGUI_TableDlg( QWidget* parent,
158 Qt::Orientation orient,
159 bool showColumnTitles )
160 : QDialog( parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint ),
161 myIntTable( 0 ), myRealTable( 0 )
163 setWindowTitle( edit ? tr( "EDIT_TABLE_TLT" ) : tr( "VIEW_TABLE_TLT" ) );
164 setSizeGripEnabled( true );
167 bool bHasIntTable = false;
168 bool bHasRealTable = false;
170 _PTR(GenericAttribute) anAttr;
171 bHasIntTable = myObject->FindAttribute( anAttr, "AttributeTableOfInteger");
172 bHasRealTable = myObject->FindAttribute( anAttr, "AttributeTableOfReal");
175 QVBoxLayout* mainLayout = new QVBoxLayout( this );
176 mainLayout->setMargin( MARGIN_SIZE );
177 mainLayout->setSpacing( SPACING_SIZE );
179 bool bDoInt = which == ttInt || which == ttBoth || which == ttAuto && bHasIntTable;
180 bool bDoReal = which == ttReal || which == ttBoth || which == ttAuto && bHasRealTable;
184 if ( bDoInt && bDoReal ) {
185 top = new QTabWidget( this );
186 //( ( QTabWidget* ) top) ->setMargin( MARGIN_SIZE );
189 top = new QWidget( this );
190 tl = new QVBoxLayout( top ); tl->setMargin( 0 ); tl->setSpacing( SPACING_SIZE );
194 myIntTable = new VisuGUI_TableWidget( top, "myIntTable", edit, orient, showColumnTitles );
195 //myIntTable->getTable()->setValidator( new QIntValidator( this ) );
196 //QAbstractItemDelegate* item = myIntTable->getTable()->itemDelegate();
197 myIntTable->getTable()->setItemDelegate( new NumDelegateItem( myIntTable, NumDelegateItem::NV_Int ) );
198 //item->deleteLater();
200 if ( bDoInt && bDoReal )
201 ( ( QTabWidget* )top )->addTab( myIntTable, tr( "TABLE_OF_INTEGER_TLT" ) );
203 tl->addWidget( myIntTable );
206 myRealTable = new VisuGUI_TableWidget( top, "myRealTable", edit, orient, showColumnTitles );
207 //myRealTable->getTable()->setValidator( new QDoubleValidator( this ) );
208 //QAbstractItemDelegate* item = myIntTable->getTable()->itemDelegate();
209 myRealTable->getTable()->setItemDelegate( new NumDelegateItem( myRealTable, NumDelegateItem::NV_Real ) );
210 //item->deleteLater();
211 if ( bDoInt && bDoReal )
212 ( ( QTabWidget* )top )->addTab( myRealTable, tr( "TABLE_OF_REAL_TLT" ) );
214 tl->addWidget( myRealTable );
216 if ( !bDoInt && !bDoReal ) {
217 QLabel *dumbLabel = new QLabel( tr( "ERR_TABLE_NOT_AVAILABLE" ), top );
218 dumbLabel->setAlignment( Qt::AlignCenter );
219 tl->addWidget( dumbLabel );
222 QHBoxLayout* btnLayout = new QHBoxLayout;
223 btnLayout->setMargin( 0 ); btnLayout->setSpacing( SPACING_SIZE );
225 myOKBtn = new QPushButton( tr( "BUT_OK" ), this );
226 myHelpBtn = new QPushButton( tr( "BUT_HELP" ), this );
228 myCancelBtn = new QPushButton( tr( "BUT_CANCEL" ), this );
229 btnLayout->addWidget( myOKBtn );
230 btnLayout->addItem( new QSpacerItem( SPACER_SIZE, SPACER_SIZE, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
231 btnLayout->addWidget( myCancelBtn );
232 btnLayout->addWidget( myHelpBtn );
233 connect( myOKBtn, SIGNAL( clicked() ), this, SLOT( onOK() ) );
234 connect( myCancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
237 btnLayout->addWidget( myOKBtn );
238 btnLayout->addItem( new QSpacerItem( SPACER_SIZE, SPACER_SIZE, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
239 btnLayout->addWidget( myHelpBtn );
240 connect( myOKBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
242 connect( myHelpBtn, SIGNAL( clicked() ), this, SLOT( onHelp() ) );
244 mainLayout->addWidget( top );
245 mainLayout->addLayout( btnLayout );
249 SUIT_Tools::centerWidget( this, parent );
255 VisuGUI_TableDlg::~VisuGUI_TableDlg()
260 <OK> button slot, saves table(s)
261 Called only in create/edit mode ( <edit> parameter for constructor is true )
263 void VisuGUI_TableDlg::onOK()
265 myOKBtn->setFocus(); // accept possible changes
269 _PTR(Study) study = myObject->GetStudy();
270 _PTR(AttributeTableOfInteger) tblIntAttr;
271 _PTR(AttributeTableOfReal) tblRealAttr;
274 _PTR(StudyBuilder) builder = study->NewBuilder();
275 builder->NewCommand(); // start transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
278 builder->RemoveAttribute( myObject, "AttributeTableOfInteger" );
279 tblIntAttr = builder->FindOrCreateAttribute( myObject, "AttributeTableOfInteger" );
282 int nbRows = myIntTable->getNumRows();
283 int nbCols = myIntTable->getNumCols();
284 QString tlt = myIntTable->getTableTitle();
285 QStringList rowTitles, colTitles, units;
286 myIntTable->getRowTitles( rowTitles );
287 myIntTable->getColTitles( colTitles );
288 myIntTable->getUnits( units );
293 tblIntAttr->SetNbColumns( nbCols );
294 for ( i = 0; i < nbRows; i++ ) {
296 myIntTable->getRowData( i, data );
297 bool bEmptyRow = true;
298 for ( int j = 0; j < data.count(); j++ ) {
299 if ( !data[ j ].isNull() ) {
300 tblIntAttr->PutValue( data[ j ].toInt(), nRow+1, j+1 );
304 if ( !bEmptyRow ) { // Skip rows with no data !!!
306 tblIntAttr->SetRowTitle( nRow+1, rowTitles[ i ].isNull() ? "" : (const char*)rowTitles[ i ].toLatin1() );
308 tblIntAttr->SetRowUnit( nRow+1, units[ i ].isNull() ? "" : (const char*)units[ i ].toLatin1() );
312 if ( nRow > 0 ) { // Set columns only if table is not empty, otherwise exception is raised !!!
314 for ( i = 0; i < colTitles.count(); i++ )
315 tblIntAttr->SetColumnTitle( i+1, colTitles[ i ].isNull() ? "" : (const char*)colTitles[ i ].toLatin1() );
319 tblIntAttr->SetTitle( (const char*)myIntTable->getTableTitle().toLatin1() );
322 builder->RemoveAttribute( myObject, "AttributeTableOfReal" );
323 tblRealAttr = builder->FindOrCreateAttribute( myObject, "AttributeTableOfReal" );
326 int nbRows = myRealTable->getNumRows();
327 int nbCols = myRealTable->getNumCols();
328 QString tlt = myRealTable->getTableTitle();
329 QStringList rowTitles, colTitles, units;
330 myRealTable->getRowTitles( rowTitles );
331 myRealTable->getColTitles( colTitles );
332 myRealTable->getUnits( units );
337 tblRealAttr->SetNbColumns( nbCols );
338 for ( i = 0; i < nbRows; i++ ) {
340 myRealTable->getRowData( i, data );
341 bool bEmptyRow = true;
342 for ( int j = 0; j < data.count(); j++ ) {
343 if ( !data[ j ].isNull() ) {
344 tblRealAttr->PutValue( data[ j ].toDouble(), nRow+1, j+1 );
348 if ( !bEmptyRow ) { // Skip rows with no data !!!
350 tblRealAttr->SetRowTitle( nRow+1, rowTitles[ i ].isNull() ? "" : (const char*)rowTitles[ i ].toLatin1() );
352 tblRealAttr->SetRowUnit( nRow+1, units[ i ].isNull() ? "" : (const char*)units[ i ].toLatin1() );
356 if ( nRow > 0 ) { // Set columns only if table is not empty, otherwise exception is raised !!!
358 for ( i = 0; i < colTitles.count(); i++ )
359 tblRealAttr->SetColumnTitle( i+1, colTitles[ i ].isNull() ? "" : (const char*)colTitles[ i ].toLatin1() );
363 tblRealAttr->SetTitle( (const char*)myRealTable->getTableTitle().toLatin1() );
365 if ( myIntTable || myRealTable)
366 builder->CommitCommand(); // commit transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!
368 builder->AbortCommand(); // abort transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!
371 MESSAGE("VisuGUI_TableDlg::onOK : Exception has been caught !!!");
372 builder->AbortCommand(); // abort transaction !!!!!!!!!!!!!!!!!!!!!!!!!!!
374 SUIT_MessageBox::critical ( this, tr("ERR_ERROR"), tr("ERR_APP_EXCEPTION") );
383 <Help> button slot, shows corresponding help page
385 void VisuGUI_TableDlg::onHelp()
387 QString aHelpFileName = "/files/displaying_tables.htm";
388 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
390 app->onHelpContextModule(app->activeModule() ? app->moduleName(app->activeModule()->moduleName()) : QString(""), aHelpFileName);
394 platform = "winapplication";
396 platform = "application";
398 SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
399 QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
400 arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(aHelpFileName) );
405 Populates table with data
407 void VisuGUI_TableDlg::initDlg()
411 _PTR(GenericAttribute) anAttr;
412 _PTR(AttributeTableOfInteger) tblIntAttr;
413 _PTR(AttributeTableOfReal) tblRealAttr;
414 if ( myObject->FindAttribute( anAttr, "AttributeTableOfInteger") ) {
417 if ( myObject->FindAttribute( anAttr, "AttributeTableOfReal") ) {
418 tblRealAttr = anAttr;
421 if ( tblIntAttr && myIntTable ) {
424 myIntTable->setTableTitle( tblIntAttr->GetTitle().c_str() );
426 int nbRows = tblIntAttr->GetNbRows() ;
427 int nbCols = tblIntAttr->GetNbColumns();
428 myIntTable->setNumRows( nbRows );
429 myIntTable->setNumCols( nbCols );
432 vector<string> rowTitles = tblIntAttr->GetRowTitles();
433 for ( i = 0; i < nbRows; i++ ) {
434 if ( rowTitles.size() > 0 )
435 strlist.append( rowTitles[i].c_str() );
437 strlist.append( "" );
439 myIntTable->setRowTitles( strlist );
442 vector<string> colTitles = tblIntAttr->GetColumnTitles();
443 for ( i = 0; i < nbCols; i++ ) {
444 if ( colTitles.size() > 0 )
445 strlist.append( colTitles[i].c_str() );
447 strlist.append( "" );
449 myIntTable->setColTitles( strlist );
452 vector<string> rowUnits = tblIntAttr->GetRowUnits();
453 if ( rowUnits.size() > 0 ) {
454 for ( i = 0; i < nbRows; i++ )
455 strlist.append( rowUnits[i].c_str() );
456 myIntTable->setUnits( strlist );
459 for ( i = 1; i <= nbRows; i++ ) {
461 for ( j = 1; j <= nbCols; j++ ) {
462 if ( tblIntAttr->HasValue( i, j ) )
463 strlist.append( QString::number( tblIntAttr->GetValue( i, j ) ) );
465 strlist.append( QString::null );
467 myIntTable->setRowData( i-1, strlist );
469 myIntTable->adjustTable();
472 MESSAGE("VisuGUI_TableDlg::initDlg : Exception has been caught !!!");
476 if ( tblRealAttr && myRealTable ) {
479 myRealTable->setTableTitle( tblRealAttr->GetTitle().c_str() );
481 int nbRows = tblRealAttr->GetNbRows() ;
482 int nbCols = tblRealAttr->GetNbColumns();
483 myRealTable->setNumRows( nbRows );
484 myRealTable->setNumCols( nbCols );
487 vector<string> rowTitles = tblRealAttr->GetRowTitles();
488 for ( i = 0; i < nbRows; i++ ) {
489 if ( rowTitles.size() > 0 )
490 strlist.append( rowTitles[i].c_str() );
492 strlist.append( "" );
494 myRealTable->setRowTitles( strlist );
497 vector<string> colTitles = tblRealAttr->GetColumnTitles();
498 for ( i = 0; i < nbCols; i++ ) {
499 if ( colTitles.size() > 0 )
500 strlist.append( colTitles[i].c_str() );
502 strlist.append( "" );
504 myRealTable->setColTitles( strlist );
507 vector<string> rowUnits = tblRealAttr->GetRowUnits();
508 if ( rowUnits.size() > 0 ) {
509 for ( i = 0; i < nbRows; i++ )
510 strlist.append( rowUnits[i].c_str() );
511 myRealTable->setUnits( strlist );
514 for ( i = 1; i <= nbRows; i++ ) {
516 for ( j = 1; j <= nbCols; j++ ) {
517 if ( tblRealAttr->HasValue( i, j ) )
518 strlist.append( QString::number( tblRealAttr->GetValue( i, j ) ) );
520 strlist.append( QString::null );
522 myRealTable->setRowData( i-1, strlist );
524 myRealTable->adjustTable();
527 MESSAGE("VisuGUI_TableDlg::initDlg : Exception has been caught !!!");
534 Provides help on F1 button click
536 void VisuGUI_TableDlg::keyPressEvent( QKeyEvent* e )
538 QDialog::keyPressEvent( e );
539 if ( e->isAccepted() )
542 if ( e->key() == Qt::Key_F1 )
552 VisuGUI_TableWidget::VisuGUI_TableWidget( QWidget* parent,
555 Qt::Orientation orient,
556 bool showColumnTitles )
557 : QWidget( parent ), myOrientation( orient )
559 QGridLayout* mainLayout = new QGridLayout( this );
560 mainLayout->setMargin( 0 );
561 mainLayout->setSpacing( SPACING_SIZE );
563 myTitleEdit = new QLineEdit( this );
564 myTitleEdit->setAlignment( Qt::AlignCenter );
565 myTitleEdit->setReadOnly( !edit );
566 QFont fnt = myTitleEdit->font();
568 myTitleEdit->setFont( fnt );
570 //myTable = new VisuGUI_Table( orient, this );
571 myTable = new QTableWidget( 5, 5, this );
572 //myTable->setNumRows( 5 );
573 //myTable->setNumCols( 5 );
574 myTable->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
575 myTable->setMinimumSize( MIN_TABLE_WIDTH, MIN_TABLE_HEIGHT );
576 myTable->setSelectionMode( QAbstractItemView::SingleSelection );
577 myTable->setShowGrid( true );
578 myTable->horizontalHeader()->setMovable( false );
579 myTable->verticalHeader()->setMovable( false );
580 //myTable->setColumnMovingEnabled( false );
581 //myTable->setRowMovingEnabled( false );
582 myTable->setDragEnabled( false );
583 //myTable->setReadOnly( !edit );
584 myTable->setEditTriggers( edit ? QAbstractItemView::AllEditTriggers : QAbstractItemView::NoEditTriggers );
586 setUnitsTitle( tr( "UNITS_TLT" ) );
588 if ( !showColumnTitles ) {
589 if ( myOrientation == Qt::Horizontal ) {
590 myTable->horizontalHeader()->hide();
591 //myTable->setTopMargin( 0 );
594 myTable->verticalHeader()->hide();
595 //myTable->setLeftMargin( 0 );
599 mainLayout->addWidget( myTitleEdit, 0, 0 );
600 mainLayout->addWidget( myTable, 1, 0 );
603 myAddRowBtn = new QPushButton( tr( "ADD_ROW_BTN" ), this );
604 myDelRowBtn = new QPushButton( tr( "REMOVE_ROW_BTN" ), this );
605 myAddColBtn = new QPushButton( tr( "ADD_COLUMN_BTN" ), this );
606 myDelColBtn = new QPushButton( tr( "REMOVE_COLUMN_BTN" ), this );
607 myAdjustBtn = new QPushButton( tr( "ADJUST_CELLS_BTN" ), this );
608 mySelectAllBtn = new QPushButton( tr( "SELECT_ALL_BTN" ), this );
609 myClearBtn = new QPushButton( tr( "CLEAR_BTN"), this );
610 QVBoxLayout* btnLayout = new QVBoxLayout; btnLayout->setMargin( 0 ); btnLayout->setSpacing( SPACING_SIZE );
611 btnLayout->addWidget( myAddRowBtn );
612 btnLayout->addWidget( myDelRowBtn );
613 btnLayout->addWidget( myAddColBtn );
614 btnLayout->addWidget( myDelColBtn );
615 btnLayout->addStretch();
616 btnLayout->addWidget( myAdjustBtn );
617 btnLayout->addStretch();
618 btnLayout->addWidget( mySelectAllBtn );
619 btnLayout->addWidget( myClearBtn );
620 mainLayout->addLayout( btnLayout, 1, 1 );
621 connect( myTable, SIGNAL( selectionChanged() ), this, SLOT( updateButtonsState() ) );
622 connect( myTable, SIGNAL( currentChanged( int, int) ), this, SLOT( updateButtonsState() ) );
623 connect( myAddRowBtn, SIGNAL( clicked() ), this, SLOT( addRow() ) );
624 connect( myAddColBtn, SIGNAL( clicked() ), this, SLOT( addCol() ) );
625 connect( myDelRowBtn, SIGNAL( clicked() ), this, SLOT( delRow() ) );
626 connect( myDelColBtn, SIGNAL( clicked() ), this, SLOT( delCol() ) );
627 connect( myAdjustBtn, SIGNAL( clicked() ), this, SLOT( adjustTable() ) );
628 connect( mySelectAllBtn, SIGNAL( clicked() ), this, SLOT( selectAll() ) );
629 connect( myClearBtn, SIGNAL( clicked() ), this, SLOT( clearTable() ) );
630 myTable->horizontalHeader()->installEventFilter( this );
631 myTable->verticalHeader()->installEventFilter( this );
632 myTable->installEventFilter( this );
634 updateButtonsState();
639 VisuGUI_TableWidget::~VisuGUI_TableWidget()
645 void VisuGUI_TableWidget::setTableTitle( const QString& title )
647 myTitleEdit->setText( title );
652 QString VisuGUI_TableWidget::getTableTitle()
654 return myTitleEdit->text();
657 Sets total number of rows
659 void VisuGUI_TableWidget::setNumRows( const int num )
661 myOrientation == Qt::Horizontal ? myTable->setRowCount( num ) : myTable->setColumnCount( num );
664 Gets total number of rows
666 int VisuGUI_TableWidget::getNumRows()
668 return myOrientation == Qt::Horizontal ? myTable->rowCount() : myTable->columnCount();
671 Sets total number of columns
673 void VisuGUI_TableWidget::setNumCols( const int num )
675 // !!! first column contains units !!!
676 myOrientation == Qt::Horizontal ? myTable->setColumnCount( num+1 ) : myTable->setRowCount( num+1 );
677 // myOrientation == Qt::Horizontal ? myTable->setColumnReadOnly( 0, true ) : myTable->setRowReadOnly( 0, true );
680 Gets total number of columns
682 int VisuGUI_TableWidget::getNumCols()
684 // !!! first column contains units !!!
685 return myOrientation == Qt::Horizontal ? myTable->columnCount()-1 : myTable->rowCount()-1;
690 void VisuGUI_TableWidget::setRowTitles( QStringList& tlts )
693 for ( int i = 0; i < tlts.count(); i++ )
694 tlts[i].isNull() ? aLabels.append("") : aLabels.append( tlts[i] );
696 myOrientation == Qt::Horizontal ?
697 myTable->setVerticalHeaderLabels( aLabels ) :
698 myTable->setHorizontalHeaderLabels( aLabels );
700 // myTable->verticalHeader()->setLabel( i, tlts[i] ) :
701 //myTable->horizontalHeader()->setLabel( i, tlts[i] );
707 void VisuGUI_TableWidget::getRowTitles( QStringList& tlts )
710 if ( myOrientation == Qt::Horizontal ) {
711 for ( int i = 0; i < myTable->rowCount(); i++ ) {
712 tlts.append( myTable->verticalHeaderItem(i) ? myTable->verticalHeaderItem(i)->text() : "" );
716 for ( int i = 0; i < myTable->columnCount(); i++ ) {
717 tlts.append( myTable->horizontalHeaderItem(i) ? myTable->horizontalHeaderItem(i)->text() : "" );
724 void VisuGUI_TableWidget::setColTitles( QStringList& tlts )
728 // !!! first column contains units !!!
729 aLabels.append(""); // it'll be initialized below - in setUnitsTitle() method
731 for ( int i = 0; i < tlts.count(); i++ )
732 tlts[i].isNull() ? aLabels.append("") : aLabels.append( tlts[i] );
734 myOrientation == Qt::Horizontal ?
735 myTable->setHorizontalHeaderLabels( aLabels ) :
736 myTable->setVerticalHeaderLabels( aLabels );
738 setUnitsTitle( tr( "UNITS_TLT" ) );
743 void VisuGUI_TableWidget::getColTitles( QStringList& tlts )
745 // !!! first column contains units !!!
747 if ( myOrientation == Qt::Horizontal ) {
748 for ( int i = 1; i < myTable->columnCount(); i++ ) {
749 tlts.append( myTable->horizontalHeaderItem(i) ? myTable->horizontalHeaderItem(i)->text() : "" );
753 for ( int i = 1; i < myTable->rowCount(); i++ ) {
754 tlts.append( myTable->verticalHeaderItem(i) ? myTable->verticalHeaderItem(i)->text() : "" );
761 void VisuGUI_TableWidget::setUnitsTitle( const QString& tlt ) {
762 // !!! first column contains units !!!
763 myTable->model()->setHeaderData( 0, myOrientation, QVariant(tlt.isNull() ? "" : tlt), Qt::DisplayRole );
768 void VisuGUI_TableWidget::setUnits( QStringList& units )
770 QAbstractTableModel* aModel = qobject_cast<QAbstractTableModel*>( myTable->model() );
774 for ( int i = 0; i < units.count(); i++ )
776 myOrientation == Qt::Horizontal ?
777 anIndex = aModel->index( i, 0 ) :
778 anIndex = aModel->index( 0, i );
780 aModel->setData( anIndex, QVariant( units[i].isNull() ? "" : units[i] ) );
787 void VisuGUI_TableWidget::getUnits( QStringList& units )
790 QAbstractTableModel* aModel = qobject_cast<QAbstractTableModel*>( myTable->model() );
793 if ( myOrientation == Qt::Horizontal )
795 for ( int i = 0; i < myTable->rowCount(); i++ )
796 units.append( aModel->index( i, 0 ).data().toString() );
799 for ( int i = 0; i < myTable->columnCount(); i++ )
800 units.append( aModel->index( 0, i ).data().toString() );
807 void VisuGUI_TableWidget::setRowData( int row, QStringList& data )
809 QAbstractTableModel* aModel = qobject_cast<QAbstractTableModel*>( myTable->model() );
813 if ( row >= 0 && row < getNumRows() ) {
814 for ( int i = 0; i < data.count(); i++ )
816 myOrientation == Qt::Horizontal ? anIndex = aModel->index( row, i+1 ) :
817 anIndex = aModel->index( i+1, row );
818 aModel->setData( anIndex, QVariant( data[i] ) );
827 void VisuGUI_TableWidget::getRowData( int row, QStringList& data )
830 QAbstractTableModel* aModel = qobject_cast<QAbstractTableModel*>( myTable->model() );
833 if ( row >= 0 && row < getNumRows() )
835 if ( myOrientation == Qt::Horizontal )
837 for ( int i = 1; i < myTable->columnCount(); i++ )
838 data.append( aModel->index( row, i ).data().toString() );
841 for ( int i = 1; i < myTable->rowCount(); i++ )
842 data.append( aModel->index( i, row ).data().toString() );
848 Adjusts table cell to see contents, <Adjust Cells> button slot
850 void VisuGUI_TableWidget::adjustTable()
852 myTable->resizeRowsToContents();
853 myTable->resizeColumnsToContents();
856 Called when selection changed in table
858 void VisuGUI_TableWidget::updateButtonsState()
860 if ( myTable->editTriggers() == QAbstractItemView::NoEditTriggers )
862 bool bDR = false; // <Delete Row(s)>
863 bool bDC = false; // <Delete Column(s)>
864 bool bSA = false; // <Select All>
865 bool bCT = false; // <Clear>
867 //TO DO column/row selection check
868 /*int c = myOrientation == Qt::Horizontal ? 0 : 1;
869 for ( i = c; i < myTable->rowCount(); i++ ) {
871 if ( myTable->isRowSelected( i, true ) )
876 c = myOrientation == Qt::Horizontal ? 1 : 0;
877 for ( i = c; i < myTable->columnCount(); i++ ) {
878 if ( myTable->isColumnSelected( i, true ) )
883 /*int nbSel = myTable->numSelections();
884 for ( i = 0; i < nbSel; i++ ) {
885 QTableSelection ts = myTable->selection( i );
886 for ( int j = ts.topRow(); j < ts.bottomRow()+1; j++) {
887 for ( int k = ts.leftCol(); k < ts.rightCol()+1; k++) {
888 if ( myTable->item( j, k ) )
893 QList<QTableWidgetItem*> aSelection = myTable->selectedItems();
894 QList<QTableWidgetItem*>::ConstIterator anIt = aSelection.constBegin(),
895 anEndIt = aSelection.constEnd();
896 for ( ; anIt != anEndIt; anIt++ )
905 if ( myTable->item( myTable->currentRow(), myTable->currentColumn() ) )
907 myDelRowBtn->setEnabled( bDR );
908 myDelColBtn->setEnabled( bDC );
909 mySelectAllBtn->setEnabled( bSA );
910 myClearBtn->setEnabled( bCT );
913 <Add row> button slot
915 void VisuGUI_TableWidget::addRow()
917 myTable->insertRow( myTable->rowCount() );
918 updateButtonsState();
921 <Add column> button slot
923 void VisuGUI_TableWidget::addCol()
925 myTable->insertColumn( myTable->columnCount() );
926 updateButtonsState();
929 <Delete row(s)> button slot
931 void VisuGUI_TableWidget::delRow()
934 /*int c = myOrientation == Qt::Horizontal ? 0 : 1;
937 for ( i = c; i < myTable->rowCount(); i++ )
938 if ( myTable->isRowSelected( i, true ) )
940 if ( il.count() > 0 ) {
941 QMemArray<int> ildel( il.count() );
942 for ( i = 0; i < il.count(); i++ )
943 ildel[ i ] = il[ i ];
944 myTable->removeRows( ildel );
947 updateButtonsState();
950 <Delete column(s)> button slot
952 void VisuGUI_TableWidget::delCol()
955 /*int c = myOrientation == Qt::Horizontal ? 1 : 0;
958 for ( i = c; i < myTable->numCols(); i++ )
959 if ( myTable->isColumnSelected( i, true ) )
961 if ( il.count() > 0 ) {
962 QMemArray<int> ildel( il.count() );
963 for ( i = 0; i < il.count(); i++ )
964 ildel[ i ] = il[ i ];
965 myTable->removeColumns( ildel );
967 updateButtonsState();
970 <Select All> button slot
972 void VisuGUI_TableWidget::selectAll()
974 /*myTable->clearSelection();
976 ts.init( 0, 0 ); ts.expandTo( myTable->numRows()-1, myTable->numCols()-1 );
977 myTable->addSelection( ts );*/
978 myTable->selectAll();
979 updateButtonsState();
984 void VisuGUI_TableWidget::clearTable()
986 /*int nbSel = myTable->numSelections();
987 for ( int i = 0; i < nbSel; i++ ) {
988 QTableSelection ts = myTable->selection( i );
989 for ( int j = ts.topRow(); j < ts.bottomRow()+1; j++) {
990 if ( myOrientation == Qt::Vertical && j == 0 ) {
991 // continue; // UNITS
993 for ( int k = ts.leftCol(); k < ts.rightCol()+1; k++) {
994 if ( myOrientation == Qt::Horizontal && k == 0 ) {
995 // continue; // UNITS
997 myTable->clearCell( j, k );
1002 myTable->clearCell( myTable->currentRow(), myTable->currentColumn() );
1003 myTable->clearSelection();*/
1004 myTable->clearContents();
1005 updateButtonsState();
1008 Event filter - handles titles editing
1010 bool VisuGUI_TableWidget::eventFilter( QObject* o, QEvent* e )
1012 if ( e->type() == QEvent::MouseButtonDblClick) {
1014 /*QMouseEvent* me = ( QMouseEvent* )e;
1015 if ( me->button() == Qt::LeftButton && (myTable->editTriggers() != QAbstractItemView::NoEditTriggers ) ) {
1016 if ( o == myTable->horizontalHeader() ) {
1017 for ( int i = 0; i < myTable->horizontalHeader()->count(); i++ ) {
1018 QRect rect = myTable->horizontalHeader()->sectionRect( i );
1019 rect.addCoords( 1, 1, -1, -1 );
1020 if ( rect.contains( myTable->horizontalHeader()->mapFromGlobal( me->globalPos() ) ) ) {
1021 if ( myOrientation == Qt::Vertical || i != 0 ) {
1023 QString tlt = QInputDialog::getText( tr( "SET_TITLE_TLT" ),
1026 myTable->horizontalHeader()->label( i ),
1029 if ( bOk && !tlt.isNull() )
1030 myTable->horizontalHeader()->setLabel( i, tlt );
1036 if ( o == myTable->verticalHeader() ) {
1037 for ( int i = 0; i < myTable->verticalHeader()->count(); i++ ) {
1038 QRect rect = myTable->verticalHeader()->sectionRect( i );
1039 rect.addCoords( 1, 1, -1, -1 );
1040 if ( rect.contains( myTable->verticalHeader()->mapFromGlobal( me->globalPos() ) ) ) {
1041 if ( myOrientation == Qt::Horizontal || i != 0 ) {
1043 QString tlt = QInputDialog::getText( tr( "SET_TITLE_TLT" ),
1046 myTable->verticalHeader()->label( i ),
1049 if ( bOk && !tlt.isNull() )
1050 myTable->verticalHeader()->setLabel( i, tlt );
1058 else if ( e->type() == QEvent::KeyRelease && o == myTable ) {
1059 QKeyEvent* ke = (QKeyEvent*)e;
1060 if ( ke->key() == Qt::Key_Delete && (myTable->editTriggers() != QAbstractItemView::NoEditTriggers) ) {
1063 else if ( ke->key() == Qt::Key_Backspace && (myTable->editTriggers() != QAbstractItemView::NoEditTriggers) ) {
1065 int i = myTable->currentRow();
1066 int j = myTable->currentColumn() - 1;
1067 if ( j < 0 ) { j = myTable->columnCount()-1; i--; }
1068 if ( i >= 0 && j >= 0 )
1069 myTable->setCurrentCell( i, j );
1072 return QWidget::eventFilter( o, e );