1 // Copyright (C) 2013-2016 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, or (at your option) any later version.
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>
27 #include <QHeaderView>
29 #include <QtxDoubleSpinBox.h>
31 const double DBL_MINIMUM = -10000000.;
32 const double DBL_MAXIMUM = 10000000.;
34 const int SECTION_NAME_COLUMN_WIDTH = 75;
35 const int POINT_INDEX_COLUMN_WIDTH = 40;
37 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
39 CurveCreator_TableItemDelegate::CurveCreator_TableItemDelegate( QObject* theParent )
40 : QItemDelegate( theParent )
45 * Creates an editor for the cell
47 QWidget* CurveCreator_TableItemDelegate::createEditor( QWidget* theParent,
48 const QStyleOptionViewItem& theOption,
49 const QModelIndex& theIndex ) const
51 QWidget* anEditor = 0;
53 int aColumnId = theIndex.column();
54 if ( aColumnId == 2 || aColumnId == 3 ) {
55 QDoubleSpinBox* aSpin = new QtxDoubleSpinBox( theParent );
56 aSpin->setDecimals( 2 );
57 aSpin->setRange( DBL_MINIMUM, DBL_MAXIMUM );
61 anEditor = QItemDelegate::createEditor( theParent, theOption, theIndex );
66 void CurveCreator_TableItemDelegate::setEditorData( QWidget* theEditor,
67 const QModelIndex& theIndex ) const
69 int aColumnId = theIndex.column();
70 if ( aColumnId == 2 || aColumnId == 3 ) {
71 QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
73 double aValue = theIndex.model()->data( theIndex, Qt::EditRole ).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 int aColumnId = theIndex.column();
86 if ( aColumnId == 2 || aColumnId == 3 ) {
87 QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
89 double aValue = aDblSpin->value();
90 theModel->setData( theIndex, aValue, Qt::UserRole);
94 QItemDelegate::setModelData( theEditor, theModel, theIndex );
97 CurveCreator_TableView::CurveCreator_TableView( CurveCreator_ICurve* theCurve,
99 const QStringList& theCoordTitles )
100 : QTableWidget( theParent ), myCurve( theCurve )
102 setItemDelegate( new CurveCreator_TableItemDelegate( this ) );
105 setColumnWidth( 0, SECTION_NAME_COLUMN_WIDTH );
106 setColumnWidth( 1, POINT_INDEX_COLUMN_WIDTH );
108 QString aCoord1 = theCoordTitles.size() > 0 ? theCoordTitles[0] : tr( "TABLE_X" ); // tr( "X_POSITION_LBL" )
109 QString aCoord2 = theCoordTitles.size() > 1 ? theCoordTitles[1] : tr( "TABLE_Y" ); // tr( "Y_POSITION_LBL" )
110 QString aDistance = theCoordTitles.size() > 2 ? theCoordTitles[2] : tr( "DISTANCE_PREV" );
111 //aLabels << tr( "SECTION_LABEL" ) << tr( "IDENTIFIER_LABEL" ) << aCoord1 << aCoord2;
112 aLabels << tr( "TABLE_SECTION" ) << tr("TABLE_INDEX") << aCoord1 << aCoord2 << aDistance;
113 setHorizontalHeaderLabels( aLabels );
115 connect( horizontalHeader(), SIGNAL( sectionClicked( int ) ), this, SLOT( OnHeaderClick( int ) ) );
118 void CurveCreator_TableView::setCurve( CurveCreator_ICurve* theCurve )
123 void CurveCreator_TableView::setLocalPointsToTable(
124 const CurveCreator_ICurve::SectionToPointList& thePoints )
126 setRowCount( thePoints.size() );
131 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = thePoints.begin(),
132 aLast = thePoints.end();
133 for ( ; anIt != aLast; anIt++ ) {
134 CurveCreator_ICurve::SectionToPoint aSPoint = *anIt;
135 int anISection = aSPoint.first;
136 int anIPoint = aSPoint.second;
138 QTableWidgetItem* anItem;
139 anItem = new QTableWidgetItem( myCurve->getSectionName( anISection ).c_str() );
140 anItem->setFlags( anItem->flags() & ~Qt::ItemIsEditable );
141 anItem->setData( Qt::UserRole, anISection );
142 setItem( aRowId, 0, anItem );
144 anItem = new QTableWidgetItem( QString::number( anIPoint + 1 ) );
145 anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
146 anItem->setData( Qt::UserRole, anIPoint );
147 anItem->setData( Qt::DisplayRole, anIPoint );
148 setItem( aRowId, 1, anItem );
151 CurveCreator_UtilsICurve::getPoint( myCurve, anISection, anIPoint, aPoint );
153 anItem = item( aRowId, 2 );
155 anItem = new QTableWidgetItem();
156 setItem( aRowId, 2, anItem );
158 anItem->setData( Qt::UserRole, aPoint.X() );
159 anItem->setData( Qt::DisplayRole, QString::number( aPoint.X(), 'f', 3 ).toDouble() );
161 anItem = item( aRowId, 3 );
163 anItem = new QTableWidgetItem();
164 setItem( aRowId, 3, anItem );
166 anItem->setData( Qt::UserRole, aPoint.Y() );
167 anItem->setData( Qt::DisplayRole, QString::number( aPoint.Y(), 'f', 3 ).toDouble() );
169 anItem = item( aRowId, 4 );
171 anItem = new QTableWidgetItem();
172 setItem( aRowId, 4, anItem );
176 d=sqrt((aPoint.X()-prevX)* (aPoint.X()-prevX) + (aPoint.Y()-prevY)* (aPoint.Y()-prevY));
177 anItem->setData( Qt::UserRole, d );
178 anItem->setData( Qt::DisplayRole, QString::number( d, 'f', 6 ).toDouble() );
186 int CurveCreator_TableView::getSectionId( const int theRowId ) const
188 return item( theRowId, 0 )->data( Qt::UserRole ).toInt();
192 * Returns a point index from the table
193 * \param theRowId a table row
195 int CurveCreator_TableView::getPointId( const int theRowId ) const
197 return item( theRowId, 1 )->data( Qt::UserRole ).toInt();
200 void CurveCreator_TableView::OnHeaderClick( int theLogicalId )
202 sortByColumn( theLogicalId, Qt::AscendingOrder );