1 Star 0 Fork 0

yuwei/expandImage

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
riggerscene.cpp 4.96 KB
一键复制 编辑 原始数据 按行查看 历史
yuwei 提交于 2024-07-22 00:58 . 整理代码
#include "riggerscene.h"
#include <QMenu>
#include <QRectF>
#include <QGraphicsItem>
#include <QDebug>
#include <QGraphicsSceneContextMenuEvent>
RiggerScene::RiggerScene(const QSizeF& size, int& type, QObject* parent)
: QGraphicsScene(QRectF(0, 0, size.width(), size.height()), parent),
ItemType(type)
{
setBackgroundBrush(QBrush(Qt::transparent));
setForegroundBrush(QBrush(Qt::transparent));
}
RiggerScene::~RiggerScene()
{
}
void RiggerScene::resizeScene(QRectF rect)
{
setSceneRect(rect);
}
void RiggerScene::judgmentAdsorption(QPointF childItemPos)
{
QPointF itemParentPos = QPointF(0, 0);
if (((childItemPos.x() - itemParentPos.x() >= -ADSORPTION_VALUE) && (ADSORPTION_VALUE >= (childItemPos.x() - itemParentPos.x())))
&& ((childItemPos.y() - itemParentPos.y() >= -ADSORPTION_VALUE) && (ADSORPTION_VALUE >= (childItemPos.y() - itemParentPos.y())))) {
AnchorItem* itemParent = new AnchorItem();
childItem->setPos(QPointF(0, 0));
qDebug() << childItem->mapFromScene(itemParentPos);
}
else if (((childItemPos.y() - itemParentPos.y() >= -ADSORPTION_VALUE) && (childItemPos.y() - itemParentPos.y() <= ADSORPTION_VALUE))) {
AnchorItem* itemParent = new AnchorItem();
childItem->setPos(QPointF(0, 0));
qDebug() << childItem->mapFromScene(itemParentPos);
}
else if (((childItemPos.x() - itemParentPos.x() >= -ADSORPTION_VALUE) && (ADSORPTION_VALUE >= (childItemPos.x() - itemParentPos.x())))) {
AnchorItem* itemParent = new AnchorItem();
childItem->setPos(QPointF(0, 0));
qDebug() << childItem->mapFromScene(itemParentPos);
}
update();
}
void RiggerScene::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
QGraphicsScene::mousePressEvent(event);
}
void RiggerScene::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
connect(childItem, &AnchorItem::itemPos, this, &RiggerScene::judgmentAdsorption);
QGraphicsScene::mouseMoveEvent(event);
}
void RiggerScene::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
QGraphicsScene::mouseReleaseEvent(event);
}
void RiggerScene::keyPressEvent(QKeyEvent* event)
{
if (event->key() == Qt::Key_Delete)
{
QList<QGraphicsItem*> list = selectedItems();
if (list.count())
{
foreach(QGraphicsItem * item, list)
{
removeItem(item);
}
}
}
QGraphicsScene::keyPressEvent(event);
}
void RiggerScene::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
{
QMenu menu;
QAction* action = menu.addAction(QIcon(":/icons/add_plus.png"), QString("添加图形"));
if (menu.exec(QCursor::pos()) == action)
{
QPen pen(Qt::red, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
switch (ItemType = DEF2_ITEM) {
case ELLIPSE_ITEM:
{
QGraphicsEllipseItem* ellipse = new QGraphicsEllipseItem(QRectF(event->scenePos(), QSizeF(200, 200)));
ellipse->setAcceptHoverEvents(true);
ellipse->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
pen.setWidth(1);
ellipse->setPen(pen);
ellipse->setBrush(QBrush(Qt::red));
ellipse->setOpacity(0.8);
addItem(ellipse);
}
break;
case LINE_ITEM:
{
QGraphicsLineItem* line = new QGraphicsLineItem(QLineF(event->scenePos(), event->scenePos() + QPointF(100, 50)));
line->setAcceptHoverEvents(true);
line->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
pen.setWidth(3);
line->setPen(pen);
line->setOpacity(0.8);
addItem(line);
}
break;
case RECT_ITEM:
{
QGraphicsRectItem* rect = new QGraphicsRectItem(QRectF(event->scenePos(), QSizeF(200, 150)));
rect->setAcceptHoverEvents(true);
rect->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
pen.setWidth(1);
rect->setPen(pen);
rect->setBrush(QBrush(Qt::red));
rect->setOpacity(0.8);
addItem(rect);
}
break;
case IMAGE_ITEM:
{
QGraphicsPixmapItem* pixmap = new QGraphicsPixmapItem(QPixmap(":/icons/circle_green.png"));
pixmap->setAcceptHoverEvents(true);
pixmap->setPos(event->scenePos());
pixmap->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
addItem(pixmap);
}
break;
case ANCHOR_ITEM:
{
AnchorItem* item = new AnchorItem(QRectF(event->scenePos(), QSizeF(150, 150)));
addItem(item);
}
break;
case DEF1_ITEM:
{
QPixmap map = QPixmap(":/icons/Circle1.png");
AnchorItem* item = new AnchorItem(QRectF(event->scenePos(), map.size()), map);
addItem(item);
}
break;
case DEF2_ITEM:
{
childItem = new AnchorItem(QRectF(event->scenePos(), QSizeF(100, 150)), QPixmap(":/FGF"));
addItem(childItem);
}
break;
default:
break;
}
}
QGraphicsScene::contextMenuEvent(event);
}
void RiggerScene::wheelEvent(QGraphicsSceneWheelEvent* event)
{
QGraphicsScene::wheelEvent(event);
}
void RiggerScene::dragEnterEvent(QGraphicsSceneDragDropEvent* event)
{
event->accept();
QGraphicsScene::dragEnterEvent(event);
}
void RiggerScene::dragMoveEvent(QGraphicsSceneDragDropEvent* event)
{
event->accept();
}
void RiggerScene::dropEvent(QGraphicsSceneDragDropEvent* event)
{
event->acceptProposedAction();
QGraphicsScene::dropEvent(event);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yuwei_2_838529949/expand-image.git
[email protected]:yuwei_2_838529949/expand-image.git
yuwei_2_838529949
expand-image
expandImage
master

搜索帮助