MatterElementItemDelegate.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "MatterElementItemDelegate.h"
  2. MatterElementItemSpinDelegate::MatterElementItemSpinDelegate(QObject *parent) : QStyledItemDelegate(parent) { }
  3. QWidget *MatterElementItemSpinDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */,
  4. const QModelIndex & /* index */) const
  5. {
  6. QDoubleSpinBox *editor = new QDoubleSpinBox(parent);
  7. editor->setFrame(false);
  8. editor->setMinimum(-10000);
  9. editor->setMaximum(10000);
  10. return editor;
  11. }
  12. void MatterElementItemSpinDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  13. {
  14. double value = index.model()->data(index, Qt::EditRole).toDouble();
  15. QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox *>(editor);
  16. spinBox->setValue(value);
  17. }
  18. void MatterElementItemSpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
  19. const QModelIndex &index) const
  20. {
  21. QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox *>(editor);
  22. spinBox->interpretText();
  23. int value = spinBox->value();
  24. model->setData(index, value, Qt::EditRole);
  25. }
  26. void MatterElementItemSpinDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
  27. const QModelIndex & /*index*/) const
  28. {
  29. editor->setGeometry(option.rect);
  30. }
  31. MatterElementItemInsideDelegate::MatterElementItemInsideDelegate(QObject *parent) : QStyledItemDelegate(parent) { }
  32. QWidget *MatterElementItemInsideDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
  33. const QModelIndex & /*index*/) const
  34. {
  35. InsideWidget *editor = new InsideWidget(parent);
  36. return editor;
  37. }
  38. void MatterElementItemInsideDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  39. {
  40. QString value = index.model()->data(index, Qt::EditRole).toString();
  41. InsideWidget *iw = static_cast<InsideWidget *>(editor);
  42. iw->setValue(value);
  43. }
  44. void MatterElementItemInsideDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
  45. const QModelIndex &index) const
  46. {
  47. InsideWidget *iw = static_cast<InsideWidget *>(editor);
  48. QString value = iw->result();
  49. model->setData(index, value, Qt::EditRole);
  50. }
  51. void MatterElementItemInsideDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
  52. const QModelIndex &index) const
  53. {
  54. QRect rect = option.rect;
  55. rect.adjust(-10, 0, 10, 50);
  56. editor->setGeometry(rect);
  57. }