~bladernr/checkbox/1095713-set-pipefail-on-sleep-jobs

« back to all changes in this revision

Viewing changes to hwtest/gui.py

  • Committer: Marc Tardif
  • Date: 2007-09-29 20:37:52 UTC
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: marc.tardif@canonical.com-20070929203752-5aarzgo5krdbuakk
Initial commit to refactor gui interface.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import os
2
 
import gtk
3
 
import gtk.glade
4
 
import gnomecanvas
5
 
 
6
 
from hwtest.constants import SHARE_DIR
7
 
from hwtest.excluder import Excluder
8
 
from hwtest.result import (ALL_STATUS)
9
 
from hwtest.test import (ALL_CATEGORIES)
10
 
from hwtest.ui import Ui
11
 
 
12
 
GUI_DIR = os.path.join(SHARE_DIR, 'gui')
13
 
 
14
 
class Gui(Ui):
15
 
 
16
 
    def __init__(self, application):
17
 
        super(Gui, self).__init__(application)
18
 
        self.tests = []
19
 
        self.start_color = 3638089728
20
 
        self.end_color = 4294967040
21
 
        self.wTree = gtk.glade.XML(os.path.join(GUI_DIR, 'hwtest.glade'),
22
 
            'window', 'hwtest')
23
 
 
24
 
        # Canvas
25
 
        canvas = self.wTree.get_widget("canvas")
26
 
        self.root = canvas.root().add(gnomecanvas.CanvasGroup)
27
 
 
28
 
        # Canvas pic
29
 
        canvas_bg = gtk.gdk.pixbuf_new_from_file(os.path.join(GUI_DIR,
30
 
            'canvas_bg.png'))
31
 
        self.root.add(gnomecanvas.CanvasPixbuf, pixbuf=canvas_bg, x=0,
32
 
            y=0, anchor="nw")
33
 
 
34
 
        # Header Image
35
 
        header = self.wTree.get_widget('header_image')
36
 
        header.set_from_file(os.path.join(GUI_DIR, 'hwtest_head.png'))
37
 
 
38
 
 
39
 
        # Window
40
 
        window = self.wTree.get_widget("window")
41
 
        window.set_resizable(False)
42
 
        window.set_size_request(450, 410)
43
 
        window.set_icon_from_file(os.path.join(GUI_DIR, 'ubuntu_logo.png'))
44
 
        window.connect("destroy", lambda w: gtk.main_quit())
45
 
 
46
 
        # Vertical and horizontal boxes
47
 
        self.vbox1 = gtk.VBox(False, 0)
48
 
        self.hbox1 = gtk.HBox(False, 0)
49
 
 
50
 
    def show_intro(self):
51
 
        # Fade intro
52
 
        text = "<big><b>%s</b></big>\n%s" % (
53
 
            self.application.title, self.application.intro)
54
 
        self.intro = self.root.add(gnomecanvas.CanvasText,
55
 
            markup=text, size=9000, fill_color_rgba=self.start_color,
56
 
            x=220, y=80)
57
 
        self._fade_in(self.intro)
58
 
 
59
 
        # Vertical and horizontal boxes
60
 
        self.vbox2 = gtk.VBox(False, 0)
61
 
        self.hbox2 = gtk.HBox(False, 0)
62
 
        for category in ALL_CATEGORIES:
63
 
            button_name = '%s_button' % category
64
 
            type_button = self.wTree.get_widget(button_name)
65
 
            type_button.connect("clicked", lambda w,
66
 
                category=category: self.do_intro(category))
67
 
            type_button.show()
68
 
            self.vbox2.pack_start(type_button, False, False, 2)
69
 
 
70
 
        self.hbox2.pack_start(self.vbox2, False, False, 0)
71
 
        self.hbox2.show_all()
72
 
        self.category = self.root.add(gnomecanvas.CanvasWidget, x=180, y=160,
73
 
            widget=self.hbox2, width=140, height=80)
74
 
 
75
 
    def hide_intro(self):
76
 
        self.intro.destroy()
77
 
        self.category.destroy()
78
 
 
79
 
    def do_intro(self, category):
80
 
        def exclude_func(test, category=category):
81
 
            return category not in test.categories
82
 
 
83
 
        # Cleanup intro
84
 
        self.hide_intro()
85
 
        self.show_test_common()
86
 
 
87
 
        # Setup tests
88
 
        manager_tests = self.application.test_manager.get_iterator()
89
 
        self.tests = Excluder(manager_tests, exclude_func, exclude_func)
