class QTSweepVarSettings(gobject.GObject):

    def __init__(self, label=None):
        gobject.GObject.__init__(self)

        self._label = label

        self._layout = None
        self.create_layout()

    def get_layout(self):
        return self._frame

    def create_layout(self):
        self._frame = gtk.Frame()
        if self._label is not None:
            self._frame.set_label(self._label)

        self._vbox = gtk.VBox(spacing=4)
        self._vbox.set_border_width(4)

        self._variable_dropdown = AllParametersDropdown(
                                    flags=qt.constants.FLAG_SET,
                                    types=(types.IntType, types.FloatType),
                                    tags=['sweep'])
        self._variable_dropdown.connect('changed', self._parameter_changed_cb)
        self._vbox.pack_start(gui.pack_hbox([
            gtk.Label(_L('Sweep variable')),
            self._variable_dropdown]), False, False)

        self._start_val = gtk.SpinButton(climb_rate=0.1, digits=2)
        self._start_val.set_range(0, 0)
        self._start_val.set_increments(0.01, 0.1)

        self._end_val = gtk.SpinButton(climb_rate=0.1, digits=2)
        self._end_val.set_range(0, 0)
        self._end_val.set_increments(0.01, 0.1)

        self._n_steps = gtk.SpinButton(climb_rate=0.1, digits=0)
        self._n_steps.set_range(0, 100000)
        self._n_steps.set_increments(1, 2)

        self._steps_or_size = StepToggleButton([_L('< Steps'), _L('Size >')],
            self._steps_toggle_cb, _L('Set number of steps or stepsize'))
        self._steps_or_size.set_size_request(100, 0)

        self._step_size = gtk.SpinButton(climb_rate=0.1, digits=3)
        self._step_size.set_range(0, 1000)
        self._step_size.set_increments(0.001, 0.01)
        self._step_size.set_sensitive(False)

        self._units_label = gtk.Label()

        self._vbox.pack_start(gui.pack_hbox([
            gtk.Label(_L('Start')),
            self._start_val,
            gtk.Label(_L('End')),
            self._end_val,
            self._units_label]))

        self._vbox.pack_start(gui.pack_hbox([
            gtk.Label(_L('Nr of steps')),
            self._n_steps,
            self._steps_or_size,
            gtk.Label(_L('Step size')),
            self._step_size]))

        self._frame.add(self._vbox)

    def _steps_toggle_cb(self, item):
        from gettext import gettext as _
        steps_sel = (item == _L('< Steps'))
        self._n_steps.set_sensitive(steps_sel)
        self._step_size.set_sensitive(not steps_sel)

    def _parameter_changed_cb(self, widget):
        sel = self._variable_dropdown.get_selection()
        if sel is not None:
            ins, varname = sel
            if ins is None:
                return

            opt = ins.get_shared_parameter_options(varname)
            if 'minval' in opt and 'maxval' in opt:
                logging.debug('Setting range %s - %s', opt['minval'], opt['maxval'])
                self._start_val.set_range(opt['minval'], opt['maxval'])
                self._end_val.set_range(opt['minval'], opt['maxval'])
            else:
                logging.info('No default range defined, setting 0 - 1')
                self._start_val.set_range(0, 1)
                self._end_val.set_range(0, 1)

            if 'units' in opt:
                self._units_label.set_text(opt['units'])

    def get_instrument_var(self):
        return self._variable_dropdown.get_selection()

    def get_units(self):
        return self._units_label.get_text()

    def get_start(self):
        return self._start_val.get_value()

    def get_end(self):
        return self._end_val.get_value()

    def get_sweep_range(self):
        return self.get_start(), self.get_end()

    def get_steps(self):
        return int(self._n_steps.get_value())

    def set_sensitive(self, sensitive):
        self._variable_dropdown.set_sensitive(sensitive)
        self._start_val.set_sensitive(sensitive)
        self._end_val.set_sensitive(sensitive)
        self._n_steps.set_sensitive(sensitive)
        self._steps_or_size.set_sensitive(sensitive)
        self._step_size.set_sensitive(sensitive)
