gen.py 4.54 KB
Newer Older
1
from __future__ import division
2 3 4 5
import os
import math

from . import resources
6 7 8 9
from .screens import get_path

##################################################

Jameson Rollins's avatar
Jameson Rollins committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23
MACROS = dict(
    IFO = '$(IFO)',
    SYSTEM = '$(SYSTEM)',
    SYSTEM_PATH = '$(SYSTEM_PATH)',
    )


def gen_macro_str(**kwargs):
    macros = MACROS.copy()
    if kwargs:
        macros.update(kwargs)
    return ','.join(['{}={}'.format(k, v) for k, v in macros.items()])


24 25
def gen_composite(**kwargs):
    path = get_path(kwargs['screen'])
Jameson Rollins's avatar
Jameson Rollins committed
26
    kwargs['macros'] = gen_macro_str(**kwargs.get('macros', {}))
27 28 29 30 31 32 33 34 35 36 37
    return '''
composite {{
        object {{
                x={x}
                y={y}
                width={width}
                height={height}
        }}
        "composite name"=""
        "composite file"="{path}; {macros}"
}}
Jameson Rollins's avatar
Jameson Rollins committed
38
'''.format(path=path, **kwargs)
39 40

##################################################
41 42 43 44 45 46

def write_screen_main(f, system):
    displays = system.related_displays + [('OVERVIEW',
                                           '$(USERAPPS)/sys/$(ifo)/medm/GUARD_OVERVIEW.adl',
                                           ''
                                           )]
47
    with open(get_path('GRD_MAIN.adl')) as fs:
48 49 50 51 52 53 54 55 56
        f.write(fs.read())
    f.write(resources.related_display_menu(displays,
                                           x=414,
                                           y=107,
                                           width=90,
                                           height=20,
                                           ))

def write_screen_compact(f):
57
    with open(get_path('GRD_COMPACT.adl')) as fs:
58 59 60
        f.write(fs.read())

def write_screen_minis(f):
61
    f.write(resources.header(bclr=14, height=400, width=400))
62
    y = 10
63 64 65 66
    for screen in ['GRD_NANO.adl', 'GRD_MICRO.adl', 'GRD_MINI.adl',
                   'GRD_MINI_USERMSG.adl', 'GRD_MINI_REQUEST_USERMSG.adl']: #, 'STATUSBAR']:
        f.write(gen_composite(screen=screen,
                              x=10, y=y, width=10, height=10))
67 68 69 70 71 72
        y += 70

def write_screen_states(f, system):
    states = system.states + ['NONE']
    nstates = len(states)

73 74 75 76 77 78
    margin = 10
    title_height = 18
    state_width = 305
    state_height = 25
    legend_height = 50

79
    # determine nice rectangle of state buttons
80
    sbar = state_width / float(state_height)
81 82
    rows = nstates
    cols = 1
83
    while rows > sbar*cols and nstates > 24:
84 85 86 87 88 89
        cols += 1
        rows = int(math.ceil(nstates/cols))
    def rc(i):
        col = int(math.floor(float(i)/rows))
        return i-col*rows, col

90 91
    offset = 84
    button_top = title_height + margin + offset
92
    width = margin + cols * state_width
93 94 95 96 97 98 99 100 101 102
    height = button_top + state_height*rows + margin
    if cols == 1:
        height += legend_height

    f.write(resources.header(height=height, width=width))
    f.write(resources.background(height=height, width=width))
    f.write(resources.title(x=48))

    f.write(gen_composite(screen='GRD_STATES_HEAD.adl',
                          x=10, y=title_height+margin, width=300, height=200))
103 104 105 106

    for i, state in enumerate(states):
        r,c = rc(i)
        x = margin + c*state_width
107
        y = button_top + r*state_height
108 109 110 111 112 113 114 115 116 117 118 119 120
        if state == 'NONE':
            index = -1
            is_goto = False
            is_request = True
        else:
            index = system.index(state)
            is_goto = system.is_goto(state)
            is_request = system.is_request(state)
        f.write(resources.state_request_button(x=x, y=y,
                                               state=state, index=index,
                                               is_request=is_request,
                                               is_goto=is_goto,
                                               ))
Jameson Rollins's avatar
Jameson Rollins committed
121 122
        f.write(gen_composite(screen='GRD_STATES_TARGET.adl',
                              macros={'STATE_N': index},
123 124 125 126 127 128 129
                              x=x+270, y=y,
                              width=25, height=25))

    lx = 17
    ly = button_top+rows*state_height+8
    if cols > 1:
        lx = margin + state_width + 7
130
        ly = title_height + margin + 28
131 132 133
    f.write(gen_composite(screen='GRD_STATES_LEGEND.adl',
                          x=lx, y=ly,
                          width=315, height=18))
134 135

def write_screen_spm(f):
136
    with open(get_path('GRD_SPM.adl')) as fs:
137 138 139
        f.write(fs.read())

def write_screen_usermsg(f):
140
    with open(get_path('GRD_USERMSG.adl')) as fs:
141 142
        f.write(fs.read())

143 144 145
def write_composite_status(f, x=0, y=0):
    f.write(gen_composite(screen='GRD_STATUSBAR.adl',
                          x=x, y=y, width=10, height=10))