def info_label(self, anchor, comps, title=None): """ Make an info label `plot item` (:py:class:`guiqwt.label.DataInfoLabel` object) """ basename = _("Computation") param = LabelParam(basename, icon="label.png") param.read_config(CONF, "plot", "info_label") if title is not None: param.label = title else: global LABEL_COUNT LABEL_COUNT += 1 param.label = make_title(basename, LABEL_COUNT) param.abspos = True param.absg = anchor param.anchor = anchor c = ANCHOR_OFFSETS[anchor] param.xc, param.yc = c return DataInfoLabel(param, comps)
def info_label(self, anchor, comps, title=None): """ Make an info label `plot item` (:py:class:`guiqwt.label.DataInfoLabel` object) """ basename = _("Computation") param = LabelParam(basename, icon='label.png') param.read_config(CONF, "plot", "info_label") if title is not None: param.label = title else: global LABEL_COUNT LABEL_COUNT += 1 param.label = make_title(basename, LABEL_COUNT) param.abspos = True param.absg = anchor param.anchor = anchor c = ANCHOR_OFFSETS[anchor] param.xc, param.yc = c return DataInfoLabel(param, comps)
class AbstractLabelItem(QwtPlotItem): """Draws a label on the canvas at position : G+C where G is a point in plot coordinates and C a point in canvas coordinates. G can also be an anchor string as in ANCHORS in which case the label will keep a fixed position wrt the canvas rect """ _readonly = False _private = False def __init__(self, labelparam=None): super(AbstractLabelItem, self).__init__() self.selected = False self.anchor = None self.G = None self.C = None self.border_pen = None self.bg_brush = None if labelparam is None: self.labelparam = LabelParam(_("Label"), icon="label.png") else: self.labelparam = labelparam self.labelparam.update_label(self) def set_style(self, section, option): self.labelparam.read_config(CONF, section, option) self.labelparam.update_label(self) def __reduce__(self): return (self.__class__, (self.labelparam,)) def serialize(self, writer): """Serialize object to HDF5 writer""" self.labelparam.update_param(self) writer.write(self.labelparam, group_name="labelparam") def deserialize(self, reader): """Deserialize object from HDF5 reader""" self.labelparam = LabelParam(_("Label"), icon="label.png") reader.read("labelparam", instance=self.labelparam) self.labelparam.update_label(self) def get_text_rect(self): return QRectF(0.0, 0.0, 10.0, 10.0) def types(self): return (IShapeItemType,) def set_text_style(self, font=None, color=None): raise NotImplementedError def get_top_left(self, xMap, yMap, canvasRect): x0, y0 = self.get_origin(xMap, yMap, canvasRect) x0 += self.C[0] y0 += self.C[1] rect = self.get_text_rect() pos = ANCHORS[self.anchor](rect) x0 -= pos[0] y0 -= pos[1] return x0, y0 def get_origin(self, xMap, yMap, canvasRect): if self.G in ANCHORS: return ANCHORS[self.G](canvasRect) else: x0 = xMap.transform(self.G[0]) y0 = yMap.transform(self.G[1]) return x0, y0 def set_selectable(self, state): """Set item selectable state""" self._can_select = state def set_resizable(self, state): """Set item resizable state (or any action triggered when moving an handle, e.g. rotation)""" self._can_resize = state def set_movable(self, state): """Set item movable state""" self._can_move = state def set_rotatable(self, state): """Set item rotatable state""" self._can_rotate = state def can_select(self): return True def can_resize(self): return False def can_move(self): return True def can_rotate(self): return False # TODO: Implement labels rotation? def set_readonly(self, state): """Set object readonly state""" self._readonly = state def is_readonly(self): """Return object readonly state""" return self._readonly def set_private(self, state): """Set object as private""" self._private = state def is_private(self): """Return True if object is private""" return self._private def invalidate_plot(self): plot = self.plot() if plot: plot.invalidate() def select(self): """Select item""" if self.selected: # Already selected return self.selected = True w = self.border_pen.width() self.border_pen.setWidth(w + 1) self.invalidate_plot() def unselect(self): """Unselect item""" self.selected = False self.labelparam.update_label(self) self.invalidate_plot() def hit_test(self, pos): plot = self.plot() if plot is None: return rect = self.get_text_rect() canvasRect = plot.canvas().contentsRect() xMap = plot.canvasMap(self.xAxis()) yMap = plot.canvasMap(self.yAxis()) x, y = self.get_top_left(xMap, yMap, canvasRect) rct = QRectF(x, y, rect.width(), rect.height()) inside = rct.contains(pos.x(), pos.y()) if inside: return self.click_inside(pos.x() - x, pos.y() - y) else: return 1000.0, None, False, None def click_inside(self, locx, locy): return 2.0, 1, True, None def get_item_parameters(self, itemparams): self.labelparam.update_param(self) itemparams.add("LabelParam", self, self.labelparam) def set_item_parameters(self, itemparams): update_dataset(self.labelparam, itemparams.get("LabelParam"), visible_only=True) self.labelparam.update_label(self) if self.selected: self.select() def move_local_point_to(self, handle, pos, ctrl=None): """Move a handle as returned by hit_test to the new position pos ctrl: True if <Ctrl> button is being pressed, False otherwise""" if handle != -1: return def move_local_shape(self, old_pos, new_pos): """Translate the shape such that old_pos becomes new_pos in canvas coordinates""" if self.G in ANCHORS or not self.labelparam.move_anchor: # Move canvas offset lx, ly = self.C lx += new_pos.x() - old_pos.x() ly += new_pos.y() - old_pos.y() self.C = lx, ly self.labelparam.xc, self.labelparam.yc = lx, ly else: # Move anchor plot = self.plot() if plot is None: return lx0, ly0 = self.G cx = plot.transform(self.xAxis(), lx0) cy = plot.transform(self.yAxis(), ly0) cx += new_pos.x() - old_pos.x() cy += new_pos.y() - old_pos.y() lx1 = plot.invTransform(self.xAxis(), cx) ly1 = plot.invTransform(self.yAxis(), cy) self.G = lx1, ly1 self.labelparam.xg, self.labelparam.yg = lx1, ly1 plot.emit(SIG_ITEM_MOVED, self, lx0, ly0, lx1, ly1) def move_with_selection(self, delta_x, delta_y): """ Translate the shape together with other selected items delta_x, delta_y: translation in plot coordinates """ if self.G in ANCHORS or not self.labelparam.move_anchor: return lx0, ly0 = self.G lx1, ly1 = lx0 + delta_x, ly0 + delta_y self.G = lx1, ly1 self.labelparam.xg, self.labelparam.yg = lx1, ly1 def draw_frame(self, painter, x, y, w, h): if self.labelparam.bgalpha > 0.0: painter.fillRect(x, y, w, h, self.bg_brush) if self.border_pen.width() > 0: painter.setPen(self.border_pen) painter.drawRect(x, y, w, h)
class AbstractLabelItem(QwtPlotItem): """Draws a label on the canvas at position : G+C where G is a point in plot coordinates and C a point in canvas coordinates. G can also be an anchor string as in ANCHORS in which case the label will keep a fixed position wrt the canvas rect """ _readonly = False _private = False def __init__(self, labelparam=None): super(AbstractLabelItem, self).__init__() self.selected = False self.anchor = None self.G = None self.C = None self.border_pen = None self.bg_brush = None if labelparam is None: self.labelparam = LabelParam(_("Label"), icon='label.png') else: self.labelparam = labelparam self.labelparam.update_label(self) def set_style(self, section, option): self.labelparam.read_config(CONF, section, option) self.labelparam.update_label(self) def __reduce__(self): return (self.__class__, (self.labelparam,)) def serialize(self, writer): """Serialize object to HDF5 writer""" self.labelparam.update_param(self) writer.write(self.labelparam, group_name='labelparam') def deserialize(self, reader): """Deserialize object from HDF5 reader""" self.labelparam = LabelParam(_("Label"), icon='label.png') reader.read('labelparam', instance=self.labelparam) self.labelparam.update_label(self) def get_text_rect(self): return QRectF(0.0, 0.0, 10., 10.) def types(self): return (IShapeItemType, ) def set_text_style(self, font=None, color=None): raise NotImplementedError def get_top_left(self, xMap, yMap, canvasRect): x0, y0 = self.get_origin(xMap, yMap, canvasRect) x0 += self.C[0] y0 += self.C[1] rect = self.get_text_rect() pos = ANCHORS[self.anchor](rect) x0 -= pos[0] y0 -= pos[1] return x0, y0 def get_origin(self, xMap, yMap, canvasRect): if self.G in ANCHORS: return ANCHORS[self.G](canvasRect) else: x0 = xMap.transform(self.G[0]) y0 = yMap.transform(self.G[1]) return x0, y0 def set_selectable(self, state): """Set item selectable state""" self._can_select = state def set_resizable(self, state): """Set item resizable state (or any action triggered when moving an handle, e.g. rotation)""" self._can_resize = state def set_movable(self, state): """Set item movable state""" self._can_move = state def set_rotatable(self, state): """Set item rotatable state""" self._can_rotate = state def can_select(self): return True def can_resize(self): return False def can_move(self): return True def can_rotate(self): return False #TODO: Implement labels rotation? def set_readonly(self, state): """Set object readonly state""" self._readonly = state def is_readonly(self): """Return object readonly state""" return self._readonly def set_private(self, state): """Set object as private""" self._private = state def is_private(self): """Return True if object is private""" return self._private def invalidate_plot(self): plot = self.plot() if plot: plot.invalidate() def select(self): """Select item""" if self.selected: # Already selected return self.selected = True w = self.border_pen.width() self.border_pen.setWidth(w+1) self.invalidate_plot() def unselect(self): """Unselect item""" self.selected = False self.labelparam.update_label(self) self.invalidate_plot() def hit_test(self, pos): plot = self.plot() if plot is None: return rect = self.get_text_rect() canvasRect = plot.canvas().contentsRect() xMap = plot.canvasMap(self.xAxis()) yMap = plot.canvasMap(self.yAxis()) x, y = self.get_top_left(xMap, yMap, canvasRect) rct = QRectF(x, y, rect.width(), rect.height()) inside = rct.contains( pos.x(), pos.y()) if inside: return self.click_inside(pos.x()-x, pos.y()-y) else: return 1000.0, None, False, None def click_inside(self, locx, locy): return 2.0, 1, True, None def get_item_parameters(self, itemparams): self.labelparam.update_param(self) itemparams.add("LabelParam", self, self.labelparam) def set_item_parameters(self, itemparams): update_dataset(self.labelparam, itemparams.get("LabelParam"), visible_only=True) self.labelparam.update_label(self) if self.selected: self.select() def move_local_point_to(self, handle, pos, ctrl=None): """Move a handle as returned by hit_test to the new position pos ctrl: True if <Ctrl> button is being pressed, False otherwise""" if handle != -1: return def move_local_shape(self, old_pos, new_pos): """Translate the shape such that old_pos becomes new_pos in canvas coordinates""" if self.G in ANCHORS or not self.labelparam.move_anchor: # Move canvas offset lx, ly = self.C lx += new_pos.x()-old_pos.x() ly += new_pos.y()-old_pos.y() self.C = lx, ly self.labelparam.xc, self.labelparam.yc = lx, ly else: # Move anchor plot = self.plot() if plot is None: return lx0, ly0 = self.G cx = plot.transform(self.xAxis(), lx0) cy = plot.transform(self.yAxis(), ly0) cx += new_pos.x()-old_pos.x() cy += new_pos.y()-old_pos.y() lx1 = plot.invTransform(self.xAxis(), cx) ly1 = plot.invTransform(self.yAxis(), cy) self.G = lx1, ly1 self.labelparam.xg, self.labelparam.yg = lx1, ly1 plot.SIG_ITEM_MOVED.emit(self, lx0, ly0, lx1, ly1) def move_with_selection(self, delta_x, delta_y): """ Translate the shape together with other selected items delta_x, delta_y: translation in plot coordinates """ if self.G in ANCHORS or not self.labelparam.move_anchor: return lx0, ly0 = self.G lx1, ly1 = lx0+delta_x, ly0+delta_y self.G = lx1, ly1 self.labelparam.xg, self.labelparam.yg = lx1, ly1 def draw_frame(self, painter, x, y, w, h): if self.labelparam.bgalpha > 0.0: painter.fillRect(x, y, w, h, self.bg_brush) if self.border_pen.width() > 0: painter.setPen(self.border_pen) painter.drawRect(x, y, w, h)
def create_label(self): """Return the label object associated to this annotated shape object""" label_param = LabelParam(_("Label"), icon='label.png') label_param.read_config(CONF, "plot", "shape/label") label_param.anchor = self.LABEL_ANCHOR return DataInfoLabel(label_param, [self])