22
from gi.repository import Gtk
25
from bzrlib import progress
24
26
from bzrlib.ui import UIFactory
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)
33
return with_main_iteration
36
class PromptDialog(Gtk.MessageDialog):
37
"""Prompt the user for a yes/no answer."""
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)
45
class InfoDialog(Gtk.MessageDialog):
46
"""Show the user an informational message."""
48
MESSAGE_TYPE = Gtk.MessageType.INFO
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)
56
class WarningDialog(InfoDialog):
57
"""Show the user a warning message."""
59
MESSAGE_TYPE = Gtk.MessageType.WARNING
62
class ErrorDialog(InfoDialog):
63
"""Show the user a warning message."""
65
MESSAGE_TYPE = Gtk.MessageType.ERROR
68
class GtkProgressBar(Gtk.ProgressBar):
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)
34
label = gtk.Label(prompt)
35
self.vbox.pack_start(label, padding=10)
39
self.add_buttons(gtk.STOCK_YES, gtk.RESPONSE_YES, gtk.STOCK_NO,
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
57
def child_update(self, message, current, total):
82
60
def update(self, msg=None, current_cnt=None, total_cnt=None):
84
if current_cnt is not None:
85
62
self.current = current_cnt
86
if total_cnt is not None:
87
64
self.total = total_cnt
88
65
if msg is not None:
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:
94
self.set_fraction(fraction)
98
self.set_fraction(0.0)
107
class ProgressContainerMixin:
108
"""Expose GtkProgressBar methods to a container class."""
110
def tick(self, *args, **kwargs):
112
self.pb.tick(*args, **kwargs)
114
def update(self, *args, **kwargs):
116
self.pb.update(*args, **kwargs)
127
class ProgressBarWindow(ProgressContainerMixin, Gtk.Window):
130
super(ProgressBarWindow, self).__init__(type=Gtk.WindowType.TOPLEVEL)
68
self.fraction = float(self.current) / self.total
69
self.set_fraction(self.fraction)
70
while gtk.events_pending():
74
class ProgressBarWindow(gtk.Window):
75
def __init__(self, to_file=None, show_pct=None, show_spinner=None, show_eta=None,
76
show_bar=None, show_count=None, to_messages_file=None, _stack=None):
77
super(ProgressBarWindow, self).__init__(type=gtk.WINDOW_TOPLEVEL)
131
79
self.set_border_width(0)
132
80
self.set_title("Progress")
133
self.set_position(Gtk.WindowPosition.CENTER_ALWAYS)
134
self.pb = GtkProgressBar()
81
self.set_position(gtk.WIN_POS_CENTER_ALWAYS)
82
self.pb = GtkProgressBar(self)
136
84
self.resize(250, 15)
137
85
self.set_resizable(False)
140
class ProgressPanel(ProgressContainerMixin, Gtk.Box):
88
def return_pb(self, pb):
89
self._stack.return_pb(self)
91
def update(self, *args, **kwargs):
92
self.pb.update(*args, **kwargs)
102
def child_progress(self, *args, **kwargs):
103
return self.pb.child_progress(*args, **kwargs)
105
def child_update(self, *args, **kwargs):
106
return self.pb.child_update(*args, **kwargs)
108
def get_progress_bar(self):
113
class ProgressPanel(gtk.HBox):
142
114
def __init__(self):
143
super(ProgressPanel, self).__init__(Gtk.Orientation.HORIZONTAL, 5)
144
image_loading = Gtk.Image.new_from_stock(Gtk.STOCK_REFRESH,
115
super(ProgressPanel, self).__init__()
116
image_loading = gtk.image_new_from_stock(gtk.STOCK_REFRESH,
117
gtk.ICON_SIZE_BUTTON)
146
118
image_loading.show()
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)
154
class PasswordDialog(Gtk.Dialog):
120
self.pb = GtkProgressBar(self)
122
self.set_border_width(5)
123
self.pack_start(image_loading, False, False)
124
self.pack_start(self.pb, True, True)
126
def return_pb(self, pb):
127
self._stack.return_pb(self)
129
def get_progress_bar(self, to_file=None, show_pct=None, show_spinner=None, show_eta=None,
130
show_bar=None, show_count=None, to_messages_file=None,
136
def update(self, *args, **kwargs):
137
self.pb.update(*args, **kwargs)
147
def child_progress(self, *args, **kwargs):
148
return self.pb.child_progress(*args, **kwargs)
150
def child_update(self, *args, **kwargs):
151
return self.pb.child_update(*args, **kwargs)
155
class PasswordDialog(gtk.Dialog):
155
156
""" Prompt the user for a password. """
157
157
def __init__(self, prompt):
158
super(PasswordDialog, self).__init__()
160
label = Gtk.Label(label=prompt)
161
self.get_content_area().pack_start(label, True, True, 10)
163
self.entry = Gtk.Entry()
158
gtk.Dialog.__init__(self)
160
label = gtk.Label(prompt)
161
self.vbox.pack_start(label, padding=10)
163
self.entry = gtk.Entry()
164
164
self.entry.set_visibility(False)
165
self.get_content_area().pack_end(self.entry, False, False, 10)
167
self.get_content_area().show_all()
169
self.add_buttons(Gtk.STOCK_OK, Gtk.ResponseType.OK,
170
Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL)
165
self.vbox.pack_end(self.entry, padding=10)
169
self.add_buttons(gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
172
171
def _get_passwd(self):
173
172
return self.entry.get_text()
178
177
class GtkUIFactory(UIFactory):
179
178
"""A UI factory for GTK user interfaces."""
182
"""Create a GtkUIFactory"""
183
"""Create a GtkUIFactory.
183
186
super(GtkUIFactory, self).__init__()
184
self.set_progress_bar_widget(None)
186
def set_progress_bar_widget(self, widget):
187
self._progress_bar_widget = widget
187
self.set_nested_progress_bar_widget(ProgressBarWindow)
189
189
def get_boolean(self, prompt):
190
190
"""GtkDialog with yes/no answers"""
191
191
dialog = PromptDialog(prompt)
192
192
response = dialog.run()
194
return (response == Gtk.ResponseType.YES)
196
def show_message(self, msg):
197
"""See UIFactory.show_message."""
198
dialog = InfoDialog(msg)
202
def show_warning(self, msg):
203
"""See UIFactory.show_warning."""
204
dialog = WarningDialog(msg)
208
def show_error(self, msg):
209
"""See UIFactory.show_error."""
210
dialog = ErrorDialog(msg)
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)
194
return (response == gtk.RESPONSE_YES)
220
196
def get_password(self, prompt='', **kwargs):
221
197
"""Prompt the user for a password.
224
200
:param kwargs: Arguments which will be expanded into the prompt.
225
201
This lets front ends display different things if
227
:return: The password string, return None if the user
203
:return: The password string, return None if the user
228
204
canceled the request.
230
206
dialog = PasswordDialog(prompt % kwargs)
231
207
response = dialog.run()
232
208
passwd = dialog.passwd
234
if response == Gtk.ResponseType.OK:
210
if response == gtk.RESPONSE_OK:
239
def _progress_all_finished(self):
240
"""See UIFactory._progress_all_finished."""
241
pbw = self._progress_bar_widget
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
249
self.set_progress_bar_widget(ProgressBarWindow())
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)
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()
215
def set_nested_progress_bar_widget(self, widget):
216
self._progress_bar_stack = progress.ProgressBarStack(klass=widget)
218
def nested_progress_bar(self):
219
"""Return a nested progress bar.
221
return self._progress_bar_stack.get_nested()
223
def set_progress_bar_vbox(self, vbox):
224
"""Change the vbox to put progress bars in.
226
self._progress_bar_stack = vbox
228
def clear_term(self):
229
"""Prepare the terminal for output.
231
It has no sense when talking about GTK."""