Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/widgets.py
blob: 1f3cff953eb1098c29eeb54fa58920dfc49fed76 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (C) 2013 Agustin Zubiaga <aguz@sugarlabs.org>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GdkPixbuf
from gi.repository import GObject

import os
from sugar3.activity import activity
from sugar3.graphics import style
from sugar3.graphics.icon import Icon

from gettext import gettext as _

ICONS_DIR = os.path.join(activity.get_bundle_path(), "icons")


def get_icon(icon_id):
    icon_path = os.path.join(ICONS_DIR, icon_id + ".svg")
    if not os.path.exists(icon_path):
        icon_path = os.path.join(ICONS_DIR, "bluetooth.svg")

    return icon_path


class DeviceList(Gtk.TreeView):

    def __init__(self):
        super(DeviceList, self).__init__()

        self.model = Gtk.ListStore(GdkPixbuf.Pixbuf, str, str)
        self.set_model(self.model)

        column = Gtk.TreeViewColumn('Devices')
        cell = Gtk.CellRendererPixbuf()
        column.pack_start(cell, False)
        column.add_attribute(cell, "pixbuf", 0)
        self.append_column(column)

        column = Gtk.TreeViewColumn()
        cell = Gtk.CellRendererText()
        column.pack_start(cell, True)
        column.add_attribute(cell, "markup", 1)
        self.append_column(column)

        column = Gtk.TreeViewColumn()
        cell = Gtk.CellRendererText()
        column.pack_start(cell, False)
        column.add_attribute(cell, "markup", 2)
        self.append_column(column)

        #self.modify_base(Gdk.Color(255, 255, 255))

    def add_device(self, i):
        icon = get_icon(i["Icon"])
        pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(icon,
                         style.GRID_CELL_SIZE, style.GRID_CELL_SIZE)
        self.model.append([pixbuf, '<b>%s</b>' % i["Name"], 
                            '<b>%s</b>' % i["Address"]])

    def set_devices(self, devices):
        for i in devices:
            self.add_device(i)


class EmptyWidgets(Gtk.EventBox):

    __gsignals__ = {
        'search-again': (GObject.SignalFlags.RUN_FIRST, None, [])}

    def __init__(self):
        Gtk.EventBox.__init__(self)

        self.modify_bg(Gtk.StateType.NORMAL,
                                style.COLOR_WHITE.get_gdk_color())

        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        mvbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        vbox.pack_start(mvbox, True, False, 0)

        image_icon = Icon(pixel_size=style.LARGE_ICON_SIZE,
                          file='activity/activity-bluetooth.svg',
                          stroke_color=style.COLOR_BUTTON_GREY.get_svg(),
                          fill_color=style.COLOR_TRANSPARENT.get_svg())
        mvbox.pack_start(image_icon, False, False, style.DEFAULT_PADDING)

        label = Gtk.Label('<span foreground="%s"><b>%s</b></span>' %
                          (style.COLOR_BUTTON_GREY.get_html(),
                          _('No devices found')))
        label.set_use_markup(True)
        mvbox.pack_start(label, False, False, style.DEFAULT_PADDING)

        hbox = Gtk.Box()
        search_btn = Gtk.Button()
        search_btn.connect('clicked', lambda w: self.emit('search-again'))
        refresh_image = Gtk.Image.new_from_stock(Gtk.STOCK_REFRESH,
                                             Gtk.IconSize.BUTTON)
        buttonbox = Gtk.Box()
        buttonbox.pack_start(refresh_image, False, True, 0)
        buttonbox.pack_end(Gtk.Label(_('Search again')), True, True, 5)
        search_btn.add(buttonbox)
        hbox.pack_start(search_btn, True, False, 0)
        mvbox.pack_start(hbox, False, False, style.DEFAULT_PADDING)

        self.add(vbox)
        self.show_all()