143 lines
4.6 KiB
C++
143 lines
4.6 KiB
C++
#include <QJsonArray>
|
||
#include "baseDrawingPanel.h"
|
||
#include <QMessageBox>
|
||
#include <QCloseEvent>
|
||
#include "designerView.h"
|
||
#include "graphicsDataModel/fixedPortsModel.h"
|
||
#include "graphicsItem/graphicsBaseItem.h"
|
||
#include "util/selectorManager.h"
|
||
#include "statusBar.h"
|
||
#include "powerEntity.h"
|
||
#include "diagramCavas.h"
|
||
#include "topologyManager.h"
|
||
#include "basePannelPropertyProxy.h"
|
||
#include "common/core_model/constants.h"
|
||
|
||
BaseDrawingPanel::BaseDrawingPanel(PowerEntity* pEntity,QWidget *parent,DiagramMode mode)
|
||
: QWidget(parent)
|
||
,_pModel(nullptr)
|
||
,_mode(mode)
|
||
,_pEntity(nullptr)
|
||
,_verticalLayout(nullptr)
|
||
,_horizontalLayout(nullptr)
|
||
,_hSplitter(nullptr)
|
||
{
|
||
_pEntity = pEntity;
|
||
_pModel = new FixedPortsModel(pEntity);
|
||
_pModel->setTopWidget(this);
|
||
m_pSelectorManager = new SelectorManager(_pModel,this);
|
||
m_pGraphicsScene = new DesignerScene(_pModel,this);
|
||
//设置场景大小.前两个参数为scene的坐标远点,设置到view的中心点后,无论view如何缩放,secne的坐标原点都不会动,方便后续的位置计算
|
||
m_pGraphicsScene->setSceneRect(0,0, Constants::SCENE_WIDTH*4, Constants::SCENE_HEIGHT*4);
|
||
m_pGraphicsScene->setGridVisible(true);
|
||
|
||
m_pGraphicsView = new DesignerView(this);
|
||
m_pGraphicsView->setScene(m_pGraphicsScene);
|
||
m_pGraphicsView->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
|
||
m_pGraphicsScene->setView(m_pGraphicsView);
|
||
_pModel->setScene(m_pGraphicsScene);
|
||
connect(m_pGraphicsScene, &DesignerScene::selectionChanged, _pModel, &FixedPortsModel::onSelectionChanged);
|
||
|
||
m_pStatusBar = new StatusBar(this);
|
||
m_pStatusBar->setMaximumHeight(25);
|
||
connect(m_pGraphicsView,&DesignerView::onScaleChanged,m_pStatusBar,&StatusBar::onScaleLevelChanged);
|
||
|
||
_horizontalLayout = new QHBoxLayout();
|
||
//_horizontalLayout->addWidget(m_pGraphicsView);
|
||
_horizontalLayout->setContentsMargins(0, 0, 0, 0);
|
||
_horizontalLayout->setSpacing(0);
|
||
|
||
_hSplitter = new QSplitter(Qt::Horizontal);
|
||
_hSplitter->setHandleWidth(2); // 设置分割条宽度
|
||
|
||
_hSplitter->addWidget(m_pGraphicsView);
|
||
_horizontalLayout->addWidget(_hSplitter);
|
||
|
||
_verticalLayout = new QVBoxLayout(this);
|
||
_verticalLayout->addLayout(_horizontalLayout);
|
||
_verticalLayout->addWidget(m_pStatusBar);
|
||
_verticalLayout->setContentsMargins(0, 0, 0, 0);
|
||
_verticalLayout->setSpacing(0);
|
||
|
||
_pPropertyProxy = new BasePannelPropertyProxy(this);
|
||
}
|
||
|
||
BaseDrawingPanel::~BaseDrawingPanel()
|
||
{
|
||
//if(_pModel)
|
||
// delete _pModel;
|
||
}
|
||
|
||
QGraphicsScene* BaseDrawingPanel::getQGraphicsScene()
|
||
{
|
||
return m_pGraphicsView->scene();
|
||
}
|
||
|
||
DesignerScene* BaseDrawingPanel::getDesignerScene()
|
||
{
|
||
return m_pGraphicsScene;
|
||
}
|
||
|
||
BasePannelPropertyProxy* BaseDrawingPanel::getPropertyProxy()
|
||
{
|
||
return _pPropertyProxy.data();
|
||
}
|
||
|
||
void BaseDrawingPanel::moveItemsAlignment(Qt::Alignment alignment,const QMarginsF& margins)
|
||
{
|
||
QList<QGraphicsItem*> items = m_pGraphicsScene->items();
|
||
if (items.isEmpty() || !m_pGraphicsScene) return;
|
||
|
||
// 1. 创建组
|
||
QGraphicsItemGroup* group = m_pGraphicsScene->createItemGroup(items);
|
||
|
||
// 2. 获取边界矩形
|
||
QRectF groupRect = group->boundingRect();
|
||
QRectF sceneRect = m_pGraphicsScene->sceneRect();
|
||
|
||
// 3. 计算新位置
|
||
QPointF targetPos = group->pos();
|
||
|
||
// 水平对齐
|
||
if (alignment & Qt::AlignLeft) {
|
||
targetPos.setX(sceneRect.left() + margins.left());
|
||
} else if (alignment & Qt::AlignRight) {
|
||
targetPos.setX(sceneRect.right() - groupRect.width() - margins.right());
|
||
} else if (alignment & Qt::AlignHCenter) {
|
||
targetPos.setX(sceneRect.center().x() - groupRect.width() / 2);
|
||
}
|
||
|
||
// 垂直对齐
|
||
if (alignment & Qt::AlignTop) {
|
||
targetPos.setY(sceneRect.top() + margins.top());
|
||
} else if (alignment & Qt::AlignBottom) {
|
||
targetPos.setY(sceneRect.bottom() - groupRect.height() - margins.bottom());
|
||
} else if (alignment & Qt::AlignVCenter) {
|
||
targetPos.setY(sceneRect.center().y() - groupRect.height() / 2);
|
||
}
|
||
|
||
// 4. 计算偏移量
|
||
QPointF offset = targetPos - groupRect.topLeft();
|
||
group->setPos(group->pos() + offset);
|
||
|
||
// 5. 取消分组
|
||
m_pGraphicsScene->destroyItemGroup(group);
|
||
}
|
||
|
||
void BaseDrawingPanel::centerOnTopLeft() {
|
||
// 获取场景左上角
|
||
QRectF sceneRect = m_pGraphicsScene->sceneRect();
|
||
QPointF topLeft = sceneRect.topLeft();
|
||
|
||
// 将视图中心对准左上角
|
||
m_pGraphicsView->centerOn(topLeft);
|
||
}
|
||
|
||
SelectorManager* BaseDrawingPanel::selectorManager() const
|
||
{
|
||
if(m_pSelectorManager)
|
||
return m_pSelectorManager;
|
||
else
|
||
return NULL;
|
||
}
|