WindowAppUiView.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. #include "WindowAppUiView.h"
  2. #include "WindowAppUiTool.h"
  3. #include "UiManager.h"
  4. #include "WindowAppUiNavView.h"
  5. #include "CommonDraw.h"
  6. #include "WindowAppUiFrame.h"
  7. #include "VPCommand.h"
  8. #include "vpControls/VControlObject.h"
  9. WindowAppUiView::WindowAppUiView(QWidget *parent)
  10. : QGraphicsView(parent)
  11. , m_pBkImage(nullptr)
  12. , m_parentFrame(nullptr)
  13. , m_pUiScene(nullptr)
  14. , m_nGridSize(UI_DEFAULT_GRID_SIZE)
  15. {
  16. // 设置为接收拖拽
  17. this->setAcceptDrops(true);
  18. // 初始化方格背景(只需要初始化一次即可)
  19. this->initBkImage();
  20. this->setBackgroundRole(QPalette::Light);
  21. this->setAutoFillBackground(true);
  22. m_pRubberBand = nullptr;
  23. // 设置View大小
  24. this->resize(DEFAULT_UIVIEW_WIDTH + 1, DEFAULT_UIVIEW_HEIGHT+ 1);
  25. // 初始化并绑定Scene
  26. m_pUiScene = new WindowAppUiScene(this);
  27. //QRectF rc = this->rect();
  28. m_pUiScene->setSceneRect(QRectF(0, 0, DEFAULT_UIVIEW_WIDTH, DEFAULT_UIVIEW_HEIGHT));
  29. this->setScene(m_pUiScene);
  30. this->centerOn(0, 0);
  31. // 全界面刷新,防止出现拖尾
  32. this->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
  33. //设置橡皮筋选择选项
  34. this->setDragMode(QGraphicsView::RubberBandDrag);
  35. // 设置抗锯齿(解决link 拖拽的时候,预览线有锯齿)
  36. this->setRenderHint(QPainter::Antialiasing, true);
  37. }
  38. WindowAppUiView::~WindowAppUiView()
  39. {
  40. RELEASE(m_pBkImage);
  41. }
  42. /// <summary>
  43. /// 初始化
  44. /// </summary>
  45. /// <param name="strTitle"></param>
  46. /// <param name="navView"></param>
  47. /// <param name="objectController"></param>
  48. void WindowAppUiView::init(const QString& strTitle, WindowAppUiNavView* navView, WindowAppUiFrame* parentFrame, ObjectController* objectController)
  49. {
  50. // 设置页面名称
  51. this->m_strPageName = strTitle;
  52. // 2022-8-30 加入,绑定所属的Frame
  53. this->m_parentFrame = parentFrame;
  54. // 绑定属性表
  55. m_pUiScene->m_pPropertyController = objectController;
  56. // 将当前Scene绑定到鸟瞰图中进行同步
  57. navView->setScene(m_pUiScene);
  58. // 绑定同步的鸟瞰图
  59. navView->setMainView(this);
  60. // 调整View
  61. navView->fitInView(navView->sceneRect(), Qt::KeepAspectRatio);
  62. // 向UI Manager注册本View
  63. g_pUiManager->registerNewUi(this);
  64. // 与Scene一起设置为左对齐
  65. this->setAlignment(Qt::AlignLeft | Qt::AlignTop);
  66. }
  67. /// <summary>
  68. /// 初始化方格背景
  69. /// </summary>
  70. void WindowAppUiView::initBkImage()
  71. {
  72. // 棋盘格背景 栅格背景
  73. #if 0
  74. m_pBkImage = new QPixmap(32, 32);
  75. QColor color1(60, 60, 60);
  76. QColor color2(45, 45, 45);
  77. m_pBkImage->fill(color1);
  78. QPainter painter1(m_pBkImage);
  79. painter1.fillRect(0, 0, 16, 16, color2);
  80. painter1.fillRect(16, 16, 16, 16, color2);
  81. painter1.end();
  82. #else
  83. m_pBkImage = new QPixmap(QSize(static_cast<int>(width()), static_cast<int>(height())));
  84. m_pBkImage->fill(qRgb(61, 61, 61));
  85. // QPainter painter(m_pBkImage);
  86. // int gridSize = 10;
  87. // const int maxX = static_cast<int>(std::ceil(width()) / gridSize) * gridSize;
  88. // const int maxY = static_cast<int>(std::ceil(height()) / gridSize) * gridSize;
  89. // painter.setPen(qRgb(100, 100, 120));
  90. // painter.setBrush(Qt::NoBrush);
  91. // for (int i = 0; i < maxX; i += gridSize)
  92. // {
  93. // for (int j = 0; j < maxY; j += gridSize)
  94. // {
  95. // painter.drawPoint(i, j);
  96. // }
  97. // }
  98. #endif
  99. }
  100. /// <summary>
  101. /// 绘制界面背景
  102. /// </summary>
  103. /// <param name="painter"></param>
  104. /// <param name="rect"></param>
  105. void WindowAppUiView::drawBackground(QPainter* painter, const QRectF& rect)
  106. {
  107. if (m_pBkImage)
  108. {
  109. painter->drawTiledPixmap(rect, *m_pBkImage);
  110. }
  111. // 计算坐标
  112. int nLeft = 0;
  113. int nTop = 0;
  114. int nWwidth = width() ;
  115. int nHeight = height() ;
  116. int nStepW = (nWwidth + nLeft) / 3;
  117. int nStepH = (nHeight + nTop) / 3;
  118. painter->setPen(UI_LINE_BK_PEN);
  119. // 画两条横线
  120. painter->drawLine(QLine(nLeft, nStepH, nWwidth, nStepH));
  121. painter->drawLine(QLine(nLeft, nStepH * 2, nWwidth, nStepH * 2));
  122. // 画两条竖线
  123. painter->drawLine(QLine(nStepW, nTop, nStepW, nHeight));
  124. painter->drawLine(QLine(nStepW * 2, nTop, nStepW * 2, nHeight));
  125. // 绘制背景栅格点
  126. const int maxX = static_cast<int>(std::ceil(width()) / UI_DEFAULT_GRID_SIZE) * UI_DEFAULT_GRID_SIZE;
  127. const int maxY = static_cast<int>(std::ceil(height()) / UI_DEFAULT_GRID_SIZE) * UI_DEFAULT_GRID_SIZE;
  128. painter->setPen(UI_POINT_BK_PEN);
  129. painter->setBrush(Qt::NoBrush);
  130. for (int i = 0; i < maxX; i += UI_DEFAULT_GRID_SIZE)
  131. {
  132. for (int j = 0; j < maxY; j += UI_DEFAULT_GRID_SIZE)
  133. {
  134. painter->drawPoint(i, j);
  135. }
  136. }
  137. }
  138. /// <summary>
  139. /// 控件拖动完毕,在指定位置生成新的控件
  140. /// </summary>
  141. /// <param name="event"></param>
  142. void WindowAppUiView::dropEvent(QDropEvent* event)
  143. {
  144. //根据最后按下的控件位置生成控件
  145. QPoint point = event->pos();
  146. WindowAppUiTool* source = qobject_cast<WindowAppUiTool*>(event->source());
  147. if (source)
  148. {
  149. int row = source->currentRow();
  150. if (row < 0)
  151. {
  152. return;
  153. }
  154. //// 生成控件
  155. //VControlObject* pNewControl = m_pUiScene->newControl(row, point);
  156. //// 向UI注册本控件(2022-10-7移到了Scene中进行操作)
  157. //g_pUiManager->regiterNewControl(pNewControl, this);
  158. // 将界面中用户选择的行数转换成对应的valueType
  159. VALUE_TYPE controlType = (VALUE_TYPE)(row + (int)VALUE_TYPE::Control_Base + 1);
  160. // 2022-10-7,将UI控件的添加整合进Undo体系
  161. UiControlAddCommand* controlAddCommand = new UiControlAddCommand(m_pUiScene, controlType, mapToScene(point).toPoint());
  162. m_pUiScene->m_CommandManager.executeCommand(controlAddCommand);
  163. // 表示本事件可以在本窗体中拖动对象
  164. event->acceptProposedAction();
  165. return;
  166. }
  167. else
  168. {
  169. event->ignore();
  170. QWidget::dropEvent(event);
  171. }
  172. }
  173. ///// <summary>
  174. ///// 当控件正在拖动过程中
  175. ///// </summary>
  176. ///// <param name="event"></param>
  177. //void WindowAppUiView::dragEnterEvent(QDragEnterEvent* event)
  178. //{
  179. // //if (event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist")) {
  180. // event->setDropAction(Qt::MoveAction);
  181. // event->accept();
  182. // //}
  183. // //else {
  184. // // event->ignore();
  185. // //}
  186. //}
  187. /// <summary>
  188. /// 拖动中的移动消息
  189. /// </summary>
  190. /// <param name="event"></param>
  191. void WindowAppUiView::dragMoveEvent(QDragMoveEvent* event)
  192. {
  193. event->acceptProposedAction();
  194. event->accept();
  195. }
  196. //鼠标在窗体中按下时,创建一个QRubberBand 类,QRubberBand::Rectangle 是设置橡皮筋线的类型,
  197. //这种线的效果是描绘了一个方形的区域,还有一种是QRubberBand::Line,则为一个被直线填满的方形区域,
  198. //相当于一个阴影的方形区域。QRubberBand 应用最多的函数是 setGeometry(),其作用是设置了橡皮筋线的位置及大小。
  199. void WindowAppUiView::mousePressEvent(QMouseEvent* e)
  200. {
  201. m_tmpPoint = e->pos();
  202. if (!m_pRubberBand)
  203. {
  204. m_pRubberBand = new QRubberBand(QRubberBand::Line, this);
  205. }
  206. m_pRubberBand->setGeometry(QRect(m_tmpPoint, QSize()));
  207. m_pRubberBand->show();
  208. }
  209. //在鼠标按下,并且鼠标发生移动的时候,这时就可以会出橡皮线的区域,
  210. //鼠标拖动事件函数重载如下 改区域的大小由QRect(origin,e->pos()).normalized()) 来体现,
  211. //其中normalized() 函数返回的也是一个QRect的对象,不过该对象的长和宽的值都是大于零时值
  212. void WindowAppUiView::mouseMoveEvent(QMouseEvent* e)
  213. {
  214. if (m_pRubberBand)
  215. {
  216. m_pRubberBand->setGeometry(QRect(m_tmpPoint, e->pos()).normalized());
  217. }
  218. }
  219. /// <summary>
  220. /// 当鼠标松开时,橡皮筋线就可以隐藏了
  221. /// </summary>
  222. /// <param name="e"></param>
  223. void WindowAppUiView::mouseReleaseEvent(QMouseEvent* e)
  224. {
  225. Q_UNUSED(e);
  226. if (m_pRubberBand)
  227. {
  228. m_pRubberBand->hide();
  229. }
  230. }
  231. /// <summary>
  232. /// 尺寸改变后,同步调整Scene的尺寸
  233. /// </summary>
  234. /// <param name="resizeEvent"></param>
  235. void WindowAppUiView::resizeEvent(QResizeEvent* resizeEvent)
  236. {
  237. // qDebug() << "WindowAppUiView::resizeEvent - " << resizeEvent->size();
  238. int newWidth = resizeEvent->size().width();
  239. int newHeight = resizeEvent->size().height();
  240. m_pUiScene->setSceneRect(QRectF(0, 0, newWidth, newHeight));
  241. emit navigatorViewRequired(true, transform());
  242. }
  243. /// <summary>
  244. /// 序列化
  245. /// </summary>
  246. /// <param name="out"></param>
  247. /// <param name="pView"></param>
  248. /// <returns></returns>
  249. QDataStream& operator<<(QDataStream& out, WindowAppUiView* pView)
  250. {
  251. if (pView != nullptr)
  252. {
  253. // 序列化UI中的控件
  254. pView->uiScene()->serialized(out, false);
  255. // 序列化Frame中的参数
  256. pView->m_parentFrame->serialized(out, false);
  257. }
  258. // Error
  259. else
  260. {
  261. vDebug() << "[Error] WindowAppUiView - operator<< failed, pView == nullptr";
  262. }
  263. return out;
  264. }
  265. /// <summary>
  266. /// 反序列化
  267. /// </summary>
  268. /// <param name="in"></param>
  269. /// <param name="pView"></param>
  270. /// <returns></returns>
  271. QDataStream& operator>>(QDataStream& in, WindowAppUiView* pView)
  272. {
  273. if (pView != nullptr)
  274. {
  275. // 反序列化UI中的控件
  276. pView->uiScene()->serialized(in, true);
  277. // 序列化Frame中的参数
  278. pView->m_parentFrame->serialized(in, true);
  279. }
  280. // Error
  281. else
  282. {
  283. vDebug() << "[Error] WindowAppUiView - operator>> failed, pView == nullptr";
  284. }
  285. return in;
  286. }
  287. ///// <summary>
  288. ///// 更新用户设定的尺寸
  289. ///// </summary>
  290. ///// <param name="nWidth"></param>
  291. ///// <param name="nHeight"></param>
  292. //void WindowAppUiView::updateUiSize(int nWidth, int nHeight)
  293. //{
  294. //
  295. //}
  296. ///// <summary>
  297. ///// dll中的控件同步消息
  298. ///// </summary>
  299. ///// <param name=""></param>
  300. ///// <param name="msg"></param>
  301. //void WindowAppUiView::controlSync(QWidget* pWidget, UI_SYNC_MSG msg)
  302. //{
  303. // // 查找本窗体中是否有此控件
  304. // for (int i = 0; i < selectWidgets.count(); i++)
  305. // {
  306. // VControlObject* pControlObject = qobject_cast<VControlObject*>(selectWidgets.at(i)->getWidget());
  307. //
  308. // if (pControlObject->m_pDllControl == pWidget)
  309. // {
  310. //
  311. //
  312. // break;
  313. // }
  314. // }
  315. //}
  316. ///// <summary>
  317. ///// 过滤子控件消息
  318. ///// </summary>
  319. ///// <param name=""></param>
  320. ///// <param name=""></param>
  321. ///// <returns></returns>
  322. //bool WindowAppUiView::eventFilter(QObject* object, QEvent* event)
  323. //{
  324. // if (object->objectName() == CLASS_NAME_LINEDIT)
  325. // {
  326. // if (event->type() == QEvent::Paint)
  327. // {
  328. // qDebug() << "WindowAppUiView::eventFilter - " << ((VLineEdit*)object)->getText();
  329. //
  330. // // 向dll同步
  331. // g_pUiManager->syncToDll(object, UI_SYNC_MSG::EDIT_TEXT_CHANGED);
  332. // }
  333. // }
  334. //
  335. // // qDebug() << object->objectName();
  336. //
  337. // return QWidget::eventFilter(object, event);
  338. //}