Guest User

Untitled

a guest
Oct 21st, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. pmItem = QGraphicsPixmapItem(pm, None, self.scene)
  2. pmItem.setAcceptHoverEvents(True)
  3. pmItem.hoverMoveEvent = self.pixelSelect
  4. pmItem.hoverLeaveEvent = self.hover_leave
  5.  
  6. def pixelSelect(self, event):
  7. for l in self.v_dist_lines:
  8. self.scene.removeItem(l)
  9. self.v_dist_lines = []
  10. for l in self.h_dist_lines:
  11. self.scene.removeItem(l)
  12. self.h_dist_lines = []
  13. for t in self.txt_dist_list:
  14. self.scene.removeItem(t)
  15. self.txt_dist_list = []
  16.  
  17. position = QtCore.QPoint(event.pos().x(), event.pos().y())
  18. closest_h_line, closest_v_line = None, None
  19. dist_h, dist_v = self.scene_h, self.scene_w
  20. for l in self.h_lines_list:
  21. diff = math.fabs(position.y() - l.line().y1())
  22. if diff < dist_h:
  23. dist_h = diff
  24. closest_h_line = l
  25.  
  26. for l in self.v_lines_list:
  27. diff = math.fabs(position.x() - l.line().x1())
  28. if diff < dist_v:
  29. dist_v = diff
  30. closest_v_line = l
  31.  
  32. diff_h = round(float(dist_h) / QX11Info.appDpiY() * self.in2cm * (self.draw_scale_y / self.hcm), 2)
  33. diff_v = round(float(dist_v) / QX11Info.appDpiX() * self.in2cm * (self.draw_scale_x / self.wcm), 2)
  34.  
  35. # print(diff_h, diff_v)
  36.  
  37. result = ''
  38. if closest_h_line:
  39. result += 'Distance to nearest horizontal line is {:0.2f} cm'.format(diff_h)
  40. result += ',t'
  41. self.drawLine(position.x(), position.y(),
  42. position.x(), closest_h_line.line().y1(), self.h_dist_lines)
  43. txt = QGraphicsTextItem(QtCore.QString('{:0.2f}cm'.format(diff_h)),
  44. scene=self.scene)
  45. txt_x = position.x() + 10
  46. y_diff = abs(position.y() - closest_h_line.line().y1()) / 2
  47. txt_y = position.y() + y_diff if closest_h_line.line().y1() > position.y()
  48. else position.y() - y_diff
  49. txt.setPos(QtCore.QPointF(txt_x,
  50. txt_y))
  51. self.txt_dist_list.append(txt)
  52. if closest_v_line:
  53. result += 'Distance to nearest vertical line is {:0.2f} cm'.format(diff_v)
  54. self.drawLine(position.x(), position.y(),
  55. closest_v_line.line().x1(), position.y(), self.v_dist_lines)
  56. txt = QGraphicsTextItem(QtCore.QString('{:0.2f}cm'.format(diff_v)),
  57. scene=self.scene)
  58. x_diff = abs(position.x() - closest_v_line.line().x1()) / 2
  59. txt_x = position.x() + x_diff if closest_h_line.line().x1() > position.x()
  60. else position.x() - x_diff
  61. txt_y = position.y() + 10
  62. txt.setPos(QtCore.QPointF(txt_x,
  63. txt_y))
  64. self.txt_dist_list.append(txt)
  65.  
  66. self.statusBar().showMessage(result)
Add Comment
Please, Sign In to add comment