1 // Copyright (C) 2013-2015 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, or (at your option) any later version.
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>
27 #include <QHeaderView>
29 #include <QtxDoubleSpinBox.h>
31 const double DBL_MINIMUM = -10000000.;
32 const double DBL_MAXIMUM = 10000000.;
34 const int SECTION_NAME_COLUMN_WIDTH = 75;
35 const int POINT_INDEX_COLUMN_WIDTH = 40;
37 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
39 CurveCreator_TableItemDelegate::CurveCreator_TableItemDelegate( QObject* theParent )
40 : QItemDelegate( theParent )
45 * Creates an editor for the cell
47 QWidget* CurveCreator_TableItemDelegate::createEditor( QWidget* theParent,
48 const QStyleOptionViewItem& theOption,
49 const QModelIndex& theIndex ) const
51 QWidget* anEditor = 0;
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 );
61 anEditor = QItemDelegate::createEditor( theParent, theOption, theIndex );
66 void CurveCreator_TableItemDelegate::setEditorData( QWidget* theEditor,
67 const QModelIndex& theIndex ) const
69 int aColumnId = theIndex.column();
70 if ( aColumnId == 2 || aColumnId == 3 ) {
71 QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
73 double aValue = theIndex.model()->data( theIndex, Qt::EditRole ).toDouble();
74 aDblSpin->setValue( aValue );
78 QItemDelegate::setEditorData( theEditor, theIndex );
81 void CurveCreator_TableItemDelegate::setModelData( QWidget* theEditor,
82 QAbstractItemModel* theModel,
83 const QModelIndex& theIndex ) const
85 int aColumnId = theIndex.column();
86 if ( aColumnId == 2 || aColumnId == 3 ) {
87 QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
89 double aValue = aDblSpin->value();
90 theModel->setData( theIndex, aValue, Qt::UserRole);
94 QItemDelegate::setModelData( theEditor, theModel, theIndex );
97 CurveCreator_TableView::CurveCreator_TableView( CurveCreator_ICurve* theCurve,
99 const QStringList& theCoordTitles )
100 : QTableWidget( theParent ), myCurve( theCurve )
102 setItemDelegate( new CurveCreator_TableItemDelegate( this ) );
105 setColumnWidth( 0, SECTION_NAME_COLUMN_WIDTH );
106 setColumnWidth( 1, POINT_INDEX_COLUMN_WIDTH );
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 );
114 connect( horizontalHeader(), SIGNAL( sectionClicked( int ) ), this, SLOT( OnHeaderClick( int ) ) );
117 void CurveCreator_TableView::setCurve( CurveCreator_ICurve* theCurve )
122 void CurveCreator_TableView::setLocalPointsToTable(
123 const CurveCreator_ICurve::SectionToPointList& thePoints )
125 setRowCount( thePoints.size() );
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;
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 );
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 );
148 CurveCreator_UtilsICurve::getPoint( myCurve, anISection, anIPoint, aPoint );
150 anItem = item( aRowId, 2 );
152 anItem = new QTableWidgetItem();
153 setItem( aRowId, 2, anItem );
155 anItem->setData( Qt::UserRole, aPoint.X() );
156 anItem->setData( Qt::DisplayRole, QString::number( aPoint.X(), 'f', 2 ).toDouble() );
158 anItem = item( aRowId, 3 );
160 anItem = new QTableWidgetItem();
161 setItem( aRowId, 3, anItem );
163 anItem->setData( Qt::UserRole, aPoint.Y() );
164 anItem->setData( Qt::DisplayRole, QString::number( aPoint.Y(), 'f', 2 ).toDouble() );
170 int CurveCreator_TableView::getSectionId( const int theRowId ) const
172 return item( theRowId, 0 )->data( Qt::UserRole ).toInt();
176 * Returns a point index from the table
177 * \param theRowId a table row
179 int CurveCreator_TableView::getPointId( const int theRowId ) const
181 return item( theRowId, 1 )->data( Qt::UserRole ).toInt();
184 void CurveCreator_TableView::OnHeaderClick( int theLogicalId )
186 sortByColumn( theLogicalId, Qt::AscendingOrder );