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