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"
23 #include <QTableWidget>
24 #include <QTableWidgetItem>
26 #include <QtxDoubleSpinBox.h>
28 const double DBL_MINIMUM = -10000000.;
29 const double DBL_MAXIMUM = 10000000.;
31 const int SECTION_NAME_COLUMN_WIDTH = 75;
32 const int POINT_INDEX_COLUMN_WIDTH = 40;
34 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
36 CurveCreator_TableItemDelegate::CurveCreator_TableItemDelegate( QObject* theParent )
37 : QItemDelegate( theParent )
42 * Creates an editor for the cell
44 QWidget* CurveCreator_TableItemDelegate::createEditor( QWidget* theParent,
45 const QStyleOptionViewItem& theOption,
46 const QModelIndex& theIndex ) const
48 QWidget* anEditor = 0;
50 int aColumnId = theIndex.column();
51 if ( aColumnId == 2 || aColumnId == 3 ) {
52 QDoubleSpinBox* aSpin = new QtxDoubleSpinBox( theParent );
53 aSpin->setDecimals( 2 );
54 aSpin->setRange( DBL_MINIMUM, DBL_MAXIMUM );
58 anEditor = QItemDelegate::createEditor( theParent, theOption, theIndex );
63 void CurveCreator_TableItemDelegate::setEditorData( QWidget* theEditor,
64 const QModelIndex& theIndex ) const
66 int aColumnId = theIndex.column();
67 if ( aColumnId == 2 || aColumnId == 3 ) {
68 QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
70 double aValue = theIndex.model()->data( theIndex, Qt::EditRole ).toDouble();
71 aDblSpin->setValue( aValue );
75 QItemDelegate::setEditorData( theEditor, theIndex );
78 void CurveCreator_TableItemDelegate::setModelData( QWidget* theEditor,
79 QAbstractItemModel* theModel,
80 const QModelIndex& theIndex ) const
82 int aColumnId = theIndex.column();
83 if ( aColumnId == 2 || aColumnId == 3 ) {
84 QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
86 double aValue = aDblSpin->value();
87 theModel->setData( theIndex, aValue, Qt::UserRole);
91 QItemDelegate::setModelData( theEditor, theModel, theIndex );
94 CurveCreator_TableView::CurveCreator_TableView( CurveCreator_ICurve* theCurve, QWidget* theParent )
95 : QTableWidget( theParent ), myCurve( theCurve )
97 setItemDelegate( new CurveCreator_TableItemDelegate( this ) );
100 setColumnWidth( 0, SECTION_NAME_COLUMN_WIDTH );
101 setColumnWidth( 1, POINT_INDEX_COLUMN_WIDTH );
103 //aLabels << tr( "SECTION_LABEL" ) << tr( "IDENTIFIER_LABEL" ) << tr( "X_POSITION_LBL" ) << tr( "Y_POSITION_LBL" );
104 aLabels << tr( "Section" ) << "Index" << tr( "X" ) << tr( "Y" );
105 setHorizontalHeaderLabels( aLabels );
108 void CurveCreator_TableView::setCurve( CurveCreator_ICurve* theCurve )
113 void CurveCreator_TableView::addLocalPointToTable( const double theX, const double theY )
115 CurveCreator_ICurve::SectionToPointList aPoints;
116 CurveCreator_UtilsICurve::findSectionsToPoints( myCurve, theX, theY, aPoints );
118 CurveCreator_ICurve::SectionToPointList aSkipList;
119 // table could not contain two equal value rows
120 int aRowId = rowCount();
121 double aCurrentX, aCurrentY;
122 int aSectionId, aPointId;
123 CurveCreator_ICurve::SectionToPoint aPoint;
124 for ( int i = 0; i < aRowId; i++ ) {
125 aCurrentX = item( i, 2 )->data( Qt::UserRole ).toDouble();
126 aCurrentY = item( i, 3 )->data( Qt::UserRole ).toDouble();
127 if ( fabs( aCurrentX - theX ) < LOCAL_SELECTION_TOLERANCE &&
128 fabs( aCurrentY - theY ) < LOCAL_SELECTION_TOLERANCE ) {
129 aPoint = std::make_pair<int, int>( getSectionId( i ), getPointId( i ) );
130 if ( !CurveCreator_UtilsICurve::contains( aSkipList, aPoint ) )
131 aSkipList.push_back( aPoint );
134 if ( aSkipList.size() == aPoints.size() )
137 QTableWidgetItem* anItem;
138 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints.begin(),
139 aLast = aPoints.end();
140 for ( ; anIt != aLast; anIt++ ) {
142 if ( CurveCreator_UtilsICurve::contains( aSkipList, aPoint ) )
145 setRowCount( aRowId+1 );
146 aSectionId = aPoint.first;
147 aPointId = aPoint.second;
149 anItem = new QTableWidgetItem( myCurve->getSectionName( aSectionId ).c_str() );
150 anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
151 anItem->setData( Qt::UserRole, aSectionId );
152 setItem( aRowId, 0, anItem );
154 anItem = new QTableWidgetItem( QString::number( aPointId + 1 ) );
155 anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
156 anItem->setData( Qt::UserRole, aPointId );
157 setItem( aRowId, 1, anItem );
159 anItem = new QTableWidgetItem( QString::number( theX, 'f', 2 ) );
160 anItem->setData( Qt::UserRole, theX );
161 setItem( aRowId, 2, anItem );
163 anItem = new QTableWidgetItem( QString::number( theY, 'f', 2 ) );
164 anItem->setData( Qt::UserRole, theY );
165 setItem( aRowId, 3, anItem );
169 int CurveCreator_TableView::getSectionId( const int theRowId ) const
171 return item( theRowId, 0 )->data( Qt::UserRole ).toInt();
175 * Returns a point index from the table
176 * \param theRowId a table row
178 int CurveCreator_TableView::getPointId( const int theRowId ) const
180 return item( theRowId, 1 )->data( Qt::UserRole ).toInt();