Source

vinstall / vinstall / ui / gtk.py

  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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#-*- coding: utf-8 -*-


"""An Gtk based view."""

# pylint cant understand my relative imports
# pylint: disable-msg=E1101,W0406,W0404


from __future__ import absolute_import

import gtk, time
from vinstall.core import core, model, command


gtk.gdk.threads_init()


class GtkView:
    """Base class for Gtk view."""

    # overload some methods from the interface that are not needed for this
    # view
    def show(self):
        """Toggle all the widgets visible.

        """

    def connect(self):
        """Connect signals.

        """
        pass


@core.main_window
class GtkMainWindow(object):
    """Top level widget for Gtk views.

    """
    def __init__(self):
        """View initialization.

        """
        self.title = ""
        self.intro = ""
        self.widgets = []

        self.window = gtk.Window()
        self.main_container = gtk.VBox()
        self.container = gtk.VBox()

        self.next_button = gtk.Button(gtk.STOCK_GO_FORWARD)
        self.back_button = gtk.Button(gtk.STOCK_GO_BACK)
        self.disconnect_next = None
        self.disconnect_back = None

        self.window.add(self.main_container)

        self._header()
        self._body()
        self._buttons_row()
        self._footer()
        self._setup()

    def _header(self):
        """Create a header for the main window, with things like a generic
        title and tag line

        """
        icon = gtk.Image()
        #icon.set_from_icon_name("system-software-update", gtk.ICON_SIZE_DIALOG)
        icon.set_from_file("/usr/share/pixmaps/logo.png")
        s = ('<span foreground="black" size="25000"><b>VectorLinux</b></span>\n'
                '<span foreground="black" size="large">When choice matters</span>')
        title = gtk.Label(s)
        title.set_use_markup(True)
        title.set_justify(gtk.JUSTIFY_LEFT)
        title.set_alignment(0, 0.5)
        title.set_padding(5, 5)

        container = gtk.HBox()
        container.pack_start(icon, False, False, 5)
        container.pack_start(title, False, False, 5)
        eb = gtk.EventBox()

        eb.add(container)
        eb.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
        self.main_container.pack_start(eb, False, False, 5)

    def _body(self):
        """Create the window body, this is where the widgets provided by
        controller classes are shown

        """
        scrolledwin = gtk.ScrolledWindow()
        scrolledwin.add_with_viewport(self.container)
        scrolledwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.container.set_border_width(10)
        self.main_container.pack_start(scrolledwin, True, True, 10)

    def _buttons_row(self):
        """Create the next and back buttons

        """
        self.next_button.set_use_stock(True)
        self.back_button.set_use_stock(True)
        container = gtk.HBox()
        container.pack_start(self.back_button, False, False, 5)
        container.pack_start(self.next_button, False, False, 5)
        halign = gtk.Alignment(1, 0, 0, 0)
        halign.add(container)
        self.main_container.pack_end(halign, False, False, 5)
        #self.main_container.pack_end(gtk.HSeparator(), False, False, 5)

    def _footer(self):
        """Create a footer for the main window

        """

    def _setup(self):
        """Put all the elements together

        """
        self.container.set_homogeneous(False)
        self.window.set_default_size(700, 450)
        self.window.set_border_width(5)
        self.window.connect("destroy", lambda *x: self.stop())

    def update(self):
        """Update the frame with a new body after the next or previous button
        is clicked. Used by the controller classes to update the body of the
        window.

        """
        for oldwidget in self.container:
            self.container.remove(oldwidget)

        title = gtk.Label("<span size='large'><b>%s</b></span>" % self.title)
        title.set_alignment(0, 0.5)
        title.set_use_markup(True)
        intro = gtk.Label(self.intro)
        intro.set_alignment(0, 0.5)
        intro.set_line_wrap(True)

        def label_size_allocate(widget, rect):
            "Resize label when container size changes"
            widget.set_size_request(rect.width, -1)

        intro.connect("size-allocate", label_size_allocate)

        self.container.pack_start(title, False, False, 5)
        self.container.pack_start(intro, False, False, 5)

        for widget in self.widgets:
            self.container.pack_start(widget, False, False, 5)
            widget.show()
        self.container.show()
        self.window.show_all()

    def refresh(self):
        """This can be called from a long running process to request screen
        updates.

        """
        while gtk.events_pending():
            gtk.main_iteration(False)

    def add_next_callback(self, func, user_arg=None):
        """Set a callback for the next button "click" signal. This is used by
        the controller classes to process user input and setup the next state
        of the application.

        """
        handler = self.next_button.connect("clicked", func, user_arg)
        self.disconnect_next = lambda: self.next_button.disconnect(handler)

    def add_previous_callback(self, func, user_arg=None):
        """Set a callback for the previous button "click" signal. Used by the
        controller classes for jumping to a previous state.

        """
        handler = self.back_button.connect("clicked", func, user_arg)
        self.disconnect_back = lambda: self.back_button.disconnect(handler)

    def clear_callbacks(self):
        """Remove every callback from the buttons.

        """
        if self.disconnect_next and self.disconnect_back:
            self.disconnect_next()
            self.disconnect_back()

    def disable_buttons(self):
        """Disable input on the buttons, preventing user input while tasks are
        running

        """
        self.next_button.set_sensitive(False)
        self.back_button.set_sensitive(False)

    def enable_buttons(self):
        """Enable buttons

        """
        self.next_button.set_sensitive(True)
        self.back_button.set_sensitive(True)

    def set_next_button_label(self, text):
        """Set next button label

        """
        self.next_button.set_label(text)

    def set_previous_button_label(self, text):
        """Set previous button label

        """
        self.back_button.set_label(text)

    def alert(self, text):
        """Show a popup when tasks are complete

        """
        dialog = gtk.MessageDialog(parent=self.window,
                flags=0,
                type=gtk.MESSAGE_INFO,
                buttons=gtk.BUTTONS_OK,
                message_format=text)
        dialog.run()
        dialog.destroy()

    def run(self):
        """Start the main loop.

        """
        gtk.gdk.threads_enter()
        gtk.main()

    def stop(self):
        """Stop the main loop.

        """
        gtk.gdk.threads_leave()
        gtk.main_quit()


