Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorflavio <fdanesse@gmail.com>2013-08-03 17:55:01 (GMT)
committer Gonzalo Odiard <godiard@gmail.com>2013-09-02 17:50:01 (GMT)
commite65cc133a5347a10209d0e9e6d5f96d3dc2a6886 (patch)
treea77a2b70482cf0e01b1a9588f7047e2183728858
parentf7f7e718642855220e4c96251c285655288615b8 (diff)
Generalización parcial de PollCanvas, falta generalizar la función draw_poll_details_box
-rw-r--r--Widgets.py181
-rw-r--r--poll.py156
2 files changed, 210 insertions, 127 deletions
diff --git a/Widgets.py b/Widgets.py
index 545085c..9033b6b 100644
--- a/Widgets.py
+++ b/Widgets.py
@@ -27,7 +27,6 @@ from gi.repository import GdkPixbuf
from sugar3 import mime
from sugar3 import profile
from sugar3.graphics import style
-from sugar3.graphics.alert import NotifyAlert
from sugar3.graphics.objectchooser import ObjectChooser
from sugar3.graphics.toolbarbox import ToolbarBox
@@ -118,7 +117,7 @@ class NewPollCanvas(Gtk.Box):
self.pack_start(item_poll, False, False, 10)
if self._poll.activity._use_image:
- if self._poll.activity._already_loaded_image_in_answer(choice):
+ if self.__already_loaded_image_in_answer(choice):
button = Gtk.Button(_("Change Image"))
hbox.pack_start(button, True, False, 10)
self.__show_image_thumbnail(hbox, choice)
@@ -145,6 +144,14 @@ class NewPollCanvas(Gtk.Box):
self.show_all()
+ def __already_loaded_image_in_answer(self, answer_number):
+
+ if not self._poll.images_ds_objects[int(answer_number)] == {}:
+ return True
+
+ else:
+ return False
+
def _button_choose_image_cb(self, button, data=None, data2=None):
if hasattr(mime, 'GENERIC_TYPE_IMAGE'):
@@ -189,12 +196,8 @@ class NewPollCanvas(Gtk.Box):
button.set_label(_('Change Image'))
else:
- alert = NotifyAlert(timeout=3)
- alert.props.title = _('Poll Activity')
- alert.props.msg = _('Your selection is not an image')
- self.add_alert(alert)
- alert.connect('response', self._alert_cancel_cb)
- alert.show()
+ self._poll.activity.__get_alert(_('Poll Activity'),
+ _('Your selection is not an image'))
finally:
chooser.destroy()
@@ -358,7 +361,6 @@ class OptionsCanvas(Gtk.Box):
self.poll_activity._current_view = 'options'
alignment = Gtk.Alignment.new(0.5, 0, 0, 0)
- # optionsbox is centered within self
optionsbox = Gtk.VBox()
alignment.add(optionsbox)
@@ -460,12 +462,8 @@ class OptionsCanvas(Gtk.Box):
def __button_save_options_cb(self, button):
- alert = NotifyAlert(timeout=3)
- alert.props.title = _('Poll Activity')
- alert.props.msg = _('The settings have been saved')
- self.poll_activity.add_alert(alert)
- alert.connect('response', self.poll_activity._alert_cancel_cb)
- alert.show()
+ self.poll_activity.__get_alert(_('Poll Activity'),
+ _('The settings have been saved'))
class SelectCanvas(Gtk.Box):
@@ -530,3 +528,156 @@ class SelectCanvas(Gtk.Box):
poll.createdate.strftime('%d/%m/%y')), False, False, 10)
self.show_all()
+'''
+class PollCanvas(Gtk.Box):
+
+ def __init__(self, poll_activity):
+
+ Gtk.Box.__init__(self, orientation = Gtk.Orientation.VERTICAL)
+
+ poll_activity._current_view = 'poll'
+
+ pollbuilderbox = Gtk.VBox()
+
+ alignment = Gtk.Alignment.new(0.5, 0, 1, 0)
+ alignment.add(pollbuilderbox)
+ self.pack_start(alignment, True, True, 0)
+
+ mainbox = Gtk.VBox()
+ pollbuilderbox.pack_start(mainbox, True, True, 0)
+
+ if not self._previewing:
+ mainbox.pack_start(Gtk.Label(_('VOTE!')), True, True, 0)
+
+ 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()
+
+ self.show_all()
+
+ 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)
+ 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)
+
+ 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, True, False, 10)
+
+ if choice == self.current_vote:
+ button.set_active(True)
+
+ 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)
+
+ if not self._poll.active:
+ answer_row.pack_start(Gtk.Label(self._poll.options[choice]),
+ True, 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))
+
+ graph_box = Gtk.HBox()
+ answer_row.pack_start(graph_box, True, True, 10)
+
+ graph_box.pack_start(Gtk.Label(
+ justify(self._poll.data, choice)), True, True, 10)
+
+ 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)
+
+ answer_box.pack_start(answer_row, True, True, 0)
+
+ 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)
+
+ # 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)'''
diff --git a/poll.py b/poll.py
index dee2a28..4df9e54 100644
--- a/poll.py
+++ b/poll.py
@@ -1,3 +1,6 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
# Copyright 2007 World Wide Workshop Foundation
# Copyright 2007 Collabora Ltd
# Copyright 2008 Morgan Collett
@@ -72,10 +75,13 @@ GRAPH_WIDTH = Gdk.Screen.width() / 3
GRAPH_TEXT_WIDTH = 50
RADIO_SIZE = 32'''
-from Widgets import NewPollCanvas
from Widgets import Toolbar
-from Widgets import OptionsCanvas
-from Widgets import SelectCanvas
+
+### Interfaces
+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.
class PollBuilder(activity.Activity):
"""
@@ -147,7 +153,6 @@ class PollBuilder(activity.Activity):
# toolbar.help_button.connect('clicked', self._button_lessonplan_cb)
self.set_toolbar_box(toolbar)
- #self.set_canvas(self._select_canvas())
self.set_canvas(SelectCanvas(self))
self.show_all()
@@ -155,83 +160,11 @@ class PollBuilder(activity.Activity):
self.poll_session = None # PollSession
#self.connect('shared', self._shared_cb)
#self.connect('joined', self._joined_cb)
- '''
- def _select_canvas(self):
- """
- Show the select canvas where children choose an existing poll.
- """
-
- self._current_view = 'select'
-
- mainbox = Gtk.VBox()
-
- label = Gtk.Label()
- label.set_markup('<big><b>%s</b></big>' % _('Choose a Poll'))
- mainbox.pack_start(label, False, False, 0)
-
- poll_selector_box = Gtk.VBox()
-
- scroll = Gtk.ScrolledWindow()
-
- scroll.set_policy(
- Gtk.PolicyType.AUTOMATIC,
- Gtk.PolicyType.NEVER)
-
- scroll.add_with_viewport(poll_selector_box)
-
- mainbox.pack_start(scroll, True, True, 10)
-
- row_number = 0
-
- for poll in self._polls:
- sha = poll.sha
-
- if row_number % 2:
- row_bgcolor = style.COLOR_WHITE.get_int()
-
- else:
- row_bgcolor = style.COLOR_SELECTION_GREY.get_int()
-
- row_number += 1
-
- poll_row = Gtk.HBox()
- poll_selector_box.pack_start(poll_row, False, False, 10)
-
- title = Gtk.Label(label=poll.title + ' (' + poll.author + ')')
- align = Gtk.Alignment.new(0, 0.5, 0, 0)
- align.add(title)
- poll_row.pack_start(align, True, True, 10)
-
- if poll.active:
- button = Gtk.Button(_('VOTE'))
-
- else:
- button = Gtk.Button(_('SEE RESULTS'))
-
- button.connect('clicked', self._select_poll_button_cb, sha)
- poll_row.pack_start(button, False, False, 10)
-
- if poll.author == profile.get_nick_name():
- button = Gtk.Button(_('DELETE'))
- button.connect('clicked', self.__delete_poll_button_cb, sha)
- poll_row.pack_start(button, False, False, 10)
-
- poll_row.pack_start(Gtk.Label(
- poll.createdate.strftime('%d/%m/%y')), False, False, 10)
-
- mainbox.show_all()
-
- return mainbox'''
-
- '''
- def set_root(self, widget):
-
- if self._root.get_children():
- self._root.remove(self._root.get_children()[0])
-
- self._root.pack_start(widget, True, True, 0)'''
def _create_pixbufs(self, images_ds_object_id):
+ """
+ Crea las imágenes de la encuesta, al leer desde el journal.
+ """
pixbufs = {}
@@ -249,6 +182,9 @@ class PollBuilder(activity.Activity):
return pixbufs
def _get_images_ds_objects(self, images_ds_object_id):
+ """
+ Obtiene las imagenes almacenadas en el jornal.
+ """
images_ds_objects = {}
@@ -310,7 +246,6 @@ class PollBuilder(activity.Activity):
f.close()
- #self.set_canvas(self._select_canvas())
self.set_canvas(SelectCanvas(self))
def write_file(self, file_path):
@@ -338,20 +273,20 @@ class PollBuilder(activity.Activity):
f = open(file_path, 'w')
f.write(s)
f.close()
- '''
- def alert(self, title, text=None):
+
+ def __get_alert(self, title, text):
"""
Show an alert above the activity.
"""
- alert = NotifyAlert(timeout=10)
+ alert = NotifyAlert(timeout=5)
alert.props.title = title
alert.props.msg = text
self.add_alert(alert)
- alert.connect('response', self._alert_cancel_cb)
- alert.show()'''
+ alert.connect('response', self.__alert_cancel_cb)
+ alert.show()
- def _alert_cancel_cb(self, alert, response_id):
+ def __alert_cancel_cb(self, alert, response_id):
"""
Callback for alert events
"""
@@ -450,6 +385,7 @@ class PollBuilder(activity.Activity):
self._switch_to_poll(sha)
self._has_voted = False
self.set_canvas(self._poll_canvas())
+ #self.set_canvas(PollCanvas(self)) # FIXME: Generalizacion de PollCanvas
def _delete_poll_button_cb(self, button, sha):
"""
@@ -469,7 +405,6 @@ class PollBuilder(activity.Activity):
if poll.sha == sha:
self._polls.remove(poll)
- #self.set_canvas(self._select_canvas())
self.set_canvas(SelectCanvas(self))
'''
@@ -652,19 +587,14 @@ class PollBuilder(activity.Activity):
self.draw_poll_details_box()
else:
- alert = NotifyAlert(timeout=3)
- alert.props.title = _('Poll Activity')
- alert.props.msg = _('To vote you have to select first one option')
- self.add_alert(alert)
- alert.connect('response', self._alert_cancel_cb)
- alert.show()
+ self.__get_alert(_('Poll Activity'),
+ _('To vote you have to select first one option'))
def __button_select_clicked(self, button):
"""
Show Choose a Poll canvas
"""
- #self.set_canvas(self._select_canvas())
self.set_canvas(SelectCanvas(self))
def __button_new_clicked(self, button):
@@ -683,9 +613,6 @@ class PollBuilder(activity.Activity):
self.set_canvas(NewPollCanvas(self._poll))
def button_edit_clicked(self, button):
- """
- Go back from preview to edit
- """
self.set_canvas(NewPollCanvas(self._poll))
@@ -693,14 +620,6 @@ class PollBuilder(activity.Activity):
self.set_canvas(OptionsCanvas(self))
- def _already_loaded_image_in_answer(self, answer_number):
-
- if not self._poll.images_ds_objects[int(answer_number)] == {}:
- return True
-
- else:
- return False
-
'''
def _make_default_poll(self):
"""
@@ -742,6 +661,7 @@ class PollBuilder(activity.Activity):
for poll in self._polls:
if poll.sha == sha:
self._poll = poll
+ break
'''
def get_my_polls(self):
"""
@@ -765,8 +685,8 @@ class PollBuilder(activity.Activity):
if poll.author == author and poll.title == title:
try:
poll.register_vote(choice, votersha)
- self.alert(_('Vote'),
- _('Somebody voted on %s') % title)
+ #self.alert(_('Vote'), _('Somebody voted on %s') % title))
+ self.__get_alert(_('Vote'), _('Somebody voted on %s') % title))
except OverflowError:
self._logger.debug('Ignored mesh vote %u from %s:'
@@ -826,9 +746,9 @@ class PollBuilder(activity.Activity):
if lesson_return == 'poll':
self.set_root(self._poll_canvas())
+ #self.set_canvas(PollCanvas(self)) # FIXME: Generalizacion de PollCanvas
elif lesson_return == 'select':
- #self.set_root(self._select_canvas())
self.set_canvas(SelectCanvas(self))
elif lesson_return == 'build':
@@ -892,7 +812,8 @@ class PollBuilder(activity.Activity):
return
self._logger.debug('Joined an existing shared activity')
- self.alert(_('Joined'))
+ #self.alert(_('Joined'))
+ self.__get_alert(_('Joined'), "")
self.initiating = False
self._sharing_setup()
@@ -926,12 +847,14 @@ class PollBuilder(activity.Activity):
'''
def _buddy_joined_cb(self, activity, buddy):
- self.alert(buddy.props.nick, _('Joined'))
+ #self.alert(buddy.props.nick, _('Joined'))
+ self.__get_alert(buddy.props.nick, _('Joined'))
self._logger.debug('Buddy %s joined' % buddy.props.nick)
def _buddy_left_cb(self, activity, buddy):
- self.alert(buddy.props.nick, _('Left'))
+ #self.alert(buddy.props.nick, _('Left'))
+ self.__get_alert(buddy.props.nick, _('Left'))
self._logger.debug('Buddy %s left' % buddy.props.nick)'''
'''
def _get_buddy(self, cs_handle):
@@ -1423,11 +1346,15 @@ class PollSession(ExportedGObject):
options, data, votes, images)
self.activity._polls.add(poll)
-
+ """
self.activity.alert(
_('New Poll'),
_("%(author)s shared a poll "
"'%(title)s' with you.") % {'author': author,
+ 'title': title})"""
+ self.__get_alert(('New Poll'),
+ _("%(author)s shared a poll "
+ "'%(title)s' with you.") % {'author': author,
'title': title})
def vote_cb(self, author, title, choice, votersha, sender=None):
@@ -1508,10 +1435,15 @@ class PollSession(ExportedGObject):
options, data, votes, images)
self.activity._polls.add(poll)
+ """
self.activity.alert(
_('New Poll'),
_("%(author)s shared a poll "
"'%(title)s' with you.") % {'author': author,
+ 'title': title})"""
+ self.activity.__get_alert(_('New Poll'),
+ _("%(author)s shared a poll "
+ "'%(title)s' with you.") % {'author': author,
'title': title})
@method(dbus_interface=IFACE, in_signature='s', out_signature='')