Salome HOME
Save for a list of the local selected points.
[modules/hydro.git] / src / HYDROCurveCreator / CurveCreator_TableView.cxx
1 // Copyright (C) 2013  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.
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
28 #include <QtxDoubleSpinBox.h>
29
30 const double DBL_MINIMUM = -10000000.;
31 const double DBL_MAXIMUM = 10000000.;
32
33 const int SECTION_NAME_COLUMN_WIDTH = 75;
34 const int POINT_INDEX_COLUMN_WIDTH = 40;
35
36 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
37
38 CurveCreator_TableItemDelegate::CurveCreator_TableItemDelegate( QObject* theParent )
39 : QItemDelegate( theParent )
40 {
41 }
42
43 /**
44  * Creates an editor for the cell
45  */
46 QWidget* CurveCreator_TableItemDelegate::createEditor( QWidget* theParent,
47                                                        const QStyleOptionViewItem& theOption,
48                                                        const QModelIndex& theIndex ) const
49 {
50   QWidget* anEditor = 0;
51
52   int aColumnId = theIndex.column();
53   if ( aColumnId == 2 || aColumnId == 3 ) {
54     QDoubleSpinBox* aSpin = new QtxDoubleSpinBox( theParent );
55     aSpin->setDecimals( 2 );
56     aSpin->setRange( DBL_MINIMUM, DBL_MAXIMUM );
57     anEditor = aSpin;
58   }
59   else
60     anEditor = QItemDelegate::createEditor( theParent, theOption, theIndex );
61
62   return anEditor;
63 }
64
65 void CurveCreator_TableItemDelegate::setEditorData( QWidget* theEditor,
66                                                     const QModelIndex& theIndex ) const
67 {
68   int aColumnId = theIndex.column();
69   if ( aColumnId == 2 || aColumnId == 3 ) {
70     QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
71     if ( aDblSpin ) {
72       double aValue = theIndex.model()->data( theIndex, Qt::EditRole ).toDouble();
73       aDblSpin->setValue( aValue );
74     }
75   }
76   else
77    QItemDelegate::setEditorData( theEditor, theIndex );
78 }
79
80 void CurveCreator_TableItemDelegate::setModelData( QWidget* theEditor,
81                                                    QAbstractItemModel* theModel,
82                                                    const QModelIndex& theIndex ) const
83 {
84   int aColumnId = theIndex.column();
85   if ( aColumnId == 2 || aColumnId == 3 ) {
86     QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
87     if ( aDblSpin ) {
88       double aValue = aDblSpin->value();
89       theModel->setData( theIndex, aValue, Qt::UserRole);
90     }
91   }
92   else
93     QItemDelegate::setModelData( theEditor, theModel, theIndex );
94 }
95
96 CurveCreator_TableView::CurveCreator_TableView( CurveCreator_ICurve* theCurve, QWidget* theParent )
97 : QTableWidget( theParent ), myCurve( theCurve )
98 {
99   setItemDelegate( new CurveCreator_TableItemDelegate( this ) );
100   setVisible( false );
101   setColumnCount( 4 );
102   setColumnWidth( 0, SECTION_NAME_COLUMN_WIDTH );
103   setColumnWidth( 1, POINT_INDEX_COLUMN_WIDTH );
104   QStringList aLabels;
105   //aLabels << tr( "SECTION_LABEL" ) << tr( "IDENTIFIER_LABEL" ) << tr( "X_POSITION_LBL" ) << tr( "Y_POSITION_LBL" );
106   aLabels << tr( "Section" ) << "Index" << tr( "X" ) << tr( "Y" );
107   setHorizontalHeaderLabels( aLabels );
108 }
109
110 void CurveCreator_TableView::setCurve( CurveCreator_ICurve* theCurve )
111 {
112   myCurve = theCurve;
113 }
114
115 void CurveCreator_TableView::addLocalPointToTable(
116                      const CurveCreator_ICurve::SectionToPoint& theSToPoint )
117 {
118   int aRowId = rowCount();
119   int anISection = theSToPoint.first;
120   int anIPoint = theSToPoint.second;
121   bool isFoundPoint = false;
122   for ( int i = 0; i < aRowId && !isFoundPoint; i++ )
123     isFoundPoint = getSectionId( i ) == anISection && 
124                    getPointId( i ) == anIPoint;
125   if ( isFoundPoint )
126     return;
127
128   setRowCount( aRowId+1 );
129
130   QTableWidgetItem* anItem;
131   anItem = new QTableWidgetItem( myCurve->getSectionName( anISection ).c_str() );
132   anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
133   anItem->setData( Qt::UserRole, anISection );
134   setItem( aRowId, 0, anItem );
135
136   anItem = new QTableWidgetItem( QString::number( anIPoint + 1 ) );
137   anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
138   anItem->setData( Qt::UserRole, anIPoint );
139   setItem( aRowId, 1, anItem );
140
141   gp_Pnt aPoint;
142   CurveCreator_UtilsICurve::getPoint( myCurve, anISection, anIPoint, aPoint );
143
144   anItem = new QTableWidgetItem( QString::number( aPoint.X(), 'f', 2 ) );
145   anItem->setData( Qt::UserRole, aPoint.X() );
146   setItem( aRowId, 2, anItem );
147
148   anItem = new QTableWidgetItem( QString::number( aPoint.Y(), 'f', 2 ) );
149   anItem->setData( Qt::UserRole, aPoint.Y() );
150   setItem( aRowId, 3, anItem );
151 }
152
153 int CurveCreator_TableView::getSectionId( const int theRowId ) const
154 {
155   return item( theRowId, 0 )->data( Qt::UserRole ).toInt();
156 }
157
158 /**
159  * Returns a point index from the table
160  * \param theRowId a table row
161  */
162 int CurveCreator_TableView::getPointId( const int theRowId ) const
163 {
164   return item( theRowId, 1 )->data( Qt::UserRole ).toInt();
165 }