CRectItem.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_subNode = m_menu->addAction("添加子节点");
  11. m_select = 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. QPointF CRectItem::centerLeft() const
  24. {
  25. return QPointF(m_rect.center() - QPointF(m_rect.width() / 2, 0));
  26. }
  27. QPointF CRectItem::centerRight() const
  28. {
  29. return QPointF(m_rect.center() + QPointF(m_rect.width() / 2, 0));
  30. }
  31. int CRectItem::cornerRadius() const
  32. {
  33. return m_cornerRadius;
  34. }
  35. void CRectItem::setCornerRadius(qreal radius)
  36. {
  37. m_cornerRadius = radius;
  38. updatePath();
  39. }
  40. void CRectItem::updatePath()
  41. {
  42. QPainterPath path;
  43. path.addRoundedRect(m_rect, m_cornerRadius, m_cornerRadius);
  44. setPath(path);
  45. }
  46. void CRectItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
  47. {
  48. const QString txt = highlighted() ? "取消选中" : "选中";
  49. m_select->setText(txt);
  50. m_menu->exec(event->screenPos());
  51. }
  52. void CRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { }
  53. QAction *CRectItem::selectAction() const
  54. {
  55. return m_select;
  56. }
  57. QAction *CRectItem::subNodeAction() const
  58. {
  59. return m_subNode;
  60. }
  61. QAction *CRectItem::removeAction() const
  62. {
  63. return m_remove;
  64. }
  65. void CRectItem::slotSelect()
  66. {
  67. qDebug() << __FUNCTION__ << __LINE__ << endl;
  68. }