def __initVariantRadio(cls, confid, radiobutton, configImage): model = Gtk.TreeStore(str) treeview = Gtk.TreeView(model) treeview.set_headers_visible(False) treeview.append_column(Gtk.TreeViewColumn(None, Gtk.CellRendererText(), text=0)) alignment = Gtk.Alignment.new(1, 1, 1, 1) alignment.set_padding(6, 6, 12, 12) alignment.add(treeview) ImageMenu.switchWithImage(configImage, alignment) groupNames = {VARIANTS_BLINDFOLD: _("Blindfold"), VARIANTS_ODDS: _("Odds"), VARIANTS_SHUFFLE: _("Shuffle"), VARIANTS_OTHER: _("Other (standard rules)"), VARIANTS_OTHER_NONSTANDARD: _("Other (non standard rules)"), VARIANTS_ASEAN: _("Asian variants"), } specialVariants = [v for v in variants.values() if v != NormalBoard and v.variant not in UNSUPPORTED ] specialVariants = sorted(specialVariants, key=attrgetter("variant_group")) groups = groupby(specialVariants, attrgetter("variant_group")) pathToVariant = {} variantToPath = {} for i, (id, group) in enumerate(groups): iter = model.append(None, (groupNames[id], )) for variant in group: subiter = model.append(iter, (variant.name, )) path = model.get_path(subiter) pathToVariant[path.to_string()] = variant.variant variantToPath[variant.variant] = path.to_string() treeview.expand_row(Gtk.TreePath(i), True) selection = treeview.get_selection() selection.set_mode(Gtk.SelectionMode.BROWSE) def selfunc(selection, store, path, path_selected, data): return path.get_depth() > 1 selection.set_select_function(selfunc, None) variant = conf.get(confid) if variant in variantToPath: selection.select_path(variantToPath[variant]) def callback(selection): model, iter = selection.get_selected() if iter: radiobutton.set_label("%s" % model.get(iter, 0) + _(" chess")) path = model.get_path(iter) variant = pathToVariant[path.to_string()] conf.set(confid, variant) selection.connect("changed", callback) callback(selection)
def __initVariantRadio(cls, confid, radiobutton, configImage, default): model = Gtk.TreeStore(str) treeview = Gtk.TreeView(model) treeview.set_headers_visible(False) treeview.append_column( Gtk.TreeViewColumn(None, Gtk.CellRendererText(), text=0)) alignment = Gtk.Alignment.new(1, 1, 1, 1) alignment.set_padding(6, 6, 12, 12) alignment.add(treeview) ImageMenu.switchWithImage(configImage, alignment) groupNames = { VARIANTS_BLINDFOLD: _("Blindfold"), VARIANTS_ODDS: _("Odds"), VARIANTS_SHUFFLE: _("Shuffle"), VARIANTS_OTHER: _("Other (standard rules)"), VARIANTS_OTHER_NONSTANDARD: _("Other (non standard rules)"), VARIANTS_ASEAN: _("Asian variants"), } specialVariants = [ v for v in variants.values() if v != NormalBoard and v.variant not in UNSUPPORTED ] specialVariants = sorted(specialVariants, key=attrgetter("variant_group")) groups = groupby(specialVariants, attrgetter("variant_group")) pathToVariant = {} variantToPath = {} for i, (id, group) in enumerate(groups): iter = model.append(None, (groupNames[id], )) for variant in group: subiter = model.append(iter, (variant.name, )) path = model.get_path(subiter) pathToVariant[path.to_string()] = variant.variant variantToPath[variant.variant] = path.to_string() treeview.expand_row(Gtk.TreePath(i), True) selection = treeview.get_selection() selection.set_mode(Gtk.SelectionMode.BROWSE) def selfunc(selection, store, path, path_selected, data): return path.get_depth() > 1 selection.set_select_function(selfunc, None) variant = conf.get(confid, default) if variant in variantToPath: selection.select_path(variantToPath[variant]) def callback(selection): model, iter = selection.get_selected() if iter: radiobutton.set_label("%s" % model.get(iter, 0) + _(" chess")) path = model.get_path(iter) variant = pathToVariant[path.to_string()] conf.set(confid, variant) selection.connect("changed", callback) callback(selection)
def __initTimeRadio(cls, name, id, radiobutton, configImage, defmin, defgain): #minSpin = Gtk.SpinButton(Gtk.Adjustment(1,1,240,1)) minSpin = Gtk.SpinButton() minSpin.set_adjustment(Gtk.Adjustment(1, 0, 240, 1)) #gainSpin = Gtk.SpinButton(Gtk.Adjustment(0,-60,60,1)) gainSpin = Gtk.SpinButton() gainSpin.set_adjustment(Gtk.Adjustment(0, -60, 60, 1)) setattr(cls, "%s_min" % id, minSpin) setattr(cls, "%s_gain" % id, gainSpin) uistuff.keep(minSpin, "%s min" % id, first_value=defmin) uistuff.keep(gainSpin, "%s gain" % id, first_value=defgain) table = Gtk.Table(2, 2) table.props.row_spacing = 3 table.props.column_spacing = 12 label = Gtk.Label(label=_("Minutes:")) label.props.xalign = 0 table.attach(label, 0, 1, 0, 1) table.attach(minSpin, 1, 2, 0, 1) label = Gtk.Label(label=_("Gain:")) label.props.xalign = 0 table.attach(label, 0, 1, 1, 2) table.attach(gainSpin, 1, 2, 1, 2) alignment = Gtk.Alignment.new(1, 1, 1, 1) alignment.set_padding(6, 6, 12, 12) alignment.add(table) ImageMenu.switchWithImage(configImage, alignment) def updateString(spin): minutes = minSpin.get_value_as_int() gain = gainSpin.get_value_as_int() if gain > 0: radiobutton.set_label( _("%(name)s %(minutes)d min + %(gain)d sec/move") % { 'name': name, 'minutes': minutes, 'gain': gain }) elif gain < 0: radiobutton.set_label( _("%(name)s %(minutes)d min %(gain)d sec/move") % { 'name': name, 'minutes': minutes, 'gain': gain }) else: radiobutton.set_label( _("%(name)s %(minutes)d min") % { 'name': name, 'minutes': minutes }) minSpin.connect("value-changed", updateString) gainSpin.connect("value-changed", updateString) updateString(None)
def __initTimeRadio(cls, name, id, radiobutton, configImage, defmin, defgain): # minSpin = Gtk.SpinButton(Gtk.Adjustment(1,1,240,1)) minSpin = Gtk.SpinButton() minSpin.set_adjustment(Gtk.Adjustment(1, 0, 240, 1)) # gainSpin = Gtk.SpinButton(Gtk.Adjustment(0,-60,60,1)) gainSpin = Gtk.SpinButton() gainSpin.set_adjustment(Gtk.Adjustment(0, -60, 60, 1)) setattr(cls, "%s_min" % id, minSpin) setattr(cls, "%s_gain" % id, gainSpin) uistuff.keep(minSpin, "%s min" % id, first_value=defmin) uistuff.keep(gainSpin, "%s gain" % id, first_value=defgain) table = Gtk.Table(2, 2) table.props.row_spacing = 3 table.props.column_spacing = 12 label = Gtk.Label(label=_("Minutes:")) label.props.xalign = 0 table.attach(label, 0, 1, 0, 1) table.attach(minSpin, 1, 2, 0, 1) label = Gtk.Label(label=_("Gain:")) label.props.xalign = 0 table.attach(label, 0, 1, 1, 2) table.attach(gainSpin, 1, 2, 1, 2) alignment = Gtk.Alignment.new(1, 1, 1, 1) alignment.set_padding(6, 6, 12, 12) alignment.add(table) ImageMenu.switchWithImage(configImage, alignment) def updateString(spin): minutes = minSpin.get_value_as_int() gain = gainSpin.get_value_as_int() if gain > 0: radiobutton.set_label( _("%(name)s %(minutes)d min + %(gain)d sec/move") % { 'name': name, 'minutes': minutes, 'gain': gain }) elif gain < 0: radiobutton.set_label( _("%(name)s %(minutes)d min %(gain)d sec/move") % { 'name': name, 'minutes': minutes, 'gain': gain }) else: radiobutton.set_label(_("%(name)s %(minutes)d min") % { 'name': name, 'minutes': minutes }) minSpin.connect("value-changed", updateString) gainSpin.connect("value-changed", updateString) updateString(None)
def __initVariantRadio (cls, confid, radiobutton, configImage, default): model = gtk.TreeStore(str) treeview = gtk.TreeView(model) treeview.set_headers_visible(False) treeview.append_column(gtk.TreeViewColumn(None, gtk.CellRendererText(), text=0)) alignment = gtk.Alignment(1,1,1,1) alignment.set_padding(6,6,12,12) alignment.add(treeview) ImageMenu.switchWithImage(configImage, alignment) groupNames = {VARIANTS_BLINDFOLD: _("Blindfold"), VARIANTS_ODDS: _("Odds"), VARIANTS_SHUFFLE: _("Shuffle"), VARIANTS_OTHER: _("Other (standard rules)"), VARIANTS_OTHER_NONSTANDARD: _("Other (non standard rules)"), } specialVariants = [v for v in variants.values() if v != NormalChess and v.board.variant not in UNSUPPORTED] groups = groupby(specialVariants, attrgetter("variant_group")) pathToVariant = {} variantToPath = {} for i, (id, group) in enumerate(groups): iter = model.append(None, (groupNames[id],)) for variant in group: subiter = model.append(iter, (variant.name,)) path = model.get_path(subiter) pathToVariant[path] = variant.board.variant variantToPath[variant.board.variant] = path treeview.expand_row((i,), True) selection = treeview.get_selection() selection.set_mode(gtk.SELECTION_BROWSE) selection.set_select_function(lambda path: len(path) > 1) selection.select_path(variantToPath[conf.get(confid, default)]) def callback (selection): model, iter = selection.get_selected() if iter: radiobutton.set_label("%s" % model.get(iter, 0) + _(" chess")) path = model.get_path(iter) variant = pathToVariant[path] conf.set(confid, variant) selection.connect("changed", callback) callback(selection)
def __initVariantRadio (cls, confid, radiobutton, configImage, default): model = gtk.TreeStore(str) treeview = gtk.TreeView(model) treeview.set_headers_visible(False) treeview.append_column(gtk.TreeViewColumn(None, gtk.CellRendererText(), text=0)) alignment = gtk.Alignment(1,1,1,1) alignment.set_padding(6,6,12,12) alignment.add(treeview) ImageMenu.switchWithImage(configImage, alignment) groupNames = {VARIANTS_BLINDFOLD: _("Blindfold"), VARIANTS_ODDS: _("Odds"), VARIANTS_SHUFFLE: _("Shuffle"), VARIANTS_OTHER: _("Other")} specialVariants = [v for v in variants.values() if v != NormalChess] groups = groupby(specialVariants, attrgetter("variant_group")) pathToVariant = {} variantToPath = {} for i, (id, group) in enumerate(groups): iter = model.append(None, (groupNames[id],)) for variant in group: subiter = model.append(iter, (variant.name,)) path = model.get_path(subiter) pathToVariant[path] = variant.board.variant variantToPath[variant.board.variant] = path treeview.expand_row((i,), True) selection = treeview.get_selection() selection.set_mode(gtk.SELECTION_BROWSE) selection.set_select_function(lambda path: len(path) > 1) selection.select_path(variantToPath[conf.get(confid, default)]) def callback (selection): model, iter = selection.get_selected() if iter: radiobutton.set_label("%s" % model.get(iter, 0) + _(" chess")) path = model.get_path(iter) variant = pathToVariant[path] conf.set(confid, variant) selection.connect("changed", callback) callback(selection)
def __initTimeRadio(cls, id, radiobutton, configImage, defmin, defgain, defmoves): minSpin = Gtk.SpinButton() minSpin.set_adjustment(Gtk.Adjustment(1, 0, 240, 1)) setattr(cls, "%s_min" % id, minSpin) uistuff.keep(minSpin, "%s min" % id) movesSpin = Gtk.SpinButton() movesSpin.set_adjustment(Gtk.Adjustment(0, 0, 60, 20)) setattr(cls, "%s_moves" % id, movesSpin) uistuff.keep(movesSpin, "%s moves" % id) gainSpin = Gtk.SpinButton() gainSpin.set_adjustment(Gtk.Adjustment(0, -60, 60, 1)) setattr(cls, "%s_gain" % id, gainSpin) uistuff.keep(gainSpin, "%s gain" % id) table = Gtk.Table(2, 2) table.props.row_spacing = 3 table.props.column_spacing = 12 label = Gtk.Label(label=_("Minutes:")) label.props.xalign = 0 table.attach(label, 0, 1, 0, 1) table.attach(minSpin, 1, 2, 0, 1) label = Gtk.Label(label=_("Moves:") if defmoves > 0 else _("Gain:")) label.props.xalign = 0 table.attach(label, 0, 1, 1, 2) if defmoves > 0: table.attach(movesSpin, 1, 2, 1, 2) else: table.attach(gainSpin, 1, 2, 1, 2) alignment = Gtk.Alignment.new(1, 1, 1, 1) alignment.set_padding(6, 6, 12, 12) alignment.add(table) ImageMenu.switchWithImage(configImage, alignment) def updateString(spin): # Elements of the clock minutes = minSpin.get_value_as_int() gain = gainSpin.get_value_as_int() moves = movesSpin.get_value_as_int() # Duration of the game def calculate_duration(ref_moves): if moves > 0: return int(2 * minutes * ref_moves / moves) else: return max(0, int(2 * minutes + ref_moves * gain / 30)) duration_20 = calculate_duration(20) duration_40 = calculate_duration(40) duration_60 = calculate_duration(60) # Determination of the caption def get_game_name(): """ https://www.fide.com/fide/handbook.html?id=171&view=article """ if defmoves > 0: return _("Classical") if duration_60 <= 20: # 10 minutes per player return _("Blitz") if duration_60 < 120: # 60 minutes per player return _("Rapid") return _("Normal") if moves > 0: radiobutton.set_label(_("%(name)s %(minutes)d min / %(moves)d moves %(duration)s") % { 'name': get_game_name(), 'minutes': minutes, 'moves': moves, 'duration': ("(%d')" % duration_40) if duration_40 > 0 else "" }) elif gain != 0: radiobutton.set_label( _("%(name)s %(minutes)d min %(sign)s %(gain)d sec/move %(duration)s") % { 'name': get_game_name(), 'minutes': minutes, 'sign': "+" if gain > 0 else "–", 'gain': abs(gain), 'duration': ("(%d')" % duration_40) if duration_40 > 0 else "" }) else: radiobutton.set_label(_("%(name)s %(minutes)d min %(duration)s") % { 'name': get_game_name(), 'minutes': minutes, 'duration': ("(%d')" % duration_40) if duration_40 > 0 else "" }) # Determination of the tooltip if duration_20 > 0 and duration_60 > 0 and duration_20 != duration_60: radiobutton.set_tooltip_text(_("Estimated duration : %(min)d - %(max)d minutes") % ({ "min": min(duration_20, duration_60), "max": max(duration_20, duration_60)} )) else: radiobutton.set_tooltip_text("") minSpin.connect("value-changed", updateString) movesSpin.connect("value-changed", updateString) gainSpin.connect("value-changed", updateString) updateString(None)
def __initTimeRadio(cls, id, radiobutton, configImage, defmin, defgain, defmoves): minSpin = Gtk.SpinButton() minSpin.set_adjustment(Gtk.Adjustment(1, 0, 240, 1)) setattr(cls, "%s_min" % id, minSpin) uistuff.keep(minSpin, "%s min" % id) movesSpin = Gtk.SpinButton() movesSpin.set_adjustment(Gtk.Adjustment(0, 0, 60, 20)) setattr(cls, "%s_moves" % id, movesSpin) uistuff.keep(movesSpin, "%s moves" % id) gainSpin = Gtk.SpinButton() gainSpin.set_adjustment(Gtk.Adjustment(0, -60, 60, 1)) setattr(cls, "%s_gain" % id, gainSpin) uistuff.keep(gainSpin, "%s gain" % id) table = Gtk.Table(2, 2) table.props.row_spacing = 3 table.props.column_spacing = 12 label = Gtk.Label(label=_("Minutes:")) label.props.xalign = 0 table.attach(label, 0, 1, 0, 1) table.attach(minSpin, 1, 2, 0, 1) label = Gtk.Label(label=_("Moves:") if defmoves > 0 else _("Gain:")) label.props.xalign = 0 table.attach(label, 0, 1, 1, 2) if defmoves > 0: table.attach(movesSpin, 1, 2, 1, 2) else: table.attach(gainSpin, 1, 2, 1, 2) alignment = Gtk.Alignment.new(1, 1, 1, 1) alignment.set_padding(6, 6, 12, 12) alignment.add(table) ImageMenu.switchWithImage(configImage, alignment) def updateString(spin): # Elements of the clock minutes = minSpin.get_value_as_int() gain = gainSpin.get_value_as_int() moves = movesSpin.get_value_as_int() # Duration of the game def calculate_duration(ref_moves): if moves > 0: return int(2 * minutes * ref_moves / moves) else: return max(0, int(2 * minutes + ref_moves * gain / 30)) duration_20 = calculate_duration(20) duration_40 = calculate_duration(40) duration_60 = calculate_duration(60) # Determination of the caption def get_game_name(): """ https://www.fide.com/fide/handbook.html?id=171&view=article """ if defmoves > 0: return _("Classical") if duration_60 <= 20: # 10 minutes per player return _("Blitz") if duration_60 < 120: # 60 minutes per player return _("Rapid") return _("Normal") if moves > 0: radiobutton.set_label( _("%(name)s %(minutes)d min / %(moves)d moves %(duration)s" ) % { 'name': get_game_name(), 'minutes': minutes, 'moves': moves, 'duration': ("(%d')" % duration_40) if duration_40 > 0 else "" }) elif gain != 0: radiobutton.set_label( _("%(name)s %(minutes)d min %(sign)s %(gain)d sec/move %(duration)s" ) % { 'name': get_game_name(), 'minutes': minutes, 'sign': "+" if gain > 0 else "–", 'gain': abs(gain), 'duration': ("(%d')" % duration_40) if duration_40 > 0 else "" }) else: radiobutton.set_label( _("%(name)s %(minutes)d min %(duration)s") % { 'name': get_game_name(), 'minutes': minutes, 'duration': ("(%d')" % duration_40) if duration_40 > 0 else "" }) # Determination of the tooltip if duration_20 > 0 and duration_60 > 0 and duration_20 != duration_60: radiobutton.set_tooltip_text( _("Estimated duration : %(min)d - %(max)d minutes") % ({ "min": min(duration_20, duration_60), "max": max(duration_20, duration_60) })) else: radiobutton.set_tooltip_text("") minSpin.connect("value-changed", updateString) movesSpin.connect("value-changed", updateString) gainSpin.connect("value-changed", updateString) updateString(None)