/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: 2008-07-18 20:19:45 UTC
  • mto: This revision was merged to the branch mainline in revision 555.
  • Revision ID: jelmer@samba.org-20080718201945-85lowjdgx1d9ds82
UseĀ absoluteĀ imports.

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
import gtk
 
23
import sys
23
24
 
 
25
from bzrlib import progress
24
26
from bzrlib.ui import UIFactory
25
27
 
26
28
 
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):
37
 
    """Prompt the user for a yes/no answer."""
38
 
 
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
66
 
 
67
 
 
68
 
class GtkProgressBar(Gtk.ProgressBar):
69
 
 
70
 
    def __init__(self):
71
 
        super(GtkProgressBar, self).__init__()
 
29
class PromptDialog(gtk.Dialog):
 
30
    """ Prompt the user for a yes/no answer. """
 
31
    def __init__(self, prompt):
 
32
        gtk.Dialog.__init__(self)
 
33
        
 
34
        label = gtk.Label(prompt)
 
35
        self.vbox.pack_start(label, padding=10)
 
36
        
 
37
        self.vbox.show_all()
 
38
 
 
39
        self.add_buttons(gtk.STOCK_YES, gtk.RESPONSE_YES, gtk.STOCK_NO, 
 
40
                         gtk.RESPONSE_NO)
 
41
 
 
42
 
 
43
class GtkProgressBar(gtk.ProgressBar,progress._BaseProgressBar):
 
44
    def __init__(self, _stack=None):
 
45
        gtk.ProgressBar.__init__(self)
72
46
        self.set_fraction(0.0)
 
47
        progress._BaseProgressBar.__init__(self, _stack=_stack)
73
48
        self.current = None
74
49
        self.total = None
75
50
 
76
 
    @main_iteration
 
51
    def clear(self):
 
52
        self.hide()
 
53
 
77
54
    def tick(self):
78
 
        self.show()
79
55
        self.pulse()
80
56
 
81
 
    @main_iteration
 
57
    def child_update(self, message, current, total):
 
58
        pass
 
59
 
82
60
    def update(self, msg=None, current_cnt=None, total_cnt=None):
83
 
        self.show()
84
61
        if current_cnt is not None:
85
62
            self.current = current_cnt
86
63
        if total_cnt is not None:
88
65
        if msg is not None:
89
66
            self.set_text(msg)
90
67
        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):
128
 
 
129
 
    def __init__(self):
130
 
        super(ProgressBarWindow, self).__init__(type=Gtk.WindowType.TOPLEVEL)
 
68
            self.fraction = float(self.current) / self.total
 
69
            if self.fraction < 0.0 or self.fraction > 1.0:
 
70
                raise AssertionError
 
71
            self.set_fraction(self.fraction)
 
72
        while gtk.events_pending():
 
73
            gtk.main_iteration()
 
74
 
 
75
 
 
76
class ProgressBarWindow(gtk.Window):
 
77
    def __init__(self, to_file=None, show_pct=None, show_spinner=None, show_eta=None, 
 
78
                 show_bar=None, show_count=None, to_messages_file=None, _stack=None):
 
79
        super(ProgressBarWindow, self).__init__(type=gtk.WINDOW_TOPLEVEL)
 
80
        self._stack = _stack
131
81
        self.set_border_width(0)
132
82
        self.set_title("Progress")
133
 
        self.set_position(Gtk.WindowPosition.CENTER_ALWAYS)
134
 
        self.pb = GtkProgressBar()
 
83
        self.set_position(gtk.WIN_POS_CENTER_ALWAYS)
 
84
        self.pb = GtkProgressBar(self)
135
85
        self.add(self.pb)
136
86
        self.resize(250, 15)
137
87
        self.set_resizable(False)
138
 
 
139
 
 
140
 
class ProgressPanel(ProgressContainerMixin, Gtk.Box):
141
 
 
 
88
        self.show_all()
 
