diff options
author | flavio <fdanesse@gmail.com> | 2013-08-08 16:07:35 (GMT) |
---|---|---|
committer | Gonzalo Odiard <godiard@gmail.com> | 2013-09-02 17:50:02 (GMT) |
commit | 92dce63295062c39deaa2841c879431e7821b798 (patch) | |
tree | 8179d9b7e73b9c07ff361f8a0090c92bd6e8182c | |
parent | c056d14ceb6cbb0deb23139adb416fc50942acbf (diff) |
Modularización de PollCanvas
-rw-r--r-- | Widgets.py | 307 | ||||
-rw-r--r-- | poll.py | 196 |
2 files changed, 178 insertions, 325 deletions
@@ -25,6 +25,7 @@ import locale from gettext import gettext as _ from gi.repository import Gtk +from gi.repository import Gdk from gi.repository import GdkPixbuf from gi.repository import Abi @@ -554,219 +555,233 @@ class SelectCanvas(Gtk.Box): poll.createdate.strftime('%d/%m/%y')), False, False, 10) self.show_all() -''' -class PollCanvas(Gtk.Box): + +class LessonPlanCanvas(Gtk.Box): def __init__(self, poll_activity): Gtk.Box.__init__(self, orientation = Gtk.Orientation.VERTICAL) - poll_activity._current_view = 'poll' + poll_activity._current_view = 'lessonplan' - pollbuilderbox = Gtk.VBox() + self.pack_start(Gtk.Label(_('Lesson Plans')), False, False, 0) + self.pack_start(LessonPlanWidget(), True, True, 0) - alignment = Gtk.Alignment.new(0.5, 0, 1, 0) - alignment.add(pollbuilderbox) - self.pack_start(alignment, True, True, 0) + self.show_all() - mainbox = Gtk.VBox() - pollbuilderbox.pack_start(mainbox, True, True, 0) +class LessonPlanWidget(Gtk.Notebook): + """ + Create a Notebook widget for displaying lesson plans in tabs. - if not self._previewing: - mainbox.pack_start(Gtk.Label(_('VOTE!')), True, True, 0) + basepath -- string, path of directory containing lesson plans. + """ - else: - mainbox.pack_start(Gtk.Label(_('Poll Preview')), - True, True, 0) + def __init__(self): - poll_details_box = Gtk.VBox() - mainbox.pack_start(poll_details_box, True, True, 0) + Gtk.Notebook.__init__(self) - self.poll_details_box_head = Gtk.VBox() - poll_details_box.pack_start(self.poll_details_box_head, False, - False, 0) + lessons = filter( + lambda x: os.path.isdir(os.path.join(basepath, + 'lessons', x)), + os.listdir(os.path.join(basepath, 'lessons'))) - self.poll_details_box = Gtk.VBox() + lessons.sort() - poll_details_scroll = Gtk.ScrolledWindow() + for lesson in lessons: + self.__load_lesson( + os.path.join(basepath, + 'lessons', lesson), + _(lesson)) - poll_details_scroll.set_policy( - Gtk.PolicyType.AUTOMATIC, - Gtk.PolicyType.NEVER) + self.show_all() - poll_details_scroll.add_with_viewport(self.poll_details_box) - poll_details_box.pack_start(poll_details_scroll, True, True, 0) + def __load_lesson(self, path, name): + """ + Load the lesson content from a .abw, taking l10n into account. - self.poll_details_box_tail = Gtk.HBox() - poll_details_box.pack_start(self.poll_details_box_tail, False, False, 0) + path -- string, path of lesson plan file, e.g. lessons/Introduction + lesson -- string, name of lesson + """ - self.current_vote = None - self.draw_poll_details_box() + code, encoding = locale.getdefaultlocale() + canvas = Abi.Widget() + canvas.show() - self.show_all() + files = map( + lambda x: os.path.join(path, '%s.abw' % x), + ('_' + code.lower(), '_' + code.split('_')[0].lower(), + 'default')) - def draw_poll_details_box(self): - """ - (Re)draw the poll details box + files = filter(lambda x: os.path.exists(x), files) + canvas.load_file('file://%s' % files[0], '') + canvas.view_online_layout() + canvas.zoom_width() + canvas.set_show_margin(False) + self.append_page(canvas, Gtk.Label(label=name)) - self.poll_details_box should be already defined on the canvas. - """ +class PollCanvas(Gtk.Box): - poll_details_box = self.poll_details_box + def __init__(self, cabecera, poll, current_vote, view_answer, previewing): - votes_total = self._poll.vote_count + Gtk.Box.__init__(self, orientation = Gtk.Orientation.VERTICAL) - title = Gtk.Label(label=self._poll.title) - self.poll_details_box_head.pack_start(title, True, True, 10) - question = Gtk.Label(label=self._poll.question) - self.poll_details_box_head.pack_start(question, True, True, 10) + self._poll = poll - answer_box = Gtk.VBox() - poll_details_box.pack_end(answer_box, True, True, 10) + self.cabecera = Gtk.Label(cabecera) + self.pack_start(self.cabecera, True, True, 10) - group = Gtk.RadioButton() + self.title = Gtk.Label(poll.title) + self.title.set_alignment(0.0, 0.5) + self.pack_start(self.title, True, True, 10) - for choice in range(self._poll.number_of_options): - #self._logger.debug(self._poll.options[choice]) + self.question = Gtk.Label(poll.question) + self.question.set_alignment(0.0, 0.5) + self.pack_start(self.question, True, True, 10) - answer_row = Gtk.HBox() + frame = Gtk.Frame() + tabla = Gtk.Table(rows=6, columns=6) - if self._poll.active: - button = Gtk.RadioButton.new_with_label_from_widget( - group, self._poll.options[choice]) + scroll = Gtk.ScrolledWindow() + + scroll.set_policy( + Gtk.PolicyType.AUTOMATIC, + Gtk.PolicyType.NEVER) - button.connect('toggled', self.vote_choice_radio_button, choice) + scroll.add_with_viewport(tabla) - answer_box.pack_start(button, True, False, 10) + frame.add(scroll) + self.pack_start(frame, True, True, 10) - if choice == self.current_vote: - button.set_active(True) + #### + group = Gtk.RadioButton() + + row = 0 + for choice in range(poll.number_of_options): + + if poll.active: + button = Gtk.RadioButton.new_with_label_from_widget( + group, poll.options[choice]) - if not self._poll.images[int(choice)] == '': - hbox = Gtk.HBox() - hbox.add(self._load_image(self._poll.images[choice])) - hbox.show() - answer_row.pack_start(hbox, True, True, 10) + button.connect('toggled', poll.activity.vote_choice_radio_button, choice) - if not self._poll.active: - answer_row.pack_start(Gtk.Label(self._poll.options[choice]), - True, False, 10) + tabla.attach(button, 0, 1, row, row+1) - if self._view_answer or not self._poll.active: - if votes_total > 0: - #self._logger.debug(str(self._poll.data[choice] * 1.0 / - # votes_total)) + if choice == current_vote: + button.set_active(True) - graph_box = Gtk.HBox() - answer_row.pack_start(graph_box, True, True, 10) + if not poll.images[int(choice)] == '': + image = Gtk.Image() + image.set_from_pixbuf(poll.images[choice]) + tabla.attach(image, 1,2, row, row+1) - graph_box.pack_start(Gtk.Label( - justify(self._poll.data, choice)), True, True, 10) + if view_answer or not poll.active: + if poll.vote_count > 0: - graph_box.pack_start(Gtk.HBox(), True, True, 10) - graph_box.pack_start(Gtk.Label(str(self._poll.data[ - choice] * 100 / votes_total) + '%'), True, True, 10) + ### Total de votos + label = Gtk.Label(poll.data[choice]) + label.set_size_request(100, -1) + tabla.attach(label, 3,4, row, row+1) - answer_box.pack_start(answer_row, True, True, 0) + eventbox = Gtk.EventBox() + eventbox.set_size_request(300, -1) + eventbox.modify_bg(0, Gdk.Color.parse('#FF0198')[1]) + tabla.attach(eventbox, 4,5, row, row+1) - if self._view_answer or not self._poll.active: - # Line above total - line_box = Gtk.HBox() - answer_box.pack_start(line_box, True, True, 10) + row += 1 - # total votes - totals_box = Gtk.HBox() - answer_box.pack_start(totals_box, True, True, 10) + ### Barra para total + eventbox = Gtk.EventBox() + eventbox.set_size_request(300, -1) + eventbox.modify_bg(0, Gdk.Color.parse('#FF0198')[1]) + tabla.attach(eventbox, 3,5, row, row+1) - spacer = Gtk.HBox() + row += 1 - spacer.pack_start(Gtk.Label(str(votes_total)), True, True, 10) - totals_box.pack_start(spacer, True, True, 10) + label = Gtk.Label("%s %s %s %s" % (str(poll.vote_count), + _('votes'), _('(votes left to collect)'), + poll.maxvoters - poll.vote_count) ) - totals_box.pack_start(Gtk.Label(' ' + _('votes')), True, True, 10) + tabla.attach(label, 3,5, row, row+1) - if votes_total < self._poll.maxvoters: - totals_box.pack_start( - Gtk.Label(_('(%d votes left to collect)') % - (self._poll.maxvoters - votes_total)), True, True, 10) + row += 1 # Button area - if self._poll.active and not self._previewing: - button_box = Gtk.HBox() + if poll.active and not previewing: button = Gtk.Button(_("Vote")) - button.connect('clicked', self._button_vote_cb) - button_box.pack_start(button, True, False, 10) - self.poll_details_box_tail.pack_start(button_box, True, True, 10) + button.connect('clicked', poll.activity.button_vote_cb) + tabla.attach(button, 0,1, row, row+1) - elif self._previewing: - button_box = Gtk.HBox() + elif previewing: button = Gtk.Button(_("Edit Poll")) - button.connect('clicked', self.button_edit_clicked) - button_box.pack_start(button, True, True, 0) + button.connect('clicked', poll.activity.button_edit_clicked) + tabla.attach(button, 0,1, row, row+1) + button = Gtk.Button(_("Save Poll")) - button.connect('clicked', self.get_canvas().button_save_cb) - button_box.pack_start(button, True, True, 0) - self.poll_details_box_tail.pack_start(button_box, True, True, 0)''' + button.connect('clicked', self._button_save_cb) + tabla.attach(button, 1,2, row, row+1) -class LessonPlanCanvas(Gtk.Box): + self.show_all() - def __init__(self, poll_activity): + def _button_save_cb(self, button): + """ + Save button clicked. + """ - Gtk.Box.__init__(self, orientation = Gtk.Orientation.VERTICAL) + ### Validate data + failed_items = self.__validate() - poll_activity._current_view = 'lessonplan' + if failed_items: + print "*** failed_items:", failed_items + # FIXME: El parámetro highlight nunca se utilizó, la idea era + # resaltar el texto en las etiquetas para aquellas opciones no + # validadas en la encuesta. (Modificar para que suceda al perder el foco el entry) + #self.set_root(self._build_canvas(highlight=failed_items)) + #self.show_all() + return - self.pack_start(Gtk.Label(_('Lesson Plans')), False, False, 0) - self.pack_start(LessonPlanWidget(), True, True, 0) + # Data OK + self._poll.activity._previewing = False + self._poll.active = True + self._poll.activity._polls.append(self._poll) + self._poll.broadcast_on_mesh() + self._poll.activity.set_canvas(self._poll.activity._poll_canvas()) + self._poll.activity.show_all() - self.show_all() + def __validate(self): -class LessonPlanWidget(Gtk.Notebook): - """ - Create a Notebook widget for displaying lesson plans in tabs. + failed_items = [] - basepath -- string, path of directory containing lesson plans. - """ + if self._poll.title == '': + failed_items.append('title') - def __init__(self): + if self._poll.question == '': + failed_items.append('question') - Gtk.Notebook.__init__(self) + if self._poll.maxvoters == 0: + failed_items.append('maxvoters') - lessons = filter( - lambda x: os.path.isdir(os.path.join(basepath, - 'lessons', x)), - os.listdir(os.path.join(basepath, 'lessons'))) + if self._poll.options[0] == '': + failed_items.append('0') - lessons.sort() + if self._poll.options[1] == '': + failed_items.append('1') - for lesson in lessons: - self.__load_lesson( - os.path.join(basepath, - 'lessons', lesson), - _(lesson)) + if self._poll.options[3] != '' and self._poll.options[2] == '': + failed_items.append('2') - self.show_all() + if self._poll.options[4] != '' and self._poll.options[3] == '': + failed_items.append('3') - def __load_lesson(self, path, name): - """ - Load the lesson content from a .abw, taking l10n into account. + if self._poll.options[2] == '': + self._poll.number_of_options = 2 - path -- string, path of lesson plan file, e.g. lessons/Introduction - lesson -- string, name of lesson - """ + elif self._poll.options[3] == '': + self._poll.number_of_options = 3 - code, encoding = locale.getdefaultlocale() - canvas = Abi.Widget() - canvas.show() + elif self._poll.options[4] == '': + self._poll.number_of_options = 4 - files = map( - lambda x: os.path.join(path, '%s.abw' % x), - ('_' + code.lower(), '_' + code.split('_')[0].lower(), - 'default')) + else: + self._poll.number_of_options = 5 - files = filter(lambda x: os.path.exists(x), files) - canvas.load_file('file://%s' % files[0], '') - canvas.view_online_layout() - canvas.zoom_width() - canvas.set_show_margin(False) - self.append_page(canvas, Gtk.Label(label=name)) + return failed_items @@ -58,7 +58,7 @@ from Widgets import Toolbar from Widgets import NewPollCanvas #Creando una nueva encuesta. from Widgets import OptionsCanvas #Configurando opciones de encuesta. from Widgets import SelectCanvas #Seleccionando una de las encuestas disponibles. -#from Widgets import PollCanvas #Contestando una encuesta. +from Widgets import PollCanvas #Contestando una encuesta. from Widgets import LessonPlanCanvas from PollSession import PollSession @@ -277,59 +277,16 @@ class PollBuilder(activity.Activity): Show the poll canvas where children vote on an existing poll. """ - scroll = Gtk.ScrolledWindow() - scroll.set_policy( - Gtk.PolicyType.AUTOMATIC, - Gtk.PolicyType.AUTOMATIC) - self._current_view = 'poll' - canvasbox = Gtk.VBox() - - pollbuilderbox = Gtk.VBox() - - alignment = Gtk.Alignment.new(0.5, 0, 1, 0) - alignment.add(pollbuilderbox) - canvasbox.pack_start(alignment, True, True, 0) - - mainbox = Gtk.VBox() - pollbuilderbox.pack_start(mainbox, True, True, 0) + self.current_vote = None if not self._previewing: - mainbox.pack_start(Gtk.Label(_('VOTE!')), True, True, 0) + cabecera = _('VOTE!') else: - mainbox.pack_start(Gtk.Label(_('Poll Preview')), - True, True, 0) - - poll_details_box = Gtk.VBox() - mainbox.pack_start(poll_details_box, True, True, 0) - - self.poll_details_box_head = Gtk.VBox() - poll_details_box.pack_start(self.poll_details_box_head, False, - False, 0) - - self.poll_details_box = Gtk.VBox() - - poll_details_scroll = Gtk.ScrolledWindow() - - poll_details_scroll.set_policy( - Gtk.PolicyType.AUTOMATIC, - Gtk.PolicyType.NEVER) - - poll_details_scroll.add_with_viewport(self.poll_details_box) - poll_details_box.pack_start(poll_details_scroll, True, True, 0) - - self.poll_details_box_tail = Gtk.HBox() - poll_details_box.pack_start(self.poll_details_box_tail, False, False, 0) - - self.current_vote = None - self.__draw_poll_details_box() - - scroll.add_with_viewport(canvasbox) + cabecera = _('Poll Preview') - scroll.show_all() - - return scroll + return PollCanvas(cabecera, self._poll, self.current_vote, self._view_answer, self._previewing) def _select_poll_button_cb(self, button, sha=None): """ @@ -341,7 +298,7 @@ class PollBuilder(activity.Activity): return self.__switch_to_poll(sha) - self.set_canvas(self._poll_canvas()) #self.set_canvas(PollCanvas(self)) # FIXME: Generalizacion de PollCanvas + self.set_canvas(self._poll_canvas()) def _delete_poll_button_cb(self, button, sha): """ @@ -363,133 +320,7 @@ class PollBuilder(activity.Activity): self.set_canvas(SelectCanvas(self)) - def __load_image(self, pixbuf): - """ - Load an image. - @param name -- string (image file path) - """ - - if not pixbuf == '': - image = Gtk.Image() - image.set_from_pixbuf(pixbuf) - image.show() - return image - - else: - #logging.exception("Image error") - return '' - - def __draw_poll_details_box(self): - """ - (Re)draw the poll details box - - self.poll_details_box should be already defined on the canvas. - """ - - poll_details_box = self.poll_details_box - - votes_total = self._poll.vote_count - - title = Gtk.Label(label=self._poll.title) - title.set_alignment(0.0, 0.5) - self.poll_details_box_head.pack_start(title, True, True, 10) - question = Gtk.Label(label=self._poll.question) - question.set_alignment(0.0, 0.5) - self.poll_details_box_head.pack_start(question, True, True, 10) - - answer_box = Gtk.VBox() - - poll_details_box.pack_end(answer_box, True, True, 10) - - group = Gtk.RadioButton() - - for choice in range(self._poll.number_of_options): - self._logger.debug(self._poll.options[choice]) - - answer_row = Gtk.HBox() - - if self._poll.active: - button = Gtk.RadioButton.new_with_label_from_widget( - group, self._poll.options[choice]) - - button.connect('toggled', self.__vote_choice_radio_button, choice) - - answer_box.pack_start(button, False, False, 10) - - if choice == self.current_vote: - button.set_active(True) - - if not self._poll.images[int(choice)] == '': - answer_row.pack_start(self.__load_image(self._poll.images[choice]), - False, False, 10) - - if not self._poll.active: - label = Gtk.Label(self._poll.options[choice]) - label.set_size_request(100, -1) - answer_box.pack_start(label, False, False, 10) - - if self._view_answer or not self._poll.active: - if votes_total > 0: - self._logger.debug(str(self._poll.data[choice] * 1.0 / - votes_total)) - - label = Gtk.Label(self._poll.data[choice]) - label.set_size_request(100, -1) - answer_row.pack_start(label, False, False, 10) - - eventbox = Gtk.EventBox() - eventbox.set_size_request(300, -1) - eventbox.modify_bg(0, Gdk.Color.parse('#FF0198')[1]) - answer_row.pack_end(eventbox, False, False, 10) - - label = Gtk.Label("%s %s" % (str(self._poll.data[choice] * 100 / votes_total), "%") ) - label.set_size_request(100, -1) - answer_row.pack_end(label, False, False, 10) - - answer_box.pack_start(answer_row, True, True, 0) - - if self._view_answer or not self._poll.active: - # Line above total - eventbox = Gtk.EventBox() - eventbox.set_size_request(300, 15) - eventbox.modify_bg(0, Gdk.Color.parse('#FF0198')[1]) - answer_box.pack_start(eventbox, True, True, 10) - - # total votes - totals_box = Gtk.HBox() - answer_box.pack_start(totals_box, True, True, 10) - - spacer = Gtk.HBox() - - spacer.pack_start(Gtk.Label(str(votes_total)), True, True, 10) - totals_box.pack_start(spacer, True, True, 10) - - totals_box.pack_start(Gtk.Label(' ' + _('votes')), True, True, 10) - - if votes_total < self._poll.maxvoters: - totals_box.pack_start( - Gtk.Label(_('(%d votes left to collect)') % - (self._poll.maxvoters - votes_total)), True, True, 10) - - # Button area - if self._poll.active and not self._previewing: - button_box = Gtk.HBox() - button = Gtk.Button(_("Vote")) - button.connect('clicked', self.__button_vote_cb) - button_box.pack_start(button, True, False, 10) - self.poll_details_box_tail.pack_start(button_box, True, True, 10) - - elif self._previewing: - button_box = Gtk.HBox() - button = Gtk.Button(_("Edit Poll")) - button.connect('clicked', self.__button_edit_clicked) - button_box.pack_start(button, True, True, 0) - button = Gtk.Button(_("Save Poll")) - button.connect('clicked', self.get_canvas()._button_save_cb) - button_box.pack_start(button, True, True, 0) - self.poll_details_box_tail.pack_start(button_box, True, True, 0) - - def __vote_choice_radio_button(self, widget, data): + def vote_choice_radio_button(self, widget, data): """ Track which radio button has been selected @@ -508,7 +339,7 @@ class PollBuilder(activity.Activity): except (OSError, ValueError), e: logging.exception(e) - def __button_vote_cb(self, button): + def button_vote_cb(self, button): """ Register a vote @@ -541,7 +372,14 @@ class PollBuilder(activity.Activity): if not self._remember_last_vote: self.current_vote = None - self.__draw_poll_details_box() + if not self._previewing: + cabecera = _('VOTE!') + + else: + cabecera = _('Poll Preview') + + return + self.set_canvas(PollCanvas(cabecera, self._poll, self.current_vote, self._view_answer, self._previewing)) else: self.__get_alert(_('Poll Activity'), @@ -569,7 +407,7 @@ class PollBuilder(activity.Activity): self.set_canvas(NewPollCanvas(self._poll)) - def __button_edit_clicked(self, button): + def button_edit_clicked(self, button): self.set_canvas(NewPollCanvas(self._poll)) |