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