Salome HOME
OCC functionality moving out from the widget
[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 <QTableWidget>
24 #include <QTableWidgetItem>
25 //#include <QDoubleSpinBox>
26 #include <QtxDoubleSpinBox.h>
27
28 const double DBL_MINIMUM = -10000000.;
29 const double DBL_MAXIMUM = 10000000.;
30
31 const int SECTION_NAME_COLUMN_WIDTH = 75;
32 const int POINT_INDEX_COLUMN_WIDTH = 40;
33
34 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
35
36 CurveCreator_TableItemDelegate::CurveCreator_TableItemDelegate( QObject* theParent )
37 : QItemDelegate( theParent )
38 {
39 }
40
41 /**
42  * Creates an editor for the cell
43  */
44 QWidget* CurveCreator_TableItemDelegate::createEditor( QWidget* theParent,
45                                                        const QStyleOptionViewItem& theOption,
46                                                        const QModelIndex& theIndex ) const
47 {
48   QWidget* anEditor = 0;
49
50   int aColumnId = theIndex.column();
51   if ( aColumnId == 2 || aColumnId == 3 ) {
52     QDoubleSpinBox* aSpin = new QtxDoubleSpinBox( theParent );
53     aSpin->setDecimals( 6 );
54     aSpin->setRange( DBL_MINIMUM, DBL_MAXIMUM );
55     anEditor = aSpin;
56   }
57   else
58     anEditor = QItemDelegate::createEditor( theParent, theOption, theIndex );
59
60   return anEditor;
61 }
62
63 void CurveCreator_TableItemDelegate::setEditorData( QWidget* theEditor,
64                                                     const QModelIndex& theIndex ) const
65 {
66   QTableWidget* aTableWidget = dynamic_cast<QTableWidget*>( parent() );
67   QTableWidgetItem* anItem = aTableWidget ? dynamic_cast<QTableWidgetItem*>
68                                   ( aTableWidget->item( theIndex.row(), theIndex.column() ) ) : 0;
69   int aColumnId = theIndex.column();
70   if ( anItem && ( aColumnId == 2 || aColumnId == 3 ) ) {
71     QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
72     double aValue = anItem->data( Qt::UserRole ).toDouble();
73     if ( aDblSpin ) {
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   QTableWidget* aTableWidget = dynamic_cast<QTableWidget*>( parent() );
86   QTableWidgetItem* anItem = aTableWidget ? dynamic_cast<QTableWidgetItem*>
87                       ( aTableWidget->item( theIndex.row(), theIndex.column() ) ) : 0;
88
89   int aColumnId = theIndex.column();
90   if ( anItem && ( aColumnId == 2 || aColumnId == 3 ) ) {
91     QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
92     if ( aDblSpin ) {
93       double aValue = aDblSpin->value();
94       anItem->setData( Qt::UserRole, aValue );
95     }
96   }
97   else
98     QItemDelegate::setModelData( theEditor, theModel, theIndex );
99 }
100
101 CurveCreator_TableView::CurveCreator_TableView( CurveCreator_ICurve* theCurve, QWidget* theParent )
102 : QTableWidget( theParent ), myCurve( theCurve )
103 {
104   setItemDelegate( new CurveCreator_TableItemDelegate( this ) );
105   setVisible( false );
106   setColumnCount( 4 );
107   setColumnWidth( 0, SECTION_NAME_COLUMN_WIDTH );
108   setColumnWidth( 1, POINT_INDEX_COLUMN_WIDTH );
109   QStringList aLabels;
110   //aLabels << tr( "SECTION_LABEL" ) << tr( "IDENTIFIER_LABEL" ) << tr( "X_POSITION_LBL" ) << tr( "Y_POSITION_LBL" );
111   aLabels << tr( "Section" ) << "Index" << tr( "X" ) << tr( "Y" );
112   setHorizontalHeaderLabels( aLabels );
113 }
114
115 void CurveCreator_TableView::setCurve( CurveCreator_ICurve* theCurve )
116 {
117   myCurve = theCurve;
118 }
119
120 void CurveCreator_TableView::addLocalPointToTable( const double theX, const double theY )
121 {
122   CurveCreator_ICurve::SectionToPointList aPoints;
123   CurveCreator_UtilsICurve::findSectionsToPoints( myCurve, theX, theY, aPoints );
124
125   CurveCreator_ICurve::SectionToPointList aSkipList;
126   // table could not contain two equal value rows
127   int aRowId = rowCount();
128   double aCurrentX, aCurrentY;
129   int aSectionId, aPointId;
130   CurveCreator_ICurve::SectionToPoint aPoint;
131   for ( int i = 0; i < aRowId; i++ ) {
132     aCurrentX = item( i, 2 )->data( Qt::UserRole ).toDouble();
133     aCurrentY = item( i, 3 )->data( Qt::UserRole ).toDouble();
134     if ( fabs( aCurrentX - theX ) < LOCAL_SELECTION_TOLERANCE &&
135          fabs( aCurrentY - theY ) < LOCAL_SELECTION_TOLERANCE ) {
136       aPoint = std::make_pair<int, int>( getSectionId( i ), getPointId( i ) );
137       if ( !CurveCreator_UtilsICurve::contains( aSkipList, aPoint ) )
138         aSkipList.push_back( aPoint );
139     }
140   }
141   if ( aSkipList.size() == aPoints.size() )
142     return;
143
144   QTableWidgetItem* anItem;
145   CurveCreator_ICurve::SectionToPointList::const_iterator anIt = aPoints.begin(),
146                                                           aLast = aPoints.end();
147   for ( ; anIt != aLast; anIt++ ) {
148     aPoint = *anIt;
149     if ( CurveCreator_UtilsICurve::contains( aSkipList, aPoint ) )
150       continue;
151
152     setRowCount( aRowId+1 );
153     aSectionId = aPoint.first;
154     aPointId = aPoint.second;
155
156     anItem = new QTableWidgetItem( myCurve->getSectionName( aSectionId ).c_str() );
157     anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
158     anItem->setData( Qt::UserRole, aSectionId );
159     setItem( aRowId, 0, anItem );
160
161     anItem = new QTableWidgetItem( QString::number( aPointId + 1 ) );
162     anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
163     anItem->setData( Qt::UserRole, aPointId );
164     setItem( aRowId, 1, anItem );
165
166     anItem = new QTableWidgetItem( QString::number( theX ) );
167     anItem->setData( Qt::UserRole, theX );
168     setItem( aRowId, 2, anItem );
169
170     anItem = new QTableWidgetItem( QString::number( theY ) );
171     anItem->setData( Qt::UserRole, theY );
172     setItem( aRowId, 3, anItem );
173   }
174 }
175
176 int CurveCreator_TableView::getSectionId( const int theRowId ) const
177 {
178   return item( theRowId, 0 )->data( Qt::UserRole ).toInt();
179 }
180
181 /**
182  * Returns a point index from the table
183  * \param theRowId a table row
184  */
185 int CurveCreator_TableView::getPointId( const int theRowId ) const
186 {
187   return item( theRowId, 1 )->data( Qt::UserRole ).toInt();
188 }