def __init__(self, *args, **kwargs): self.add_point = AddPoint() self.control = Control() Figure.__init__(self, self.add_point) self.decomposer = BayazitDecomposer() self.polygon = [] self.convex_polygons = [] if 'figure' in kwargs: self.polygon = [dict2qpoint(p) for p in kwargs['figure']['editor']] self.decompose() self.state = self.control if len(args) > 0: self.polygon = args
class Polygon(Figure): def __init__(self, *args, **kwargs): self.add_point = AddPoint() self.control = Control() Figure.__init__(self, self.add_point) self.decomposer = BayazitDecomposer() self.polygon = [] self.convex_polygons = [] if 'figure' in kwargs: self.polygon = [dict2qpoint(p) for p in kwargs['figure']['editor']] self.decompose() self.state = self.control if len(args) > 0: self.polygon = args def decompose(self): del self.convex_polygons[:] self.convex_polygons = self.decomposer.decompose(self.polygon) def draw(self, painter, scale): painter.save() painter.setPen(QPen(QBrush(QColor(232, 109, 21) if self.state is self.add_point else QColor(21, 144, 232)), self.lineWidth(scale), Qt.SolidLine)) painter.drawPolygon(QPolygon(self.polygon)) for i, poly in enumerate(self.convex_polygons): if poly: painter.setPen(QPen(QBrush(Figure.COLORS[i % len(Figure.COLORS)]), self.lineWidth(scale), Qt.SolidLine)) color = Figure.COLORS[i % len(Figure.COLORS)] color.setAlpha(150) painter.setBrush(QBrush(QColor(color))) painter.drawPolygon(QPolygon(poly)) for point in self.polygon: self.drawControlPoint(painter, point, QColor(31, 174, 222) if point is self.polygon[0] else QColor(222, 79, 31) if point is self.polygon[-1] else QColor(78, 222, 31), scale) Figure.draw(self, painter, scale) painter.restore() def getDict(self): return {'polygon': {'editor': [qpoint2dict(point) for point in self.polygon], 'convex': [[qpoint2dict(point) for point in convex] for convex in self.convex_polygons]}} def __str__(self): return self.__repr__() def __repr__(self): return 'Polygon(' + ('{}' * len(self.polygon)).format(*[qpoint2str(point) for point in self.polygon]) + ')'