Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/browseeditpanel.py
blob: 3a8d324088f504542b53fd8ac267c41f75e721a2 (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
# -*- coding: utf-8 -*-
#Copyright (c) 2010, Kirk Winans

# 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.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
import gtk
from os import environ
from os.path import join, basename
import hippo

import shutil
import tempfile
from gettext import gettext as _
import logging
from gobject import SIGNAL_RUN_FIRST, TYPE_PYOBJECT
from sugar.graphics import style
from sugar.graphics.toolbutton import ToolButton
from sugar.graphics.icon import Icon
from sugar.graphics.palette import Palette
from port.widgets import ToggleToolButton
from port.widgets import CanvasRoundBox, ToolComboBox
#from port import chooser

from sugar.datastore import datastore
import xml.etree.ElementTree
from datetime import date
import time

import FlashcardActivity

#
# Browse/Edit panel for Flashcard Activity
#
class BrowseEditPanel(gtk.VBox):
    
    def __init__(self, deckfile):
        gtk.VBox.__init__(self)

        # Loading in tree of cards
        self.deckfile = deckfile
        self.tree = xml.etree.ElementTree.parse(self.deckfile)

        self.deck_element = self.tree.getroot()
        self.deck = self.deck_element.findall("card")
        self.current_card_index = 0

        next_card_tuple = self.next_card()

        self.viewcard_box = gtk.VBox()

        # Boxes to view the different parts of the cards
        self.viewfront_box = gtk.HBox()
        self.viewback_box = gtk.HBox()
        self.viewcatagory_box = gtk.HBox()
        self.viewstage_box = gtk.HBox()
        self.viewdate_box = gtk.HBox()

        self.front_label = gtk.Label(_('Front:'))

        self.viewfront = gtk.TextView()
        self.frontbuffer = self.viewfront.get_buffer()
        self.viewfront.set_editable(False)
        self.viewfront.set_wrap_mode(gtk.WRAP_WORD)
        self.viewfront.set_justification(gtk.JUSTIFY_CENTER)
        self.viewfront.set_pixels_below_lines(0)
        self.viewfront.set_pixels_above_lines(40)

        self.back_label = gtk.Label(_('Back:'))

        self.viewback = gtk.TextView()
        self.backbuffer = self.viewback.get_buffer()
        self.viewback.set_editable(False)
        self.viewback.set_wrap_mode(gtk.WRAP_WORD)
        self.viewback.set_justification(gtk.JUSTIFY_CENTER)
        self.viewback.set_pixels_below_lines(0)
        self.viewback.set_pixels_above_lines(40)

        self.catagory_label = gtk.Label(_('Category:'))
        
        self.viewcatagory = gtk.TextView()
        self.catagorybuffer = self.viewcatagory.get_buffer()
        self.viewcatagory.set_editable(False)
        self.viewcatagory.set_wrap_mode(gtk.WRAP_WORD)
        self.viewcatagory.set_justification(gtk.JUSTIFY_CENTER)
        self.viewcatagory.set_pixels_below_lines(0)
        self.viewcatagory.set_pixels_above_lines(40)

        self.stage_label = gtk.Label(_('Stage:'))

        self.viewstage = gtk.TextView()
        self.stagebuffer = self.viewstage.get_buffer()
        self.viewstage.set_editable(False)
        self.viewstage.set_wrap_mode(gtk.WRAP_WORD)
        self.viewstage.set_justification(gtk.JUSTIFY_CENTER)
        self.viewstage.set_pixels_below_lines(0)
        self.viewstage.set_pixels_above_lines(40)

        self.date_label = gtk.Label(_('Last Reviewed:'))

        self.viewdate = gtk.TextView()
        self.datebuffer = self.viewdate.get_buffer()
        self.viewdate.set_editable(False)
        self.viewdate.set_wrap_mode(gtk.WRAP_WORD)
        self.viewdate.set_justification(gtk.JUSTIFY_CENTER)
        self.viewdate.set_pixels_below_lines(0)
        self.viewdate.set_pixels_above_lines(40)

        self.nextbutton_bar = gtk.HButtonBox()

        
        # Next Card Button
        self._nextbutton = ToolButton(tooltip=_('Next Card'))
        self._nextbutton.connect('clicked', self._next_card_cb)
        self._nextbutton.set_icon_widget(
                self.make_label('go-next', ' ' + _('Next Card')))

        self.viewfront_box.pack_start(self.front_label)
        self.viewfront_box.pack_start(self.viewfront)
        
        self.viewback_box.pack_start(self.back_label)
        self.viewback_box.pack_start(self.viewback)

        self.viewcatagory_box.pack_start(self.catagory_label)
        self.viewcatagory_box.pack_start(self.viewcatagory)

        self.viewstage_box.pack_start(self.stage_label)
        self.viewstage_box.pack_start(self.viewstage)

        self.viewdate_box.pack_start(self.date_label)
        self.viewdate_box.pack_start(self.viewdate)
                                              
        
        self.viewcard_box.pack_start(self.viewfront_box)
        self.viewcard_box.pack_start(self.viewback_box)
        self.viewcard_box.pack_start(self.viewcatagory_box)
        self.viewcard_box.pack_start(self.viewstage_box)
        self.viewcard_box.pack_start(self.viewdate_box)

        self.nextbutton_bar.pack_start(self._nextbutton)

        self.add(self.viewcard_box)
        self.add(self.nextbutton_bar)

        self.frontbuffer.set_text(next_card_tuple[0])
        self.backbuffer.set_text(next_card_tuple[1])
        self.catagorybuffer.set_text(next_card_tuple[2])
        self.stagebuffer.set_text(next_card_tuple[3])
        self.datebuffer.set_text(next_card_tuple[4])

        self.front_label.show()
        self.back_label.show()
        self.catagory_label.show()
        self.stage_label.show()
        self.date_label.show()

        self.viewfront.show()
        self.viewback.show()
        self.viewcatagory.show()
        self.viewstage.show()
        self.viewdate.show()

        self.viewfront_box.show()
        self.viewback_box.show()
        self.viewcatagory_box.show()
        self.viewstage_box.show()
        self.viewdate_box.show()

        self.viewcard_box.show()
        self._nextbutton.show()
        self.nextbutton_bar.show()

    # Returns the info from the next card
    def next_card(self):
        card = self.deck[self.current_card_index]
        self.front = card.find("front").text
        self.back = card.find("back").text
        last = self.convert_date(card.find("last_reviewed").text)
        stage = card.find("stage").text
        self.catagory = card.find("catagory").text

        if (self.current_card_index+1) < len(self.deck):
            self.current_card_index += 1
        else:
            self.current_card_index = 0

        return (self.front, self.back, self.catagory, stage, last)

    # Converts the date from timestamp format to dd/mm/yyyy
    def convert_date(self, timestamp_string):
        timestamp = float(timestamp_string)
        time_structure = time.gmtime(timestamp)
        return (str(time_structure.tm_mday) + "/" + str(time_structure.tm_mon) 
                + "/" + str(time_structure.tm_year))

    # Next Card button callback, gets next card and sets the text buffers
    def _next_card_cb(self, button):
        next_card_tuple = self.next_card()
        self.frontbuffer.set_text(next_card_tuple[0])
        self.backbuffer.set_text(next_card_tuple[1])
        self.catagorybuffer.set_text(next_card_tuple[2])
        self.stagebuffer.set_text(next_card_tuple[3])
        self.datebuffer.set_text(next_card_tuple[4])

    # Returns card text
    def get_card_text(self):
        return [self.front, self.back, self.catagory]

    # Makes the icons for button and makes the label
    # Written by Simon Schampijer
    def make_label(self, icon_name, label):
        label_box = gtk.HBox()
        icon = Icon(
                icon_name=icon_name,
                icon_size=gtk.ICON_SIZE_LARGE_TOOLBAR)
        label_box.pack_start(icon, False)
        label = gtk.Label(label)
        label.modify_fg(gtk.STATE_NORMAL,
                style.COLOR_TOOLBAR_GREY.get_gdk_color())
        label_box.pack_start(label)
        label_box.show_all()
        return label_box