Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pmItem = QGraphicsPixmapItem(pm, None, self.scene)
- pmItem.setAcceptHoverEvents(True)
- pmItem.hoverMoveEvent = self.pixelSelect
- pmItem.hoverLeaveEvent = self.hover_leave
- def pixelSelect(self, event):
- for l in self.v_dist_lines:
- self.scene.removeItem(l)
- self.v_dist_lines = []
- for l in self.h_dist_lines:
- self.scene.removeItem(l)
- self.h_dist_lines = []
- for t in self.txt_dist_list:
- self.scene.removeItem(t)
- self.txt_dist_list = []
- position = QtCore.QPoint(event.pos().x(), event.pos().y())
- closest_h_line, closest_v_line = None, None
- dist_h, dist_v = self.scene_h, self.scene_w
- for l in self.h_lines_list:
- diff = math.fabs(position.y() - l.line().y1())
- if diff < dist_h:
- dist_h = diff
- closest_h_line = l
- for l in self.v_lines_list:
- diff = math.fabs(position.x() - l.line().x1())
- if diff < dist_v:
- dist_v = diff
- closest_v_line = l
- diff_h = round(float(dist_h) / QX11Info.appDpiY() * self.in2cm * (self.draw_scale_y / self.hcm), 2)
- diff_v = round(float(dist_v) / QX11Info.appDpiX() * self.in2cm * (self.draw_scale_x / self.wcm), 2)
- # print(diff_h, diff_v)
- result = ''
- if closest_h_line:
- result += 'Distance to nearest horizontal line is {:0.2f} cm'.format(diff_h)
- result += ',t'
- self.drawLine(position.x(), position.y(),
- position.x(), closest_h_line.line().y1(), self.h_dist_lines)
- txt = QGraphicsTextItem(QtCore.QString('{:0.2f}cm'.format(diff_h)),
- scene=self.scene)
- txt_x = position.x() + 10
- y_diff = abs(position.y() - closest_h_line.line().y1()) / 2
- txt_y = position.y() + y_diff if closest_h_line.line().y1() > position.y()
- else position.y() - y_diff
- txt.setPos(QtCore.QPointF(txt_x,
- txt_y))
- self.txt_dist_list.append(txt)
- if closest_v_line:
- result += 'Distance to nearest vertical line is {:0.2f} cm'.format(diff_v)
- self.drawLine(position.x(), position.y(),
- closest_v_line.line().x1(), position.y(), self.v_dist_lines)
- txt = QGraphicsTextItem(QtCore.QString('{:0.2f}cm'.format(diff_v)),
- scene=self.scene)
- x_diff = abs(position.x() - closest_v_line.line().x1()) / 2
- txt_x = position.x() + x_diff if closest_h_line.line().x1() > position.x()
- else position.x() - x_diff
- txt_y = position.y() + 10
- txt.setPos(QtCore.QPointF(txt_x,
- txt_y))
- self.txt_dist_list.append(txt)
- self.statusBar().showMessage(result)
Add Comment
Please, Sign In to add comment