90
 
        self.test = self.tests.next()
91
 
        self.show_test()
92
 
 
93
 
    def show_test_common(self):
94
 
        # Direction buttons
95
 
        self.direction_buttons = {}
96
 
        for direction in ['previous', 'next']:
97
 
            button = self.wTree.get_widget("%s_button" % direction)
98
 
            func = getattr(self, "do_%s" % direction)
99
 
            button.connect("clicked", lambda w, func=func: func())
100
 
            button.show()
101
 
            self.direction_buttons[direction] = button
102
 
 
103
 
    def hide_test_common(self):
104
 
        for direction_button in self.direction_buttons.values():
105
 
            direction_button.destroy()
106
 
 
107
 
    def show_test(self):
108
 
        result = self.test.result
109
 
 
110
 
        # Content
111
 
        gtk.rc_parse_string('gtk_font_name = "Sans 8"')
112
 
        self.content_txt = self.root.add(gnomecanvas.CanvasRichText, x=30,
113
 
            y=10, width=300, height=180, text=self.test.description, cursor_visible=False)
114
 
        self.content_line = self.root.add(gnomecanvas.CanvasLine,
115
 
            points=[30, 120, 430, 120], width_pixels=1,
116
 
            fill_color_rgba=288581136)
117
 
        gtk.rc_reset_styles(gtk.settings_get_default())
118
 
 
119
 
        # Radio buttons
120
 
        previous = None
121
 
        self.radio_buttons = {}
122
 
        for status in ALL_STATUS:
123
 
            button = gtk.RadioButton(previous, status.capitalize())
124
 
            previous = button
125
 
            self.radio_buttons[status] = button
126
 
            if result and result.status is status:
127
 
                button.set_active(True)
128
 
 
129
 
        radio_vbox = gtk.VBox(False, 0)
130
 
        radio_hbox = gtk.HBox(False, 0)
131
 
        for status in ALL_STATUS:
132
 
            radio_vbox.pack_start(self.radio_buttons[status], False, False, 2)
133
 
        radio_hbox.pack_start(radio_vbox, False, False, 0)
134
 
        radio_hbox.show_all()
135
 
        self.radio_win = self.root.add(gnomecanvas.CanvasWidget,
136
 
            x=40, y=160, widget=radio_hbox, width=140, height=100)
137
 
 
138
 
        # Direction buttons
139
 
        self.direction_buttons["previous"].set_sensitive(self.tests.has_prev())
140
 
 
141
 
        # Comment
142
 
        self.comment_txt = self.root.add(gnomecanvas.CanvasText,
143
 
            markup="<span size='10000'><b>%s</b></span>" % "Comments:",
144
 
            fill_color_rgba=288581375, x=225, y=135)
145
 
 
146
 
        comment_bg = gtk.gdk.pixbuf_new_from_file(os.path.join(GUI_DIR, 'comment_bg.png'))
147
 
        self.comment_bg = self.root.add(gnomecanvas.CanvasPixbuf, pixbuf=comment_bg,
148
 
            x=175, y=140, anchor="nw")
149
 
 
150
 
        comment_win = gtk.ScrolledWindow(None, None)
151
 
        comment_win.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
152
 
        self.comment_buf = gtk.TextView()
153
 
        self.comment_buf.set_editable(True)
154
 
        self.comment_buf.set_cursor_visible(True)
155
 
        self.comment_buf.set_wrap_mode(gtk.WRAP_WORD)
156
 
        if result and result.data:
157
 
            buffer = gtk.TextBuffer()
158
 
            buffer.set_text(result.data)
159
 
            self.comment_buf.set_buffer(buffer)
160
 
        comment_win.add(self.comment_buf)
161
 
        comment_win.show_all()
162
 
        self.comment_win = self.root.add(gnomecanvas.CanvasWidget,
163
 
            x=185, y=150, widget=comment_win, width=220, height=120)
164
 
 
165
 
    def hide_test(self):
166
 
        self.content_txt.destroy()
167
 
        self.content_line.destroy()
168
 
        for radio_button in self.radio_buttons.values():
169
 
            radio_button.destroy()
170
 
        self.radio_win.destroy()
171
 
        self.comment_txt.destroy()
172
 
        self.comment_bg.destroy()
173
 
        self.comment_buf.destroy()
174
 
        self.comment_win.destroy()
175
 
 
176
 
    def do_previous(self):
177
 
        if self.tests.has_prev():
178
 
            self.test = self.tests.prev()
