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,
98 const QStringList& theCoordTitles )
99 : QTableWidget( theParent ), myCurve( theCurve )
101 setItemDelegate( new CurveCreator_TableItemDelegate( this ) );
104 setColumnWidth( 0, SECTION_NAME_COLUMN_WIDTH );
105 setColumnWidth( 1, POINT_INDEX_COLUMN_WIDTH );
107 QString aCoord1 = theCoordTitles.size() > 0 ? theCoordTitles[0] : tr( "X" ); // tr( "X_POSITION_LBL" )
108 QString aCoord2 = theCoordTitles.size() > 1 ? theCoordTitles[1] : tr( "Y" ); // tr( "Y_POSITION_LBL" )
109 //aLabels << tr( "SECTION_LABEL" ) << tr( "IDENTIFIER_LABEL" ) << aCoord1 << aCoord2;
110 aLabels << tr( "Section" ) << "Index" << aCoord1 << aCoord2;
111 setHorizontalHeaderLabels( aLabels );
114 void CurveCreator_TableView::setCurve( CurveCreator_ICurve* theCurve )
119 void CurveCreator_TableView::setLocalPointsToTable(
120 const CurveCreator_ICurve::SectionToPointList& thePoints )
122 setRowCount( thePoints.size() );
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;
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 );
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 );
144 CurveCreator_UtilsICurve::getPoint( myCurve, anISection, anIPoint, aPoint );
146 anItem = item( aRowId, 2 );
148 anItem = new QTableWidgetItem();
149 setItem( aRowId, 2, anItem );
151 anItem->setData( Qt::UserRole, aPoint.X() );
152 anItem->setData( Qt::DisplayRole, QString::number( aPoint.X(), 'f', 2 ) );
154 anItem = item( aRowId, 3 );
156 anItem = new QTableWidgetItem();
157 setItem( aRowId, 3, anItem );
159 anItem->setData( Qt::UserRole, aPoint.Y() );
160 anItem->setData( Qt::DisplayRole, QString::number( aPoint.Y(), 'f', 2 ) );
166 int CurveCreator_TableView::getSectionId( const int theRowId ) const
168 return item( theRowId, 0 )->data( Qt::UserRole ).toInt();
172 * Returns a point index from the table
173 * \param theRowId a table row
175 int CurveCreator_TableView::getPointId( const int theRowId ) const
177 return item( theRowId, 1 )->data( Qt::UserRole ).toInt();