Medical Imaging Interaction Toolkit  2016.11.0
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,
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 
18 
19 #include <QPainter>
20 #include <QApplication>
21 #include <QCheckBox>
22 #include <QLabel>
23 #include <QMouseEvent>
24 
25 static QRect CheckBoxRect(const QStyleOptionViewItem &view_item_style_options) {
26  QStyleOptionButton check_box_style_option;
27  QRect check_box_rect = QApplication::style()->subElementRect(
28  QStyle::SE_CheckBoxIndicator,
29  &check_box_style_option);
30  QPoint check_box_point(view_item_style_options.rect.x() +
31  view_item_style_options.rect.width() / 2 -
32  check_box_rect.width() / 2,
33  view_item_style_options.rect.y() +
34  view_item_style_options.rect.height() / 2 -
35  check_box_rect.height() / 2);
36  return QRect(check_box_point, check_box_rect.size());
37 }
38 
40 {
41 }
42 
43 void QmitkDoseVisualStyleDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
44 {
45  bool checkValue = index.data(Qt::DisplayRole).toBool();
46 
47  QStyleOptionButton BtnStyle;
48  BtnStyle.state = QStyle::State_Enabled;
49 
50  if(checkValue)
51  {
52  BtnStyle.state |= QStyle::State_On;
53  }
54  else
55  {
56  BtnStyle.state |= QStyle::State_Off;
57  }
58 
59  BtnStyle.direction = QApplication::layoutDirection();
60  BtnStyle.rect = CheckBoxRect(option);
61  QApplication::style()->drawControl(QStyle::CE_CheckBox,&BtnStyle,painter );
62 }
63 
64 bool QmitkDoseVisualStyleDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option,
65  const QModelIndex &index)
66 {
67  Q_ASSERT(event);
68  Q_ASSERT(model);
69 
70  // make sure that the item is checkable
71  Qt::ItemFlags flags = model->flags(index);
72  if (!(flags & Qt::ItemIsEditable) || !(flags & Qt::ItemIsEnabled))
73  {
74  return false;
75  }
76 
77  // make sure that we have the right event type
78  QMouseEvent* mouseEvent = dynamic_cast<QMouseEvent*>(event);
79  if (!mouseEvent)
80  {
81  return false;
82  }
83  else
84  {
85  if (mouseEvent->type() != QEvent::MouseButtonRelease || mouseEvent->button() != Qt::LeftButton)
86  {
87  return false;
88  }
89  }
90 
91  bool newState = !(index.data(Qt::EditRole).toBool());
92 
93  return model->setData(index, QVariant(newState), Qt::EditRole);
94 };
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
static QRect CheckBoxRect(const QStyleOptionViewItem &view_item_style_options)