/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to ui.py

Merged UI work from progressbar branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
"""GTK UI
20
20
"""
21
21
 
22
 
from gi.repository import Gtk
 
22
from gi.repository import (
 
23
    Gtk,
 
24
    )
23
25
 
24
26
from bzrlib.ui import UIFactory
25
27
 
26
28
 
 
29
def main_iteration(function):
 
30
    def with_main_iteration(self, *args, **kwargs):
 
31
        result = function(self, *args, **kwargs)
 
32
        while Gtk.events_pending():
 
33
            Gtk.main_iteration_do(False)
 
34
        return result
 
35
    return with_main_iteration
 
36
 
 
37
 
27
38
class PromptDialog(Gtk.Dialog):
28
39
    """Prompt the user for a yes/no answer."""
29
40
 
46
57
        self.current = None
47
58
        self.total = None
48
59
 
 
60
    @main_iteration
49
61
    def tick(self):
50
62
        self.show()
51
63
        self.pulse()
52
64
 
 
65
    @main_iteration
53
66
    def update(self, msg=None, current_cnt=None, total_cnt=None):
54
67
        self.show()
55
68
        if current_cnt is not None:
59
72
        if msg is not None:
60
73
            self.set_text(msg)
61
74
        if None not in (self.current, self.total):
62
 
            self.fraction = float(self.current) / self.total
63
 
            if self.fraction < 0.0 or self.fraction > 1.0:
64
 
                raise AssertionError
65
 
            self.set_fraction(self.fraction)
66
 
        while Gtk.events_pending():
67
 
            Gtk.main_iteration()
68
 
 
69
 
    def finished(self):
70
 
        self.hide()
71
 
 
72
 
    def clear(self):
73
 
        self.hide()
74
 
 
75
 
 
76
 
class ProgressBarWindow(Gtk.Window):
 
75
            fraction = float(self.current) / self.total
 
76
            if fraction < 0.0 or fraction > 1.0:
 
77
                raise ValueError
 
78
            self.set_fraction(fraction)
 
79
 
 
80
    @main_iteration
 
81
    def finished(self):
 
82
        self.set_fraction(0.0)
 
83
        self.current = None
 
84
        self.total = None
 
85
        self.hide()
 
86
 
 
87
    def clear(self):
 
88
        self.finished()
 
89
 
 
90
 
 
91
class ProgressContainerMixin:
 
92
    """Expose GtkProgressBar methods to a container class."""
 
93
 
 
94
    def tick(self, *args, **kwargs):
 
95
        self.show_all()
 
96
        self.pb.tick(*args, **kwargs)
 
97
 
 
98
    def update(self, *args, **kwargs):
 
99
        self.show_all()
 
100
        self.pb.update(*args, **kwargs)
 
101
 
 
102
    def finished(self):
 
103
        self.hide()
 
104
        self.pb.finished()
 
105
 
 
106
    def clear(self):
 
107
        self.hide()
 
108
        self.pb.clear()
 
109
 
 
110
 
 
111
class ProgressBarWindow(ProgressContainerMixin, Gtk.Window):
77
112
 
78
113
    def __init__(self):
79
114
        super(ProgressBarWindow, self).__init__(type=Gtk.WindowType.TOPLEVEL)
85
120
        self.resize(250, 15)
86
121
        self.set_resizable(False)
87
122
 
88
 
    def tick(self, *args, **kwargs):
89
 
        self.show_all()
90
 
        self.pb.tick(*args, **kwargs)
91
 
 
92
 
    def update(self, *args, **kwargs):
93
 
        self.show_all()
94
 
        self.pb.update(*args, **kwargs)
95
 
 
96
 
    def finished(self):
97
 
        self.pb.finished()
98
 
        self.hide()
99
 
        self.destroy()
100
 
 
101
 
    def clear(self):
102
 
        self.pb.clear()
103
 
        self.hide()
104
 
 
105
 
 
106
 
class ProgressPanel(Gtk.HBox):
 
123
 
 
124
class ProgressPanel(ProgressContainerMixin, Gtk.Box):
107
125
 
108
126
    def __init__(self):
109
 
        super(ProgressPanel, self).__init__()
 
127
        super(ProgressPanel, self).__init__(Gtk.Orientation.HORIZONTAL, 5)
110
128
        image_loading = Gtk.Image.new_from_stock(Gtk.STOCK_REFRESH,
111
129
                                                 Gtk.IconSize.BUTTON)
112
130
        image_loading.show()
113
131
 
114
132
        self.pb = GtkProgressBar()
115
 
        self.set_spacing(5)
116
133
        self.set_border_width(5)
117
134
        self.pack_start(image_loading, False, False, 0)
118
135
        self.pack_start(self.pb, True, True, 0)
119
136
 
120
 
    def tick(self, *args, **kwargs):
121
 
        self.show_all()
122
 
        self.pb.tick(*args, **kwargs)
123
 
 
124
 
    def update(self, *args, **kwargs):
125
 
        self.show_all()
126
 
        self.pb.update(*args, **kwargs)
127
 
 
128
 
    def finished(self):
129
 
        self.pb.finished()
130
 
        self.hide()
131
 
 
132
 
    def clear(self):
133
 
        self.pb.clear()
134
 
        self.hide()
135
 
 
136
137
 
137
138
class PasswordDialog(Gtk.Dialog):
138
139
    """ Prompt the user for a password. """
183
184
        :param kwargs: Arguments which will be expanded into the prompt.
184
185
                       This lets front ends display different things if
185
186
                       they so choose.
186
 
        :return: The password string, return None if the user 
 
187
        :return: The password string, return None if the user
187
188
                 canceled the request.
188
189
        """
189
190
        dialog = PasswordDialog(prompt % kwargs)
201
202
        if pbw:
202
203
            pbw.finished()
203
204
 
204
 
    def _progress_updated(self, task):
205
 
        """See UIFactory._progress_updated"""
 
205
    def _ensure_progress_widget(self):
206
206
        if self._progress_bar_widget is None:
207
 
            # Default to a window since nobody gave us a better mean to report
 
207
            # Default to a window since nobody gave us a better means to report
208
208
            # progress.
209
209
            self.set_progress_bar_widget(ProgressBarWindow())
 
210
 
 
211
    def _progress_updated(self, task):
 
212
        """See UIFactory._progress_updated"""
 
213
        self._ensure_progress_widget()
210
214
        self._progress_bar_widget.update(task.msg,
211
215
                                         task.current_cnt, task.total_cnt)
212
216
 
 
217
    def report_transport_activity(self, transport, byte_count, direction):
 
218
        """See UIFactory.report_transport_activity"""
 
219
        self._ensure_progress_widget()
 
220
        self._progress_bar_widget.tick()