class QTSweepVarSettings(gobject.GObject):
    def __init__(self, label=None):
        gobject.GObject.__init__(self)

        self._label = label

        self._layout = None
        self.create_layout()

    def get_layout(self):
        return self._frame

    def create_layout(self):
        self._frame = gtk.Frame()
        if self._label is not None:
            self._frame.set_label(self._label)

        self._vbox = gtk.VBox(spacing=4)
        self._vbox.set_border_width(4)

        self._variable_dropdown = AllParametersDropdown(
            flags=qt.constants.FLAG_SET,
            types=(types.IntType, types.FloatType),
            tags=['sweep'])
        self._variable_dropdown.connect('changed', self._parameter_changed_cb)
        self._vbox.pack_start(
            gui.pack_hbox(
                [gtk.Label(_L('Sweep variable')), self._variable_dropdown]),
            False, False)

        self._start_val = gtk.SpinButton(climb_rate=0.1, digits=2)
        self._start_val.set_range(0, 0)
        self._start_val.set_increments(0.01, 0.1)

        self._end_val = gtk.SpinButton(climb_rate=0.1, digits=2)
        self._end_val.set_range(0, 0)
        self._end_val.set_increments(0.01, 0.1)

        self._n_steps = gtk.SpinButton(climb_rate=0.1, digits=0)
        self._n_steps.set_range(0, 100000)
        self._n_steps.set_increments(1, 2)

        self._steps_or_size = StepToggleButton(
            [_L('< Steps'), _L('Size >')], self._steps_toggle_cb,
            _L('Set number of steps or stepsize'))
        self._steps_or_size.set_size_request(100, 0)

        self._step_size = gtk.SpinButton(climb_rate=0.1, digits=3)
        self._step_size.set_range(0, 1000)
        self._step_size.set_increments(0.001, 0.01)
        self._step_size.set_sensitive(False)

        self._units_label = gtk.Label()

        self._vbox.pack_start(
            gui.pack_hbox([
                gtk.Label(_L('Start')), self._start_val,
                gtk.Label(_L('End')), self._end_val, self._units_label
            ]))

        self._vbox.pack_start(
            gui.pack_hbox([
                gtk.Label(_L('Nr of steps')), self._n_steps,
                self._steps_or_size,
                gtk.Label(_L('Step size')), self._step_size
            ]))

        self._frame.add(self._vbox)

    def _steps_toggle_cb(self, item):
        from gettext import gettext as _
        steps_sel = (item == _L('< Steps'))
        self._n_steps.set_sensitive(steps_sel)
        self._step_size.set_sensitive(not steps_sel)

    def _parameter_changed_cb(self, widget):
        sel = self._variable_dropdown.get_selection()
        if sel is not None:
            ins, varname = sel
            if ins is None:
                return

            opt = ins.get_shared_parameter_options(varname)
            if 'minval' in opt and 'maxval' in opt:
                logging.debug('Setting range %s - %s', opt['minval'],
                              opt['maxval'])
                self._start_val.set_range(opt['minval'], opt['maxval'])
                self._end_val.set_range(opt['minval'], opt['maxval'])
            else:
                logging.info('No default range defined, setting 0 - 1')
                self._start_val.set_range(0, 1)
                self._end_val.set_range(0, 1)

            if 'units' in opt:
                self._units_label.set_text(opt['units'])

    def get_instrument_var(self):
        return self._variable_dropdown.get_selection()

    def get_units(self):
        return self._units_label.get_text()

    def get_start(self):
        return self._start_val.get_value()

    def get_end(self):
        return self._end_val.get_value()

    def get_sweep_range(self):
        return self.get_start(), self.get_end()

    def get_steps(self):
        return int(self._n_steps.get_value())

    def set_sensitive(self, sensitive):
        self._variable_dropdown.set_sensitive(sensitive)
        self._start_val.set_sensitive(sensitive)
        self._end_val.set_sensitive(sensitive)
        self._n_steps.set_sensitive(sensitive)
        self._steps_or_size.set_sensitive(sensitive)
        self._step_size.set_sensitive(sensitive)