@core.renders(model.SimpleText)
class GtkTextLabel(GtkView):
    """ A simple text label for information purposes only.

    """
    def __init__(self, label):
        """GtkTextLabel initialization.

        """
        self.text = label.text
        self.label = None

    def accept(self):
        """Return a text widget.

        """
        return self.label

    def configure(self):
        """We dont need it in this simple widget.

        """
	def label_size_allocate(widget, rect):
            "Resize label when container size changes"
            widget.set_size_request(rect.width, -1)

        self.label = gtk.Label(self.text)
        self.label.set_use_markup(True)
        self.label.set_line_wrap(True)
        self.label.connect("size-allocate", label_size_allocate)

    def get_value(self):
        """This widget has no user input. Return None.

        """
        return None


@core.renders(model.TextOption)
class GtkTextInput(GtkView):
    """A text input widget for rendering the TextOption model.

    """
    def __init__(self, text_option):

        # Expose the model
        self.option_name = text_option.name
        self.option_maxlen = text_option.maxlen
        self.option_short_desc = text_option.short_desc
        self.option_help = text_option.help_text

    def configure(self):
        """Setup the elements composing the model view."""

        self.label = gtk.Label(self.option_name)
        #self.label.set_alignment(1, 0.5)
        self.edit = gtk.Entry()
        self.container = gtk.HBox()
        self.container.pack_start(self.label)
        self.container.pack_start(self.edit)

    def get_value(self):
        """Returns the text entered by the user."""

        return self.edit.get_text()

    def accept(self):
        """Returns the configured top level container widget."""

        return self.container


@core.renders(model.NumericOption)
class GtkNumericInput(GtkView):
    """A widget accepting numeric input for rendering the NumericOption model.

    """
    def __init__(self, text_option):

        # Expose the model
        self.option_name = text_option.name
        self.option_short_desc = text_option.short_desc
        self.option_help = text_option.help_text

    def configure(self):
        """Setup the elements composing the model view."""

        # Widgets
        self.label = gtk.Label(self.option_name)
        self.edit = gtk.SpinButton()
        self.container = gtk.HBox()
        self.container.pack_start(self.label)
        self.container.pack_start(self.edit)

    def get_value(self):
        """Returns the text entered by the user."""

        return self.edit.get_value()

    def accept(self):
        """Returns the configured top level container widget."""

        return self.container


@core.renders(model.BoolOption)
class GtkBoolInput(GtkView):
    """A a two-states widget for boolean options.

    """
    def __init__(self, bool_option):

        self.option_name = bool_option.name
        self.option_short_desc = bool_option.short_desc
        self.option_help = bool_option.help_text

    def configure(self):
        """Set up the container widget.

        """
        self.check_box = gtk.CheckButton(label=self.option_name)

    def get_value(self):
        """Return the user input.

        """
        return self.check_box.get_active()

    def accept(self):
        """Return the toplevel widget.

        """
        return self.check_box


