GreyClusteringItemDelegate.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "GreyClusteringItemDelegate.h"
  2. #include <QMessageBox>
  3. GreyClusteringItemRangeDelegate::GreyClusteringItemRangeDelegate(QObject *parent) : QStyledItemDelegate(parent) { }
  4. QWidget *GreyClusteringItemRangeDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */,
  5. const QModelIndex & /* index */) const
  6. {
  7. RangeSpin *editor = new RangeSpin(parent);
  8. return editor;
  9. }
  10. void GreyClusteringItemRangeDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  11. {
  12. QString value = index.model()->data(index, Qt::EditRole).toString();
  13. RangeSpin *spinBox = static_cast<RangeSpin *>(editor);
  14. spinBox->setValue(value);
  15. }
  16. void GreyClusteringItemRangeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
  17. const QModelIndex &index) const
  18. {
  19. RangeSpin *spinBox = static_cast<RangeSpin *>(editor);
  20. if (!spinBox->valid()) {
  21. QMessageBox::warning(nullptr, "警告", QString("设置的值%1区间错误,请重新设置!").arg(spinBox->result()));
  22. return;
  23. }
  24. QString value = spinBox->result();
  25. model->setData(index, value, Qt::EditRole);
  26. }
  27. void GreyClusteringItemRangeDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
  28. const QModelIndex & /* index */) const
  29. {
  30. QRect rect = option.rect;
  31. rect.adjust(-20, -10, 20, 10);
  32. editor->setGeometry(rect);
  33. }
  34. GreyClusteringItemSpinDelegate::GreyClusteringItemSpinDelegate(QObject *parent) : QStyledItemDelegate(parent) { }
  35. QWidget *GreyClusteringItemSpinDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */,
  36. const QModelIndex & /* index */) const
  37. {
  38. QDoubleSpinBox *editor = new QDoubleSpinBox(parent);
  39. editor->setFrame(false);
  40. editor->setMinimum(-1000000);
  41. editor->setMaximum(1000000);
  42. return editor;
  43. }
  44. void GreyClusteringItemSpinDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  45. {
  46. double value = index.model()->data(index, Qt::EditRole).toDouble();
  47. QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox *>(editor);
  48. spinBox->setValue(value);
  49. }
  50. void GreyClusteringItemSpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
  51. const QModelIndex &index) const
  52. {
  53. QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox *>(editor);
  54. spinBox->interpretText();
  55. double value = spinBox->value();
  56. model->setData(index, value, Qt::EditRole);
  57. }
  58. void GreyClusteringItemSpinDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
  59. const QModelIndex & /*index*/) const
  60. {
  61. editor->setGeometry(option.rect);
  62. }