class QTMeasureVarSettings(gobject.GObject):

    def __init__(self, label=None):
        gobject.GObject.__init__(self)

        self._label = label

        self._layout = None
        self.create_layout()

    def get_layout(self):
        return self._frame

    def create_layout(self):
        self._frame = gtk.Frame()
        if self._label is not None:
            self._frame.set_label(self._label)

        self._vbox = gtk.VBox(spacing=4)
        self._vbox.set_border_width(4)

        self._variable_dropdown = AllParametersDropdown(
                                    flags=qt.constants.FLAG_GET,
                                    types=(types.IntType, types.FloatType),
                                    tags=['measure'])
        self._variable_dropdown.connect('changed', self._parameter_changed_cb)
        self._vbox.pack_start(gui.pack_hbox([
            gtk.Label(_L('Measurement variable')),
            self._variable_dropdown]), False, False)

        self._scale = gtk.Entry()
        self._scale.set_width_chars(12)
        self._scale.set_text('1')
        self._units = gtk.Entry()
        self._units.set_width_chars(12)

        self._vbox.pack_start(gui.pack_hbox([
            gtk.Label(_L('Scaling')),
            self._scale,
            gtk.Label(_L('Units')),
            self._units]))

        self._frame.add(self._vbox)

    def _parameter_changed_cb(self, widget):
        sel = self._variable_dropdown.get_selection()
        if sel is not None:
            ins, varname = sel
            if ins is None:
                return

            opt = ins.get_shared_parameter_options(varname)
            if 'units' in opt:
                self._units.set_text(opt['units'])

    def get_instrument_var(self):
        return self._variable_dropdown.get_selection()

    def set_sensitive(self, sensitive):
        self._variable_dropdown.set_sensitive(sensitive)
        self._scale.set_sensitive(sensitive)
        self._units.set_sensitive(sensitive)
class QTMeasureVarSettings(gobject.GObject):
    def __init__(self, label=None):
        gobject.GObject.__init__(self)

        self._label = label

        self._layout = None
        self.create_layout()

    def get_layout(self):
        return self._frame

    def create_layout(self):
        self._frame = gtk.Frame()
        if self._label is not None:
            self._frame.set_label(self._label)

        self._vbox = gtk.VBox(spacing=4)
        self._vbox.set_border_width(4)

        self._variable_dropdown = AllParametersDropdown(
            flags=qt.constants.FLAG_GET,
            types=(types.IntType, types.FloatType),
            tags=['measure'])
        self._variable_dropdown.connect('changed', self._parameter_changed_cb)
        self._vbox.pack_start(
            gui.pack_hbox([
                gtk.Label(_L('Measurement variable')), self._variable_dropdown
            ]), False, False)

        self._scale = gtk.Entry()
        self._scale.set_width_chars(12)
        self._scale.set_text('1')
        self._units = gtk.Entry()
        self._units.set_width_chars(12)

        self._vbox.pack_start(
            gui.pack_hbox([
                gtk.Label(_L('Scaling')), self._scale,
                gtk.Label(_L('Units')), self._units
            ]))

        self._frame.add(self._vbox)

    def _parameter_changed_cb(self, widget):
        sel = self._variable_dropdown.get_selection()
        if sel is not None:
            ins, varname = sel
            if ins is None:
                return

            opt = ins.get_shared_parameter_options(varname)
            if 'units' in opt:
                self._units.set_text(opt['units'])

    def get_instrument_var(self):
        return self._variable_dropdown.get_selection()

    def set_sensitive(self, sensitive):
        self._variable_dropdown.set_sensitive(sensitive)
        self._scale.set_sensitive(sensitive)
        self._units.set_sensitive(sensitive)