Salome HOME
Refs #289 - Spline profile is represented in OCC view as polyline profile
[modules/hydro.git] / src / HYDROCurveCreator / CurveCreator_TableView.cxx
index 994992214e6e689a78bc1d074ad11a864e9862fd..56d3a507e9f7cbcf01a4a80ad54bf042068aebbc 100644 (file)
 //
 
 #include "CurveCreator_TableView.h"
+#include "CurveCreator_UtilsICurve.hxx"
+
+#include <gp_Pnt.hxx>
 
 #include <QTableWidget>
 #include <QTableWidgetItem>
-//#include <QDoubleSpinBox>
+
 #include <QtxDoubleSpinBox.h>
 
 const double DBL_MINIMUM = -10000000.;
 const double DBL_MAXIMUM = 10000000.;
 
+const int SECTION_NAME_COLUMN_WIDTH = 75;
+const int POINT_INDEX_COLUMN_WIDTH = 40;
+
+const double LOCAL_SELECTION_TOLERANCE = 0.0001;
+
 CurveCreator_TableItemDelegate::CurveCreator_TableItemDelegate( QObject* theParent )
 : QItemDelegate( theParent )
 {
@@ -44,7 +52,7 @@ QWidget* CurveCreator_TableItemDelegate::createEditor( QWidget* theParent,
   int aColumnId = theIndex.column();
   if ( aColumnId == 2 || aColumnId == 3 ) {
     QDoubleSpinBox* aSpin = new QtxDoubleSpinBox( theParent );
-    aSpin->setDecimals( 6 );
+    aSpin->setDecimals( 2 );
     aSpin->setRange( DBL_MINIMUM, DBL_MAXIMUM );
     anEditor = aSpin;
   }
@@ -57,14 +65,11 @@ QWidget* CurveCreator_TableItemDelegate::createEditor( QWidget* theParent,
 void CurveCreator_TableItemDelegate::setEditorData( QWidget* theEditor,
                                                     const QModelIndex& theIndex ) const
 {
-  QTableWidget* aTableWidget = dynamic_cast<QTableWidget*>( parent() );
-  QTableWidgetItem* anItem = aTableWidget ? dynamic_cast<QTableWidgetItem*>
-                                  ( aTableWidget->item( theIndex.row(), theIndex.column() ) ) : 0;
   int aColumnId = theIndex.column();
-  if ( anItem && ( aColumnId == 2 || aColumnId == 3 ) ) {
+  if ( aColumnId == 2 || aColumnId == 3 ) {
     QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
-    double aValue = anItem->data( Qt::UserRole ).toDouble();
     if ( aDblSpin ) {
+      double aValue = theIndex.model()->data( theIndex, Qt::EditRole ).toDouble();
       aDblSpin->setValue( aValue );
     }
   }
@@ -76,19 +81,94 @@ void CurveCreator_TableItemDelegate::setModelData( QWidget* theEditor,
                                                    QAbstractItemModel* theModel,
                                                    const QModelIndex& theIndex ) const
 {
-  QTableWidget* aTableWidget = dynamic_cast<QTableWidget*>( parent() );
-  QTableWidgetItem* anItem = aTableWidget ? dynamic_cast<QTableWidgetItem*>
-                      ( aTableWidget->item( theIndex.row(), theIndex.column() ) ) : 0;
-
   int aColumnId = theIndex.column();
-  if ( anItem && ( aColumnId == 2 || aColumnId == 3 ) ) {
+  if ( aColumnId == 2 || aColumnId == 3 ) {
     QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
     if ( aDblSpin ) {
       double aValue = aDblSpin->value();
-      anItem->setData( Qt::UserRole, aValue );
+      theModel->setData( theIndex, aValue, Qt::UserRole);
     }
   }
   else
     QItemDelegate::setModelData( theEditor, theModel, theIndex );
 }
 
+CurveCreator_TableView::CurveCreator_TableView( CurveCreator_ICurve* theCurve, QWidget* theParent )
+: QTableWidget( theParent ), myCurve( theCurve )
+{
+  setItemDelegate( new CurveCreator_TableItemDelegate( this ) );
+  setVisible( false );
+  setColumnCount( 4 );
+  setColumnWidth( 0, SECTION_NAME_COLUMN_WIDTH );
+  setColumnWidth( 1, POINT_INDEX_COLUMN_WIDTH );
+  QStringList aLabels;
+  //aLabels << tr( "SECTION_LABEL" ) << tr( "IDENTIFIER_LABEL" ) << tr( "X_POSITION_LBL" ) << tr( "Y_POSITION_LBL" );
+  aLabels << tr( "Section" ) << "Index" << tr( "X" ) << tr( "Y" );
+  setHorizontalHeaderLabels( aLabels );
+}
+
+void CurveCreator_TableView::setCurve( CurveCreator_ICurve* theCurve )
+{
+  myCurve = theCurve;
+}
+
+void CurveCreator_TableView::setLocalPointsToTable(
+  const CurveCreator_ICurve::SectionToPointList& thePoints )
+{
+  setRowCount( thePoints.size() );
+
+  int aRowId = 0;
+  CurveCreator_ICurve::SectionToPointList::const_iterator anIt = thePoints.begin(),
+                                                          aLast = thePoints.end();
+  for ( ; anIt != aLast; anIt++ ) {
+    CurveCreator_ICurve::SectionToPoint aSPoint = *anIt;
+    int anISection = aSPoint.first;
+    int anIPoint = aSPoint.second;
+
+    QTableWidgetItem* anItem;
+    anItem = new QTableWidgetItem( myCurve->getSectionName( anISection ).c_str() );
+    anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
+    anItem->setData( Qt::UserRole, anISection );
+    setItem( aRowId, 0, anItem );
+
+    anItem = new QTableWidgetItem( QString::number( anIPoint + 1 ) );
+    anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
+    anItem->setData( Qt::UserRole, anIPoint );
+    setItem( aRowId, 1, anItem );
+
+    gp_Pnt aPoint;
+    CurveCreator_UtilsICurve::getPoint( myCurve, anISection, anIPoint, aPoint );
+
+    anItem = item( aRowId, 2 );
+    if ( !anItem ) {
+      anItem = new QTableWidgetItem();
+      setItem( aRowId, 2, anItem );
+    }
+    anItem->setData( Qt::UserRole, aPoint.X() );
+    anItem->setData( Qt::DisplayRole, QString::number( aPoint.X(), 'f', 2 ) );
+
+    anItem = item( aRowId, 3 );
+    if ( !anItem ) {
+      anItem = new QTableWidgetItem();
+      setItem( aRowId, 3, anItem );
+    }
+    anItem->setData( Qt::UserRole, aPoint.Y() );
+    anItem->setData( Qt::DisplayRole, QString::number( aPoint.Y(), 'f', 2 ) );
+
+    aRowId++;
+  }
+}
+
+int CurveCreator_TableView::getSectionId( const int theRowId ) const
+{
+  return item( theRowId, 0 )->data( Qt::UserRole ).toInt();
+}
+
+/**
+ * Returns a point index from the table
+ * \param theRowId a table row
+ */
+int CurveCreator_TableView::getPointId( const int theRowId ) const
+{
+  return item( theRowId, 1 )->data( Qt::UserRole ).toInt();
+}