summaryrefslogtreecommitdiff
path: root/gui.py
blob: bb4d25e775d7b28f8a312b35e5395263053df950 (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
import gi

gi.require_version('Gtk', '3.0')
import logging

from gi.repository import GObject, Gtk, Gdk

BUTTON_SPACING = 10


class PyATEMSwitcherGui():
    def __init__(self, config, switcher):
        self.log = logging.getLogger('GUI')
        self.config = config

        cssProvider = Gtk.CssProvider()
        cssProvider.load_from_path('style.css')
        screen = Gdk.Screen.get_default()
        styleContext = Gtk.StyleContext()
        styleContext.add_provider_for_screen(
            screen,
            cssProvider,
            Gtk.STYLE_PROVIDER_PRIORITY_USER,
        )

        self.window = Gtk.Window()
        self.window.connect("destroy", self._exit)

        self.switcher = switcher
        self.switcher.on_connect(self._switcher_connected)
        self.switcher.on_connect_attempt(self._switcher_connect_attempt)
        self.switcher.on_disconnect(self._switcher_disconnected)

        self.window.set_border_width(BUTTON_SPACING)

        self.header = Gtk.HeaderBar()
        self.header.props.title = 'PyATEMSwitcherGui: Idle'
        self.window.set_titlebar(self.header)

        self.box = None
        self.buttons = {}

    def _exit(self, *args, **kwargs):
        self.switcher.disconnect()
        Gtk.main_quit(*args, **kwargs)

    def _button_clicked(self, button, name):
        # TODO actually do something
        self.log.info(f'Button {name} was pressed')
        for btn in self.buttons:
            ctx = self.buttons[btn].get_style_context()
            if btn == name:
                self.log.debug(f'{btn}.add_class("selected")')
                ctx.add_class('selected')
            else:
                self.log.debug(f'{btn}.remove_class("selected")')
                ctx.remove_class('selected')

    def _switcher_connected(self, params):
        self.box = Gtk.FlowBox()
        self.box.set_column_spacing(BUTTON_SPACING)
        self.box.set_max_children_per_line(2)
        self.box.set_row_spacing(BUTTON_SPACING)
        self.box.set_selection_mode(Gtk.SelectionMode.NONE)
        self.box.set_valign(Gtk.Align.START)

        # TODO get input list from switcher
        for i in range(1, 7):
            self.buttons[f'input{i}'] = Gtk.Button.new_with_label(
                f'input{i}'
            )
            self.buttons[f'input{i}'].connect(
                'clicked',
                self._button_clicked,
                f'input{i}',
            )
            self.box.add(self.buttons[f'input{i}'])

        self.window.add(self.box)

    def _switcher_connect_attempt(self, params):
        self.header.props.title = 'PyATEMSwitcherGui: Connecting ...'

    def _switcher_disconnected(self, params):
        if self.box is not None:
            self.window.remove(self.box)
        self.box = None
        self.buttons = {}
        self.header.props.title = 'PyATEMSwitcherGui: Not connected'

    def _switcher_ping(self):
        # TODO actually do something here
        self.log.debug('_switcher_ping()')
        return True

    def main_loop(self):
        self.switcher.connect()
        self.window.show_all()
        GObject.timeout_add(500, self._switcher_ping)
        Gtk.main()