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>
25 //#include <QDoubleSpinBox>
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( 6 );
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 QTableWidget* aTableWidget = dynamic_cast<QTableWidget*>( parent() );
67 QTableWidgetItem* anItem = aTableWidget ? dynamic_cast<QTableWidgetItem*>
68 ( aTableWidget->item( theIndex.row(), theIndex.column() ) ) : 0;
69 int aColumnId = theIndex.column();
70 if ( anItem && ( aColumnId == 2 || aColumnId == 3 ) ) {
71 QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
72 double aValue = anItem->data( Qt::UserRole ).toDouble();
74 aDblSpin->setValue( aValue );
78 QItemDelegate::setEditorData( theEditor, theIndex );
81 void CurveCreator_TableItemDelegate::setModelData( QWidget* theEditor,
82 QAbstractItemModel* theModel,
83 const QModelIndex& theIndex ) const
85 QTableWidget* aTableWidget = dynamic_cast<QTableWidget*>( parent() );
86 QTableWidgetItem* anItem = aTableWidget ? dynamic_cast<QTableWidgetItem*>
87 ( aTableWidget->item( theIndex.row(), theIndex.column() ) ) : 0;
89 int aColumnId = theIndex.column();
90 if ( anItem && ( aColumnId == 2 || aColumnId == 3 ) ) {
91 QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
93 double aValue = aDblSpin->value();
94 anItem->setData( Qt::UserRole, aValue );
98 QItemDelegate::setModelData( theEditor, theModel, theIndex );
101 CurveCreator_TableView::CurveCreator_TableView( CurveCreator_ICurve* theCurve, QWidget* theParent )
102 : QTableWidget( theParent ), myCurve( theCurve )
104 setItemDelegate( new CurveCreator_TableItemDelegate( this ) );
107 setColumnWidth( 0, SECTION_NAME_COLUMN_WIDTH );
108 setColumnWidth( 1, POINT_INDEX_COLUMN_WIDTH );
110 //aLabels << tr( "SECTION_LABEL" ) << tr( "IDENTIFIER_LABEL" ) << tr( "X_POSITION_LBL" ) << tr( "Y_POSITION_LBL" );
111 aLabels << tr( "Section" ) << "Index" << tr( "X" ) << tr( "Y" );
112 setHorizontalHeaderLabels( aLabels );
115 void CurveCreator_TableView::setCurve( CurveCreator_ICurve* theCurve )
120 void CurveCreator_TableView::addLocalPointToTable( const double theX, const double theY )
122 CurveCreator_ICurve::SectionToPointList aPoints;
123 CurveCreator_UtilsICurve::findSectionsToPoints( myCurve, theX, theY, aPoints );
125 CurveCreator_ICurve::SectionToPointList aSkipList;
126 // table could not contain two equal value rows
127 int aRowId = rowCount();
128 double aCurrentX, aCurrentY;
129 int aSectionId, aPointId;
130 CurveCreator_ICurve::SectionToPoint aPoint;
131 for ( int i = 0; i < aRowId; i++ ) {
132 aCurrentX = item( i, 2 )->data( Qt::UserRole ).toDouble();
133 aCurrentY = item( i, 3 )->data( Qt::UserRole ).toDouble();
134 if ( fabs( aCurrentX - theX ) < LOCAL_SELECTION_TOLERANCE &&
135 fabs( aCurrentY - theY ) < LOCAL_SELECTION_TOLERANCE ) {
136 aPoint = std::make_pair<int, int>( getSectionId( i ), getPointId( i ) );
137 if ( !CurveCreator_UtilsICurve::contains( aSkipList, aPoint ) )
138 aSkipList.push_back( aPoint );
141 if ( aSkipList.size() == aPoints.size() )
144 QTableWidgetItem* anItem;
145 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints.begin(),
146 aLast = aPoints.end();
147 for ( ; anIt != aLast; anIt++ ) {
149 if ( CurveCreator_UtilsICurve::contains( aSkipList, aPoint ) )
152 setRowCount( aRowId+1 );
153 aSectionId = aPoint.first;
154 aPointId = aPoint.second;
156 anItem = new QTableWidgetItem( myCurve->getSectionName( aSectionId ).c_str() );
157 anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
158 anItem->setData( Qt::UserRole, aSectionId );
159 setItem( aRowId, 0, anItem );
161 anItem = new QTableWidgetItem( QString::number( aPointId + 1 ) );
162 anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
163 anItem->setData( Qt::UserRole, aPointId );
164 setItem( aRowId, 1, anItem );
166 anItem = new QTableWidgetItem( QString::number( theX ) );
167 anItem->setData( Qt::UserRole, theX );
168 setItem( aRowId, 2, anItem );
170 anItem = new QTableWidgetItem( QString::number( theY ) );
171 anItem->setData( Qt::UserRole, theY );
172 setItem( aRowId, 3, anItem );
176 int CurveCreator_TableView::getSectionId( const int theRowId ) const
178 return item( theRowId, 0 )->data( Qt::UserRole ).toInt();
182 * Returns a point index from the table
183 * \param theRowId a table row
185 int CurveCreator_TableView::getPointId( const int theRowId ) const
187 return item( theRowId, 1 )->data( Qt::UserRole ).toInt();