Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkDoseColorDelegate.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 
13 #include "QmitkDoseColorDelegate.h"
14 
15 #include <QPainter>
16 #include <QApplication>
17 #include <QLabel>
18 #include <QPushButton>
19 #include <QColorDialog>
20 #include <QMouseEvent>
21 
23 {
24 }
25 
26 void QmitkDoseColorDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
27  const QModelIndex &index) const
28 {
29  QVariant data = index.data(Qt::EditRole);
30 
31  if (data.canConvert<QColor>())
32  {
33  QColor color = data.value<QColor>();
34 
35  painter->fillRect(option.rect, color);
36  }
37  else
38  {
39  QStyledItemDelegate::paint(painter, option, index);
40  }
41 }
42 
43 bool QmitkDoseColorDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &,
44  const QModelIndex &index)
45 {
46  Q_ASSERT(event);
47  Q_ASSERT(model);
48 
49  // make sure that the item is checkable
50  Qt::ItemFlags flags = model->flags(index);
51  if (!(flags & Qt::ItemIsEditable) || !(flags & Qt::ItemIsEnabled))
52  {
53  return false;
54  }
55 
56  // make sure that we have the right event type
57  QMouseEvent* mouseEvent = dynamic_cast<QMouseEvent*>(event);
58  if (!mouseEvent)
59  {
60  return false;
61  }
62  else
63  {
64  if (mouseEvent->type() != QEvent::MouseButtonRelease || mouseEvent->button() != Qt::LeftButton)
65  {
66  return false;
67  }
68  }
69 
70  QColor oldcolor = index.data(Qt::EditRole).value<QColor>();
71  QColor newColor = QColorDialog::getColor(oldcolor, nullptr);
72 
73  if (newColor.isValid())
74  {
75  return model->setData(index, QVariant(newColor), Qt::EditRole);
76  }
77 
78  return false;
79 };
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
QmitkDoseColorDelegate(QObject *parent=nullptr)
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override