24
24
from bzrlib.ui import UIFactory
27
class PromptDialog(Gtk.Dialog):
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):
28
37
"""Prompt the user for a yes/no answer."""
30
def __init__(self, prompt):
31
super(PromptDialog, self).__init__()
33
label = Gtk.Label(label=prompt)
34
self.get_content_area().pack_start(label, True, True, 10)
35
self.get_content_area().show_all()
37
self.add_buttons(Gtk.STOCK_YES, Gtk.ResponseType.YES, Gtk.STOCK_NO,
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
41
68
class GtkProgressBar(Gtk.ProgressBar):
59
88
if msg is not None:
61
90
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:
65
self.set_fraction(self.fraction)
66
while Gtk.events_pending():
76
class ProgressBarWindow(Gtk.Window):
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):
78
129
def __init__(self):
79
130
super(ProgressBarWindow, self).__init__(type=Gtk.WindowType.TOPLEVEL)
85
136
self.resize(250, 15)
86
137
self.set_resizable(False)
88
def tick(self, *args, **kwargs):
90
self.pb.tick(*args, **kwargs)
92
def update(self, *args, **kwargs):
94
self.pb.update(*args, **kwargs)
106
class ProgressPanel(Gtk.HBox):
140
class ProgressPanel(ProgressContainerMixin, Gtk.Box):
108
142
def __init__(self):
109
super(ProgressPanel, self).__init__()
143
super(ProgressPanel, self).__init__(Gtk.Orientation.HORIZONTAL, 5)
110
144
image_loading = Gtk.Image.new_from_stock(Gtk.STOCK_REFRESH,
111
145
Gtk.IconSize.BUTTON)
112
146
image_loading.show()
114
148
self.pb = GtkProgressBar()
116
149
self.set_border_width(5)
117
150
self.pack_start(image_loading, False, False, 0)
118
151
self.pack_start(self.pb, True, True, 0)
120
def tick(self, *args, **kwargs):
122
self.pb.tick(*args, **kwargs)
124
def update(self, *args, **kwargs):
126
self.pb.update(*args, **kwargs)
137
154
class PasswordDialog(Gtk.Dialog):
138
155
""" Prompt the user for a password. """
146
163
self.entry = Gtk.Entry()
147
164
self.entry.set_visibility(False)
148
self.get_content_area().pack_end(self.entry, padding=10)
165
self.get_content_area().pack_end(self.entry, False, False, 10)
150
167
self.get_content_area().show_all()
177
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)
179
220
def get_password(self, prompt='', **kwargs):
180
221
"""Prompt the user for a password.
183
224
:param kwargs: Arguments which will be expanded into the prompt.
184
225
This lets front ends display different things if
186
:return: The password string, return None if the user
227
:return: The password string, return None if the user
187
228
canceled the request.
189
230
dialog = PasswordDialog(prompt % kwargs)
198
239
def _progress_all_finished(self):
199
"""See UIFactory._progress_all_finished"""
240
"""See UIFactory._progress_all_finished."""
200
241
pbw = self._progress_bar_widget
204
def _progress_updated(self, task):
205
"""See UIFactory._progress_updated"""
245
def _ensure_progress_widget(self):
206
246
if self._progress_bar_widget is None:
207
# Default to a window since nobody gave us a better mean to report
247
# Default to a window since nobody gave us a better means to report
209
249
self.set_progress_bar_widget(ProgressBarWindow())
251
def _progress_updated(self, task):
252
"""See UIFactory._progress_updated."""
253
self._ensure_progress_widget()
210
254
self._progress_bar_widget.update(task.msg,
211
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()