File: baseview.py

package info (click to toggle)
lirc 0.10.2-0.10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,268 kB
  • sloc: ansic: 26,981; cpp: 9,187; sh: 5,875; python: 4,507; makefile: 1,049; xml: 63
file content (100 lines) | stat: -rw-r--r-- 3,583 bytes parent folder | download | duplicates (4)
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
''' Simple lirc setup tool - common parts of view components '''


import sys
import gi
from gi.repository import Gtk         # pylint: disable=no-name-in-module
gi.require_version('Gtk', '3.0')

def _hasitem(dict_, key_):
    ''' Test if dict contains a non-null value for key. '''
    return key_ in dict_ and dict_[key_]


def on_window_delete_event_cb(window, event):
    ''' Generic window close event. '''
    window.hide()
    return True


class Baseview(object):
    ''' Common parts in MVC view components. '''

    def __init__(self, view):
        self.connected = set()
        self.builder = view.builder

    def test_and_set_connected(self, window_id):
        ''' Test and set window_id in self.connected. '''
        if window_id in self.connected:
            return True
        self.connected.add(window_id)
        return False

    def reconnect(self, widget, signal, callback, data=None):
        ''' Reconnect callback as handling signal, clear old connects. '''
        if isinstance(widget, str):
            widget = self.builder.get_object(widget)
        try:
            widget.disconnect_by_func(callback)
        except TypeError:
            pass
        widget.connect(signal, callback, data)

    def _create_treeview(self, object_id, columns):
        ''' Construct a treeview with some string columns. '''

        treeview = self.builder.get_object(object_id)
        if len(treeview.get_columns()) > 0:
            return treeview
        treeview.set_vscroll_policy(Gtk.ScrollablePolicy.NATURAL)
        types = [str for c in columns]
        treeview.set_model(Gtk.ListStore(*types))
        renderers = {}
        for i, colname in enumerate(columns):
            renderers[colname] = Gtk.CellRendererText()
            column = Gtk.TreeViewColumn(colname, renderers[colname], text=i)
            column.clickable = True
            treeview.append_column(column)
        return treeview

    def _message_dialog(self, header, kind, body, exit_):
        ''' Return a standard error/warning/info dialog. '''
        # pylint: disable=not-callable
        d = Gtk.MessageDialog(self.builder.get_object('main_window'),
                              0,
                              kind,
                              Gtk.ButtonsType.OK,
                              header)
        if body:
            body = body.replace('@', ' at ').replace('|', ' pipe ')
            d.format_secondary_markup(body)
        d.run()
        d.destroy()
        if exit_:
            sys.exit()

    def show_warning(self, header, body=None, exit_=False):
        ''' Display standard warning dialog. '''
        self._message_dialog(header, Gtk.MessageType.WARNING, body, exit_)

    def show_error(self, header, body=None, exit_=False):
        ''' Display standard error dialog. '''
        self._message_dialog(header, Gtk.MessageType.ERROR, body, exit_)

    def show_info(self, header, body=None, exit_=False):
        ''' Display standard error dialog. '''
        self._message_dialog(header, Gtk.MessageType.INFO, body, exit_)

    def show_link_info(self, header, body):
        ''' Show a info message, handling "See: <link>" by linking. '''
        template = 'See: <a href="{link}" title="{link}">{link}</a>'
        if body.lower().startswith('see') and (len(body.split(' ')) == 2):
            link = body.split(' ')[1]
            if link.endswith('.'):
                link = link[:-1]
            body = template.format(link=link)
        self._message_dialog(header, Gtk.MessageType.INFO, body, False)


# vim: set expandtab ts=4 sw=4: