CRectItem.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "CRectItem.h"
  2. #include <QGraphicsSceneContextMenuEvent>
  3. #include <QMenu>
  4. #include <QDebug>
  5. CRectItem::CRectItem(QGraphicsItem *parent) : CRectItem(QRectF(), parent) { }
  6. CRectItem::CRectItem(const QRectF &rect, QGraphicsItem *parent) : CPathItem(parent), m_rect(rect)
  7. {
  8. updatePath();
  9. m_menu = new QMenu();
  10. m_select = m_menu->addAction("选中");
  11. m_subNode = m_menu->addAction("添加子节点");
  12. m_remove = m_menu->addAction("删除");
  13. }
  14. QRectF CRectItem::rect() const
  15. {
  16. return m_rect;
  17. }
  18. void CRectItem::setRect(const QRectF &rect)
  19. {
  20. m_rect = rect;
  21. updatePath();
  22. }
  23. int CRectItem::cornerRadius() const
  24. {
  25. return m_cornerRadius;
  26. }
  27. void CRectItem::setCornerRadius(qreal radius)
  28. {
  29. m_cornerRadius = radius;
  30. updatePath();
  31. }
  32. void CRectItem::updatePath()
  33. {
  34. QPainterPath path;
  35. path.addRoundedRect(m_rect, m_cornerRadius, m_cornerRadius);
  36. setPath(path);
  37. }
  38. void CRectItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
  39. {
  40. qDebug() << __FUNCTION__ << __LINE__ << endl;
  41. const QString txt = highlighted() ? "取消选中" : "选中";
  42. m_select->setText(txt);
  43. m_menu->exec(event->screenPos());
  44. }
  45. void CRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
  46. {
  47. qDebug() << __FUNCTION__ << __LINE__ << endl;
  48. }
  49. QAction *CRectItem::selectAction() const
  50. {
  51. return m_select;
  52. }
  53. QAction *CRectItem::subNodeAction() const
  54. {
  55. return m_subNode;
  56. }
  57. QAction *CRectItem::removeAction() const
  58. {
  59. return m_remove;
  60. }
  61. void CRectItem::slotSelect()
  62. {
  63. qDebug() << __FUNCTION__ << __LINE__ << endl;
  64. }