89
 
 
90
    def return_pb(self, pb):
 
91
        self._stack.return_pb(self)
 
92
    
 
93
    def update(self, *args, **kwargs):
 
94
        self.pb.update(*args, **kwargs)
 
95
 
 
96
    def finished(self):
 
97
        self.pb.finished()
 
98
        self.hide_all()
 
99
 
 
100
    def clear(self):
 
101
        self.pb.clear()
 
102
        self.destroy()
 
103
 
 
104
    def child_progress(self, *args, **kwargs):
 
105
        return self.pb.child_progress(*args, **kwargs)
 
106
 
 
107
    def child_update(self, *args, **kwargs):
 
108
        return self.pb.child_update(*args, **kwargs)
 
109
 
 
110
    def get_progress_bar(self):
 
111
        self.show_all()
 
112
        return self
 
113
 
 
114
 
 
115
class ProgressPanel(gtk.HBox):
142
116
    def __init__(self):
143
 
        super(ProgressPanel, self).__init__(Gtk.Orientation.HORIZONTAL, 5)
144
 
        image_loading = Gtk.Image.new_from_stock(Gtk.STOCK_REFRESH,
145
 
                                                 Gtk.IconSize.BUTTON)
 
117
        super(ProgressPanel, self).__init__()
 
118
        image_loading = gtk.image_new_from_stock(gtk.STOCK_REFRESH,
 
119
                                                 gtk.ICON_SIZE_BUTTON)
146
120
        image_loading.show()
147
 
 
148
 
        self.pb = GtkProgressBar()
149
 
        self.set_border_width(5)
150
 
        self.pack_start(image_loading, False, False, 0)
151
 
        self.pack_start(self.pb, True, True, 0)
152
 
 
153
 
 
154
 
class PasswordDialog(Gtk.Dialog):
 
121
        
 
122
        self.pb = GtkProgressBar(self)
 
123
        self.set_spacing(5)
 
124
        self.set_border_width(5)        
 
125
        self.pack_start(image_loading, False, False)
 
126
        self.pack_start(self.pb, True, True)
 
127
 
 
128
    def return_pb(self, pb):
 
129
        self._stack.return_pb(self)
 
130
 
 
131
    def get_progress_bar(self, to_file=None, show_pct=None, show_spinner=None, show_eta=None, 
 
132
                         show_bar=None, show_count=None, to_messages_file=None, 
 
133
                         _stack=None):
 
134
        self._stack = _stack
 
135
        self.show_all()
 
136
        return self
 
137
    
 
138
    def update(self, *args, **kwargs):
 
139
        self.pb.update(*args, **kwargs)
 
140
 
 
141
    def finished(self):
 
142
        self.pb.finished()
 
143
        self.hide_all()
 
144
 
 
145
    def clear(self):
 
146
        self.pb.clear()
 
147
        self.hide_all()
 
148
 
 
149
    def child_progress(self, *args, **kwargs):
 
150
        return self.pb.child_progress(*args, **kwargs)
 
151
 
 
152
    def child_update(self, *args, **kwargs):
 
153
        return self.pb.child_update(*args, **kwargs)
 
154
 
 
155
 
 
156
 
 
157
class PasswordDialog(gtk.Dialog):
155
158
    """ Prompt the user for a password. """
156
 
 
157
159
    def __init__(self, prompt):
158
 
        super(PasswordDialog, self).__init__()
159
 
 
160
 
        label = Gtk.Label(label=prompt)
161
 
        self.get_content_area().pack_start(label, True, True, 10)
162
 
 
163
 
        self.entry = Gtk.Entry()
 
160
        gtk.Dialog.__init__(self)
 
161
        
 
162
        label = gtk.Label(prompt)
 
163
        self.vbox.pack_start(label, padding=10)
 
164
        
 
165
        self.entry = gtk.Entry()
164
166
        self.entry.set_visibility(False)
165
 
        self.get_content_area().pack_end(self.entry, False, False, 10)
