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
22 #include <QTableWidget>
23 #include <QTableWidgetItem>
24 //#include <QDoubleSpinBox>
25 #include <QtxDoubleSpinBox.h>
26
27 const double DBL_MINIMUM = -10000000.;
28 const double DBL_MAXIMUM = 10000000.;
29
30 CurveCreator_TableItemDelegate::CurveCreator_TableItemDelegate( QObject* theParent )
31 : QItemDelegate( theParent )
32 {
33 }
34
35 /**
36  * Creates an editor for the cell
37  */
38 QWidget* CurveCreator_TableItemDelegate::createEditor( QWidget* theParent,
39                                                        const QStyleOptionViewItem& theOption,
40                                                        const QModelIndex& theIndex ) const
41 {
42   QWidget* anEditor = 0;
43
44   int aColumnId = theIndex.column();
45   if ( aColumnId == 2 || aColumnId == 3 ) {
46     QDoubleSpinBox* aSpin = new QtxDoubleSpinBox( theParent );
47     aSpin->setDecimals( 6 );
48     aSpin->setRange( DBL_MINIMUM, DBL_MAXIMUM );
49     anEditor = aSpin;
50   }
51   else
52     anEditor = QItemDelegate::createEditor( theParent, theOption, theIndex );
53
54   return anEditor;
55 }
56
57 void CurveCreator_TableItemDelegate::setEditorData( QWidget* theEditor,
58                                                     const QModelIndex& theIndex ) const
59 {
60   QTableWidget* aTableWidget = dynamic_cast<QTableWidget*>( parent() );
61   QTableWidgetItem* anItem = aTableWidget ? dynamic_cast<QTableWidgetItem*>
62                                   ( aTableWidget->item( theIndex.row(), theIndex.column() ) ) : 0;
63   int aColumnId = theIndex.column();
64   if ( anItem && ( aColumnId == 2 || aColumnId == 3 ) ) {
65     QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
66     double aValue = anItem->data( Qt::UserRole ).toDouble();
67     if ( aDblSpin ) {
68       aDblSpin->setValue( aValue );
69     }
70   }
71   else
72    QItemDelegate::setEditorData( theEditor, theIndex );
73 }
74
75 void CurveCreator_TableItemDelegate::setModelData( QWidget* theEditor,
76                                                    QAbstractItemModel* theModel,
77                                                    const QModelIndex& theIndex ) const
78 {
79   QTableWidget* aTableWidget = dynamic_cast<QTableWidget*>( parent() );
80   QTableWidgetItem* anItem = aTableWidget ? dynamic_cast<QTableWidgetItem*>
81                       ( aTableWidget->item( theIndex.row(), theIndex.column() ) ) : 0;
82
83   int aColumnId = theIndex.column();
84   if ( anItem && ( aColumnId == 2 || aColumnId == 3 ) ) {
85     QDoubleSpinBox* aDblSpin = dynamic_cast<QDoubleSpinBox*>( theEditor );
86     if ( aDblSpin ) {
87       double aValue = aDblSpin->value();
88       anItem->setData( Qt::UserRole, aValue );
89     }
90   }
91   else
92     QItemDelegate::setModelData( theEditor, theModel, theIndex );
93 }
94