def combo_factory(combo_array,
                  toolbar,
                  callback,
                  cb_arg=None,
                  tooltip=None,
                  default=None):
    '''Factory for making a toolbar combo box'''
    combo = ComboBox()
    if tooltip is not None and hasattr(combo, 'set_tooltip_text'):
        combo.set_tooltip_text(tooltip)
    if cb_arg is not None:
        combo.connect('changed', callback, cb_arg)
    else:
        combo.connect('changed', callback)
    for i, selection in enumerate(combo_array):
        combo.append_item(i, selection, None)
    combo.show()
    toolitem = gtk.ToolItem()
    toolitem.add(combo)
    if hasattr(toolbar, 'insert'):  # the main toolbar
        toolbar.insert(toolitem, -1)
    else:  # or a secondary toolbar
        toolbar.props.page.insert(toolitem, -1)
    toolitem.show()
    if default is not None:
        combo.set_active(combo_array.index(default))
    return combo
def _combo_factory(options, tooltip, toolbar, callback, default=0):
    ''' Combo box factory '''
    combo = ComboBox()
    if hasattr(combo, 'set_tooltip_text'):
        combo.set_tooltip_text(tooltip)
    combo.connect('changed', callback)
    for i, option in enumerate(options):
        combo.append_item(i, option.replace('-', ' '), None)
    combo.set_active(default)
    combo.show()
    tool = ToolComboBox(combo)
    tool.show()
    toolbar.insert(tool, -1)
    return combo
class IconComboBox(gtk.ToolItem):
    def __init__(self, icon_name, **kwargs):
        gtk.ToolItem.__init__(self, **kwargs)

        self.icon_name = icon_name
        self.set_border_width(style.DEFAULT_PADDING)

        self.combo = ComboBox()
        self.combo.set_focus_on_click(False)
        self.combo.show()

        self.add(self.combo)

    def append_item(self, i, text):
        self.combo.append_item(i, text, icon_name=self.icon_name)
Exemplo n.º 4
0
def combo_factory(combo_array, toolbar, callback, cb_arg=None,
                  tooltip=None, default=None):
    '''Factory for making a toolbar combo box'''
    combo = ComboBox()
    if tooltip is not None and hasattr(combo, 'set_tooltip_text'):
        combo.set_tooltip_text(tooltip)
    if cb_arg is not None:
        combo.connect('changed', callback, cb_arg)
    else:
        combo.connect('changed', callback)
    for i, selection in enumerate(combo_array):
        combo.append_item(i, selection, None)
    combo.show()
    toolitem = gtk.ToolItem()
    toolitem.add(combo)
    if hasattr(toolbar, 'insert'):  # the main toolbar
        toolbar.insert(toolitem, -1)
    else:  # or a secondary toolbar
        toolbar.props.page.insert(toolitem, -1)
    toolitem.show()
    if default is not None:
        combo.set_active(combo_array.index(default))
    return combo
class ToolComboBox(gtk.ToolItem):
    __gproperties__ = {
        'label-text' : (str, None, None, None,
                        gobject.PARAM_WRITABLE),
    }

    def __init__(self, combo=None, **kwargs):
        self.label = None
        self._label_text = ''

        gobject.GObject.__init__(self, **kwargs)

        self.set_border_width(style.DEFAULT_PADDING)

        hbox = gtk.HBox(False, style.DEFAULT_SPACING)

        self.label = gtk.Label(self._label_text)
        hbox.pack_start(self.label, False)
        self.label.show()

        if combo:
            self.combo = combo
        else:
            self.combo = ComboBox()

        hbox.pack_start(self.combo)
        self.combo.show()

        self.add(hbox)
        hbox.show()

    def do_set_property(self, pspec, value):
        if pspec.name == 'label-text':
            self._label_text = value
            if self.label:
                self.label.set_text(self._label_text)
Exemplo n.º 6
0
class ToolComboBox(gtk.ToolItem):

    __gproperties__ = {
        'label-text': (str, None, None, None, gobject.PARAM_WRITABLE),
    }

    def __init__(self, combo=None, **kwargs):
        self.label = None
        self._label_text = ''

        gobject.GObject.__init__(self, **kwargs)

        self.set_border_width(style.DEFAULT_PADDING)

        hbox = gtk.HBox(False, style.DEFAULT_SPACING)

        self.label = gtk.Label(self._label_text)
        hbox.pack_start(self.label, False)
        self.label.show()

        if combo:
            self.combo = combo
        else:
            self.combo = ComboBox()

        hbox.pack_start(self.combo)
        self.combo.show()

        self.add(hbox)
        hbox.show()

    def do_set_property(self, pspec, value):
        if pspec.name == 'label-text':
            self._label_text = value
            if self.label:
                self.label.set_text(self._label_text)
