File: box.py

package info (click to toggle)
python-mcstasscript 0.0.46%2Bgit20250402111921.bfa5a26-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,440 kB
  • sloc: python: 13,421; makefile: 14
file content (87 lines) | stat: -rw-r--r-- 2,977 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import matplotlib.pyplot as plt
from mcstasscript.helper.mcstas_objects import Component

class ComponentBox:
    """
    Helper class for creating text boxes describing components
    """
    def __init__(self, box_input):
        """
        Text box object
        """
        # set defaults
        self.position_x = None
        self.position_y = None
        self.box_height = None
        self.box_indent = None
        self.background_color = "white"
        self.outline_style = "-"
        self.outline_width = 1

        self.t = None  # text object
        self.graph_box_end = None  # graph position where text ends
        self.graph_box_start = None  # graph position where text ends

        # Load component
        if isinstance(box_input, str):
            self.component_object = None
            self.name = box_input
        elif isinstance(box_input, Component):
            self.component_object = box_input
            self.name = self.component_object.name

            # Decorate the box depending on the McStas features used
            if self.component_object.WHEN != "":
                self.outline_style = "--"

            if self.component_object.EXTEND != "":
                self.outline_width = 2.5
        else:
            raise ValueError("Input for box needs to be of type Component or str, not "
                             + str(type(box_input)))

        # Produced weighted length of name, capital letters count for 1.2
        self.weighted_name_length = len(self.name) + 0.2*sum(1 for c in self.name if c.isupper())

    def set_box_height(self, box_height):
        self.box_height = box_height

    def set_x(self, x):
        self.position_x = x

    def set_y(self, y):
        self.position_y = y

    def set_box_indent(self, value):
        self.box_indent = value

    def set_background_color(self, value):
        self.background_color = value

    def plot_box(self, ax):
        bbox = dict(boxstyle="round", facecolor=self.background_color,
                    edgecolor="black", linestyle=self.outline_style,
                    linewidth=self.outline_width)

        self.t = ax.text(self.position_x + self.box_indent, self.position_y, self.name,
                         va="center", fontweight="semibold", color="black", #font="monospace",
                         bbox=bbox)

    def get_text_start(self):
        return self.graph_box_start

    def get_text_end(self):
        return self.graph_box_end

    def calculate_bbox_dimensions(self, ax, graph_width):
        """
        Method that gets dimensions of plotted text box
        """
        transf = ax.transAxes.inverted()
        bb = self.t.get_window_extent(renderer=plt.gcf().canvas.get_renderer())
        bbox = bb.transformed(transf)

        TEXT_PADDING_IN_FIG_UNITS = 0.08
        box_padding = TEXT_PADDING_IN_FIG_UNITS / graph_width
        self.graph_box_start = bbox.x0 - box_padding
        self.graph_box_end = bbox.x1 + box_padding