Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/atoidejouer/ui/screen/activity.py
blob: 54cd04fb5102d6a5202825a4a00173a4a398950f (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# python import
import logging, os
from gettext import gettext as _

# gtk import
import gtk

# atoidejouer import
from atoidejouer.tools import config, ui, storage

# get application logger
logger = logging.getLogger('atoidejouer')


def _on_toggle_click(radio, screen, option, value):
    # manage change state manually
    _previous = config.Config().get('activity>%s' % option)
    # remove screens to recreate them after
    if screen._initialized is True\
    and _previous != value:
        # update
        config.Config().set('activity>%s' % option, value)
        # ...
        if option in ['mode', 'dnd']:
            screen._activity.remove_screen('graphics')
            screen._activity.remove_screen('sounds')
        else:
            pass
    else:
        pass


def _on_button_click(button, screen, name, msg_label, value):
    if name == 'import':
        try:
            # int file chooser
            _chooser = ObjectChooser(button.get_label(), screen,
                    gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT)
            # get chooser result
            _r = _chooser.run()
            # check result
            if _r == gtk.RESPONSE_ACCEPT:
                # get selected object
                _object = _chooser.get_selected_object()
                # do import
                storage.import_project(screen._activity, _object.file_path,
                        msg_label)
            else:
                pass
        except Exception, e:
            # ERROR
            logger.error('[screens] _on_button_click - e: %s' % e)
    elif name == 'export':
        # do export
        storage.export_project(screen._activity, msg_label, value)
    else:
        pass

def _on_recent_click(button, screen, timestamp):
    _path = storage.get_path_from_journal(timestamp, 'atoidejouer/db')
    screen._activity.read_file(_path)


[
    ADVANCED,
    EASY,
] = range(2)

[
    FASTER,
    SMOOTH,
    NORMAL,
] = range(3)

[
    YES,
    NO,
] = range(2)


class ScreenActivity(gtk.ScrolledWindow):

    def __init__(self, activity_):
        gtk.ScrolledWindow.__init__(self)
        self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        # keep toolbar
        self._activity = activity_
        # dummy flag
        self._initialized = False
        # keep result label
        self.msg_label_import = None
        self.msg_label_export = None
        # init main box
        self.__main_box = gtk.HBox(homogeneous=True, spacing=2)
        self.__main_box.show()
        # ..
        self.add_with_viewport(self.__main_box)
        # add left menu
        self._add_left_part()
        # ..
        self.__main_box.pack_start(gtk.EventBox(), expand=False, fill=True)
        # add right menu
        self._add_right_part()
        # do show
        self._show()

    def _get_title(self, text):
        # init 
        _label = gtk.Label()
        # show
        _label.show()
        # set markup
        _label.set_use_markup(True)
        _label.set_markup('<span size="x-large" weight="heavy">%s</span>' % text)
        # align
        _label.set_padding(5, 5)
        _label.set_alignment(0, 0.5)
        # return it
        return _label

    def _get_toggle_box(self, group, option, value, text):
        # create checkbox
        _toggle = gtk.RadioButton(group=group, label=text)
        _toggle.show()
        # set current
        _current = config.Config().get('activity>%s' % option)
        _toggle.set_active(_current==value)
        # set cb
        _toggle.connect('toggled', _on_toggle_click, self, option, value)
        # return it
        return _toggle

    def _get_button(self, name, text, stock_id, msg_label=None, value=None):
        # init button
        _button = ui.get_button(label=text, stock_id=stock_id, width=48,
                                padding=(10, 0))
        _button.show()
        # set cb
        _button.connect('clicked', _on_button_click, self, name, msg_label,
                        value)
        # return it
        return _button

    def _get_hidden_text(self):
        # init 
        _label = gtk.Label()
        # set markup
        _label.set_use_markup(True)
        # align
        _label.set_padding(5, 5)
        _label.set_alignment(0, 0.5)
        # return it
        return _label

    def _get_included_part(self):
        _vbox = gtk.VBox(homogeneous=False, spacing=5)
        _vbox.set_border_width(5)
        _vbox.show()
        # add first part title
        _vbox.pack_start(self._get_title(_('INCLUDED STORIES')),
                expand=False, fill=True)
        return _vbox

    def _add_left_part(self):
        # ..
        _vbox = gtk.VBox(homogeneous=False, spacing=5)
        # border
        _vbox.set_border_width(5)
        # show
        _vbox.show()
        # add
        self.__main_box.pack_start(_vbox, expand=False, fill=True)
        # add part choices
        _vbox.pack_start(self._get_included_part(), expand=False, fill=True)

    def _get_export_part(self):
        _vbox = gtk.VBox(homogeneous=False, spacing=5)
        _vbox.set_border_width(5)
        _vbox.show()
        # add first part title
        _vbox.pack_start(self._get_title(_('IMPORT')),
                expand=False, fill=True)
        self.msg_label_import = self._get_hidden_text()
        _vbox.pack_start(self._get_button('import', _('Choose a File'),
            'import', msg_label=self.msg_label_import), expand=False, fill=True)
        _vbox.pack_start(self.msg_label_import, expand=False, fill=True)
        # add first part title
        _vbox.pack_start(self._get_title(_('EXPORT')),
                expand=False, fill=True)
        self.msg_label_export = self._get_hidden_text()
        # list the possible medias
        for _media in os.listdir('/media'):
            # current path
            _path = os.path.join('/media', _media)
            # should be a dir
            if os.path.isdir(_path):
                # set export button text
                _button_text = _('Export to') + ' "' + _media + '"'
                # init button
                _button = self._get_button('export', _button_text, 'export',
                        msg_label=self.msg_label_export, value=_media)
                # add button
                _vbox.pack_start(_button, expand=False, fill=True)
            else:
                continue
        # add label
        _vbox.pack_start(self.msg_label_export, expand=False, fill=True)
        # ..
        return _vbox

    def _get_story_button(self, ds_object):
        _button = ui.get_button(label=ds_object.metadata['title'],
                stock_id="play", width=48, padding=(10, 0))
        _button.connect('clicked', _on_recent_click, self,
                ds_object.metadata['timestamp'])
        _button.show()
        return _button

    def _get_recent_part(self):
        _vbox = gtk.VBox(homogeneous=False, spacing=5)
        _vbox.set_border_width(5)
        _vbox.show()
        # add first part title
        _vbox.pack_start(self._get_title(_('RECENT STORIES')),
                expand=False, fill=True)
        # list stories
        _nb_of_recent = config.Config().get('activity>recent', type_=int)
        _cur = 0
        for ds_obj in storage.journal_query({
            'activity': 'org.laptop.AToiDeJouerActivity'}):
            if _cur == _nb_of_recent:
                break
            elif os.path.exists(ds_obj.file_path):
                _cur += 1
                _vbox.pack_start(self._get_story_button(ds_obj), expand=False,
                                 fill=True)
        return _vbox

    def _add_right_part(self):
        # ..
        _vbox = gtk.VBox(homogeneous=False, spacing=5)
        # border
        _vbox.set_border_width(5)
        # show
        _vbox.show()
        # add
        self.__main_box.pack_start(_vbox, expand=False, fill=True)
        # add export
        _vbox.pack_start(self._get_export_part(), expand=False, fill=True)
        # add recent part
        _vbox.pack_start(self._get_recent_part(), expand=False, fill=True)

    def _show(self):
        self._initialized = True
        # show all
        self.show()
        # update toolbar
        self._activity.set_canvas(self)