def paintEvent(self, event): width = self.width() height = self.height() x_right, x_left, y_top, y_bottom = self.zoom_to_fit_limit() x_diff = x_left - x_right y_diff = y_top - y_bottom x_diff = x_diff if x_diff != 0 else 1 y_diff = y_diff if y_diff != 0 else 1 if width / x_diff < height / y_diff: factor = width / x_diff else: factor = height / y_diff self.zoom = factor * 0.95 self.ox = width / 2 - (x_left + x_right) / 2 * self.zoom self.oy = height / 2 + (y_top + y_bottom) / 2 * self.zoom super(DynamicCanvas, self).paintEvent(event) #Points that in the current angle section. self.Point = [] for i, name in enumerate(self.exp_symbol): if (name in self.mechanism['Driver']) or ( name in self.mechanism['Follower']): self.Point.append(self.mechanism[name]) else: try: x, y = self.Path.path[i][self.index] except IndexError: self.index += 1 return else: if isnan(x): self.index += 1 return self.Point.append((x, y)) #Draw links. for i, exp in enumerate(self.links): if i == 0: continue name = "link_{}".format(i) self.drawLink(name, tuple(self.exp_symbol.index(tag) for tag in exp)) #Draw path. self.drawPath() #Draw solving path. self.drawSlvsPath() #Draw points. for i, name in enumerate(self.exp_symbol): coordinate = self.Point[i] if coordinate: color = colorQt('Green') fixed = False if name in self.mechanism['Target']: color = colorQt('Dark-Orange') elif name in self.mechanism['Driver']: color = colorQt('Red') fixed = True elif name in self.mechanism['Follower']: color = colorQt('Blue') fixed = True self.drawPoint(i, coordinate[0], coordinate[1], fixed, color) self.painter.end()
def drawPath(self): pen = QPen() Path = self.Path.path for i, path in enumerate(Path): color = colorQt('Green') if self.exp_symbol[i] in self.mechanism['Target']: color = colorQt('Dark-Orange') pen.setColor(color) pen.setWidth(self.pathWidth) self.painter.setPen(pen) self.drawCurve(path)
def drawPath(self): """Draw a path. A simple function than main canvas. """ pen = QPen() Path = self.Path.path for i, path in enumerate(Path): color = colorQt('Green') if self.exp_symbol[i] in self.mechanism['Target']: color = colorQt('Dark-Orange') pen.setColor(color) pen.setWidth(self.pathWidth) self.painter.setPen(pen) self._BaseCanvas__drawCurve(path)
def drawLink(self, name: str, points: Tuple[int]): color = colorQt('Blue') pen = QPen(color) pen.setWidth(self.linkWidth) self.painter.setPen(pen) brush = QColor(226, 219, 190) brush.setAlphaF(0.70) self.painter.setBrush(brush) qpoints = tuple( QPointF(self.Point[i][0] * self.zoom, self.Point[i][1] * -self.zoom) for i in points if self.Point[i] and not isnan(self.Point[i][0])) if len(qpoints) == len(points): self.painter.drawPolygon(*qpoints) self.painter.setBrush(Qt.NoBrush) if self.showPointMark and name != 'ground' and qpoints: pen.setColor(Qt.darkGray) self.painter.setPen(pen) self.painter.setFont(QFont('Arial', self.fontSize)) text = "[{}]".format(name) cenX = sum(self.Point[i][0] for i in points if self.Point[i]) / len(points) cenY = sum(self.Point[i][1] for i in points if self.Point[i]) / len(points) self.painter.drawText(QPointF(cenX * self.zoom, cenY * -self.zoom), text)
def __drawPath(self): """Draw paths. Recording first.""" pen = QPen() if self.autoPathShow and self.rightInput(): """Replace to auto preview path.""" exprs = self.getTriangle(self.Points) self.Path.path = expr_path( exprs, {n: 'P{}'.format(n) for n in range(len(self.Points))}, self.Points, self.pathInterval()) if self.solutionShow: for expr in exprs: self._BaseCanvas__drawSolution(expr[0], expr[1:-1], expr[-1], self.Points) if hasattr(self, 'path_record'): paths = self.path_record else: paths = self.Path.path for i, path in enumerate(paths): if ((self.Path.show != i) and (self.Path.show != -1) or (len(path) <= 1)): continue try: color = self.Points[i].color except: color = colorQt('Green') pen.setColor(color) pen.setWidth(self.pathWidth) self.painter.setPen(pen) if self.Path.curve: self._BaseCanvas__drawCurve(path) else: self._BaseCanvas__drawDot(path)
def drawPath(self): pen = QPen() if hasattr(self, 'PathRecord'): Path = self.PathRecord else: Path = self.Path.path for i, path in enumerate(Path): if self.Path.show != i and self.Path.show != -1: continue if len(set(path)) > 1: try: color = self.Point[i].color except: color = colorQt('Green') pen.setColor(color) pen.setWidth(self.pathWidth) self.painter.setPen(pen) if self.Path.curve: self.drawCurve(path) else: self.drawDot(path)
def color(self) -> 'QColor': return colorQt(self.__color)
def paintEvent(self, event): """Drawing functions.""" width = self.width() height = self.height() x_right, x_left, y_top, y_bottom = self.__zoomToFitLimit() x_diff = x_left - x_right y_diff = y_top - y_bottom x_diff = x_diff if x_diff != 0 else 1 y_diff = y_diff if y_diff != 0 else 1 if width / x_diff < height / y_diff: factor = width / x_diff else: factor = height / y_diff self.zoom = factor * 0.95 self.ox = width / 2 - (x_left + x_right) / 2 * self.zoom self.oy = height / 2 + (y_top + y_bottom) / 2 * self.zoom super(DynamicCanvas, self).paintEvent(event) #Points that in the current angle section. """First check.""" for path in self.Path.path: if not path: continue x, y = path[self.index] if isnan(x): self.index, self.no_error = self.no_error, self.index self.ERROR = True self.Point = [] for i, name in enumerate(self.exp_symbol): if (name in self.mechanism['Driver']) or ( name in self.mechanism['Follower']): self.Point.append(self.mechanism[name]) else: x, y = self.Path.path[i][self.index] self.Point.append((x, y)) #Draw links. for i, exp in enumerate(self.links): if i == 0: continue name = "link_{}".format(i) self.drawLink(name, tuple(self.exp_symbol.index(tag) for tag in exp)) #Draw path. self.drawPath() #Draw solving path. self._BaseCanvas__drawTargetPath() #Draw points. for i, name in enumerate(self.exp_symbol): if not self.Point[i]: continue x, y = self.Point[i] color = colorQt('Green') fixed = False if name in self.mechanism['Target']: color = colorQt('Dark-Orange') elif name in self.mechanism['Driver']: color = colorQt('Red') fixed = True elif name in self.mechanism['Follower']: color = colorQt('Blue') fixed = True self._BaseCanvas__drawPoint(i, x, y, fixed, color) self.painter.end() if self.ERROR: self.ERROR = False self.index, self.no_error = self.no_error, self.index else: self.no_error = self.index
def paintEvent(self, event): """Draw the structure.""" width = self.width() height = self.height() self.ox = width/2 self.oy = height/2 sq_w = 240 if width <= height: self.zoom = width / sq_w else: self.zoom = height / sq_w super(PreviewCanvas, self).paintEvent(event) self.drawLimit(sq_w) pen = QPen() pen.setWidth(self.r) self.painter.setPen(pen) self.painter.setBrush(QBrush(QColor(226, 219, 190, 150))) #Links for link in self.G.nodes: if link==self.grounded: continue points = [] #Points that is belong with the link. for num, edge in edges_view(self.G): if link in edge: if num in self.same: num = self.same[num] x, y = self.pos[num] points.append((x*self.zoom, y*-self.zoom)) #Customize points. for name, link_ in self.cus.items(): if link==link_: num = int(name.replace('P', '')) x, y = self.pos[num] points.append((x*self.zoom, y*-self.zoom)) self.painter.drawPolygon(*convex_hull(points)) self.painter.setFont(QFont("Arial", self.fontSize*1.5)) #Nodes for node, (x, y) in self.pos.items(): if node in self.same: continue x *= self.zoom y *= -self.zoom if node in (self.Driver, self.Target): if node==self.Driver: pen.setColor(colorQt('Red')) elif node==self.Target: pen.setColor(colorQt('Yellow')) self.painter.setPen(pen) self.painter.drawEllipse(QPointF(x, y), self.r, self.r) if self.getStatus(node): color = colorQt('Dark-Magenta') else: color = colorQt('Green') pen.setColor(color) self.painter.setPen(pen) self.painter.setBrush(QBrush(color)) self.painter.drawEllipse(QPointF(x, y), self.r, self.r) pen.setColor(colorQt('Black')) self.painter.setPen(pen) #Solutions if self.showSolutions: solutions = ';'.join(self.get_solutions()) if solutions: for func, args, target in triangle_expr(solutions): self.drawSolution(func, args, target) #Text of node. pen.setColor(Qt.black) self.painter.setPen(pen) for node, (x, y) in self.pos.items(): if node in self.same: continue name = 'P{}'.format(node) if self.name_dict: name = self.name_dict[name] self.painter.drawText(QPointF( x*self.zoom + 2*self.r, y*-self.zoom ), name) self.painter.end()
def paintEvent(self, event): width = self.width() height = self.height() self.ox = width / 2 self.oy = height / 2 sq_w = 240 if width <= height: self.zoom = width / sq_w else: self.zoom = height / sq_w super(PreviewCanvas, self).paintEvent(event) #Center square. limit = sq_w / 2 * self.zoom self.painter.drawLine(QPointF(-limit, limit), QPointF(limit, limit)) self.painter.drawLine(QPointF(-limit, limit), QPointF(-limit, -limit)) self.painter.drawLine(QPointF(-limit, -limit), QPointF(limit, -limit)) self.painter.drawLine(QPointF(limit, -limit), QPointF(limit, limit)) r = 4.5 pen = QPen() pen.setWidth(r) self.painter.setPen(pen) self.painter.setBrush(QBrush(QColor(226, 219, 190, 150))) #Links for link in self.G.nodes: if link == self.grounded: continue points = [] #Points that is belong with the link. for num, edge in edges_view(self.G): if link in edge: if num in self.same: num = self.same[num] x, y = self.pos[num] points.append((x * self.zoom, y * -self.zoom)) #Customize points. for name, link_ in self.cus.items(): if link == link_: num = int(name.replace('P', '')) x, y = self.pos[num] points.append((x * self.zoom, y * -self.zoom)) self.painter.drawPolygon(*distance_sorted(points)) self.painter.setFont(QFont("Arial", self.fontSize * 1.5)) #Nodes for node, (x, y) in self.pos.items(): if node in self.same: continue color = colorQt('Dark-Magenta') if self.getStatus( node) else colorQt('Green') pen.setColor(color) self.painter.setPen(pen) self.painter.setBrush(QBrush(color)) self.painter.drawEllipse(QPointF(x * self.zoom, y * -self.zoom), r, r) pen.setColor(colorQt('Black')) self.painter.setPen(pen) #Solutions if self.showSolutions: solutions = ';'.join(self.get_solutions()) if solutions: for func, args, target in triangle_expr(solutions): params = [args[0], args[-1]] params.append(target) color = QColor(121, 171, 252) if func == 'PLLP' else QColor( 249, 84, 216) color.setAlpha(255) pen.setColor(color) self.painter.setPen(pen) for n in (0, 1): x, y = self.pos[int(params[-1].replace('P', ''))] x2, y2 = self.pos[int(params[n].replace('P', ''))] self.drawArrow(x * self.zoom, y * -self.zoom, x2 * self.zoom, y2 * -self.zoom) color.setAlpha(30) self.painter.setBrush(QBrush(color)) qpoints = [] for name in params: x, y = self.pos[int(name.replace('P', ''))] qpoints.append(QPointF(x * self.zoom, y * -self.zoom)) self.painter.drawPolygon(*qpoints) #Text of node. pen.setColor(Qt.black) self.painter.setPen(pen) for node, (x, y) in self.pos.items(): if node in self.same: continue name = 'P{}'.format(node) if self.name_dict: name = self.name_dict[name] self.painter.drawText( QPointF(x * self.zoom + 2 * r, y * -self.zoom), name) self.painter.end()