paperedithandle.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. #include "paperedithandle.h"
  2. #include "qpainter.h"
  3. #include <QMouseEvent>
  4. #include <QDateTime>
  5. #include <QMessageBox>
  6. #include <QDir>
  7. #define painter_offset 46
  8. CPaperEditHandle::CPaperEditHandle(QWidget *parent)
  9. : QWidget(parent), m_room(1.0f), m_offset(QPoint(0, 0)), m_mousestatus(Mouse_Normal),
  10. m_bpressed(false), m_startPoint(QPoint(0, 0)), m_endPoint(QPoint(0, 0)), m_pBatchInfo(nullptr),
  11. m_pPapers(nullptr), m_nCurrentPaper(0), m_locateW(0), m_locateH(0), m_bLocateChecked(false),
  12. m_bEraserChecked(false)
  13. {
  14. ui.setupUi(this);
  15. ui.widget_eraser->hide();
  16. ui.widget_locate->hide();
  17. ClickDrag();
  18. connect(ui.toolButton_drag, SIGNAL(clicked()), this, SLOT(slot_onButtonClick()));
  19. connect(ui.toolButton_roomin, SIGNAL(clicked()), this, SLOT(slot_onButtonClick()));
  20. connect(ui.toolButton_rommout, SIGNAL(clicked()), this, SLOT(slot_onButtonClick()));
  21. connect(ui.toolButton_90, SIGNAL(clicked()), this, SLOT(slot_onButtonClick()));
  22. connect(ui.toolButton_N90, SIGNAL(clicked()), this, SLOT(slot_onButtonClick()));
  23. connect(ui.toolButton_locate, SIGNAL(clicked()), this, SLOT(slot_onButtonClick()));
  24. connect(ui.toolButton_eraser, SIGNAL(clicked()), this, SLOT(slot_onButtonClick()));
  25. connect(ui.toolButton_switchpage, SIGNAL(clicked()), this, SLOT(slot_onButtonClick()));
  26. connect(ui.toolButton_undo, SIGNAL(clicked()), this, SLOT(slot_onButtonClick()));
  27. connect(ui.toolButton_restore, SIGNAL(clicked()), this, SLOT(slot_onButtonClick()));
  28. connect(ui.toolButton_locate1, SIGNAL(clicked()), this, SLOT(slot_onButtonClick()));
  29. connect(ui.toolButton_locate2, SIGNAL(clicked()), this, SLOT(slot_onButtonClick()));
  30. connect(ui.toolButton_locate3, SIGNAL(clicked()), this, SLOT(slot_onButtonClick()));
  31. connect(ui.toolButton_locate4, SIGNAL(clicked()), this, SLOT(slot_onButtonClick()));
  32. connect(ui.toolButton_eraser1, SIGNAL(clicked()), this, SLOT(slot_onButtonClick()));
  33. connect(ui.toolButton_eraser2, SIGNAL(clicked()), this, SLOT(slot_onButtonClick()));
  34. connect(ui.toolButton_eraser3, SIGNAL(clicked()), this, SLOT(slot_onButtonClick()));
  35. connect(ui.toolButton_page0, SIGNAL(clicked()), this, SLOT(slot_onButtonClick()));
  36. connect(ui.toolButton_page1, SIGNAL(clicked()), this, SLOT(slot_onButtonClick()));
  37. connect(ui.toolButton_paper_pre, SIGNAL(clicked()), this, SLOT(slot_onButtonClick()));
  38. connect(ui.toolButton_paper_next, SIGNAL(clicked()), this, SLOT(slot_onButtonClick()));
  39. ui.widget_painter->m_bgimage = &m_bgimage;
  40. ui.widget_painter->m_offset = &m_offset;
  41. ui.widget_painter->m_room = &m_room;
  42. this->setMouseTracking(true);// 追踪鼠标
  43. //初始化定位点映射map
  44. m_locatePointMap[ui.toolButton_locate] = QPoint(0, 0);
  45. m_locatePointMap[ui.toolButton_locate1] = QPoint(21, 10);
  46. m_locatePointMap[ui.toolButton_locate2] = QPoint(40, 20);
  47. m_locatePointMap[ui.toolButton_locate3] = QPoint(50, 28);
  48. m_locatePointMap[ui.toolButton_locate4] = QPoint(60, 30);
  49. }
  50. CPaperEditHandle::~CPaperEditHandle()
  51. {
  52. }
  53. void CPaperEditHandle::slot_onButtonClick()
  54. {
  55. ui.widget_locate->hide();
  56. ui.widget_eraser->hide();
  57. QObject* sender = this->sender();
  58. if (sender == ui.toolButton_drag)
  59. {
  60. ClickDrag();
  61. }
  62. else if (sender == ui.toolButton_roomin)
  63. {
  64. ClickRoomin();
  65. }
  66. else if (sender == ui.toolButton_rommout)
  67. {
  68. ClickRoomout();
  69. }
  70. else if (sender == ui.toolButton_90)
  71. {
  72. ClickRotate90();
  73. }
  74. else if (sender == ui.toolButton_N90)
  75. {
  76. ClickRotateN90();
  77. }
  78. else if (m_locatePointMap.count(sender) == 1)
  79. {
  80. ClickLocate(sender);
  81. }
  82. else if (sender == ui.toolButton_eraser || sender == ui.toolButton_eraser1 || sender == ui.toolButton_eraser2 || sender == ui.toolButton_eraser3)
  83. {
  84. ClickEraser(sender);
  85. }
  86. else if (sender == ui.toolButton_switchpage)
  87. {
  88. ClickSwitchPage();
  89. }
  90. else if (sender == ui.toolButton_undo)
  91. {
  92. ClickUndo();
  93. }
  94. else if (sender == ui.toolButton_restore)
  95. {
  96. ClickRestore();
  97. }
  98. else if (sender == ui.toolButton_page0 || sender == ui.toolButton_page1)
  99. {
  100. if (!m_pPapers || (*m_pPapers).size() == 0 || m_nCurrentPaper >= (*m_pPapers).size())
  101. {
  102. return;
  103. }
  104. if (sender == ui.toolButton_page0)
  105. {
  106. m_nCurrentPage = 0;
  107. }
  108. else
  109. {
  110. m_nCurrentPage = 1;
  111. }
  112. m_bgimage = QImage(m_mapStackPage[m_nCurrentPage].top().c_str());
  113. CalcRoom();
  114. update();
  115. }
  116. else if (sender == ui.toolButton_paper_next || sender == ui.toolButton_paper_pre)
  117. {
  118. if (!m_pPapers || (*m_pPapers).size() == 0 || m_nCurrentPaper >= (*m_pPapers).size())
  119. {
  120. return;
  121. }
  122. if (sender == ui.toolButton_paper_pre)
  123. {
  124. if (m_nCurrentPaper == 0)
  125. {
  126. return;
  127. }
  128. m_nCurrentPaper--;
  129. }
  130. else
  131. {
  132. if (m_nCurrentPaper + 1 == (*m_pPapers).size())
  133. {
  134. return;
  135. }
  136. m_nCurrentPaper++;
  137. }
  138. ui.label_total_count->setText(QString::fromStdWString(L"%1/%2").arg(m_nCurrentPaper + 1).arg((*m_pPapers).size()));
  139. ui.toolButton_page0->setChecked(true);
  140. m_nCurrentPage = 0;
  141. m_bgimage = QImage((*m_pPapers)[m_nCurrentPaper].page0_path);
  142. CalcRoom();
  143. m_mapStackPage.clear();
  144. m_mapStackPage[0].push_back((*m_pPapers)[m_nCurrentPaper].page0_path);
  145. m_mapStackPage[1].push_back((*m_pPapers)[m_nCurrentPaper].page1_path);
  146. update();
  147. }
  148. }
  149. void CPaperEditHandle::ClickDrag()
  150. {
  151. if (ui.toolButton_drag->isChecked())
  152. {
  153. m_mousestatus = Mouse_Drag;
  154. }
  155. else
  156. {
  157. m_mousestatus = Mouse_Normal;
  158. }
  159. setCursor(Qt::SizeAllCursor);
  160. m_bLocateChecked = false;
  161. m_bEraserChecked = false;
  162. }
  163. void CPaperEditHandle::ClickRoomin()
  164. {
  165. m_offset.setX(0);
  166. m_offset.setY(0);
  167. m_room *= 1.1f;
  168. update();
  169. //if (mousestatus == Mouse_Locate)
  170. //{
  171. // QPixmap pixmap((locateW + 1)*room, (locateH + 1)*room);
  172. // QPainter painterim(&pixmap);
  173. // painterim.setRenderHint(QPainter::Antialiasing);
  174. // pixmap.fill(Qt::black); //填充1像素值
  175. // painterim.drawRect(0, 0, (locateW + 1)*room, (locateH + 1)*room);
  176. // QCursor cursor(pixmap, 0, 0); //加载xpm生成的图标文件
  177. // setCursor(cursor); //设置自定义的鼠标样式
  178. //}
  179. }
  180. void CPaperEditHandle::ClickRoomout()
  181. {
  182. m_offset.setX(0);
  183. m_offset.setY(0);
  184. m_room /= 1.1f;
  185. update();
  186. //if (mousestatus == Mouse_Locate)
  187. //{
  188. // QPixmap pixmap((locateW + 1)*room, (locateH + 1)*room);
  189. // QPainter painterim(&pixmap);
  190. // painterim.setRenderHint(QPainter::Antialiasing);
  191. // pixmap.fill(Qt::black); //填充1像素值
  192. // painterim.drawRect(0, 0, (locateW + 1)*room, (locateH + 1)*room);
  193. // QCursor cursor(pixmap, 0, 0); //加载xpm生成的图标文件
  194. // setCursor(cursor); //设置自定义的鼠标样式
  195. //}
  196. }
  197. void CPaperEditHandle::ClickRotate90()
  198. {
  199. m_offset.setX(0);
  200. m_offset.setY(0);
  201. QMatrix matrix;
  202. matrix.rotate(90.0);
  203. m_bgimage = m_bgimage.transformed(matrix, Qt::FastTransformation);
  204. update();
  205. SaveTempImg();
  206. /*if (!ui.toolButton_cur->isChecked())
  207. {
  208. return;
  209. }
  210. m_bIsEdited = true;
  211. SaveTempImg(&bgimage);*/
  212. }
  213. void CPaperEditHandle::ClickRotateN90()
  214. {
  215. m_offset.setX(0);
  216. m_offset.setY(0);
  217. QMatrix matrix;
  218. matrix.rotate(-90.0);
  219. m_bgimage = m_bgimage.transformed(matrix, Qt::FastTransformation);
  220. update();
  221. SaveTempImg();
  222. /*if (!ui.toolButton_cur->isChecked())
  223. {
  224. return;
  225. }
  226. m_bIsEdited = true;
  227. SaveTempImg(&bgimage);*/
  228. }
  229. void CPaperEditHandle::ClickLocate(QObject* sender)
  230. {
  231. int locX, locY;
  232. if (sender == ui.toolButton_locate)
  233. {
  234. m_bEraserChecked = false;
  235. if (ui.toolButton_locate->isChecked() && m_bLocateChecked == false)
  236. {
  237. m_mousestatus = Mouse_Locate;
  238. m_bLocateChecked = true;
  239. ui.widget_locate->show();
  240. QObject * tObj = ui.toolButton_locate1->isChecked() ? ui.toolButton_locate1 : ui.toolButton_locate2;
  241. tObj = ui.toolButton_locate3->isChecked() ? ui.toolButton_locate3 : tObj;
  242. locX = m_locatePointMap[tObj].x();
  243. locY = m_locatePointMap[sender].y();
  244. }
  245. else
  246. {
  247. /*m_mousestatus = Mouse_Normal;
  248. ui.widget_locate->hide();*/
  249. m_bLocateChecked = false;
  250. ui.toolButton_drag->setChecked(true);
  251. ClickDrag();
  252. return;
  253. }
  254. }
  255. else
  256. {
  257. locX = m_locatePointMap[sender].x();
  258. locY = m_locatePointMap[sender].y();
  259. }
  260. double pdiX = m_bgimage.dotsPerMeterX();
  261. double pdiY = m_bgimage.dotsPerMeterY();
  262. int x = qRound(pdiX*0.0254);
  263. int y = qRound(pdiY*0.0254);
  264. m_locateW = locX * (x / 96);
  265. m_locateH = locY * (y / 96);
  266. QPixmap pixmap((m_locateW + 1)*m_room, (m_locateH + 1)*m_room);
  267. QPainter painterim(&pixmap);
  268. painterim.setRenderHint(QPainter::Antialiasing);
  269. pixmap.fill(Qt::black); //填充1像素值
  270. painterim.drawRect(0, 0, (m_locateW + 1)*m_room, (m_locateH + 1)*m_room);
  271. QCursor cursor(pixmap, 0, 0); //加载xpm生成的图标文件
  272. setCursor(cursor); //设置自定义的鼠标样式
  273. }
  274. void CPaperEditHandle::ClickEraser(QObject* sender)
  275. {
  276. if (sender == ui.toolButton_eraser)
  277. {
  278. m_bLocateChecked = false;
  279. if (ui.toolButton_eraser->isChecked() && m_bEraserChecked == false)
  280. {
  281. m_mousestatus = Mouse_Eraser;
  282. m_bEraserChecked = true;
  283. ui.widget_eraser->show();
  284. }
  285. else
  286. {
  287. /*m_mousestatus = Mouse_Normal;
  288. ui.widget_eraser->hide();*/
  289. m_bEraserChecked = false;
  290. ui.toolButton_drag->setChecked(true);
  291. ClickDrag();
  292. return;
  293. }
  294. m_eraserW = 17;
  295. m_eraserH = 13;
  296. }
  297. else if (sender == ui.toolButton_eraser1)
  298. {
  299. m_eraserW = 13;
  300. m_eraserH = 9;
  301. }
  302. else if (sender == ui.toolButton_eraser2)
  303. {
  304. m_eraserW = 17;
  305. m_eraserH = 13;
  306. }
  307. else if (sender == ui.toolButton_eraser3)
  308. {
  309. m_eraserW = 23;
  310. m_eraserH = 17;
  311. }
  312. QPixmap pixmap(m_eraserW + 1, m_eraserH + 1);
  313. QPainter painterim(&pixmap);
  314. painterim.setRenderHint(QPainter::Antialiasing);
  315. pixmap.fill(Qt::white); //填充1像素值
  316. painterim.drawRect(0, 0, m_eraserW + 1, m_eraserH + 1);
  317. QCursor cursor(pixmap, 0, 0); //加载xpm生成的图标文件
  318. setCursor(cursor); //设置自定义的鼠标样式
  319. }
  320. void CPaperEditHandle::ClickSwitchPage()
  321. {
  322. QString msg = QString::fromLocal8Bit("换页后,将清空所有对图片的修改,");
  323. msg += QString::fromLocal8Bit("确定要换页吗?");
  324. QMessageBox::StandardButton result = QMessageBox::information(NULL, QString::fromLocal8Bit("提示"), msg, QMessageBox::Yes | QMessageBox::No);
  325. if (result == QMessageBox::Yes)
  326. {
  327. if (!m_pPapers || (*m_pPapers).size() == 0 || m_nCurrentPaper >= (*m_pPapers).size())
  328. {
  329. return;
  330. }
  331. string strPath0 = (*m_pPapers)[m_nCurrentPaper].page0_path;
  332. string strPath1 = (*m_pPapers)[m_nCurrentPaper].page1_path;
  333. strcpy_s((*m_pPapers)[m_nCurrentPaper].page0_path, strPath1.c_str());
  334. strcpy_s((*m_pPapers)[m_nCurrentPaper].page1_path, strPath0.c_str());
  335. m_bgimage = QImage((*m_pPapers)[m_nCurrentPaper].page0_path);
  336. m_mapStackPage.clear();
  337. m_mapStackPage[0].push_back((*m_pPapers)[m_nCurrentPaper].page0_path);
  338. m_mapStackPage[1].push_back((*m_pPapers)[m_nCurrentPaper].page1_path);
  339. ui.toolButton_page0->click();
  340. }
  341. }
  342. void CPaperEditHandle::ClickUndo()
  343. {
  344. if (m_mapStackPage[m_nCurrentPage].size() == 1) // 只有原始试卷的路径
  345. {
  346. return;
  347. }
  348. m_mapStackPage[m_nCurrentPage].pop();
  349. m_bgimage = QImage(m_mapStackPage[m_nCurrentPage].top().c_str());
  350. update();
  351. }
  352. void CPaperEditHandle::ClickRestore()
  353. {
  354. QString msg = QString::fromLocal8Bit("还原后,将清空所有对图片的修改,");
  355. while (m_mapStackPage[m_nCurrentPage].size() > 1)
  356. {
  357. m_mapStackPage[m_nCurrentPage].pop();
  358. }
  359. m_bgimage = QImage(m_mapStackPage[m_nCurrentPage].top().c_str());
  360. update();
  361. }
  362. void CPaperEditHandle::resizeEvent(QResizeEvent *event)
  363. {
  364. QRect rcClient = geometry();
  365. QRect rcTool = rcClient;
  366. rcTool.setBottom(rcTool.top() + ui.widget_tool->height());
  367. ui.widget_tool->setGeometry(rcTool);
  368. QRect rcLocate = ui.toolButton_locate->geometry();
  369. rcLocate.setTop(rcLocate.bottom() + 2);
  370. rcLocate.setBottom(rcLocate.top() + 44);
  371. rcLocate.setLeft(245);
  372. rcLocate.setRight(rcLocate.left() + 211);
  373. ui.widget_locate->setGeometry(rcLocate);
  374. QRect rcEraser = ui.toolButton_eraser->geometry();
  375. rcEraser.setTop(rcEraser.bottom() + 2);
  376. rcEraser.setBottom(rcEraser.top() + 44);
  377. rcEraser.setLeft(297);
  378. rcEraser.setRight(rcEraser.left() + 162);
  379. ui.widget_eraser->setGeometry(rcEraser);
  380. QRect rcLeft = ui.toolButton_drag->geometry();
  381. rcLeft.setTop(rcLeft.bottom() + 2);
  382. rcLeft.setBottom(rcLeft.top() + ui.widget_leftbutton->height());
  383. rcLeft.setRight(rcLeft.x() + ui.widget_leftbutton->width());
  384. ui.widget_leftbutton->setGeometry(rcLeft);
  385. QRect rcRight = rcTool;
  386. rcRight.setTop(rcRight.bottom() + 2);
  387. rcRight.setBottom(rcRight.top() + ui.widget_rightbutton->height());
  388. rcRight.setLeft(rcRight.x() + ui.widget_tool->width() - ui.widget_rightbutton->width());
  389. ui.widget_rightbutton->setGeometry(rcRight);
  390. QRect rcPainter = rcClient;
  391. rcPainter.setLeft(painter_offset);
  392. rcPainter.setTop(painter_offset);
  393. rcPainter.setWidth(rcClient.width() - painter_offset * 2);
  394. rcPainter.setHeight(rcClient.height() - painter_offset);
  395. ui.widget_painter->setGeometry(rcPainter);
  396. }
  397. QPoint CPaperEditHandle::PointSwitchToImg(QPoint sPoint)
  398. {
  399. sPoint.setX(sPoint.x() - painter_offset);
  400. sPoint.setY(sPoint.y() - painter_offset);
  401. int x = sPoint.x() - m_offset.x();
  402. x = x / m_room;
  403. int y = sPoint.y() - m_offset.y();
  404. y = y / m_room;
  405. return QPoint(x, y);
  406. }
  407. QRect CPaperEditHandle::RectSwitchImg(QRect sRect)
  408. {
  409. QPoint lefttop = PointSwitchToImg(QPoint(sRect.left(), sRect.top()));
  410. QPoint rightbottom = PointSwitchToImg(QPoint(sRect.right(), sRect.bottom()));
  411. return QRect(lefttop, rightbottom);
  412. }
  413. void CPaperEditHandle::SaveTempImg()
  414. {
  415. QDateTime currentTime = QDateTime::currentDateTime();
  416. char path[512] = { 0 };
  417. sprintf(path, "%stemp/%s.jpg", m_pBatchInfo->work_dir, currentTime.toString("yyyyMMddhhmmsszzz").toStdString().c_str());
  418. m_bgimage.save(path, "JPG", 255);
  419. m_mapStackPage[m_nCurrentPage].push(path);
  420. }
  421. void CPaperEditHandle::mouseMoveEvent(QMouseEvent *event)
  422. {
  423. QPoint pos(event->pos());
  424. QPainter painterim(&m_bgimage);
  425. painterim.setRenderHint(QPainter::Antialiasing);
  426. if (m_bpressed)
  427. {
  428. if (m_mousestatus == Mouse_Eraser)
  429. {
  430. painterim.eraseRect(RectSwitchImg(QRect(pos.x(), pos.y(), m_eraserW, m_eraserH)));
  431. update();
  432. SaveTempImg();
  433. }
  434. else if (m_mousestatus == Mouse_Drag)
  435. {
  436. m_endPoint = pos;
  437. m_offset.setX(m_endPoint.x() - m_startPoint.x() + m_offset.x());
  438. m_offset.setY(m_endPoint.y() - m_startPoint.y() + m_offset.y());
  439. m_startPoint = m_endPoint;
  440. QRect rcClient = ui.widget_painter->geometry();
  441. if (m_bgimage.width() * m_room <= rcClient.width()) //不能拖动
  442. {
  443. m_offset.setX(0);
  444. }
  445. else
  446. {
  447. int offxMax = 0, offxMin = rcClient.width() - m_bgimage.width() * m_room;
  448. if (m_offset.x() < offxMin)
  449. {
  450. m_offset.setX(offxMin);
  451. }
  452. else if (m_offset.x() > offxMax)
  453. {
  454. m_offset.setX(offxMax);
  455. }
  456. }
  457. if (m_bgimage.height()*m_room <= rcClient.height()) //不能拖动
  458. {
  459. m_offset.setY(0);
  460. }
  461. else
  462. {
  463. int offyMax = 0, offyMin = rcClient.height() - m_bgimage.height()*m_room;
  464. if (m_offset.y() < offyMin)
  465. {
  466. m_offset.setY(offyMin);
  467. }
  468. else if (m_offset.y() > offyMax)
  469. {
  470. m_offset.setY(offyMax);
  471. }
  472. }
  473. update();
  474. }
  475. }
  476. }
  477. void CPaperEditHandle::mouseReleaseEvent(QMouseEvent *event)
  478. {
  479. QPoint pos(event->pos());
  480. m_bpressed = false;
  481. if (m_mousestatus == Mouse_Eraser)
  482. {
  483. QPainter painterim(&m_bgimage);
  484. painterim.setRenderHint(QPainter::Antialiasing);
  485. painterim.eraseRect(RectSwitchImg(QRect(pos.x(), pos.y(), m_eraserW, m_eraserH)));
  486. SaveTempImg();
  487. update();
  488. }
  489. else if (m_mousestatus == Mouse_Locate)
  490. {
  491. QPainter painterim(&m_bgimage);
  492. painterim.setRenderHint(QPainter::SmoothPixmapTransform);
  493. QPoint pt = PointSwitchToImg(pos);
  494. QRect rc(pt.x(), pt.y(), m_locateW, m_locateH);
  495. QRect target = rc;
  496. painterim.setBrush(Qt::black);
  497. painterim.drawRect(target);
  498. SaveTempImg();
  499. update();
  500. }
  501. }
  502. void CPaperEditHandle::mousePressEvent(QMouseEvent *event)
  503. {
  504. ui.widget_locate->hide();
  505. ui.widget_eraser->hide();
  506. QPoint pos(event->pos());
  507. m_startPoint = pos;
  508. m_endPoint = pos;
  509. /*if (!ui.widget_Drag->isVisible())
  510. {
  511. ispressed = true;
  512. }*/
  513. m_bpressed = true;
  514. }
  515. void CPaperEditHandle::paintEvent(QPaintEvent *e)
  516. {
  517. /*QRect rc(this->rect());
  518. QPainter painter(this);
  519. painter.setRenderHint(QPainter::Antialiasing);
  520. painter.translate(m_offset.x() + 50, m_offset.y() + 50);
  521. painter.scale(m_room, m_room);
  522. painter.drawImage(QPoint(0, 0), m_bgimage);*/
  523. }
  524. void CPaperEditHandle::setCurrentBatch(batch_info* batchInfo, vector<paper_info>* papers)
  525. {
  526. m_pBatchInfo = batchInfo;
  527. m_pPapers = papers;
  528. m_nCurrentPaper = 0;
  529. m_nCurrentPage = 0;
  530. m_room = 1.0f;
  531. m_locateW = 0;
  532. m_locateH = 0;
  533. m_offset = QPoint(0, 0);
  534. ui.toolButton_drag->setChecked(true);
  535. ui.widget_locate->hide();
  536. ui.widget_eraser->hide();
  537. ClickDrag();
  538. m_mapStackPage.clear();
  539. if (!m_pPapers || (*m_pPapers).size() == 0)
  540. {
  541. return;
  542. }
  543. QString temp_path(m_pBatchInfo->work_dir);
  544. temp_path.append("temp");
  545. QDir dir(temp_path);
  546. if (!dir.exists())
  547. {
  548. dir.mkpath(temp_path);
  549. }
  550. ui.label_total_count->setText(QString::fromStdWString(L"%1/%2").arg(m_nCurrentPaper + 1).arg((*m_pPapers).size()));
  551. m_bgimage = QImage((*m_pPapers)[m_nCurrentPaper].page0_path);
  552. CalcRoom();
  553. // m_stackImage.push_back(m_bgimage);
  554. m_mapStackPage[0].push_back((*m_pPapers)[m_nCurrentPaper].page0_path);
  555. m_mapStackPage[1].push_back((*m_pPapers)[m_nCurrentPaper].page1_path);
  556. update();
  557. }
  558. void CPaperEditHandle::GetCurrentPaper(paper_info& paperInfo)
  559. {
  560. if (!m_pPapers || (*m_pPapers).size() == 0 || m_nCurrentPaper >= (*m_pPapers).size() || m_mapStackPage.size() == 0)
  561. {
  562. return;
  563. }
  564. paperInfo.id = (*m_pPapers)[m_nCurrentPaper].id;
  565. strcpy_s(paperInfo.page0_path, m_mapStackPage[0].top().c_str());
  566. strcpy_s(paperInfo.page1_path, m_mapStackPage[1].top().c_str());
  567. }
  568. void CPaperEditHandle::CalcRoom()
  569. {
  570. if (m_bgimage.width() == 0 || m_bgimage.height() == 0)
  571. {
  572. return;
  573. }
  574. QRect rcClient = geometry();
  575. int _width = rcClient.width() - 2 * painter_offset;
  576. int _height = rcClient.height() - painter_offset;
  577. if (m_bgimage.width() > _width || m_bgimage.height() > _height)
  578. {
  579. m_room = min(_width / (float)m_bgimage.width(), _height / (float)m_bgimage.height());
  580. }
  581. }
  582. void CPaperEditHandle::wheelEvent(QWheelEvent *event) // 滚轮事件
  583. {
  584. /*if (QApplication::keyboardModifiers() == Qt::ControlModifier)*/
  585. {
  586. if (event->delta() > 0)
  587. { // 当滚轮远离使用者时
  588. ClickRoomin(); // 进行放大
  589. }
  590. else
  591. { // 当滚轮向使用者方向旋转时
  592. ClickRoomout(); // 进行缩小
  593. }
  594. }
  595. }