166
 
 
167
 
        self.get_content_area().show_all()
168
 
 
169
 
        self.add_buttons(Gtk.STOCK_OK, Gtk.ResponseType.OK,
170
 
                         Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL)
171
 
 
 
167
        self.vbox.pack_end(self.entry, padding=10)
 
168
        
 
169
        self.vbox.show_all()
 
170
        
 
171
        self.add_buttons(gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
 
172
    
172
173
    def _get_passwd(self):
173
174
        return self.entry.get_text()
174
175
 
178
179
class GtkUIFactory(UIFactory):
179
180
    """A UI factory for GTK user interfaces."""
180
181
 
181
 
    def __init__(self):
182
 
        """Create a GtkUIFactory"""
 
182
    def __init__(self,
 
183
                 stdout=None,
 
184
                 stderr=None):
 
185
        """Create a GtkUIFactory.
 
186
 
 
187
        """
183
188
        super(GtkUIFactory, self).__init__()
184
 
        self.set_progress_bar_widget(None)
185
 
 
186
 
    def set_progress_bar_widget(self, widget):
187
 
        self._progress_bar_widget = widget
 
189
        self.set_nested_progress_bar_widget(ProgressBarWindow)
188
190
 
189
191
    def get_boolean(self, prompt):
190
192
        """GtkDialog with yes/no answers"""
191
193
        dialog = PromptDialog(prompt)
192
194
        response = dialog.run()
193
195
        dialog.destroy()
194
 
        return (response == Gtk.ResponseType.YES)
195
 
 
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
 
 
 
196
        return (response == gtk.RESPONSE_YES)
 
197
        
220
198
    def get_password(self, prompt='', **kwargs):
221
199
        """Prompt the user for a password.
222
200
 
224
202
        :param kwargs: Arguments which will be expanded into the prompt.
225
203
                       This lets front ends display different things if
226
204
                       they so choose.
227
 
        :return: The password string, return None if the user
 
205
        :return: The password string, return None if the user 
228
206
                 canceled the request.
229
207
        """
230
208
        dialog = PasswordDialog(prompt % kwargs)
231
209
        response = dialog.run()
232
210
        passwd = dialog.passwd
233
211
        dialog.destroy()
234
 
        if response == Gtk.ResponseType.OK:
 
212
        if response == gtk.RESPONSE_OK:
235
213
            return passwd
236
214
        else:
237
215
            return None
238
216
 
239
 
    def _progress_all_finished(self):
240
 
        """See UIFactory._progress_all_finished."""
241
 
        pbw = self._progress_bar_widget
242
 
        if pbw:
243
 
            pbw.finished()
244
 
 
245
 
    def _ensure_progress_widget(self):
246
 
        if self._progress_bar_widget is None:
247
 
            # Default to a window since nobody gave us a better means to report
248
 
            # progress.
249
 
            self.set_progress_bar_widget(ProgressBarWindow())
250
 
 
251
 
    def _progress_updated(self, task):
252
 
        """See UIFactory._progress_updated."""
253
 
        self._ensure_progress_widget()
254
 
        self._progress_bar_widget.update(task.msg,
255
 
                                         task.current_cnt, task.total_cnt)
256
 
 
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()
 
217
    def set_nested_progress_bar_widget(self, widget):
 
218
        self._progress_bar_stack = progress.ProgressBarStack(klass=widget)
 
219
 
 
220
    def nested_progress_bar(self):
 
221
        """Return a nested progress bar.
 
222
        """
 
223
        return self._progress_bar_stack.get_nested()
 
224
 
 
225
    def set_progress_bar_vbox(self, vbox):
 
226
        """Change the vbox to put progress bars in.
 
227
        """
 
228
        self._progress_bar_stack = vbox
 
229
 
 
230
    def clear_term(self):
 
231
        """Prepare the terminal for output.
 
232
 
 
233
        It has no sense when talking about GTK."""
 
234
        pass