PowerDesigner/source/designerScene.cpp

146 lines
3.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "designerScene.h"
#include "util/selectorManager.h"
#include "graphicsItem/graphicsItemGroup.h"
#include <QPainter>
#include <QGraphicsSceneMouseEvent>
DesignerScene::DesignerScene(QObject *parent)
: QGraphicsScene(parent)
{
m_bGridVisible = true;
m_pView = nullptr;
}
DesignerScene::~DesignerScene()
{
}
void DesignerScene::drawBackground(QPainter* painter, const QRectF& rect)
{
QGraphicsScene::drawBackground(painter, rect);
painter->fillRect(sceneRect(), Qt::white);
if(!m_bGridVisible)
return;
QRectF sceneRect = this->sceneRect();
QPen pen;
pen.setBrush(Qt::darkCyan);//藏青色
pen.setStyle(Qt::DashLine);
pen.setWidthF(0.2);
painter->setPen(pen);
int nGridSpace = 20; //暂时写死
//竖线
for(int nX = sceneRect.left(); nX < sceneRect.right(); nX += nGridSpace)
painter->drawLine(nX,sceneRect.top(),nX,sceneRect.bottom());
//横线
for(int nY = sceneRect.top(); nY < sceneRect.bottom(); nY += nGridSpace)
painter->drawLine(sceneRect.left(),nY,sceneRect.right(),nY);
//补齐最后两条
painter->drawLine(sceneRect.right(), sceneRect.top(), sceneRect.right(), sceneRect.bottom());
painter->drawLine(sceneRect.left(), sceneRect.bottom(), sceneRect.right(), sceneRect.bottom());
//原点
// painter->setPen(Qt::red);
// painter->setBrush(Qt::red);
// painter->drawEllipse(0, 0, 50, 50);
}
void DesignerScene::mousePressEvent(QGraphicsSceneMouseEvent* mouseEvent)
{
if(SelectorManager::getInstance())
{
SelectorManager::getInstance()->getWorkingSelector()->mousePressEvent(mouseEvent, this);
update();
}
else
QGraphicsScene::mousePressEvent(mouseEvent);
}
void DesignerScene::mouseMoveEvent(QGraphicsSceneMouseEvent* mouseEvent)
{
if(SelectorManager::getInstance())
{
SelectorManager::getInstance()->getWorkingSelector()->mouseMoveEvent(mouseEvent, this);
update();
}
else
QGraphicsScene::mouseMoveEvent(mouseEvent);
}
void DesignerScene::mouseReleaseEvent(QGraphicsSceneMouseEvent* mouseEvent)
{
if(SelectorManager::getInstance())
{
SelectorManager::getInstance()->getWorkingSelector()->mouseReleaseEvent(mouseEvent, this);
update();
}
else
QGraphicsScene::mouseReleaseEvent(mouseEvent);
}
void DesignerScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* mouseEvent)
{
if(SelectorManager::getInstance())
{
SelectorManager::getInstance()->getWorkingSelector()->mouseDoubleClickEvent(mouseEvent, this);
update();
}
else
QGraphicsScene::mouseReleaseEvent(mouseEvent);
}
void DesignerScene::keyPressEvent(QKeyEvent* event)
{
QGraphicsScene::keyPressEvent(event);
}
void DesignerScene::keyReleaseEvent(QKeyEvent* event)
{
QGraphicsScene::keyReleaseEvent(event);
}
void DesignerScene::setGridVisible(bool bVisible)
{
m_bGridVisible = bVisible;
update();
}
void DesignerScene::callParentEvent(QGraphicsSceneMouseEvent* mouseEvent)
{
//调用QGraphicsScene的函数会直接触发一些操作比如取消item的selected状态从而触发item的一些函数itemChange,然后在item的相应函数中书写执行一些操作
switch (mouseEvent->type())
{
case QEvent::GraphicsSceneMousePress:
QGraphicsScene::mousePressEvent(mouseEvent);
break;
case QEvent::GraphicsSceneMouseMove:
QGraphicsScene::mouseMoveEvent(mouseEvent);
break;
case QEvent::GraphicsSceneMouseRelease:
QGraphicsScene::mouseReleaseEvent(mouseEvent);
break;
default:
break;
}
}
GraphicsItemGroup* DesignerScene::createGroup()
{
QList<QGraphicsItem*> listItem = selectedItems();
if(listItem.isEmpty())
return nullptr;
GraphicsItemGroup* group = new GraphicsItemGroup();
group->addItems(listItem);
addItem(group);
return group;
}
void DesignerScene::destroyGroup()
{
}