class TuningToolbar(gtk.Toolbar):
    ''' The toolbar for tuning instruments '''
    def __init__(self, activity):
        gtk.Toolbar.__init__(self)

        self.activity = activity
        self._show_tuning_line = False
        self._updating_note = True
        self._tuning_tool = None

        # Set up Instrument Combo box
        self.instrument_combo = ComboBox()
        self.instrument = [_('None')]
        for k in INSTRUMENT_DICT.keys():
            self.instrument.append(k)
        self._instrument_changed_id = self.instrument_combo.connect(
            'changed', self.update_instrument_control)
        for i, instrument in enumerate(self.instrument):
            self.instrument_combo.append_item(i, instrument, None)
        self.instrument_combo.set_active(0)
        if hasattr(self.instrument_combo, 'set_tooltip_text'):
            self.instrument_combo.set_tooltip_text(_('Tune an instrument.'))
        self._instrument_tool = ToolComboBox(self.instrument_combo)
        self.insert(self._instrument_tool, -1)

        if self.activity.has_toolbarbox:
            separator = gtk.SeparatorToolItem()
            separator.props.draw = True
            self.insert(separator, -1)

        self._notes_combo = ComboBox()
        n = 0
        for octave in range(9):
            for i in range(len(NOTES)):
                if octave == 0 and i < 9:  # Start with A0
                    continue
                self._notes_combo.append_item(n, note_octave(i, octave), None)
                n += 1
        self._notes_combo.set_active(48)  # A4
        self._notes_changed_id = self._notes_combo.connect(
            'changed', self.update_note)
        if hasattr(self._notes_combo, 'set_tooltip_text'):
            self._notes_combo.set_tooltip_text(_('Notes'))
        self._notes_tool = ToolComboBox(self._notes_combo)
        self.insert(self._notes_tool, -1)

        # The entry is used to display a note or for direct user input
        self._freq_entry = gtk.Entry()
        self._freq_entry.set_text('440')  # A
        self._freq_entry_changed_id = self._freq_entry.connect(
            'changed', self.update_freq_entry)
        if hasattr(self._freq_entry, 'set_tooltip_text'):
            self._freq_entry.set_tooltip_text(
                _('Enter a frequency to display.'))
        self._freq_entry.set_width_chars(8)
        self._freq_entry.show()
        toolitem = gtk.ToolItem()
        toolitem.add(self._freq_entry)
        self.insert(toolitem, -1)
        toolitem.show()

        self._new_tuning_line = ToolButton('tuning-tools')
        self._new_tuning_line.show()
        self.insert(self._new_tuning_line, -1)
        self._new_tuning_line.set_tooltip(_('Show tuning line.'))
        self._new_tuning_line.connect('clicked', self.tuning_line_cb)

        if self.activity.has_toolbarbox:
            separator = gtk.SeparatorToolItem()
            separator.props.draw = True
            self.insert(separator, -1)

        self._harmonic = ToolButton('harmonics')
        self._harmonic.show()
        self.insert(self._harmonic, -1)
        self._harmonic.set_tooltip(_('Show harmonics.'))
        self._harmonic.connect('clicked', self.harmonic_cb)

        if self.activity.has_toolbarbox:
            separator = gtk.SeparatorToolItem()
            separator.props.draw = True
            self.insert(separator, -1)

        self._play_tone = ToolButton('media-playback-start')
        self._play_tone.show()
        self.insert(self._play_tone, -1)
        self._play_tone.set_tooltip(_('Play a note.'))
        self._play_tone.connect('clicked', self.play_cb)

        if self.activity.has_toolbarbox:
            separator = gtk.SeparatorToolItem()
            separator.props.draw = False
            separator.set_expand(True)
            self.insert(separator, -1)

        self.label = gtk.Label('')
        self.label.set_use_markup(True)
        self.label.show()
        toolitem = gtk.ToolItem()
        toolitem.add(self.label)
        self.insert(toolitem, -1)
        toolitem.show()

        self.show_all()

    def update_note(self, *args):
        ''' Calculate the frequency based on note combo '''
        if not hasattr(self, '_freq_entry'):  # Still setting up toolbar
            return
        i = self._notes_combo.get_active()
        freq = A0 * pow(TWELTHROOT2, i)
        self._updating_note = True
        self._freq_entry.set_text('%0.3f' % (freq))
        self.label.set_markup(
            SPAN % (style.COLOR_WHITE.get_html(),
                    note_octave(index_to_note(i), index_to_octave(i))))
        if self._show_tuning_line:
            self.activity.wave.tuning_line = freq
        return

    def update_tuning_control(self, *args):
        ''' Update note '''
        if not hasattr(self, '_freq_entry'):  # Still setting up toolbar?
            return
        instrument = self.instrument[self.instrument_combo.get_active()]
        if not instrument in INSTRUMENT_DICT:
            return
        if self.tuning[self._tuning_combo.get_active()] == _('All notes'):
            self._notes_combo.set_active(
                freq_index(INSTRUMENT_DICT[instrument][0]))
            self.activity.wave.instrument = instrument
            self.activity.wave.tuning_line = 0.0
            self._new_tuning_line.set_icon('tuning-tools')
            self._new_tuning_line.set_tooltip(_('Show tuning line.'))
            self._show_tuning_line = False
        else:
            freq = INSTRUMENT_DICT[instrument][self._tuning_combo.get_active()
                                               - 1]  # All notes is 0
            self._notes_combo.set_active(
                freq_index(INSTRUMENT_DICT[instrument][
                    self._tuning_combo.get_active() - 1]))
            self.activity.wave.instrument = None
            self.activity.wave.tuning_line = freq
            self._new_tuning_line.set_icon('tuning-tools-off')
            self._new_tuning_line.set_tooltip(_('Hide tuning line.'))
            self._show_tuning_line = True
        self._updating_note = False

    def update_freq_entry(self, *args):
        # Calcualte a note from a frequency
        if not self._updating_note:  # Only if user types in a freq.
            try:
                freq = float(self._freq_entry.get_text())
                # Only consider notes in piano range
                if freq < A0 * 0.97:
                    self.label.set_text('< A0')
                    return
                if freq > C8 * 1.03:
                    self.label.set_text('> C8')
                    return
                self.label.set_markup(freq_note(freq, flatsharp=True))
            except ValueError:
                return
        self._updating_note = False

    def update_instrument_control(self, *args):
        ''' Callback for instrument control '''
        instrument = self.instrument[self.instrument_combo.get_active()]
        if self._tuning_tool is not None:
            self.remove(self._tuning_tool)
        if instrument == _('None'):
            self.activity.wave.instrument = None
            if hasattr(self, '_notes_tool'):
                self.insert(self._notes_tool, 2)
            return
        self.remove(self._notes_tool)
        self.activity.wave.instrument = instrument
        # If we are not already in freq. base, switch.
        if not self.activity.wave.get_fft_mode():
            self.activity.timefreq_control()
        # Add a Tuning Combo box for this instrument
        self._tuning_combo = ComboBox()
        self.tuning = [_('All notes')]
        for f in INSTRUMENT_DICT[instrument]:
            self.tuning.append(freq_note(f))
        self._tuning_changed_id = self._tuning_combo.connect(
            'changed', self.update_tuning_control)
        for i, s in enumerate(self.tuning):
            self._tuning_combo.append_item(i, s, None)
        self._tuning_combo.set_active(0)
        if hasattr(self._tuning_combo, 'set_tooltip_text'):
            self._tuning_combo.set_tooltip_text(instrument)
        self._tuning_tool = ToolComboBox(self._tuning_combo)
        self.insert(self._tuning_tool, 1)
        self._tuning_combo.show()
        self._tuning_tool.show()
        self.show_all()

    def harmonic_cb(self, *args):
        ''' Callback for harmonics control '''
        self.activity.wave.harmonics = not self.activity.wave.harmonics
        if self.activity.wave.harmonics:
            self._harmonic.set_icon('harmonics-off')
            self._harmonic.set_tooltip(_('Hide harmonics.'))
            if self.activity.wave.instrument is None and \
               self.activity.wave.tuning_line == 0.0:
                self._load_tuning_line()
        else:
            self._harmonic.set_icon('harmonics')
            self._harmonic.set_tooltip(_('Show harmonics.'))

    def tuning_line_cb(self, *args):
        ''' Callback for tuning insert '''
        if self._show_tuning_line:
            self.activity.wave.tuning_line = 0.0
            self._new_tuning_line.set_icon('tuning-tools')
            self._new_tuning_line.set_tooltip(_('Show tuning line.'))
            self._show_tuning_line = False
        else:
            self._load_tuning_line()

    def _load_tuning_line(self):
        ''' Read the freq entry and use value to set tuning line '''
        freq = self._freq_entry.get_text()
        try:
            self.activity.wave.tuning_line = float(freq)
            if freq < 0:
                freq = -freq
            self._new_tuning_line.set_icon('tuning-tools-off')
            self._new_tuning_line.set_tooltip(_('Hide tuning line.'))
            self._show_tuning_line = True
        except ValueError:
            self.activity.wave.tuning_line = 0.0
            self._freq_entry.set_text('0')
        # If we are not already in freq. base, switch.
        if not self.activity.wave.get_fft_mode():
            self.activity.timefreq_control()

    def play_cb(self, *args):
        ''' Save settings, turn off display, and then play a tone at
        the current frequency '''
        freq = float(self._freq_entry.get_text())
        channels = []
        for c in range(self.activity.audiograb.channels):
            channels.append(self.activity.wave.get_visibility(channel=c))
            self.activity.wave.set_visibility(False, channel=c)
        wave_status = self.activity.wave.get_active()
        self.activity.wave.set_context_off()
        self.activity.wave.set_active(False)
        gobject.timeout_add(200, self.play_sound, freq, channels, wave_status)

    def play_sound(self, freq, channels, wave_status):
        ''' Play the sound and then restore wave settings '''
        if hasattr(subprocess, 'check_output'):
            try:
                output = subprocess.check_output([
                    'speaker-test', '-t', 'sine', '-l', '1', '-f',
                    '%f' % (freq)
                ])
            except subprocess.CalledProcessError:
                log.warning('call to speaker-test failed?')
        else:
            import commands
            (status, output) = commands.getstatusoutput(
                'speaker-test -t sine -l 1 -f %f' % (freq))
            if status != 0:
                log.warning('call to speaker-test failed?')
        for c in range(self.activity.audiograb.channels):
            self.activity.wave.set_visibility(channels[c], channel=c)
        self.activity.wave.set_context_on()
        self.activity.wave.set_active(wave_status)
