#include "WindowAppBlockPortOld.h"
#include "WindowAppItemInterface.h"
#include "WindowAppPouManager.h"
extern WindowAppPouManager pouManager;
WindowAppBlockPortOld::WindowAppBlockPortOld(const TOOL_EX* pTool, QGraphicsItem* parent)
: QGraphicsTextItem(parent)
{
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemIsSelectable);
setFlag(QGraphicsItem::ItemSendsGeometryChanges);
toolInfo = pTool;
editAction = new QAction(tr("&Edit Value"), this);
editAction->setStatusTip(tr("Edit value"));
connect(editAction, &QAction::triggered, this, &WindowAppBlockPortOld::onMenuEdit);
setFont(FONT_PBD);
setTextInteractionFlags(Qt::TextEditorInteraction);
setDefaultTextColor(COLOR_TEXT);
UpdateRect();
}
/////////////////////////////////////////////////////////
// 返回功能块图形的总体矩形区域
QRectF WindowAppBlockPortOld::boundingRect() const
{
return blockBoundingRect;
}
///////////////////////////////////////////////////
// 根据用户的输入实时更新矩形区域的尺寸
void WindowAppBlockPortOld::UpdateRect()
{
// 计算高度
int blockHeight = PBD_BASIC_HEIGHT;
// 计算宽度
int blocWidth = PBD_BASIC_WIDTH;
// TODO : 根据工具名字字符的宽度计算实际需要的宽度
// 主体矩形区域
blockRect.setRect(
0,
0,
blocWidth,
blockHeight
);
// 序号矩形区域
blockIndexRect.setRect(
blockRect.right() - TBD_INDEX_WIDTH,
blockRect.top() - TBD_INDEX_HEIGHT / 2,
TBD_INDEX_WIDTH,
TBD_INDEX_HEIGHT
);
//// 总体矩形边界(双接口)
//blockBoundingRect.setRect(
// blockRect.left() - TBD_INF_LINE,
// blockRect.top() - TBD_INDEX_HEIGHT / 2,
// blockRect.width() + TBD_INF_LINE + TBD_INF_LINE,
// blockRect.height() + TBD_INDEX_HEIGHT + TBD_SHADOW_COUNT * PEN_LINE_WIDTH
//);
//// 总体矩形边界(单接口)
//blockBoundingRect.setRect(
// blockRect.left(),
// blockRect.top() - TBD_INDEX_HEIGHT / 2,
// blockRect.width() + TBD_INF_LINE,
// blockRect.height() + TBD_INDEX_HEIGHT + TBD_SHADOW_COUNT * PEN_LINE_WIDTH
//);
// 总体矩形边界(未计算Interface的范围)
blockBoundingRect.setRect(
blockRect.left(),
blockRect.top() - TBD_INDEX_HEIGHT / 2,
blockRect.width() + TBD_SHADOW_COUNT * PEN_LINE_WIDTH,
blockRect.height() + TBD_INDEX_HEIGHT / 2 + TBD_SHADOW_COUNT * PEN_LINE_WIDTH
);
}
void WindowAppBlockPortOld::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
//if (option->state & QStyle::State_Selected)
//{
// isSelected = true;
//}
//else
//{
// isSelected = false;
//}
bSel = isSelected();
Q_UNUSED(widget);
painter->save();
//// 每次绘制之前都更新一下矩形区域
//UpdateRect();
// 绘制外框
DrawBlock(painter);
// 绘制阴影
DrawBlockShadow(painter);
// 绘制序号
DrawIndex(painter);
//// 绘制接口
//DrawInterface(painter);
painter->restore();
QGraphicsTextItem::paint(painter, option, widget);
}
///////////////////////////////////////////////////////
// 显示右键菜单
void WindowAppBlockPortOld::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
{
scene()->clearSelection();
setSelected(true);
//blockMenu->exec(event->screenPos());
}
///
/// 获得焦点时
///
///
void WindowAppBlockPortOld::focusInEvent(QFocusEvent* event)
{
if (event->reason() != Qt::PopupFocusReason)
{
// 保存原始文本
// 注意右键菜单再次进入焦点时不保存原始文本
m_strInput = toPlainText();
}
QGraphicsTextItem::focusInEvent(event);
}
///
/// 焦点移除时
///
///
void WindowAppBlockPortOld::focusOutEvent(QFocusEvent* event)
{
// 右键点击其他地方失去焦点,定义为取消操作
if (event->reason() == Qt::MouseFocusReason && QApplication::mouseButtons() == Qt::RightButton)
{
// 恢复原始文本
setPlainText(m_strInput);
// 恢复不能编辑状态
setTextInteractionFlags(Qt::NoTextInteraction);
}
//右键弹出菜单时不做处理
else if (event->reason() == Qt::PopupFocusReason)
{
}
// 其他情况,例如键盘输入回车等,表示编辑成功,发送信号给父对象
else
{
setTextInteractionFlags(Qt::NoTextInteraction);
// TODO:本信号暂未用到
emit inputOverSignal(toPlainText());
}
QGraphicsTextItem::focusOutEvent(event);
}
///
/// 鼠标双击时进入可编辑状态并打开焦点
///
///
void WindowAppBlockPortOld::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
{
if (event->button() == Qt::LeftButton)
{
setTextInteractionFlags(Qt::TextEditorInteraction);
setFocus();
QGraphicsTextItem::mouseDoubleClickEvent(event);
}
}
///
/// 鼠标单击时
///
///
void WindowAppBlockPortOld::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
// 左键点击是可编辑状态的话响应单击事件
if (event->button() == Qt::LeftButton)
{
// 是不可编辑状态的话,也就是未双击进入编辑状态时不响应,一遍产生双击事件
if (textInteractionFlags() != Qt::NoTextInteraction)
{
QGraphicsTextItem::mousePressEvent(event);
}
//return;
}
QGraphicsTextItem::mousePressEvent(event);
}
///
/// 键盘按下时
///
///
void WindowAppBlockPortOld::keyPressEvent(QKeyEvent* event)
{
// 点击回车失去焦点,编辑完成,后续给FocusOutEvent处理
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
{
clearFocus();
return;
}
QGraphicsTextItem::keyPressEvent(event);
}
///
/// 添加接口
///
void WindowAppBlockPortOld::addItemInterfaces()
{
// 为本Block添加新的ItemInterface
for (TOOL_INTERFACE_EX* pInf : qAsConst(toolInfo->Interfaces))
{
WindowAppItemInterface* pNewItemInf = new WindowAppItemInterface(pInf, this);
//// 输入接口(Port类型基本没有输入接口,所以此处应该不会执行)
//if (pInf->infDirection == INF_DIRECTION::INF_IN)
//{
// pNewItemInf->bInfOut = false;
//}
pouManager.AddRunningInterface(pNewItemInf, pInf);
// 保存接口Item信息
itemInterfaces.append(pNewItemInf);
// 界面中添加此Interface(设置为从属关系之后,都不需要手工添加此Item了,真是方便啊)
// scene()->addItem(pNewItemInf);
}
// 更新Interface的位置(注意此处坐标系需要转换一下)
QPointF pt = mapToScene(0, 0);
QRectF rcBlock = blockRect;
rcBlock.translate(pt);
__super::updateAllInterfacesPostion(rcBlock);
}
///
/// 功能块移动时,通知对应的Interface同步移动
///
///
///
///
QVariant WindowAppBlockPortOld::itemChange(GraphicsItemChange change, const QVariant& value)
{
if (change == QGraphicsItem::ItemSelectedHasChanged)
{
emit selectedChange(this);
}
else if (change == QGraphicsItem::ItemPositionChange)
{
// 更新Interface的位置(此处Qt自动处理了,不需要额外处理)
// __super::updateAllInterfacesPostion(blockRect, true);
emit blockMoveSignal(this);
}
else if (change == QGraphicsItem::ItemSelectedChange)
{
// 如果选中状态变更的话,就重绘所有的Interface
__super::redrawAllInterface();
}
return value;
}
///////////////////////////////////////////////////////////////////
// 菜单 - Edit
void WindowAppBlockPortOld::onMenuEdit()
{
QMessageBox::information(nullptr, "Vision Plus", "onMenuEdit");
//// 获取当前选中的Block
//QGraphicsItem* activeBlock = this->selectedItems().first();
//pouManager.ToolExecuteSub(activeBlock);
}