12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- #include "CRectItem.h"
- #include <Widgets/Menu.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)
- {
- setHandlesChildEvents(false);
- updatePath();
- m_menu = new RoundMenu("menu");
- m_subNode = new QAction("添加子节点");
- m_select = new QAction("选中");
- m_remove = new QAction("删除");
- m_menu->addAction(m_subNode);
- m_menu->addAction(m_select);
- m_menu->addAction(m_remove);
- }
- 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() + QPoint(-40, -20));
- }
- 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;
- }
|