1 // Copyright (C) 2013 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 #include "CurveCreator_TableView.h"
21 #include "CurveCreator_UtilsICurve.hxx"
25 #include <QTableWidget>
26 #include <QTableWidgetItem>
28 #include <QtxDoubleSpinBox.h>
30 const double DBL_MINIMUM = -10000000.;
31 const double DBL_MAXIMUM = 10000000.;
33 const int SECTION_NAME_COLUMN_WIDTH = 75;
34 const int POINT_INDEX_COLUMN_WIDTH = 40;
36 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
38 CurveCreator_TableItemDelegate::CurveCreator_TableItemDelegate( QObject* theParent )
39 : QItemDelegate( theParent )
44 * Creates an editor for the cell
46 QWidget* CurveCreator_TableItemDelegate::createEditor( QWidget* theParent,
47 const QStyleOptionViewItem& theOption,
48 const QModelIndex& theIndex ) const
50 QWidget* anEditor = 0;
52 int aColumnId = theIndex.column();
53 if ( aColumnId == 2 || aColumnId == 3 ) {
54 QDoubleSpinBox* aSpin = new QtxDoubleSpinBox( theParent );
55 aSpin->setDecimals( 2 );
56 aSpin->setRange( DBL_MINIMUM, DBL_MAXIMUM );
60 anEditor = QItemDelegate::createEditor( theParent, theOption, theIndex );
65 void CurveCreator_TableItemDelegate::setEditorData( QWidget* theEditor,
66 const QModelIndex& theIndex ) const
68 int aColumnId = theIndex.column();
69 if ( aColumnId == 2 || aColumnId == 3 ) {
70 QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
72 double aValue = theIndex.model()->data( theIndex, Qt::EditRole ).toDouble();
73 aDblSpin->setValue( aValue );
77 QItemDelegate::setEditorData( theEditor, theIndex );
80 void CurveCreator_TableItemDelegate::setModelData( QWidget* theEditor,
81 QAbstractItemModel* theModel,
82 const QModelIndex& theIndex ) const
84 int aColumnId = theIndex.column();
85 if ( aColumnId == 2 || aColumnId == 3 ) {
86 QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
88 double aValue = aDblSpin->value();
89 theModel->setData( theIndex, aValue, Qt::UserRole);
93 QItemDelegate::setModelData( theEditor, theModel, theIndex );
96 CurveCreator_TableView::CurveCreator_TableView( CurveCreator_ICurve* theCurve, QWidget* theParent )
97 : QTableWidget( theParent ), myCurve( theCurve )
99 setItemDelegate( new CurveCreator_TableItemDelegate( this ) );
102 setColumnWidth( 0, SECTION_NAME_COLUMN_WIDTH );
103 setColumnWidth( 1, POINT_INDEX_COLUMN_WIDTH );
105 //aLabels << tr( "SECTION_LABEL" ) << tr( "IDENTIFIER_LABEL" ) << tr( "X_POSITION_LBL" ) << tr( "Y_POSITION_LBL" );
106 aLabels << tr( "Section" ) << "Index" << tr( "X" ) << tr( "Y" );
107 setHorizontalHeaderLabels( aLabels );
110 void CurveCreator_TableView::setCurve( CurveCreator_ICurve* theCurve )
115 void CurveCreator_TableView::addLocalPointToTable(
116 const CurveCreator_ICurve::SectionToPoint& theSToPoint )
118 int aRowId = rowCount();
119 int anISection = theSToPoint.first;
120 int anIPoint = theSToPoint.second;
121 bool isFoundPoint = false;
122 for ( int i = 0; i < aRowId && !isFoundPoint; i++ )
123 isFoundPoint = getSectionId( i ) == anISection &&
124 getPointId( i ) == anIPoint;
128 setRowCount( aRowId+1 );
130 QTableWidgetItem* anItem;
131 anItem = new QTableWidgetItem( myCurve->getSectionName( anISection ).c_str() );
132 anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
133 anItem->setData( Qt::UserRole, anISection );
134 setItem( aRowId, 0, anItem );
136 anItem = new QTableWidgetItem( QString::number( anIPoint + 1 ) );
137 anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
138 anItem->setData( Qt::UserRole, anIPoint );
139 setItem( aRowId, 1, anItem );
142 CurveCreator_UtilsICurve::getPoint( myCurve, anISection, anIPoint, aPoint );
144 anItem = new QTableWidgetItem( QString::number( aPoint.X(), 'f', 2 ) );
145 anItem->setData( Qt::UserRole, aPoint.X() );
146 setItem( aRowId, 2, anItem );
148 anItem = new QTableWidgetItem( QString::number( aPoint.Y(), 'f', 2 ) );
149 anItem->setData( Qt::UserRole, aPoint.Y() );
150 setItem( aRowId, 3, anItem );
153 int CurveCreator_TableView::getSectionId( const int theRowId ) const
155 return item( theRowId, 0 )->data( Qt::UserRole ).toInt();
159 * Returns a point index from the table
160 * \param theRowId a table row
162 int CurveCreator_TableView::getPointId( const int theRowId ) const
164 return item( theRowId, 1 )->data( Qt::UserRole ).toInt();