1 // Copyright (C) 2013 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "CurveCreator_TableView.h"
21 #include "CurveCreator_UtilsICurve.hxx"
25 #include <QTableWidget>
26 #include <QTableWidgetItem>
28 #include <QtxDoubleSpinBox.h>
30 const double DBL_MINIMUM = -10000000.;
31 const double DBL_MAXIMUM = 10000000.;
33 const int SECTION_NAME_COLUMN_WIDTH = 75;
34 const int POINT_INDEX_COLUMN_WIDTH = 40;
36 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
38 CurveCreator_TableItemDelegate::CurveCreator_TableItemDelegate( QObject* theParent )
39 : QItemDelegate( theParent )
44 * Creates an editor for the cell
46 QWidget* CurveCreator_TableItemDelegate::createEditor( QWidget* theParent,
47 const QStyleOptionViewItem& theOption,
48 const QModelIndex& theIndex ) const
50 QWidget* anEditor = 0;
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 );
60 anEditor = QItemDelegate::createEditor( theParent, theOption, theIndex );
65 void CurveCreator_TableItemDelegate::setEditorData( QWidget* theEditor,
66 const QModelIndex& theIndex ) const
68 int aColumnId = theIndex.column();
69 if ( aColumnId == 2 || aColumnId == 3 ) {
70 QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
72 double aValue = theIndex.model()->data( theIndex, Qt::EditRole ).toDouble();
73 aDblSpin->setValue( aValue );
77 QItemDelegate::setEditorData( theEditor, theIndex );
80 void CurveCreator_TableItemDelegate::setModelData( QWidget* theEditor,
81 QAbstractItemModel* theModel,
82 const QModelIndex& theIndex ) const
84 int aColumnId = theIndex.column();
85 if ( aColumnId == 2 || aColumnId == 3 ) {
86 QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
88 double aValue = aDblSpin->value();
89 theModel->setData( theIndex, aValue, Qt::UserRole);
93 QItemDelegate::setModelData( theEditor, theModel, theIndex );
96 CurveCreator_TableView::CurveCreator_TableView( CurveCreator_ICurve* theCurve, QWidget* theParent )
97 : QTableWidget( theParent ), myCurve( theCurve )
99 setItemDelegate( new CurveCreator_TableItemDelegate( this ) );
102 setColumnWidth( 0, SECTION_NAME_COLUMN_WIDTH );
103 setColumnWidth( 1, POINT_INDEX_COLUMN_WIDTH );
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 );
110 void CurveCreator_TableView::setCurve( CurveCreator_ICurve* theCurve )
115 void CurveCreator_TableView::setLocalPointsToTable(
116 const CurveCreator_ICurve::SectionToPointList& thePoints )
118 setRowCount( thePoints.size() );
121 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = thePoints.begin(),
122 aLast = thePoints.end();
123 for ( ; anIt != aLast; anIt++ ) {
124 CurveCreator_ICurve::SectionToPoint aSPoint = *anIt;
125 int anISection = aSPoint.first;
126 int anIPoint = aSPoint.second;
128 QTableWidgetItem* anItem;
129 anItem = new QTableWidgetItem( myCurve->getSectionName( anISection ).c_str() );
130 anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
131 anItem->setData( Qt::UserRole, anISection );
132 setItem( aRowId, 0, anItem );
134 anItem = new QTableWidgetItem( QString::number( anIPoint + 1 ) );
135 anItem->setFlags( anItem->flags() & ~Qt::ItemIsEnabled );
136 anItem->setData( Qt::UserRole, anIPoint );
137 setItem( aRowId, 1, anItem );
140 CurveCreator_UtilsICurve::getPoint( myCurve, anISection, anIPoint, aPoint );
142 anItem = item( aRowId, 2 );
144 anItem = new QTableWidgetItem();
145 setItem( aRowId, 2, anItem );
147 anItem->setData( Qt::UserRole, aPoint.X() );
148 anItem->setData( Qt::DisplayRole, QString::number( aPoint.X(), 'f', 2 ) );
150 anItem = item( aRowId, 3 );
152 anItem = new QTableWidgetItem();
153 setItem( aRowId, 3, anItem );
155 anItem->setData( Qt::UserRole, aPoint.Y() );
156 anItem->setData( Qt::DisplayRole, QString::number( aPoint.Y(), 'f', 2 ) );
162 int CurveCreator_TableView::getSectionId( const int theRowId ) const
164 return item( theRowId, 0 )->data( Qt::UserRole ).toInt();
168 * Returns a point index from the table
169 * \param theRowId a table row
171 int CurveCreator_TableView::getPointId( const int theRowId ) const
173 return item( theRowId, 1 )->data( Qt::UserRole ).toInt();