Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkSimpleBarrierParametersDelegate.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 <QLabel>
18 #include <QMouseEvent>
19 #include <QListWidget>
20 
22 {
23 }
24 
26  const QStyleOptionViewItem& option, const QModelIndex& index) const
27 {
28  QVariant data = index.data(Qt::DisplayRole);
29  QStyleOptionViewItem opt = option;
30  initStyleOption(&opt, index);
31  QStyle* style = QApplication::style();
32 
33  QString text = "Invalid data";
34 
35  if (data.isValid())
36  {
37  QStringList names = data.toStringList();
38  text.clear();
39 
40  for (QStringList::const_iterator pos = names.begin(); pos != names.end(); ++pos)
41  {
42  if (pos != names.begin())
43  {
44  text.append(", ");
45  }
46 
47  text.append(*pos);
48  }
49  }
50 
51  style->drawItemText(painter, opt.rect.adjusted(0, 0, -5, 0), Qt::AlignRight | Qt::AlignVCenter,
52  opt.palette, true, text);
53 }
54 
56  const QStyleOptionViewItem&
57  , const QModelIndex& index) const
58 {
59  QVariant data = index.data(Qt::EditRole);
60  QVariant displayData = index.data(Qt::DisplayRole);
61 
62  if (data.isValid())
63  {
64  QListWidget* list = new QListWidget(parent);
65  list->setFixedSize(100, 100);
66 
67  list->installEventFilter(const_cast<QmitkSimpleBarrierParametersDelegate*>(this));
68 
69  return list;
70 
71  }
72  else
73  {
74  return new QLabel(displayData.toString(), parent);
75  }
76 }
77 
79  const QModelIndex& index) const
80 {
81  QVariant data = index.data(Qt::EditRole);
82  QVariant displayData = index.data(Qt::DisplayRole);
83 
84  if (data.isValid() && displayData.isValid())
85  {
86 
87  QListWidget* list = qobject_cast<QListWidget*>(editor);
88 
89  if (list)
90  {
91  list->clear();
92 
93  QStringList names = data.toStringList();
94  QStringList checkedNames = displayData.toStringList();
95 
96  for (QStringList::const_iterator pos = names.begin(); pos != names.end(); ++pos)
97  {
98  QListWidgetItem* item = new QListWidgetItem(*pos, list);
99  item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
100 
101  if (checkedNames.contains(*pos))
102  {
103  item->setCheckState(Qt::Checked);
104  }
105  else
106  {
107  item->setCheckState(Qt::Unchecked);
108  }
109  }
110  }
111  else
112  {
113  QStyledItemDelegate::setEditorData(editor, index);
114  }
115  }
116 }
117 
118 void QmitkSimpleBarrierParametersDelegate::setModelData(QWidget* editor, QAbstractItemModel* model
119  , const QModelIndex& index) const
120 {
121  QVariant data = index.data(Qt::EditRole);
122 
123  if (data.isValid())
124  {
125  QListWidget* list = qobject_cast<QListWidget*>(editor);
126  QStringList selectedItems;
127 
128  if (list)
129  {
130  for (int row = 0; row < list->count(); ++row)
131  {
132  QListWidgetItem* item = list->item(row);
133 
134  if (item->checkState() == Qt::Checked)
135  {
136  selectedItems.append(item->text());
137  }
138  }
139  }
140 
141  QVariant selectedVariant(selectedItems);
142  model->setData(index, selectedVariant);
143  }
144  else
145  {
146  QStyledItemDelegate::setModelData(editor, model, index);
147  }
148 }
void setEditorData(QWidget *editor, const QModelIndex &index) const override
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override