/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

  • Committer: Jelmer Vernooij
  • Date: 2011-12-11 17:14:12 UTC
  • Revision ID: jelmer@samba.org-20111211171412-cgcn0yas3zlcahzg
StartĀ onĀ 0.104.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
from bzrlib.ui import UIFactory
25
25
 
26
26
 
27
 
def main_iteration(function):
28
 
    def with_main_iteration(self, *args, **kwargs):
29
 
        result = function(self, *args, **kwargs)
30
 
        while Gtk.events_pending():
31
 
            Gtk.main_iteration_do(False)
32
 
        return result
33
 
    return with_main_iteration
34
 
 
35
 
 
36
 
class PromptDialog(Gtk.MessageDialog):
 
27
class PromptDialog(Gtk.Dialog):
37
28
    """Prompt the user for a yes/no answer."""
38
29
 
39
 
    def __init__(self, prompt, parent=None):
40
 
        super(PromptDialog, self).__init__(
41
 
            parent, Gtk.DialogFlags.DESTROY_WITH_PARENT,
42
 
            Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO, prompt)
43
 
 
44
 
 
45
 
class InfoDialog(Gtk.MessageDialog):
46
 
    """Show the user an informational message."""
47
 
 
48
 
    MESSAGE_TYPE = Gtk.MessageType.INFO
49
 
 
50
 
    def __init__(self, prompt, parent=None):
51
 
        super(InfoDialog, self).__init__(
52
 
            parent, Gtk.DialogFlags.DESTROY_WITH_PARENT,
53
 
            self.MESSAGE_TYPE, Gtk.ButtonsType.CLOSE, prompt)
54
 
 
55
 
 
56
 
class WarningDialog(InfoDialog):
57
 
    """Show the user a warning message."""
58
 
 
59
 
    MESSAGE_TYPE = Gtk.MessageType.WARNING
60
 
 
61
 
 
62
 
class ErrorDialog(InfoDialog):
63
 
    """Show the user a warning message."""
64
 
 
65
 
    MESSAGE_TYPE = Gtk.MessageType.ERROR
 
30
    def __init__(self, prompt):
 
31
        super(PromptDialog, self).__init__()
 
32
 
 
33
        label = Gtk.Label(label=prompt)
 
34
        self.get_content_area().pack_start(label, True, True, 10)
 
35
        self.get_content_area().show_all()
 
36
 
 
37
        self.add_buttons(Gtk.STOCK_YES, Gtk.ResponseType.YES, Gtk.STOCK_NO,
 
38
                         Gtk.ResponseType.NO)
66
39
 
67
40
 
68
41
class GtkProgressBar(Gtk.ProgressBar):
73
46
        self.current = None
74
47
        self.total = None
75
48
 
76
 
    @main_iteration
77
49
    def tick(self):
78
50
        self.show()
79
51
        self.pulse()
80
52
 
81
 
    @main_iteration
82
53
    def update(self, msg=None, current_cnt=None, total_cnt=None):
83
54
        self.show()
84
55
        if current_cnt is not None:
88
59
        if msg is not None:
89
60
            self.set_text(msg)
90
61
        if None not in (self.current, self.total):
91
 
            fraction = float(self.current) / self.total
92
 
            if fraction < 0.0 or fraction > 1.0:
93
 
                raise ValueError
94
 
            self.set_fraction(fraction)
95
 
 
96
 
    @main_iteration
97
 
    def finished(self):
98
 
        self.set_fraction(0.0)
99
 
        self.current = None
100
 
        self.total = None
101
 
        self.hide()
102
 
 
103
 
    def clear(self):
104
 
        self.finished()
105
 
 
106
 
 
107
 
class ProgressContainerMixin:
108
 
    """Expose GtkProgressBar methods to a container class."""
109
 
 
110
 
    def tick(self, *args, **kwargs):
111
 
        self.show_all()
112
 
        self.pb.tick(*args, **kwargs)
113
 
 
114
 
    def update(self, *args, **kwargs):
115
 
        self.show_all()
116
 
        self.pb.update(*args, **kwargs)
117
 
 
118
 
    def finished(self):
119
 
        self.hide()
120
 
        self.pb.finished()
121
 
 
122
 
    def clear(self):
