Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Widgets.py
blob: 286be2a87055c5b944fd6a5183c2594be9982aa5 (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#   Widgets.py por:
#   Flavio Danesse <fdanesse@gmail.com>
#   CeibalJAM! - Uruguay

# 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 2 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

#import os
from gettext import gettext as _

from gi.repository import Gtk

class NewPollCanvas(Gtk.Box):

    def __init__(self, poll, editing=False, highlight=[]):

        # FIXME: El parámetro highlight nunca se utilizó, la idea era
        # resaltar el texto en las etiquetas para aquellas opciones no
        # validadas de una encuesta.
        Gtk.Box.__init__(self, orientation = Gtk.Orientation.VERTICAL)

        """
        Show the canvas to set up a new poll.

        editing is False to start a new poll, or
        True to edit the current poll

        highlight is a list of strings denoting items failing validation.
        """

        #self._current_view = 'build'

        label = Gtk.Label()
        label.set_markup('<big><b>%s</b></big>' % _('Build a Poll'))
        self.pack_start(label, False, False, 10)

        hbox = Gtk.HBox()
        hbox.pack_start(Gtk.Label(_('Poll Title:')), False, False, 10)
        entrybox = Gtk.Entry()
        entrybox.set_text(poll.title)
        #entrybox.connect('changed', self._entry_activate_cb, 'title')
        hbox.pack_start(entrybox, True, True, 10)
        self.pack_start(hbox, False, False, 10)

        hbox = Gtk.HBox()
        hbox.pack_start(Gtk.Label(_('Question:')), False, False, 10)
        entrybox = Gtk.Entry()
        entrybox.set_text(poll.question)
        #entrybox.connect('changed', self._entry_activate_cb, 'question')
        hbox.pack_start(entrybox, True, True, 10)
        self.pack_start(hbox, False, False, 10)

        hbox = Gtk.HBox()
        hbox.pack_start(Gtk.Label(_('Number of votes to collect:')),
            False, False, 10)
        entrybox = Gtk.Entry()
        entrybox.set_text(str(poll.maxvoters))
        #entrybox.connect('changed', self._entry_activate_cb, 'maxvoters')
        hbox.pack_start(entrybox, True, True, 10)
        self.pack_start(hbox, False, False, 10)

        for choice in poll.options.keys():
            hbox = Gtk.HBox()
            hbox.pack_start(Gtk.Label(_('Answer %d:') % (choice + 1)),
                False, False, 10)
            entrybox = Gtk.Entry()
            entrybox.set_text(poll.options[choice])
            #entrybox.connect('changed', self._entry_activate_cb, str(choice))
            hbox.pack_start(entrybox, True, True, 10)
            '''
            if self._use_image:
                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)

                else:
                    button = Gtk.Button(_("Add Image"))
                    hbox.pack_start(button, True, False, 10)

                button.connect('clicked', self._button_choose_image_cb,
                    str(choice), hbox)'''

            self.pack_start(hbox, False, False, 10)

        # PREVIEW & SAVE buttons
        hbox = Gtk.HBox()
        button = Gtk.Button(_("Step 1: Preview"))
        #button.connect('clicked', self._button_preview_cb)
        hbox.pack_start(button, True, True, 10)
        button = Gtk.Button(_("Step 2: Save"))
        button.connect('clicked', self.__button_save_cb)
        hbox.pack_start(button, True, True, 10)

        self.pack_start(hbox, False, False, 10)

        self.show_all()

    def __button_save_cb(self, button, data=None):
        """
        Save button clicked.
        """

        ### Validate data
        failed_items = self.__validate()

        if failed_items:
            #self.set_root(self._build_canvas(highlight=failed_items))
            #self.show_all()
            return

        ''' Vienen de poll.py
        # Data OK
        self._previewing = False
        self._poll.active = True
        self._polls.add(self._poll)
        self._poll.broadcast_on_mesh()
        self.set_root(self._poll_canvas())
        self.show_all()'''

    '''
    def __button_preview_cb(self, button, data=None):
        """
        Preview button clicked.
        """

        # Validate data
        failed_items = self._validate()

        if failed_items:
            self.set_root(self._build_canvas(highlight=failed_items))
            self.show_all()
            return

        # Data OK
        self._poll.active = True  # Show radio buttons
        self._previewing = True
        self.set_root(self._poll_canvas())

        self.show_all()'''

    def __validate(self):

        failed_items = []

        if self._poll.title == '':
            failed_items.append('title')

        if self._poll.question == '':
            failed_items.append('question')

        if self._poll.maxvoters == 0:
            failed_items.append('maxvoters')

        if self._poll.options[0] == '':
            failed_items.append('0')

        if self._poll.options[1] == '':
            failed_items.append('1')

        if self._poll.options[3] != '' and self._poll.options[2] == '':
            failed_items.append('2')

        if self._poll.options[4] != '' and self._poll.options[3] == '':
            failed_items.append('3')

        if self._poll.options[2] == '':
            self._poll.number_of_options = 2

        elif self._poll.options[3] == '':
            self._poll.number_of_options = 3

        elif self._poll.options[4] == '':
            self._poll.number_of_options = 4

        else:
            self._poll.number_of_options = 5

        return failed_items