QXRD  0.11.16
qxrdroitypedelegate.cpp
Go to the documentation of this file.
1 #include "qxrdroitypedelegate.h"
3 #include <QComboBox>
4 #include "qxrdroicoordinates.h"
5 #include <QMenu>
6 
8  : QStyledItemDelegate(parent)
9 {
10 
11 }
12 
13 QWidget *QxrdROITypeDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
14  const QModelIndex &index) const
15 {
16  if (index.column() == QxrdROICoordinatesListModel::TypeCol) {
17  QComboBox *editor = new QComboBox(parent);
18 
19  for (int i=0; i<QxrdROICoordinates::roiTypeCount(); i++) {
20  QString name = QxrdROICoordinates::roiTypeName(i);
21  editor->addItem(name);
22  }
23 
24  return editor;
25  } else {
26  return QStyledItemDelegate::createEditor(parent, option, index);
27  }
28 }
29 
30 void QxrdROITypeDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
31 {
32  if (index.column() == QxrdROICoordinatesListModel::TypeCol) {
33  QComboBox *cb = qobject_cast<QComboBox*>(editor);
34 
35  if (cb) {
36  QString roiTypeName = index.data().toString();
37 
38  int cbindex = cb->findText(roiTypeName);
39 
40  if (cbindex >= 0) {
41  cb->setCurrentIndex(cbindex);
42  }
43 
44  connect(cb, (void (QComboBox::*)(int)) &QComboBox::currentIndexChanged,
46  }
47  } else {
48  QStyledItemDelegate::setEditorData(editor, index);
49  }
50 }
51 
52 void QxrdROITypeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
53  const QModelIndex &index) const
54 {
55  if (index.column() == QxrdROICoordinatesListModel::TypeCol) {
56  QComboBox *cb = qobject_cast<QComboBox*>(editor);
57 
58  if (cb) {
59  QString roiType = cb->currentText();
60 
61  model->setData(index, roiType, Qt::EditRole);
62  }
63  } else {
64  QStyledItemDelegate::setModelData(editor, model, index);
65  }
66 }
67 
69 {
70  QComboBox *cb = qobject_cast<QComboBox*>(sender());
71 
72  if (cb) {
73  emit commitData(cb);
74  emit closeEditor(cb);
75  }
76 }
QxrdROITypeDelegate(QWidget *parent=0)
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE
void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const Q_DECL_OVERRIDE
void typeChanged(int newType)