Salome HOME
Initial version of interface 'CurveCreator_ISection' was created for the curve sections.
[modules/geom.git] / src / CurveCreator / CurveCreator_TableView.cxx
1 // Copyright (C) 2013-2015  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
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,
97                                                 QWidget* theParent,
98                                                 const QStringList& theCoordTitles )
99 : QTableWidget( theParent ), myCurve( theCurve )
100 {
101   setItemDelegate( new CurveCreator_TableItemDelegate( this ) );
102   setVisible( false );
103   setColumnCount( 4 );
104   setColumnWidth( 0, SECTION_NAME_COLUMN_WIDTH );
105   setColumnWidth( 1, POINT_INDEX_COLUMN_WIDTH );
106   QStringList aLabels;
107   QString aCoord1 = theCoordTitles.size() > 0 ? theCoordTitles[0] : tr( "TABLE_X" ); // tr( "X_POSITION_LBL" )
108   QString aCoord2 = theCoordTitles.size() > 1 ? theCoordTitles[1] : tr( "TABLE_Y" ); // tr( "Y_POSITION_LBL" )
109   //aLabels << tr( "SECTION_LABEL" ) << tr( "IDENTIFIER_LABEL" ) << aCoord1 << aCoord2;
110   aLabels << tr( "TABLE_SECTION" ) << tr("TABLE_INDEX") << aCoord1 << aCoord2;
111   setHorizontalHeaderLabels( aLabels );
112 }
113
114 void CurveCreator_TableView::setCurve( CurveCreator_ICurve* theCurve )
115 {
116   myCurve = theCurve;
117 }
118
119 void CurveCreator_TableView::setLocalPointsToTable(
120   const CurveCreator_ICurve::SectionToPointList& thePoints )
121 {
122   setRowCount( thePoints.size() );
123
124   int aRowId = 0;
125   CurveCreator_ICurve::SectionToPointList::const_iterator anIt = thePoints.begin(),
126                                                           aLast = thePoints.end();
127   for ( ; anIt != aLast; anIt++ ) {
128     CurveCreator_ICurve::SectionToPoint aSPoint = *anIt;
129     int anISection = aSPoint.first;
130     int anIPoint = aSPoint.second;
131
132     QTableWidgetItem* anItem;
133     anItem = new QTableWidgetItem( myCurve->getSectionName( anISection ).c_str() );
134     anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
135     anItem->setData( Qt::UserRole, anISection );
136     setItem( aRowId, 0, anItem );
137
138     anItem = new QTableWidgetItem( QString::number( anIPoint + 1 ) );
139     anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
140     anItem->setData( Qt::UserRole, anIPoint );
141     setItem( aRowId, 1, anItem );
142
143     gp_Pnt aPoint;
144     CurveCreator_UtilsICurve::getPoint( myCurve, anISection, anIPoint, aPoint );
145
146     anItem = item( aRowId, 2 );
147     if ( !anItem ) {
148       anItem = new QTableWidgetItem();
149       setItem( aRowId, 2, anItem );
150     }
151     anItem->setData( Qt::UserRole, aPoint.X() );
152     anItem->setData( Qt::DisplayRole, QString::number( aPoint.X(), 'f', 2 ) );
153
154     anItem = item( aRowId, 3 );
155     if ( !anItem ) {
156       anItem = new QTableWidgetItem();
157       setItem( aRowId, 3, anItem );
158     }
159     anItem->setData( Qt::UserRole, aPoint.Y() );
160     anItem->setData( Qt::DisplayRole, QString::number( aPoint.Y(), 'f', 2 ) );
161
162     aRowId++;
163   }
164 }
165
166 int CurveCreator_TableView::getSectionId( const int theRowId ) const
167 {
168   return item( theRowId, 0 )->data( Qt::UserRole ).toInt();
169 }
170
171 /**
172  * Returns a point index from the table
173  * \param theRowId a table row
174  */
175 int CurveCreator_TableView::getPointId( const int theRowId ) const
176 {
177   return item( theRowId, 1 )->data( Qt::UserRole ).toInt();
178 }