123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- #include "CNodeItem.h"
- #include "CRectItem.h"
- #include "CTextItem.h"
- #include "CLineItem.h"
- #include <QAction>
- #include <QTextDocument>
- #include <QDebug>
- CNodeItem::CNodeItem(const CNodeData data, CNodeItem *parent) : QObject(parent), m_data(data)
- {
- m_rectItem = new CRectItem();
- m_textItem = new CTextItem(data.name, m_rectItem);
- m_lineItem = new CLineItem(m_rectItem);
- updateItemsGeometry();
- connectSignalsAndSlots();
- }
- CNodeData CNodeItem::data() const
- {
- return m_data;
- }
- void CNodeItem::addSubNode(CNodeItem *n)
- {
- if (n == nullptr) {
- return;
- }
- if (n->data().pNumber == data().number) {
- n->setParent(this);
- } else {
- for (QObject *obj : children()) {
- CNodeItem *item = dynamic_cast<CNodeItem *>(obj);
- item->addSubNode(n);
- }
- }
- }
- void CNodeItem::removeNode(int number)
- {
- for (QObject *obj : children()) {
- CNodeItem *item = dynamic_cast<CNodeItem *>(obj);
- if (item->data().number == number) {
- item->setParent(nullptr);
- delete item;
- } else {
- item->removeNode(number);
- }
- }
- }
- CRectItem *CNodeItem::rectItem() const
- {
- return m_rectItem;
- }
- CTextItem *CNodeItem::textItem() const
- {
- return m_textItem;
- }
- QPointF CNodeItem::pos() const
- {
- return m_pos;
- }
- void CNodeItem::setPos(const QPointF pos)
- {
- m_pos = pos;
- updateItemsGeometry();
- }
- qreal CNodeItem::xMargin() const
- {
- return m_xMargin;
- }
- void CNodeItem::setXMargin(qreal x)
- {
- m_xMargin = x;
- updateItemsGeometry();
- }
- qreal CNodeItem::yMargin() const
- {
- return m_yMargin;
- }
- void CNodeItem::setYMargin(qreal y)
- {
- m_yMargin = y;
- updateItemsGeometry();
- }
- qreal CNodeItem::minWidth() const
- {
- return m_minWidth;
- }
- void CNodeItem::setMinWidth(qreal w)
- {
- m_minWidth = w;
- updateItemsGeometry();
- }
- qreal CNodeItem::minHeight() const
- {
- return m_minHeight;
- }
- void CNodeItem::setMinHeight(qreal h)
- {
- m_minHeight = h;
- updateItemsGeometry();
- }
- void CNodeItem::connectSignalsAndSlots()
- {
- connect(m_rectItem->selectAction(), &QAction::triggered, this, &CNodeItem::slotSelect);
- connect(m_rectItem->subNodeAction(), &QAction::triggered, this, &CNodeItem::slotSubNode);
- connect(m_rectItem->removeAction(), &QAction::triggered, this, &CNodeItem::slotRemove);
- }
- void CNodeItem::updateItemsGeometry()
- {
- QRectF tr = m_textItem->boundingRect();
- qreal w = std::max(tr.width() + m_xMargin * 2, m_minWidth);
- qreal h = std::max(tr.height() + m_yMargin * 2, m_minHeight);
- m_rectItem->setRect(QRectF(m_pos, QSizeF(w, h)));
- m_textItem->setPos(m_pos + QPointF(m_xMargin, (h - tr.height()) / 2));
- }
- void CNodeItem::slotSelect()
- {
- qDebug() << __FUNCTION__ << __LINE__ << endl;
- m_rectItem->setHighlighted(!m_rectItem->highlighted());
- }
- void CNodeItem::slotSubNode()
- {
- qDebug() << __FUNCTION__ << __LINE__ << endl;
- emit sigAddSubItem(data().number);
- }
- void CNodeItem::slotRemove()
- {
- qDebug() << __FUNCTION__ << __LINE__ << endl;
- emit sigRemoveItem(data().number);
- }
|