12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- #include "CSchemeView.h"
- #include "CSchemeItem.h"
- #include "CRectItem.h"
- #include "CLineItem.h"
- #include "CTextItem.h"
- #include <QGraphicsItem>
- #include <QDebug>
- CSchemeView::CSchemeView(QWidget *parent) : QGraphicsView(new QGraphicsScene(), parent)
- {
- setRenderHints(QPainter::Antialiasing); // 抗锯齿
- m_group = new QGraphicsItemGroup();
- m_group->setFlags(QGraphicsItem::ItemIsMovable);
- scene()->addItem(m_group);
- setStyleSheet("QGraphicsView {border: 1px solid rgba(0, 0, 0, 0.073);background: rgb(244, 244, "
- "255);}");
- }
- void CSchemeView::addItem(CSchemeItem *item)
- {
- m_schemes.append(item);
- refreshItems();
- }
- void CSchemeView::clear()
- {
- qDeleteAll(m_schemes);
- m_schemes.clear();
- refreshItems();
- }
- void CSchemeView::refreshItems()
- {
- for (QGraphicsItem *item : m_items) {
- scene()->removeItem(item);
- }
- m_items.clear();
- refreshItemsGeometry();
- for (QGraphicsItem *item : m_items) {
- m_group->addToGroup(item);
- }
- QRectF r = m_group->childrenBoundingRect();
- setSceneRect(QRectF(QPointF(), r.size()));
- }
- qreal CSchemeView::maxItemHeight() const
- {
- qreal h = 0;
- for (CSchemeItem *item : m_schemes) {
- h = std::max(h, item->borderHeight());
- }
- return h;
- }
- void CSchemeView::refreshItemsGeometry()
- {
- qreal mh = maxItemHeight();
- int x = 0;
- CSchemeItem *left = nullptr;
- for (CSchemeItem *item : m_schemes) {
- QRect borderRect = QRect(x, (mh - item->borderHeight()) / 2, item->borderWidth(), item->borderHeight());
- item->rectItem()->setRect(borderRect);
- QPointF textPos = QPointF(borderRect.left() + (item->borderWidth() - item->textWidth()) / 2,
- borderRect.top() + (item->borderHeight() - item->textHeight()) / 2);
- item->textItem()->setPos(textPos);
- item->rectItem()->setPos(QPoint(0, 0));
- if (left != nullptr) {
- item->lineItem()->setStartPos(left->rectItem()->centerRight());
- item->lineItem()->setEndPos(item->rectItem()->centerLeft());
- }
- m_items.append(item->rectItem());
- x += borderRect.width() + m_hNodeSpace;
- left = item;
- }
- }
|