123
 
        self.hide()
124
 
        self.pb.clear()
125
 
 
126
 
 
127
 
class ProgressBarWindow(ProgressContainerMixin, Gtk.Window):
 
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):
128
77
 
129
78
    def __init__(self):
130
79
        super(ProgressBarWindow, self).__init__(type=Gtk.WindowType.TOPLEVEL)
136
85
        self.resize(250, 15)
137
86
        self.set_resizable(False)
138
87
 
139
 
 
140
 
class ProgressPanel(ProgressContainerMixin, Gtk.Box):
 
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):
141
107
 
142
108
    def __init__(self):
143
 
        super(ProgressPanel, self).__init__(Gtk.Orientation.HORIZONTAL, 5)
 
109
        super(ProgressPanel, self).__init__()
144
110
        image_loading = Gtk.Image.new_from_stock(Gtk.STOCK_REFRESH,
145
111
                                                 Gtk.IconSize.BUTTON)
146
112
        image_loading.show()
147
113
 
148
114
        self.pb = GtkProgressBar()
 
115
        self.set_spacing(5)
149
116
        self.set_border_width(5)
150
117
        self.pack_start(image_loading, False, False, 0)
151
118
        self.pack_start(self.pb, True, True, 0)
152
119
 
 
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
 
153
136
 
154
137
class PasswordDialog(Gtk.Dialog):
155
138
    """ Prompt the user for a password. """
193
176
        dialog.destroy()
194
177
        return (response == Gtk.ResponseType.YES)
195
178
 
196
 
    def show_message(self, msg):
197
 
        """See UIFactory.show_message."""
198
 
        dialog = InfoDialog(msg)
199
 
        dialog.run()
200
 
        dialog.destroy()
201
 
 
202
 
    def show_warning(self, msg):
203
 
        """See UIFactory.show_warning."""
204
 
        dialog = WarningDialog(msg)
205
 
        dialog.run()
206
 
        dialog.destroy()
207
 
 
208
 
    def show_error(self, msg):
209
 
        """See UIFactory.show_error."""
210
 
        dialog = ErrorDialog(msg)
211
 
        dialog.run()
212
 
        dialog.destroy()
213
 
 
214
 
    def show_user_warning(self, warning_id, **message_args):
215
 
        """See UIFactory.show_user_warning."""
216
 
        if warning_id not in self.suppressed_warnings:
217
 
            message = self.format_user_warning(warning_id, message_args)
218
 
            self.show_warning(message)
219
 
 
220
179
    def get_password(self, prompt='', **kwargs):
221
180
        """Prompt the user for a password.
222
181
 
224
183
        :param kwargs: Arguments which will be expanded into the prompt.
225
184
                       This lets front ends display different things if
226
185
                       they so choose.
227
 
        :return: The password string, return None if the user
 
186
        :return: The password string, return None if the user 
228
187
                 canceled the request.
229
188
        """
230
189
        dialog = PasswordDialog(prompt % kwargs)
237
196
            return None
238
197
 
239
198
    def _progress_all_finished(self):
240
 
        """See UIFactory._progress_all_finished."""
 
199
        """See UIFactory._progress_all_finished"""
241
200
        pbw = self._progress_bar_widget
242
201
        if pbw:
243
202
            pbw.finished()
244
203
 
245
 
    def _ensure_progress_widget(self):
 
204
    def _progress_updated(self, task):
 
205
        """See UIFactory._progress_updated"""
246
206
        if self._progress_bar_widget is None:
247
 
            # Default to a window since nobody gave us a better means to report
 
207
            # Default to a window since nobody gave us a better mean to report
248
208
            # progress.
249
209
            self.set_progress_bar_widget(ProgressBarWindow())
250
 
 
251
 
    def _progress_updated(self, task):
252
 
        """See UIFactory._progress_updated."""
253
 
        self._ensure_progress_widget()
254
210
        self._progress_bar_widget.update(task.msg,
255
211
                                         task.current_cnt, task.total_cnt)
256
212
 
257
 
    def report_transport_activity(self, transport, byte_count, direction):
258
 
        """See UIFactory.report_transport_activity."""
259
 
        self._ensure_progress_widget()
260
 
        self._progress_bar_widget.tick()