Exemplo n.º 8
0
class FontToolbarButton(ToolbarButton):
    __gsignals__ = {
        'load-pango-context':
        (gobject.SIGNAL_RUN_LAST, gobject.TYPE_PYOBJECT, tuple()),
        'font-changed':
        (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
         (gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_INT))
    }

    def __init__(self):
        ToolbarButton.__init__(self)
        self.toolbar = gtk.Toolbar()
        self.props.page = self.toolbar
        self.props.icon_name = 'format-text'
        self.family = "Monospace"
        self.current_face = "Regular"

    def size_changed(self, adjustment):
        self.emit("font-changed", self.family, self.current_face,
                  adjustment.get_value())

    def face_changed(self, widget):
        iter = widget.get_active_iter()
        self.current_face = self.faces[self.family].get_value(iter, 0)
        self.emit('font-changed', self.family, self.current_face,
                  self.size_adj.get_value())

    def family_changed(self, widget):
        iter = widget.get_active_iter()
        self.family = self.family_model.get_value(iter, 0)
        self.face_combo.set_model(self.faces[self.family])
        self.face_combo.set_active(0)

    def load_toolbar(self):
        self.context = self.emit("load-pango-context")
        self.family_combo = ComboBox()
        family_renderer = gtk.CellRendererText()
        family_renderer.set_property("family-set", True)
        self.family_combo.pack_start(family_renderer)
        self.family_combo.add_attribute(family_renderer, 'text', 0)
        self.family_combo.add_attribute(family_renderer, 'family', 0)
        self.family_model = gtk.ListStore(str)
        monospace_index = 0
        count = 0
        self.faces = {}
        for i in self.context.list_families():
            name = i.get_name()
            monospace_index = count if name == "Monospace" else 0
            count += 1
            self.family_model.append([name])
            family_faces = gtk.ListStore(str, str)
            for face in i.list_faces():
                face_name = face.get_face_name()
                family_faces.append([face_name, "%s %s" % (name, face_name)])
            self.faces[name] = family_faces
        self.family_combo.set_model(self.family_model)
        self.family_combo.set_active(monospace_index)
        self.family_combo.connect("changed", self.family_changed)
        self.family_combo.show()
        self.family_tool_item = ToolComboBox(self.family_combo)
        self.family_tool_item.show()
        self.toolbar.insert(self.family_tool_item, -1)

        self.face_combo = ComboBox()
        face_renderer = gtk.CellRendererText()
        face_renderer.set_property("family-set", True)
        self.face_combo.pack_start(face_renderer)
        self.face_combo.add_attribute(face_renderer, 'text', 0)
        self.face_combo.add_attribute(face_renderer, 'font', 1)
        current_model = self.faces["Monospace"]
        self.face_combo.set_model(current_model)
        self.face_combo.set_active(0)
        self.face_combo.connect("changed", self.face_changed)
        self.face_combo.show()
        self.face_tool_item = ToolComboBox(self.face_combo)
        self.face_tool_item.show()
        self.toolbar.insert(self.face_tool_item, -1)

        self.size_adj = gtk.Adjustment(value=10,
                                       lower=5,
                                       upper=100,
                                       step_incr=1)
        self.size_adj.connect("value-changed", self.size_changed)
        self.size_spin = gtk.SpinButton(self.size_adj)
        self.size_spin.show()
        self.size_spin_item = gtk.ToolItem()
        self.size_spin_item.add(self.size_spin)
        self.size_spin_item.show()
        self.toolbar.insert(self.size_spin_item, -1)

        self.toolbar.show()
