class Column(QGraphicsPolygonItem): "Column number marker" def __init__(self): # The collision box is rectangular poly = QPolygonF() poly.append(QPointF(-0.25, 0.48)) poly.append(QPointF(-0.25, 0.02)) poly.append(QPointF(0.25, 0.02)) poly.append(QPointF(0.25, 0.48)) #l = 0.49/cos30 #for i in range(6): #a = i*tau/6-tau/12 #poly.append(QPointF(l*math.sin(a), -l*math.cos(a))) QGraphicsPolygonItem.__init__(self, poly) self.setBrush(QColor(255, 255, 255, 0)) #self.setPen(QPen(qt.red, 0)) self.setPen(no_pen) self._text = QGraphicsSimpleTextItem('v') self._text.setBrush(Color.dark_text) fit_inside(self, self._text, 0.8) #self._text.setY(self.text._y()+0.2) @property def text(self): return self._text.text() @text.setter def text(self, value): self._text.setText(value) if value: self._text.setX(-self._text.boundingRect().width() * self._text.scale() / 2) def upd(self): txt = str(self.value) together = self.together if together is not None: txt = ('{{{}}}' if together else '-{}-').format(txt) self.text = txt self.update() def paint(self, g, option, widget): QGraphicsPolygonItem.paint(self, g, option, widget) g.setTransform(self._text.sceneTransform(), True) self._text.paint(g, option, widget)
class Column(QGraphicsPolygonItem): "Column number marker" def __init__(self): # The collision box is rectangular poly = QPolygonF() poly.append(QPointF(-0.25, 0.48)) poly.append(QPointF(-0.25, 0.02)) poly.append(QPointF(0.25, 0.02)) poly.append(QPointF(0.25, 0.48)) # l = 0.49/cos30 # for i in range(6): # a = i*tau/6-tau/12 # poly.append(QPointF(l*math.sin(a), -l*math.cos(a))) QGraphicsPolygonItem.__init__(self, poly) self.setBrush(QColor(255, 255, 255, 0)) # self.setPen(QPen(qt.red, 0)) self.setPen(no_pen) self._text = QGraphicsSimpleTextItem("v") self._text.setBrush(Color.dark_text) fit_inside(self, self._text, 0.8) # self._text.setY(self.text._y()+0.2) @property def text(self): return self._text.text() @text.setter def text(self, value): self._text.setText(value) if value: self._text.setX(-self._text.boundingRect().width() * self._text.scale() / 2) def upd(self): txt = str(self.value) together = self.together if together is not None: txt = ("{{{}}}" if together else "-{}-").format(txt) self.text = txt self.update() def paint(self, g, option, widget): QGraphicsPolygonItem.paint(self, g, option, widget) g.setTransform(self._text.sceneTransform(), True) self._text.paint(g, option, widget)
class Column(QGraphicsPolygonItem, Item): "Column number marker" def __init__(self): QGraphicsPolygonItem.__init__(self, _col_poly) self.show_info = False self.setBrush(QColor(255, 255, 255, 0)) self.setPen(no_pen) self._text = QGraphicsSimpleTextItem('v') self._text.setBrush(Color.dark_text) update_font(self._text, lambda f: f.setWeight(55)) fit_inside(self, self._text, 0.86) #self._text.setY(self._text.y()+0.2) @setter_property def angle(self, value): if value not in (-60, 0, 60): raise ValueError(value) yield value @property def cell(self): return self.members[0] @cached_property def members(self): try: x, y = self.coord except AttributeError: return result = [] dx, dy = _col_angle_deltas[self.angle] while True: x += dx y += dy it = self.scene().grid.get((x, y)) if not it and not self.scene().grid_bounds.contains(x, y, False): break if isinstance(it, Cell): result.append(it) return result @cached_property def value(self): return sum(1 for it in self.members if it.kind is Cell.full) @cached_property def together(self): if self.show_info: groups = itertools.groupby(self.members, key=lambda it: it.kind is Cell.full) return sum(1 for full, _ in groups if full) <= 1 def reset_cache(self): for attr in ['members', 'value', 'together']: try: delattr(self, attr) except AttributeError: pass def upd(self): self.reset_cache() self.setRotation(self.angle or 1e-3) # not zero so font doesn't look different from rotated variants if not self.placed: return txt = str(self.value) together = self.together if together is not None: txt = ('{{{}}}' if together else '-{}-').format(txt) self._text.setText(txt) if txt: self._text.setX(-self._text.boundingRect().width()*self._text.scale()/2) self.update() def paint(self, g, option, widget): QGraphicsPolygonItem.paint(self, g, option, widget) g.setTransform(self._text.sceneTransform(), True) self._text.paint(g, option, widget) def __repr__(self): r = ['Column'] r.append(self._text.text()) try: r.append('#{}'.format(self.id)) except AttributeError: pass r.append('members:[{}]'.format(' '.join(m.__repr__(False) for m in self.members))) return '<{}>'.format(' '.join(str(p) for p in r if str(p)))