1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- #include "CRectItem.h"
- #include <QGraphicsSceneContextMenuEvent>
- #include <QMenu>
- #include <QDebug>
- CRectItem::CRectItem(QGraphicsItem *parent) : CRectItem(QRectF(), parent) { }
- CRectItem::CRectItem(const QRectF &rect, QGraphicsItem *parent) : CPathItem(parent), m_rect(rect)
- {
- updatePath();
- m_menu = new QMenu();
- m_subNode = m_menu->addAction("添加子节点");
- m_select = m_menu->addAction("选中");
- m_remove = m_menu->addAction("删除");
- }
- QRectF CRectItem::rect() const
- {
- return m_rect;
- }
- void CRectItem::setRect(const QRectF &rect)
- {
- m_rect = rect;
- updatePath();
- }
- QPointF CRectItem::centerLeft() const
- {
- return QPointF(m_rect.center() - QPointF(m_rect.width() / 2, 0));
- }
- QPointF CRectItem::centerRight() const
- {
- return QPointF(m_rect.center() + QPointF(m_rect.width() / 2, 0));
- }
- int CRectItem::cornerRadius() const
- {
- return m_cornerRadius;
- }
- void CRectItem::setCornerRadius(qreal radius)
- {
- m_cornerRadius = radius;
- updatePath();
- }
- void CRectItem::updatePath()
- {
- QPainterPath path;
- path.addRoundedRect(m_rect, m_cornerRadius, m_cornerRadius);
- setPath(path);
- }
- void CRectItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
- {
- const QString txt = highlighted() ? "取消选中" : "选中";
- m_select->setText(txt);
- m_menu->exec(event->screenPos());
- }
- void CRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { }
- QAction *CRectItem::selectAction() const
- {
- return m_select;
- }
- QAction *CRectItem::subNodeAction() const
- {
- return m_subNode;
- }
- QAction *CRectItem::removeAction() const
- {
- return m_remove;
- }
- void CRectItem::slotSelect()
- {
- qDebug() << __FUNCTION__ << __LINE__ << endl;
- }
|