Exemplo n.º 9
0
    def build_toolbar(self):
        # make a toolbox
        toolbox = activity.ActivityToolbox(self)

        # modify the Activity tab
        activity_toolbar = toolbox.get_activity_toolbar()
        activity_toolbar.share.props.visible = False
        self.activity_toolbar = activity_toolbar

        # make the toolbars
        toolbars = {}
        for b in tools.allButtons:
            if not toolbars.has_key(b.toolBar):
                toolbars[b.toolBar] = gtk.Toolbar()

        # make + add the buttons
        self.buttonList = {}
        for c in tools.allButtons:
            button = ToolButton(c.icon)
            button.set_tooltip(_(c.toolTip))
            button.connect('clicked', self.buttonClicked)
            toolbars[c.toolBar].insert(button, -1)
            button.show()
            self.buttonList[button] = c.name

        # add the toolbars to the toolbox
        for bar in toolbars:
            toolbox.add_toolbar(bar, toolbars[bar])
            toolbars[bar].show()

        # make the level chooser combo box
        lbl = gtk.Label("Load a level: ")
        lbl.show()
        cmb = ComboBox()
        cmb.connect('changed', self.levelChanged)
        files = os.listdir("levels")
        for i in files:
            if i[-6:] == ".level":
                f = open("levels/" + i)
                try:
                    n = pickle.load(f)['name']
                except:
                    continue
                f.close()
                cmb.append_item("levels/" + i, n)

        cmb = ToolComboBox(cmb)
        lbl_ti = gtk.ToolItem()
        lbl_ti.add(lbl)
        toolbars['Run'].insert(lbl_ti, -1)
        lbl_ti.show()
        toolbars['Run'].insert(cmb, -1)
        cmb.show()
        button = ToolButton("load")
        button.set_tooltip(_("Load your own level"))
        button.connect('clicked', self.loadButtonClicked)
        toolbars['Run'].insert(button, -1)
        button.show()

        # add some level saving stuff to the save toolbar

        self.quantBox = ComboBox()
        self.quantBox.connect('changed', self.quantBoxChanged)
        self.quantBoxIndexes = {}
        for i in range(26):
            self.quantBox.append_item(i, str(i))
            self.quantBoxIndexes[i] = i
        self.quantBox.append_item(float('inf'), "Infinity")
        self.quantBoxIndexes[float('inf')] = 26
        cmb = ToolComboBox(self.quantBox)
        activity_toolbar.insert(cmb, 1)
        cmb.show()

        cmb = ComboBox()
        cmb.connect('changed', self.compQuantBoxChanged)
        for c in components.allComponents:
            if c.name != "X" and c.name != "O":
                cmb.append_item(c.name, c.name)
        cmb = ToolComboBox(cmb)
        activity_toolbar.insert(cmb, 1)
        cmb.show()

        lbl = gtk.Label(" Component amounts: ")
        lbl.show()
        ti = gtk.ToolItem()
        ti.add(lbl)
        activity_toolbar.insert(ti, 1)
        ti.show()

        toolbox.show()
        self.set_toolbox(toolbox)
        toolbox.set_current_toolbar(1)

        return activity_toolbar