def _layout_as_overlay(self, size=None, force=False): """ Lays out the label as an overlay on another component. """ if self.component is not None: orientation = self.overlay_position outside = True if "inside" in orientation: tmp = orientation.split() tmp.remove("inside") orientation = tmp[0] outside = False elif "outside" in orientation: tmp = orientation.split() tmp.remove("outside") orientation = tmp[0] if orientation in ("left", "right"): self.y = self.component.y self.height = self.component.height if not outside: gc = font_metrics_provider() self.width = self._label.get_bounding_box(gc)[0] if orientation == "left": if outside: self.x = self.component.outer_x self.width = self.component.padding_left else: self.outer_x = self.component.x elif orientation == "right": if outside: self.x = self.component.x2 + 1 self.width = self.component.padding_right else: self.x = self.component.x2 - self.outer_width elif orientation in ("bottom", "top"): self.x = self.component.x self.width = self.component.width if not outside: gc = font_metrics_provider() self.height = self._label.get_bounding_box(gc)[1] if orientation == "bottom": if outside: self.y = self.component.outer_y self.height = self.component.padding_bottom else: self.outer_y = self.component.y elif orientation == "top": if outside: self.y = self.component.y2 + 1 self.height = self.component.padding_top else: self.y = self.component.y2 - self.outer_height else: # Leave the position alone pass return
def _do_layout(self, component=None): if component is None: component = self.component if component is not None: self.x = component.x # FIXME: Adding 2 to the self.y because there is a tiny gap # at the top of the toolbar where components from the block # canvas show through. self.y = component.y2 - self.toolbar_height + 2 self.height = self.toolbar_height self.width = component.width metrics = font_metrics_provider() if self.order == "right-to-left": last_button_position = self.width - self.button_spacing for b in self.components: x, y, w, h = metrics.get_text_extent(b.label) b.width = w + 2*b.label_padding b.x = last_button_position - b.width b.y = self.button_vposition last_button_position -= b.width + self.button_spacing*2 else: last_button_position = 0 for b in self.components: x, y, w, h = metrics.get_text_extent(b.label) b.width = w + 2*b.label_padding b.x = self.button_spacing + last_button_position b.y = self.button_vposition last_button_position += b.width + self.button_spacing*2
def get_preferred_size(self): """ Returns the label's preferred size. Overrides PlotComponent. """ dummy_gc = font_metrics_provider() size = self._label.get_bounding_box(dummy_gc) return size
def __init__(self, *args, **kwargs): """ Override __init__ so that we can initialize bounds. """ self._font_metrics_provider = font_metrics_provider() super(CanvasBox, self).__init__(*args, **kwargs) self.tools.append(BoxSelectionTool(self)) self.move_tool = BoxMoveTool(self) self.tools.append(self.move_tool)
def __init__(self, *args, **kwargs): super(EditField, self).__init__(*args, **kwargs) if self.metrics is None: self.metrics = font_metrics_provider() # If no bounds have been set, make sure it is wide enough to # display the text if self.height == 0 and self.width == 0 and len(self._text) > 0: self.update_bounds()
def get_ticks_and_labels(self, data_low, data_high, bounds_low, bounds_high, orientation = "h"): # TODO: add support for Interval # TODO: add support for vertical labels metrics = font_metrics_provider() if self.font is not None and hasattr(metrics, "set_font"): metrics.set_font(self.font) test_str = "0123456789-+" charsize = metrics.get_full_text_extent(test_str)[0] / len(test_str) numchars = (bounds_high - bounds_low) / charsize tmp = zip(*self.scale.labels(data_low, data_high, numlabels=8, char_width=numchars)) # Check to make sure we actually have labels/ticks to show before # unpacking the return tuple into (tick_array, labels). if len(tmp) == 0: return array([]), [] else: return array(tmp[0]), tmp[1]
def _recompute_font_metrics(self): if self.label != "": metrics = font_metrics_provider() metrics.set_font(self.label_font) self._text_extents = metrics.get_text_extent(self.label)
def __font_metrics_provider_default(self): return font_metrics_provider()
def get_preferred_size(self): """ Computes the size and position of the legend based on the maximum size of the labels, the alignment, and position of the component to overlay. """ # Gather the names of all the labels we will create if len(self.plots) == 0: return [0, 0] plot_names, visible_plots = map(list, zip(*sorted(self.plots.items()))) label_names = self.labels if len(label_names) == 0: if len(self.plots) > 0: label_names = plot_names else: self._cached_labels = [] self._cached_label_sizes = [] self._cached_label_names = [] self._cached_visible_plots = [] self.outer_bounds = [0, 0] return [0, 0] if self.hide_invisible_plots: visible_labels = [] visible_plots = [] for i, name in enumerate(label_names): val = self.plots[plot_names[i]] # Rather than checking for a list/TraitListObject/etc., we just check # for the attribute first if hasattr(val, 'visible'): if val.visible: visible_labels.append(name) visible_plots.append(val) else: # If we have a list of renderers, add the name if any of them are # visible for renderer in val: if renderer.visible: visible_labels.append(name) visible_plots.append(val) break label_names = visible_labels # Create the labels labels = [self._create_label(text) for text in label_names] # For the legend title if self.title_at_top: labels.insert(0, self._create_label(self.title)) label_names.insert(0, 'Legend Label') visible_plots.insert(0, None) else: labels.append(self._create_label(self.title)) label_names.append(self.title) visible_plots.append(None) # We need a dummy GC in order to get font metrics dummy_gc = font_metrics_provider() label_sizes = array([label.get_width_height(dummy_gc) for label in labels]) if len(label_sizes) > 0: max_label_width = max(label_sizes[:, 0]) total_label_height = sum(label_sizes[:, 1]) + (len(label_sizes)-1)*self.line_spacing else: max_label_width = 0 total_label_height = 0 legend_width = max_label_width + self.icon_spacing + self.icon_bounds[0] \ + self.hpadding + 2*self.border_padding legend_height = total_label_height + self.vpadding + 2*self.border_padding self._cached_labels = labels self._cached_label_sizes = label_sizes self._cached_label_positions = zeros_like(label_sizes) self._cached_label_names = label_names self._cached_visible_plots = visible_plots if "h" not in self.resizable: legend_width = self.outer_width if "v" not in self.resizable: legend_height = self.outer_height return [legend_width, legend_height]
def __init__(self, *args, **kwargs): super(FontMetricsCache, self).__init__(*args, **kwargs) if self.metrics is None: self.metrics = font_metrics_provider()