paperedithandle.cpp 18 KB

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