Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QmitkDoseValueDelegate.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "QmitkDoseValueDelegate.h"
18 
19 #include <QPainter>
20 #include <QApplication>
21 #include <QLabel>
22 #include <QDoubleSpinBox>
23 
25 {
26 }
27 
28 void QmitkDoseValueDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option
29  , const QModelIndex &index) const
30 {
31  QVariant data = index.data(Qt::DisplayRole);
32 
33  QStyleOptionViewItemV4 opt = option;
34  initStyleOption(&opt, index);
35 
36  QStyle *style = QApplication::style();
37 
38 
39  style->drawItemText(painter, opt.rect.adjusted(0,0,-5,0), Qt::AlignRight | Qt::AlignVCenter, opt.palette,true, data.toString());
40 }
41 
42 QWidget* QmitkDoseValueDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &
43  , const QModelIndex &index) const
44 {
45  QVariant data = index.data(Qt::EditRole);
46  QVariant displayData = index.data(Qt::DisplayRole);
47  QVariant absoluteDose = index.data(Qt::UserRole+1);
48 
49  if(data.isValid())
50  {
51  QDoubleSpinBox* spinBox = new QDoubleSpinBox(parent);
52  spinBox->setDecimals(2);
53  if (absoluteDose.toBool())
54  {
55  spinBox->setSingleStep(0.5);
56  spinBox->setSuffix(QString(" Gy"));
57  }
58  else
59  {
60  spinBox->setSingleStep(1.0);
61  spinBox->setSuffix(QString(" %"));
62  }
63 
64  spinBox->setMinimum(0.0);
65  spinBox->setMaximum(9999.0);
66 
67  spinBox->installEventFilter( const_cast<QmitkDoseValueDelegate*>(this) );
68 
69  return spinBox;
70 
71  }
72  else
73  return new QLabel(displayData.toString(), parent);
74 
75 }
76 
77 void QmitkDoseValueDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
78 {
79  QVariant data = index.data(Qt::EditRole);
80 
81  if(data.isValid())
82  {
83 
84  QDoubleSpinBox* spinBox = qobject_cast<QDoubleSpinBox *>(editor);
85  if (spinBox)
86  {
87  spinBox->setValue(data.toDouble());
88  }
89  else
90  {
91  QStyledItemDelegate::setEditorData(editor, index);
92  }
93  }
94 }
95 
96 void QmitkDoseValueDelegate::setModelData(QWidget *editor, QAbstractItemModel* model
97  , const QModelIndex &index) const
98 {
99  QVariant data = index.data(Qt::EditRole);
100 
101  if(data.isValid())
102  {
103  QDoubleSpinBox* spinBox = qobject_cast<QDoubleSpinBox *>(editor);
104  double doubleValue = spinBox->value();
105 
106  QVariant doubleValueVariant(doubleValue);
107  model->setData(index, doubleValueVariant);
108  }
109  else
110  {
111  QStyledItemDelegate::setModelData(editor, model, index);
112  }
113 }
QmitkDoseValueDelegate(QObject *parent=0)
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
void setEditorData(QWidget *editor, const QModelIndex &index) const
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const