WindowAppBlockPortOld.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #include "WindowAppBlockPortOld.h"
  2. #include "WindowAppItemInterface.h"
  3. #include "WindowAppPouManager.h"
  4. extern WindowAppPouManager pouManager;
  5. WindowAppBlockPortOld::WindowAppBlockPortOld(const TOOL_EX* pTool, QGraphicsItem* parent)
  6. : QGraphicsTextItem(parent)
  7. {
  8. setFlag(QGraphicsItem::ItemIsMovable);
  9. setFlag(QGraphicsItem::ItemIsSelectable);
  10. setFlag(QGraphicsItem::ItemSendsGeometryChanges);
  11. toolInfo = pTool;
  12. editAction = new QAction(tr("&Edit Value"), this);
  13. editAction->setStatusTip(tr("Edit value"));
  14. connect(editAction, &QAction::triggered, this, &WindowAppBlockPortOld::onMenuEdit);
  15. setFont(FONT_PBD);
  16. setTextInteractionFlags(Qt::TextEditorInteraction);
  17. setDefaultTextColor(COLOR_TEXT);
  18. UpdateRect();
  19. }
  20. /////////////////////////////////////////////////////////
  21. // 返回功能块图形的总体矩形区域
  22. QRectF WindowAppBlockPortOld::boundingRect() const
  23. {
  24. return blockBoundingRect;
  25. }
  26. ///////////////////////////////////////////////////
  27. // 根据用户的输入实时更新矩形区域的尺寸
  28. void WindowAppBlockPortOld::UpdateRect()
  29. {
  30. // 计算高度
  31. int blockHeight = PBD_BASIC_HEIGHT;
  32. // 计算宽度
  33. int blocWidth = PBD_BASIC_WIDTH;
  34. // TODO : 根据工具名字字符的宽度计算实际需要的宽度
  35. // 主体矩形区域
  36. blockRect.setRect(
  37. 0,
  38. 0,
  39. blocWidth,
  40. blockHeight
  41. );
  42. // 序号矩形区域
  43. blockIndexRect.setRect(
  44. blockRect.right() - TBD_INDEX_WIDTH,
  45. blockRect.top() - TBD_INDEX_HEIGHT / 2,
  46. TBD_INDEX_WIDTH,
  47. TBD_INDEX_HEIGHT
  48. );
  49. //// 总体矩形边界(双接口)
  50. //blockBoundingRect.setRect(
  51. // blockRect.left() - TBD_INF_LINE,
  52. // blockRect.top() - TBD_INDEX_HEIGHT / 2,
  53. // blockRect.width() + TBD_INF_LINE + TBD_INF_LINE,
  54. // blockRect.height() + TBD_INDEX_HEIGHT + TBD_SHADOW_COUNT * PEN_LINE_WIDTH
  55. //);
  56. //// 总体矩形边界(单接口)
  57. //blockBoundingRect.setRect(
  58. // blockRect.left(),
  59. // blockRect.top() - TBD_INDEX_HEIGHT / 2,
  60. // blockRect.width() + TBD_INF_LINE,
  61. // blockRect.height() + TBD_INDEX_HEIGHT + TBD_SHADOW_COUNT * PEN_LINE_WIDTH
  62. //);
  63. // 总体矩形边界(未计算Interface的范围)
  64. blockBoundingRect.setRect(
  65. blockRect.left(),
  66. blockRect.top() - TBD_INDEX_HEIGHT / 2,
  67. blockRect.width() + TBD_SHADOW_COUNT * PEN_LINE_WIDTH,
  68. blockRect.height() + TBD_INDEX_HEIGHT / 2 + TBD_SHADOW_COUNT * PEN_LINE_WIDTH
  69. );
  70. }
  71. void WindowAppBlockPortOld::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
  72. {
  73. //if (option->state & QStyle::State_Selected)
  74. //{
  75. // isSelected = true;
  76. //}
  77. //else
  78. //{
  79. // isSelected = false;
  80. //}
  81. bSel = isSelected();
  82. Q_UNUSED(widget);
  83. painter->save();
  84. //// 每次绘制之前都更新一下矩形区域
  85. //UpdateRect();
  86. // 绘制外框
  87. DrawBlock(painter);
  88. // 绘制阴影
  89. DrawBlockShadow(painter);
  90. // 绘制序号
  91. DrawIndex(painter);
  92. //// 绘制接口
  93. //DrawInterface(painter);
  94. painter->restore();
  95. QGraphicsTextItem::paint(painter, option, widget);
  96. }
  97. ///////////////////////////////////////////////////////
  98. // 显示右键菜单
  99. void WindowAppBlockPortOld::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
  100. {
  101. scene()->clearSelection();
  102. setSelected(true);
  103. //blockMenu->exec(event->screenPos());
  104. }
  105. /// <summary>
  106. /// 获得焦点时
  107. /// </summary>
  108. /// <param name="event"></param>
  109. void WindowAppBlockPortOld::focusInEvent(QFocusEvent* event)
  110. {
  111. if (event->reason() != Qt::PopupFocusReason)
  112. {
  113. // 保存原始文本
  114. // 注意右键菜单再次进入焦点时不保存原始文本
  115. m_strInput = toPlainText();
  116. }
  117. QGraphicsTextItem::focusInEvent(event);
  118. }
  119. /// <summary>
  120. /// 焦点移除时
  121. /// </summary>
  122. /// <param name="event"></param>
  123. void WindowAppBlockPortOld::focusOutEvent(QFocusEvent* event)
  124. {
  125. // 右键点击其他地方失去焦点,定义为取消操作
  126. if (event->reason() == Qt::MouseFocusReason && QApplication::mouseButtons() == Qt::RightButton)
  127. {
  128. // 恢复原始文本
  129. setPlainText(m_strInput);
  130. // 恢复不能编辑状态
  131. setTextInteractionFlags(Qt::NoTextInteraction);
  132. }
  133. //右键弹出菜单时不做处理
  134. else if (event->reason() == Qt::PopupFocusReason)
  135. {
  136. }
  137. // 其他情况,例如键盘输入回车等,表示编辑成功,发送信号给父对象
  138. else
  139. {
  140. setTextInteractionFlags(Qt::NoTextInteraction);
  141. // TODO:本信号暂未用到
  142. emit inputOverSignal(toPlainText());
  143. }
  144. QGraphicsTextItem::focusOutEvent(event);
  145. }
  146. /// <summary>
  147. /// 鼠标双击时进入可编辑状态并打开焦点
  148. /// </summary>
  149. /// <param name="event"></param>
  150. void WindowAppBlockPortOld::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
  151. {
  152. if (event->button() == Qt::LeftButton)
  153. {
  154. setTextInteractionFlags(Qt::TextEditorInteraction);
  155. setFocus();
  156. QGraphicsTextItem::mouseDoubleClickEvent(event);
  157. }
  158. }
  159. /// <summary>
  160. /// 鼠标单击时
  161. /// </summary>
  162. /// <param name="event"></param>
  163. void WindowAppBlockPortOld::mousePressEvent(QGraphicsSceneMouseEvent* event)
  164. {
  165. // 左键点击是可编辑状态的话响应单击事件
  166. if (event->button() == Qt::LeftButton)
  167. {
  168. // 是不可编辑状态的话,也就是未双击进入编辑状态时不响应,一遍产生双击事件
  169. if (textInteractionFlags() != Qt::NoTextInteraction)
  170. {
  171. QGraphicsTextItem::mousePressEvent(event);
  172. }
  173. //return;
  174. }
  175. QGraphicsTextItem::mousePressEvent(event);
  176. }
  177. /// <summary>
  178. /// 键盘按下时
  179. /// </summary>
  180. /// <param name="event"></param>
  181. void WindowAppBlockPortOld::keyPressEvent(QKeyEvent* event)
  182. {
  183. // 点击回车失去焦点,编辑完成,后续给FocusOutEvent处理
  184. if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
  185. {
  186. clearFocus();
  187. return;
  188. }
  189. QGraphicsTextItem::keyPressEvent(event);
  190. }
  191. /// <summary>
  192. /// 添加接口
  193. /// </summary>
  194. void WindowAppBlockPortOld::addItemInterfaces()
  195. {
  196. // 为本Block添加新的ItemInterface
  197. for (TOOL_INTERFACE_EX* pInf : qAsConst(toolInfo->Interfaces))
  198. {
  199. WindowAppItemInterface* pNewItemInf = new WindowAppItemInterface(pInf, this);
  200. //// 输入接口(Port类型基本没有输入接口,所以此处应该不会执行)
  201. //if (pInf->infDirection == INF_DIRECTION::INF_IN)
  202. //{
  203. // pNewItemInf->bInfOut = false;
  204. //}
  205. pouManager.AddRunningInterface(pNewItemInf, pInf);
  206. // 保存接口Item信息
  207. itemInterfaces.append(pNewItemInf);
  208. // 界面中添加此Interface(设置为从属关系之后,都不需要手工添加此Item了,真是方便啊)
  209. // scene()->addItem(pNewItemInf);
  210. }
  211. // 更新Interface的位置(注意此处坐标系需要转换一下)
  212. QPointF pt = mapToScene(0, 0);
  213. QRectF rcBlock = blockRect;
  214. rcBlock.translate(pt);
  215. __super::updateAllInterfacesPostion(rcBlock);
  216. }
  217. /// <summary>
  218. /// 功能块移动时,通知对应的Interface同步移动
  219. /// </summary>
  220. /// <param name="change"></param>
  221. /// <param name="value"></param>
  222. /// <returns></returns>
  223. QVariant WindowAppBlockPortOld::itemChange(GraphicsItemChange change, const QVariant& value)
  224. {
  225. if (change == QGraphicsItem::ItemSelectedHasChanged)
  226. {
  227. emit selectedChange(this);
  228. }
  229. else if (change == QGraphicsItem::ItemPositionChange)
  230. {
  231. // 更新Interface的位置(此处Qt自动处理了,不需要额外处理)
  232. // __super::updateAllInterfacesPostion(blockRect, true);
  233. emit blockMoveSignal(this);
  234. }
  235. else if (change == QGraphicsItem::ItemSelectedChange)
  236. {
  237. // 如果选中状态变更的话,就重绘所有的Interface
  238. __super::redrawAllInterface();
  239. }
  240. return value;
  241. }
  242. ///////////////////////////////////////////////////////////////////
  243. // 菜单 - Edit
  244. void WindowAppBlockPortOld::onMenuEdit()
  245. {
  246. QMessageBox::information(nullptr, "Vision Plus", "onMenuEdit");
  247. //// 获取当前选中的Block
  248. //QGraphicsItem* activeBlock = this->selectedItems().first();
  249. //pouManager.ToolExecuteSub(activeBlock);
  250. }