Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkDoseVisualStyleDelegate.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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 
15 #include <QPainter>
16 #include <QApplication>
17 #include <QCheckBox>
18 #include <QLabel>
19 #include <QMouseEvent>
20 
21 static QRect CheckBoxRect(const QStyleOptionViewItem &view_item_style_options) {
22  QStyleOptionButton check_box_style_option;
23  QRect check_box_rect = QApplication::style()->subElementRect(
24  QStyle::SE_CheckBoxIndicator,
25  &check_box_style_option);
26  QPoint check_box_point(view_item_style_options.rect.x() +
27  view_item_style_options.rect.width() / 2 -
28  check_box_rect.width() / 2,
29  view_item_style_options.rect.y() +
30  view_item_style_options.rect.height() / 2 -
31  check_box_rect.height() / 2);
32  return QRect(check_box_point, check_box_rect.size());
33 }
34 
36 {
37 }
38 
39 void QmitkDoseVisualStyleDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
40 {
41  bool checkValue = index.data(Qt::DisplayRole).toBool();
42 
43  QStyleOptionButton BtnStyle;
44  BtnStyle.state = QStyle::State_Enabled;
45 
46  if(checkValue)
47  {
48  BtnStyle.state |= QStyle::State_On;
49  }
50  else
51  {
52  BtnStyle.state |= QStyle::State_Off;
53  }
54 
55  BtnStyle.direction = QApplication::layoutDirection();
56  BtnStyle.rect = CheckBoxRect(option);
57  QApplication::style()->drawControl(QStyle::CE_CheckBox,&BtnStyle,painter );
58 }
59 
60 bool QmitkDoseVisualStyleDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &,
61  const QModelIndex &index)
62 {
63  Q_ASSERT(event);
64  Q_ASSERT(model);
65 
66  // make sure that the item is checkable
67  Qt::ItemFlags flags = model->flags(index);
68  if (!(flags & Qt::ItemIsEditable) || !(flags & Qt::ItemIsEnabled))
69  {
70  return false;
71  }
72 
73  // make sure that we have the right event type
74  QMouseEvent* mouseEvent = dynamic_cast<QMouseEvent*>(event);
75  if (!mouseEvent)
76  {
77  return false;
78  }
79  else
80  {
81  if (mouseEvent->type() != QEvent::MouseButtonRelease || mouseEvent->button() != Qt::LeftButton)
82  {
83  return false;
84  }
85  }
86 
87  bool newState = !(index.data(Qt::EditRole).toBool());
88 
89  return model->setData(index, QVariant(newState), Qt::EditRole);
90 };
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override
QmitkDoseVisualStyleDelegate(QObject *parent=nullptr)
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
static QRect CheckBoxRect(const QStyleOptionViewItem &view_item_style_options)