Пример #1
0
class TwissView(object):

    """Instanciate an FigurePair + XYCurve(Envelope)."""

    @classmethod
    def create(cls, session, frame, basename):
        """Create a new view panel as a page in the notebook frame."""
        if not session.segment:
            return
        view = cls(session.segment, basename, frame.app.conf['line_view'])
        panel = frame.AddView(view, view.title)
        return view

    def __init__(self, segment, basename, line_view_config):

        self.hook = HookCollection(
            plot=None,
            plot_ax=None,
            destroy=None,
        )

        # create figure
        self.figure = figure = FigurePair()
        self.segment = segment
        self.config = line_view_config

        self.title = line_view_config['title'][basename]

        self.sname = sname = 's'
        self.xname = xname = basename + 'x'
        self.yname = yname = basename + 'y'
        axx, axy = figure.axx, figure.axy
        axx.twiss_name, axx.twiss_conj = xname, yname
        axy.twiss_name, axy.twiss_conj = yname, xname
        self.axes = {xname: axx, yname: axy}

        # plot style
        self._label = line_view_config['label']
        unit_names = line_view_config['unit']
        self.unit = {col: getattr(units, unit_names[col])
                     for col in [sname, xname, yname]}

        # subscribe for updates
        TwissCurveSegment(segment, self)
        DrawLineElements(self, self.config['element_style'])
        self.segment.hook.update.connect(self.update)

    def destroy(self):
        self.segment.hook.update.disconnect(self.update)
        self.hook.destroy()

    def update(self):
        self.figure.draw()

    def get_label(self, name):
        return self._label[name] + ' ' + get_unit_label(self.unit[name])

    def plot(self):
        fig = self.figure
        axx = fig.axx
        axy = fig.axy
        sname, xname, yname = self.sname, self.xname, self.yname
        # start new plot
        fig.start_plot()
        axx.set_ylabel(self.get_label(xname))
        axy.set_ylabel(self.get_label(yname))
        fig.set_slabel(self.get_label(sname))
        # invoke plot hooks
        self.hook.plot_ax(axx, xname)
        self.hook.plot_ax(axy, yname)
        self.hook.plot()
        # finish and draw:
        fig.draw()

    def get_axes_name(self, axes):
        return axes.twiss_name

    def get_conjugate(self, name):
        return axes.twiss_conj