@core.renders(model.ExclusiveOptionList)
class GtkExclusiveList(GtkView):
    """A list of options, from which the user can pick only one.

    """
    def __init__(self, options_list):

        self.options_list = options_list.options
        self.selected_item = 0

    def configure(self):
        """Setup widgets.

        """
        self.widgets = []
        self.container = gtk.VBox()
        first_radio = gtk.RadioButton(label=self.options_list[0]["name"])
        first_radio.connect("toggled", self._on_radio_toggled, 0)
        first_radio.set_active(True)
        self.container.pack_start(first_radio)
        for index, option in enumerate(self.options_list[1:]):
            radio_button = gtk.RadioButton(group=first_radio,
                    label=option["name"])
            radio_button.connect("toggled", self._on_radio_toggled, index + 1)
            self.container.pack_start(radio_button)
            self.widgets.append(radio_button)

    def get_value(self):
        """Return the user input.

        """
        return self.selected_item

    def accept(self):
        """Return the top level widget.

        """
        return self.container

    def _on_radio_toggled(self, widget, data=None):
        """Set the self.selected_item attribute"""

        if widget.get_active():
            self.selected_item = data


@core.renders(model.DropdownOptionList)
class GtkDropdownList(GtkView):
    """A list of options, from which the user can pick only one.

    """
    def __init__(self, options):

        self.text = options.label
        self.options = options.options
        self.selected_item = 0

    def configure(self):
        """Setup widgets.

        """
        self.label = gtk.Label(self.text)
        self.widget = gtk.combo_box_new_text()
        self.container = gtk.HBox()
        for d in self.options:
            self.widget.append_text(d["option"])
        self.widget.set_active(0)
        self.container.pack_start(self.label, False, False, 5)
        self.container.pack_start(self.widget, False, False, 5)
        self.container.show_all()

    def get_value(self):
        """Return the user input.

        """
        return self.widget.get_active()

    def accept(self):
        """Return the top level widget.

        """
        return self.container


@core.renders(model.IPAddress)
class GtkIPAddress(GtkView):
    """A widget designed for entering an IP address.

    """
    def __init__(self, ip_option):

        self.option_name = ip_option.name
        self.short_desc = ip_option.short_desc
        self.help = ip_option.help_text

    def configure(self):
        """Setup the input widgets and a container.

        """
        self.label = gtk.Label(self.option_name)
        self.container = gtk.HBox()
        self.container.pack_start(self.label)
        self.widgets = []
        for i in xrange(4):
            entry = gtk.Entry(max=3)
            self.container.pack_start(entry)
            self.widgets.append(entry)

    def get_value(self):
        """Return user input.

        """
        values = [ i.get_value() for i in self.widgets ]
        values = [ str(value) for value in values ]
        return ".".join(values)

    def accept(self):
        """Return the top level widget.

        """
        return self.container


@core.renders(command.ProcessingFacade)
class GtkBatchProcessing(GtkView):
    """Renders a Batch Processing class in a progressbar

    """
    def __init__(self, bp):

        bp.observer = self
        self.bp = bp
        self.finished = False
        self.running = False

    def configure(self):
        """Setup widgets

        """
        self.caption = gtk.Label()
        self.progress_bar = gtk.ProgressBar()
        self.container = gtk.VBox()
        self.container.pack_start(self.caption)
        self.container.pack_start(self.progress_bar)
        self.container.set_border_width(40)

    def get_value(self):
        """Return user input. We return None since this widget does not accept focus

        """
        return None

    def on_job_start(self, queue):
        """Called before starting to process the input queue

        """
        self.running = True
        self.progress_bar.set_fraction(0.01)

    def on_job_end(self, queue):
        """Called when the input queue is done

        """
        self.running = False
        self.finished = True
        self.progress_bar.set_fraction(1)

    def on_item_start(self, item):
        """Called before processing the item

        """
        self.caption.set_text(item.description)

    def on_item_end(self, args):
        """Called after item is complete. Args is a tuple with
        item, result of the process, items completed and total items

        """
        item, result, completed, total = args
        total = float(total)
        self.progress_bar.set_fraction(completed / total)
        time.sleep(0.15)

    def accept(self):
        """Return the top level widget.

        """
        return self.container