CRectItem.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. const QString txt = highlighted() ? "取消选中" : "选中";
  57. m_select->setText(txt);
  58. m_menu->exec(event->screenPos() + QPoint(-40, -20));
  59. }
  60. void CRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
  61. {
  62. Q_UNUSED(event)
  63. }
  64. QAction *CRectItem::selectAction() const
  65. {
  66. return m_select;
  67. }
  68. QAction *CRectItem::subNodeAction() const
  69. {
  70. return m_subNode;
  71. }
  72. QAction *CRectItem::removeAction() const
  73. {
  74. return m_remove;
  75. }
  76. QAction *CRectItem::editAction() const
  77. {
  78. return m_edit;
  79. }
  80. QRectF CRectItem::boundingRect() const
  81. {
  82. return m_rect;
  83. }
  84. void CRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  85. {
  86. painter->setPen(pen());
  87. painter->setBrush(Qt::blue);
  88. QGraphicsPathItem::paint(painter, option, widget);
  89. }
  90. void CRectItem::slotSelect()
  91. {
  92. qDebug() << __FUNCTION__ << __LINE__ << endl;
  93. }