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