def on_pos(self, *args): del args self.pos = set_position(self.parent.width, self.parent.height, self.width, self.height, self.config['x'], self.config['y'], self.config['anchor_x'], self.config['anchor_y'], self.config['adjust_top'], self.config['adjust_right'], self.config['adjust_bottom'], self.config['adjust_left']) with self.canvas: Color(*self.config['color']) if self.config['corner_radius']: RoundedRectangle(pos=self.pos, size=self.size, radius=(self.config['corner_radius'], self.config['corner_radius']), segments=self.config['corner_segments']) else: KivyRectangle(pos=self.pos, size=self.size)
def add_widgets_from_config(self, config, key=None, play_kwargs=None, widget_settings=None): if not isinstance(config, list): config = [config] widgets_added = list() if not play_kwargs: play_kwargs = dict() # todo for widget in config: if widget_settings: widget_settings = self.mc.config_validator.validate_config( 'widgets:{}'.format(widget['type']), widget_settings, base_spec='widgets:common', add_missing_keys=False) widget.update(widget_settings) configured_key = widget.get('key', None) if (configured_key and key and "." not in key and configured_key != key): raise KeyError("Widget has incoming key '{}' which does not " "match the key in the widget's config " "'{}'.".format(key, configured_key)) if configured_key: this_key = configured_key else: this_key = key widget_obj = self.mc.widgets.type_map[widget['type']]( mc=self.mc, config=widget, slide=self, key=this_key, play_kwargs=play_kwargs) top_widget = widget_obj # some widgets (like slide frames) have parents, so we need to make # sure that we add the parent widget to the slide while top_widget.parent: top_widget = top_widget.parent self.add_widget(top_widget) widget_obj.pos = set_position(self.width, self.height, widget_obj.width, widget_obj.height, widget_obj.config['x'], widget_obj.config['y'], widget_obj.config['anchor_x'], widget_obj.config['anchor_y'], widget_obj.config['adjust_top'], widget_obj.config['adjust_right'], widget_obj.config['adjust_bottom'], widget_obj.config['adjust_left']) widgets_added.append(widget_obj) return widgets_added
def add_widget(self, widget, **kwargs): del kwargs widget.pos = set_position(self.width, self.height, widget.width, widget.height) self.stencil.add_widget(widget, bisect_left(self.stencil.children, widget))
def set_position(self): try: self.pos = set_position( self.parent.width, self.parent.height, self.width, self.height, self.config['x'], self.config['y'], self.config['anchor_x'], self.config['anchor_y'], self.config['adjust_top'], self.config['adjust_right'], self.config['adjust_bottom'], self.config['adjust_left']) except AttributeError: pass
def on_pos(self, *args): self.dmd_frame.pos = set_position(self.parent.width, self.parent.height, self.width, self.height, self.config['x'], self.config['y'], self.config['anchor_x'], self.config['anchor_y'], self.config['adjust_top'], self.config['adjust_right'], self.config['adjust_bottom'], self.config['adjust_left'])
def on_pos(self, *args): # if this is the initial positioning, calculate it from the config # otherwise just update the slide frame and stencil if not self.slide_frame.pos: self.pos = set_position(self.parent.width, self.parent.height, self.width, self.height, self.slide_frame.config['x'], self.slide_frame.config['y'], self.slide_frame.config['anchor_x'], self.slide_frame.config['anchor_y']) self.stencil.pos = self.pos self.slide_frame.pos = self.pos
def on_pos(self, *args): del args self.pos = set_position( self.parent.width, self.parent.height, self.width, self.height, self.config['x'], self.config['y'], self.config['anchor_x'], self.config['anchor_y'], self.config['adjust_top'], self.config['adjust_right'], self.config['adjust_bottom'], self.config['adjust_left']) with self.canvas: Color(*self.config['color']) KivyEllipse(pos=self.pos, size=self.size, segments=self.config['segments'], angle_start=self.config['angle_start'], angle_end=self.config['angle_end'])
def add_widget(self, widget, **kwargs): del kwargs widget.pos = set_position(self.width, self.height, widget.width, widget.height, widget.config['x'], widget.config['y'], widget.config['anchor_x'], widget.config['anchor_y'], widget.config.get('adjust_top', 0), widget.config.get('adjust_right', 0), widget.config.get('adjust_bottom', 0), widget.config.get('adjust_left', 0)) self.stencil.add_widget(widget, bisect_left( self.stencil.children, widget))
def set_position(self): if self.config['anchor_y'] == 'baseline': try: self.pos = set_position(self.parent.width, self.parent.height, self.width, self.height, self.config['x'], self.config['y'], self.config['anchor_x'], 'bottom', # anchor_y self.config['adjust_top'], self.config['adjust_right'], self._label.get_descent() * -1, # adjust_bottom self.config['adjust_left']) except AttributeError: pass else: super().set_position()
def add_widget(self, widget, **kwargs): """Adds a widget to this slide. Args: widget: An MPF-enhanced widget (which will include details like z order and removal keys.) This method respects the z-order of the widget it's adding and inserts it into the proper position in the widget tree. Higher numbered z order values will be inserted after (so they draw on top) of existing ones. If the new widget has the same priority of existing widgets, the new one is inserted after the widgets of that priority, meaning the newest widget will be displayed on top of existing ones with the same priority. """ del kwargs if widget.config['z'] < 0: self.add_widget_to_parent_frame(widget) return self.stencil.add_widget(widget, bisect(self.stencil.children, widget)) widget.pos = set_position(self.size[0], self.size[1], widget.width, widget.height, widget.config['x'], widget.config['y'], widget.config['anchor_x'], widget.config['anchor_y'], widget.config['adjust_top'], widget.config['adjust_right'], widget.config['adjust_bottom'], widget.config['adjust_left'])
def test_set_position(self): parent_w = 100 parent_h = 100 w = 10 h = 10 x = None y = None anchor_x = None anchor_y = None # For all these tests, the widget is 10x10, and the resulting x,y # positions represent the lower left corner of the widget. # No anchor set, widget should be centered in the parent. Parent is # 100x100, widget is 10x10, so center of the parent is 50, 50, and # lower left corner of the widget is 45, 45 # test with all defaults res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (45, 45)) # test anchors anchor_x = 'left' anchor_y = 'bottom' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (50, 50)) # add adjustments res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y, 2, 0, 0, 0) self.assertEqual((res_x, res_y), (50, 50)) res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y, 0, 2, 0, 0) self.assertEqual((res_x, res_y), (50, 50)) res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y, 0, 0, 2, 0) self.assertEqual((res_x, res_y), (50, 48)) res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y, 0, 0, 0, 2) self.assertEqual((res_x, res_y), (48, 50)) anchor_x = 'middle' anchor_y = 'middle' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (45, 45)) # add adjustments res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y, 2, 0, 0, 0) self.assertEqual((res_x, res_y), (45, 46)) res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y, 0, 2, 0, 0) self.assertEqual((res_x, res_y), (46, 45)) res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y, 0, 0, 2, 0) self.assertEqual((res_x, res_y), (45, 44)) res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y, 0, 0, 0, 2) self.assertEqual((res_x, res_y), (44, 45)) anchor_x = 'center' anchor_y = 'center' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (45, 45)) anchor_x = 'right' anchor_y = 'top' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (40, 40)) # add adjustments res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y, 2, 0, 0, 0) self.assertEqual((res_x, res_y), (40, 42)) res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y, 0, 2, 0, 0) self.assertEqual((res_x, res_y), (42, 40)) res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y, 0, 0, 2, 0) self.assertEqual((res_x, res_y), (40, 40)) res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y, 0, 0, 0, 2) self.assertEqual((res_x, res_y), (40, 40)) # test positive x, y numbers x = 10 y = 10 anchor_x = 'left' anchor_y = 'bottom' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (10, 10)) anchor_x = 'middle' anchor_y = 'middle' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (5, 5)) anchor_x = 'center' anchor_y = 'center' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (5, 5)) anchor_x = 'right' anchor_y = 'top' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (0, 0)) # test negative x, y numbers x = -10 y = -10 anchor_x = None anchor_y = None anchor_x = 'left' anchor_y = 'bottom' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (-10, -10)) anchor_x = 'middle' anchor_y = 'middle' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (-15, -15)) anchor_x = 'center' anchor_y = 'center' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (-15, -15)) anchor_x = 'right' anchor_y = 'top' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (-20, -20)) # test positive percentages x = '80%' y = '20%' anchor_x = 'left' anchor_y = 'bottom' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (80, 20)) anchor_x = 'middle' anchor_y = 'middle' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (75, 15)) anchor_x = 'center' anchor_y = 'center' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (75, 15)) anchor_x = 'right' anchor_y = 'top' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (70, 10)) # test negative percentages (dunno how useful these are, but they work) x = '-80%' y = '-20%' anchor_x = 'left' anchor_y = 'bottom' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (-80, -20)) anchor_x = 'middle' anchor_y = 'middle' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (-85, -25)) anchor_x = 'center' anchor_y = 'center' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (-85, -25)) anchor_x = 'right' anchor_y = 'top' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (-90, -30)) # test positioning strings anchor_x = None anchor_y = None x = 'center' y = 'center' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (45, 45)) x = 'center+10' y = 'center + 10' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (55, 55)) x = 'middle-10' y = 'middle - 10' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (35, 35)) x = 'left' y = 'bottom' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (-5, -5)) x = 'right' y = 'top' res_x, res_y = set_position(parent_w, parent_h, w, h, x, y, anchor_x, anchor_y) self.assertEqual((res_x, res_y), (95, 95))