Salome HOME
6cd0b35ae4d2a4f331ccef53b748b9cc87b52f7c
[modules/geom.git] / src / CurveCreator / CurveCreator_TableView.cxx
1 // Copyright (C) 2013-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "CurveCreator_TableView.h"
21 #include "CurveCreator_UtilsICurve.hxx"
22
23 #include <gp_Pnt.hxx>
24
25 #include <QTableWidget>
26 #include <QTableWidgetItem>
27 #include <QHeaderView>
28
29 #include <QtxDoubleSpinBox.h>
30
31 const double DBL_MINIMUM = -10000000.;
32 const double DBL_MAXIMUM = 10000000.;
33
34 const int SECTION_NAME_COLUMN_WIDTH = 75;
35 const int POINT_INDEX_COLUMN_WIDTH = 40;
36
37 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
38
39 CurveCreator_TableItemDelegate::CurveCreator_TableItemDelegate( QObject* theParent )
40 : QItemDelegate( theParent )
41 {
42 }
43
44 /**
45  * Creates an editor for the cell
46  */
47 QWidget* CurveCreator_TableItemDelegate::createEditor( QWidget* theParent,
48                                                        const QStyleOptionViewItem& theOption,
49                                                        const QModelIndex& theIndex ) const
50 {
51   QWidget* anEditor = 0;
52
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 );
58     anEditor = aSpin;
59   }
60   else
61     anEditor = QItemDelegate::createEditor( theParent, theOption, theIndex );
62
63   return anEditor;
64 }
65
66 void CurveCreator_TableItemDelegate::setEditorData( QWidget* theEditor,
67                                                     const QModelIndex& theIndex ) const
68 {
69   int aColumnId = theIndex.column();
70   if ( aColumnId == 2 || aColumnId == 3 ) {
71     QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
72     if ( aDblSpin ) {
73       double aValue = theIndex.model()->data( theIndex, Qt::EditRole ).toDouble();
74       aDblSpin->setValue( aValue );
75     }
76   }
77   else
78    QItemDelegate::setEditorData( theEditor, theIndex );
79 }
80
81 void CurveCreator_TableItemDelegate::setModelData( QWidget* theEditor,
82                                                    QAbstractItemModel* theModel,
83                                                    const QModelIndex& theIndex ) const
84 {
85   int aColumnId = theIndex.column();
86   if ( aColumnId == 2 || aColumnId == 3 ) {
87     QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
88     if ( aDblSpin ) {
89       double aValue = aDblSpin->value();
90       theModel->setData( theIndex, aValue, Qt::UserRole);
91     }
92   }
93   else
94     QItemDelegate::setModelData( theEditor, theModel, theIndex );
95 }
96
97
98
99
100 CurveCreator_TableView::CurveCreator_TableView( CurveCreator_ICurve* theCurve,
101                                                 QWidget* theParent,
102                                                 const QStringList& theCoordTitles )
103   : QTableWidget( theParent ), myCurve( theCurve ), myCurrentSortId( -1 ), myCurrentSortOrder( Qt::AscendingOrder )
104 {
105   setItemDelegate( new CurveCreator_TableItemDelegate( this ) );
106   setVisible( false );
107   setColumnCount( 5 );
108   setColumnWidth( 0, SECTION_NAME_COLUMN_WIDTH );
109   setColumnWidth( 1, POINT_INDEX_COLUMN_WIDTH );
110   QStringList aLabels;
111   QString aCoord1 = theCoordTitles.size() > 0 ? theCoordTitles[0] : tr( "TABLE_X" ); // tr( "X_POSITION_LBL" )
112   QString aCoord2 = theCoordTitles.size() > 1 ? theCoordTitles[1] : tr( "TABLE_Y" ); // tr( "Y_POSITION_LBL" )
113   QString aDistance = theCoordTitles.size() > 2 ? theCoordTitles[2] : tr( "DISTANCE_PREV" );
114   //aLabels << tr( "SECTION_LABEL" ) << tr( "IDENTIFIER_LABEL" ) << aCoord1 << aCoord2;
115   aLabels << tr( "TABLE_SECTION" ) << tr("TABLE_INDEX") << aCoord1 << aCoord2 << aDistance;
116   setHorizontalHeaderLabels( aLabels );
117
118   connect( horizontalHeader(), SIGNAL( sectionClicked( int ) ), this, SLOT( OnHeaderClick( int ) ) );
119 }
120
121 void CurveCreator_TableView::setCurve( CurveCreator_ICurve* theCurve )
122 {
123   myCurve = theCurve;
124 }
125
126 void CurveCreator_TableView::setLocalPointsToTable(
127   const CurveCreator_ICurve::SectionToPointList& thePoints )
128 {
129   setRowCount( thePoints.size() );
130
131   int aRowId = 0;
132   double prevX=0;
133   double prevY=0;
134   CurveCreator_ICurve::SectionToPointList::const_iterator anIt = thePoints.begin(),
135                                                           aLast = thePoints.end();
136   for ( ; anIt != aLast; anIt++ ) {
137     CurveCreator_ICurve::SectionToPoint aSPoint = *anIt;
138     int anISection = aSPoint.first;
139     int anIPoint = aSPoint.second;
140
141     QTableWidgetItem* anItem;
142     anItem = new QTableWidgetItem( myCurve->getSectionName( anISection ).c_str() );
143     anItem->setFlags( anItem->flags() & ~Qt::ItemIsEditable );
144     anItem->setData( Qt::UserRole, anISection );
145     setItem( aRowId, 0, anItem );
146
147     anItem = new QTableWidgetItem( QString::number( anIPoint + 1 ) );
148     anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
149     anItem->setData( Qt::UserRole, anIPoint );
150     anItem->setData( Qt::DisplayRole, anIPoint );
151     setItem( aRowId, 1, anItem );
152
153     gp_Pnt aPoint;
154     CurveCreator_UtilsICurve::getPoint( myCurve, anISection, anIPoint, aPoint );
155
156     anItem = item( aRowId, 2 );
157     if ( !anItem ) {
158       anItem = new QTableWidgetItem();
159       setItem( aRowId, 2, anItem );
160     }
161     anItem->setData( Qt::UserRole, aPoint.X() );
162     anItem->setData( Qt::DisplayRole, QString::number( aPoint.X(), 'f', 3 ).toDouble() );
163
164     anItem = item( aRowId, 3 );
165     if ( !anItem ) {
166       anItem = new QTableWidgetItem();
167       setItem( aRowId, 3, anItem );
168     }
169     anItem->setData( Qt::UserRole, aPoint.Y() );
170     anItem->setData( Qt::DisplayRole, QString::number( aPoint.Y(), 'f', 3 ).toDouble() );
171
172     anItem = item( aRowId, 4 );
173     if ( !anItem ) {
174       anItem = new QTableWidgetItem();
175       setItem( aRowId, 4, anItem );
176     }
177     double d=0;
178     if (aRowId>0)
179       d=sqrt((aPoint.X()-prevX)* (aPoint.X()-prevX) + (aPoint.Y()-prevY)* (aPoint.Y()-prevY));
180     anItem->setData( Qt::UserRole, d );
181     anItem->setData( Qt::DisplayRole, QString::number( d, 'f', 6 ).toDouble() );
182     prevX = aPoint.X();
183     prevY = aPoint.Y();
184
185     aRowId++;
186   }
187 }
188
189 int CurveCreator_TableView::getSectionId( const int theRowId ) const
190 {
191   return item( theRowId, 0 )->data( Qt::UserRole ).toInt();
192 }
193
194 /**
195  * Returns a point index from the table
196  * \param theRowId a table row
197  */
198 int CurveCreator_TableView::getPointId( const int theRowId ) const
199 {
200   return item( theRowId, 1 )->data( Qt::UserRole ).toInt();
201 }
202
203 void CurveCreator_TableView::OnHeaderClick( int theLogicalId )
204 {
205   if( theLogicalId == myCurrentSortId )
206     if( myCurrentSortOrder == Qt::AscendingOrder )
207       myCurrentSortOrder = Qt::DescendingOrder;
208     else
209       myCurrentSortOrder = Qt::AscendingOrder;
210
211   sortByColumn( theLogicalId, myCurrentSortOrder );
212   myCurrentSortId = theLogicalId;
213 }