CRectItem.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "CRectItem.h"
  2. #include <Widgets/Menu.h>
  3. #include <QGraphicsSceneContextMenuEvent>
  4. #include <QMenu>
  5. #include <QPainter>
  6. #include <QDebug>
  7. CRectItem::CRectItem(QGraphicsItem *parent) : CRectItem(QRectF(), parent) { }
  8. CRectItem::CRectItem(const QRectF &rect, QGraphicsItem *parent) : CPathItem(parent), m_rect(rect)
  9. {
  10. setHandlesChildEvents(false);
  11. updatePath();
  12. m_menu = new RoundMenu("menu");
  13. m_subNode = new QAction("添加子节点");
  14. m_select = new QAction("选中");
  15. m_remove = new QAction("删除");
  16. m_edit = new QAction("编辑节点");
  17. m_menu->addAction(m_edit);
  18. m_menu->addAction(m_subNode);
  19. // m_menu->addAction(m_select);
  20. m_menu->addAction(m_remove);
  21. }
  22. QRectF CRectItem::rect() const
  23. {
  24. return m_rect;
  25. }
  26. void CRectItem::setRect(const QRectF &rect)
  27. {
  28. m_rect = rect;
  29. updatePath();
  30. }
  31. QPointF CRectItem::centerLeft() const
  32. {
  33. return QPointF(m_rect.center() - QPointF(m_rect.width() / 2, 0));
  34. }
  35. QPointF CRectItem::centerRight() const
  36. {
  37. return QPointF(m_rect.center() + QPointF(m_rect.width() / 2, 0));
  38. }
  39. int CRectItem::cornerRadius() const
  40. {
  41. return m_cornerRadius;
  42. }
  43. void CRectItem::setCornerRadius(qreal radius)
  44. {
  45. m_cornerRadius = radius;
  46. updatePath();
  47. }
  48. void CRectItem::updatePath()
  49. {
  50. QPainterPath path;
  51. path.addRoundedRect(m_rect, m_cornerRadius, m_cornerRadius);
  52. setPath(path);
  53. }
  54. void CRectItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
  55. {
  56. if (!m_allowEdit) {
  57. return;
  58. }
  59. const QString txt = highlighted() ? "取消选中" : "选中";
  60. m_select->setText(txt);
  61. m_menu->exec(event->screenPos() + QPoint(-40, -20));
  62. }
  63. void CRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
  64. {
  65. Q_UNUSED(event)
  66. }
  67. QAction *CRectItem::selectAction() const
  68. {
  69. return m_select;
  70. }
  71. QAction *CRectItem::subNodeAction() const
  72. {
  73. return m_subNode;
  74. }
  75. QAction *CRectItem::removeAction() const
  76. {
  77. return m_remove;
  78. }
  79. QAction *CRectItem::editAction() const
  80. {
  81. return m_edit;
  82. }
  83. QRectF CRectItem::boundingRect() const
  84. {
  85. return m_rect;
  86. }
  87. void CRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  88. {
  89. painter->setPen(pen());
  90. painter->setBrush(Qt::blue);
  91. QGraphicsPathItem::paint(painter, option, widget);
  92. }
  93. void CRectItem::setAllowEdit(bool allow)
  94. {
  95. m_allowEdit = allow;
  96. }
  97. void CRectItem::slotSelect()
  98. {
  99. qDebug() << __FUNCTION__ << __LINE__ << endl;
  100. }