179
 
            self.hide_test()
180
 
            self.show_test()
181
 
 
182
 
    def do_next(self):
183
 
        # Get status
184
 
        status = None
185
 
        for radio, button in self.radio_buttons.items():
186
 
            if button.get_active():
187
 
                status = radio
188
 
 
189
 
        # Get comment
190
 
        buffer = self.comment_buf.get_buffer()
191
 
        data = buffer.get_text(buffer.get_start_iter(), buffer.get_end_iter())
192
 
 
193
 
        self.test.create_result(status, data)
194
 
 
195
 
        if self.tests.has_next():
196
 
            self.test = self.tests.next()
197
 
            self.hide_test()
198
 
            self.show_test()
199
 
        else:
200
 
            self.hide_test()
201
 
            self.hide_test_common()
202
 
            self.show_send_common()
203
 
            self.show_send()
204
 
 
205
 
    def show_send_common(self):
206
 
        txt = """
207
 
Please enter the secure ID corresponding
208
 
to the machine being tested."""
209
 
        self.send_txt = self.root.add(gnomecanvas.CanvasText, markup=txt,
210
 
            size=9000, fill_color_rgba=288581136, x=220, y=80)
211
 
        self._fade_in(self.send_txt)
212
 
 
213
 
        # Login entries
214
 
        table = gtk.Table(rows=1, columns=2, homogeneous=False)
215
 
        self.vbox1.pack_start(table, True, True, 0)
216
 
 
217
 
        secure_id_label = gtk.Label('Secure ID')
218
 
        table.attach(secure_id_label, 0, 1, 0, 1, gtk.EXPAND, gtk.EXPAND, 2, 2)
219
 
        secure_id_entry = gtk.Entry()
220
 
        table.attach(secure_id_entry, 1, 2, 0, 1, gtk.EXPAND, gtk.EXPAND, 2, 2)
221
 
 
222
 
        # Send button
223
 
        send_button = self.wTree.get_widget('send_button')
224
 
        send_button.connect("clicked",
225
 
            lambda w: self.do_send(secure_id_entry.get_text()))
226
 
        self.vbox1.pack_start(send_button, False, False, 2)
227
 
 
228
 
        self.hbox1.pack_start(self.vbox1, False, False, 0)
229
 
        self.hbox1.show_all()
230
 
 
231
 
        self.login = self.root.add(gnomecanvas.CanvasWidget, x=100, y=120,
232
 
            widget=self.hbox1, width=140, height=160)
233
 
 
234
 
    def hide_send_common(self):
235
 
        self.send_txt.destroy()
236
 
        self.login.destroy()
237
 
 
238
 
    def show_send(self, error=None):
239
 
        if error:
240
 
            self.error_txt = self.root.add(gnomecanvas.CanvasText, markup=error,
241
 
                size=9000, fill_color_rgba=0xFF000000, x=190, y=20)
242
 
            self._fade_in(self.error_txt)
243
 
        else:
244
 
            self.error_txt = None
245
 
 
246
 
    def hide_send(self):
247
 
        if self.error_txt:
248
 
            self.error_txt.destroy()
249
 
 
250
 
    def do_send(self, secure_id):
251
 
        self.hide_send()
252
 
        if not secure_id:
253
 
            error = '''Must provide a secure ID.'''
254
 
        else:
255
 
            application = self.application
256
 
            application.report.secure_id = secure_id
257
 
            application.run()
258
 
            error = self.application.plugin_manager.get_error()
259
 
 
260
 
        if error:
261
 
            self.show_send(error)
262
 
        else:
263
 
            self.hide_send_common()
264
 
            gtk.main_quit()
265
 
 
266
 
    def main(self):
267
 
        self.show_intro()
268
 
        gtk.main()
269
 
 
270
 
    def _fade_in(self, text):
271
 
            i = self.start_color - 538976288
272
 
            while i > 0:
273
 
                while gtk.events_pending():
274
 
                        gtk.main_iteration(1)
275
 
                if i <= 0:
276
 
                    i = 0
277
 
                text.set(fill_color_rgba = i)
278
 
                i = i - 353703189
279
 
            return 1
280
 
 
281
 
    def _fade_out(self, text):
282
 
            i = 1
283
 
            while i <= 4294967040:
284
 
                while gtk.events_pending():
285
 
                        gtk.main_iteration(1)
286
 
                text.set(fill_color_rgba = i)
287
 
                i = i + 623191333
288
 
            text.hide()
289
 
            return 1