1 // Copyright (C) 2013-2020 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"
22 #include "CurveCreator_Widget.h"
26 #include <QTableWidget>
27 #include <QTableWidgetItem>
28 #include <QHeaderView>
30 #include <QtxDoubleSpinBox.h>
32 const double DBL_MINIMUM = -10000000.;
33 const double DBL_MAXIMUM = 10000000.;
35 const int SECTION_NAME_COLUMN_WIDTH = 75;
36 const int POINT_INDEX_COLUMN_WIDTH = 40;
38 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
40 CurveCreator_TableItemDelegate::CurveCreator_TableItemDelegate( QObject* theParent )
41 : QItemDelegate( theParent )
46 * Creates an editor for the cell
48 QWidget* CurveCreator_TableItemDelegate::createEditor( QWidget* theParent,
49 const QStyleOptionViewItem& theOption,
50 const QModelIndex& theIndex ) const
52 QWidget* anEditor = 0;
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 );
62 anEditor = QItemDelegate::createEditor( theParent, theOption, theIndex );
67 void CurveCreator_TableItemDelegate::setEditorData( QWidget* theEditor,
68 const QModelIndex& theIndex ) const
70 int aColumnId = theIndex.column();
71 if ( aColumnId == 2 || aColumnId == 3 ) {
72 QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
74 double aValue = theIndex.model()->data( theIndex, Qt::EditRole ).toDouble();
75 aDblSpin->setValue( aValue );
79 QItemDelegate::setEditorData( theEditor, theIndex );
82 void CurveCreator_TableItemDelegate::setModelData( QWidget* theEditor,
83 QAbstractItemModel* theModel,
84 const QModelIndex& theIndex ) const
86 int aColumnId = theIndex.column();
87 if ( aColumnId == 2 || aColumnId == 3 ) {
88 QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
90 double aValue = aDblSpin->value();
91 theModel->setData( theIndex, aValue, Qt::UserRole);
95 QItemDelegate::setModelData( theEditor, theModel, theIndex );
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 )
107 setItemDelegate( new CurveCreator_TableItemDelegate( this ) );
110 setColumnWidth( 0, SECTION_NAME_COLUMN_WIDTH );
111 setColumnWidth( 1, POINT_INDEX_COLUMN_WIDTH );
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 );
120 connect( horizontalHeader(), SIGNAL( sectionClicked( int ) ), this, SLOT( OnHeaderClick( int ) ) );
123 void CurveCreator_TableView::setCurve( CurveCreator_ICurve* theCurve )
128 void CurveCreator_TableView::setLocalPointsToTable(
129 const CurveCreator_ICurve::SectionToPointList& thePoints )
131 setRowCount( thePoints.size() );
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;
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 );
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 );
156 CurveCreator_UtilsICurve::getPoint( myCurve, anISection, anIPoint, aPoint );
158 anItem = item( aRowId, 2 );
160 anItem = new QTableWidgetItem();
161 setItem( aRowId, 2, anItem );
163 anItem->setData( Qt::UserRole, aPoint.X() );
164 anItem->setData( Qt::DisplayRole, QString::number( aPoint.X(), 'f', 3 ).toDouble() );
166 anItem = item( aRowId, 3 );
168 anItem = new QTableWidgetItem();
169 setItem( aRowId, 3, anItem );
171 anItem->setData( Qt::UserRole, aPoint.Y() );
172 anItem->setData( Qt::DisplayRole, QString::number( aPoint.Y(), 'f', 3 ).toDouble() );
174 anItem = item( aRowId, 4 );
176 anItem = new QTableWidgetItem();
177 setItem( aRowId, 4, anItem );
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() );
191 int CurveCreator_TableView::getSectionId( const int theRowId ) const
193 return item( theRowId, 0 )->data( Qt::UserRole ).toInt();
197 * Returns a point index from the table
198 * \param theRowId a table row
200 int CurveCreator_TableView::getPointId( const int theRowId ) const
202 return item( theRowId, 1 )->data( Qt::UserRole ).toInt();
205 void CurveCreator_TableView::OnHeaderClick( int theLogicalId )
207 if( theLogicalId == myCurrentSortId )
209 if( myCurrentSortOrder == Qt::AscendingOrder )
210 myCurrentSortOrder = Qt::DescendingOrder;
212 myCurrentSortOrder = Qt::AscendingOrder;
215 sortByColumn( theLogicalId, myCurrentSortOrder );
217 CurveCreator_ICurve::SectionToPointList selected;
218 for( int r=0, n=rowCount(); r<n; r++ )
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 ) );
224 myWidget->setSelectedPoints( selected );
226 myCurrentSortId = theLogicalId;