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

« back to all changes in this revision

Viewing changes to hwtest/gui.py

  • Committer: David Murphy
  • Date: 2007-09-18 15:26:03 UTC
  • Revision ID: david.murphy@canonical.com-20070918152603-wqjqtdyqftleulho
Initial release

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