/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: Curtis Hovey
  • Date: 2012-09-02 19:29:42 UTC
  • mfrom: (794.1.1 get_style_context)
  • Revision ID: sinzui.is@verizon.net-20120902192942-cwtww9wxvznx0wod
Do not call deprecated get_style(); use get_style_context().

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
"""GTK UI
20
20
"""
21
21
 
22
 
import gtk
23
 
import sys
 
22
from gi.repository import Gtk
24
23
 
25
 
from bzrlib import progress
26
24
from bzrlib.ui import UIFactory
27
25
 
28
26
 
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
 
        self.vbox.show_all()
37
 
 
38
 
        self.add_buttons(gtk.STOCK_YES, gtk.RESPONSE_YES, gtk.STOCK_NO,
39
 
                         gtk.RESPONSE_NO)
40
 
 
41
 
 
42
 
class GtkProgressBar(gtk.ProgressBar):
 
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):
43
69
 
44
70
    def __init__(self):
45
 
        gtk.ProgressBar.__init__(self)
 
71
        super(GtkProgressBar, self).__init__()
46
72
        self.set_fraction(0.0)
47
73
        self.current = None
48
74
        self.total = None
49
75
 
 
76
    @main_iteration
50
77
    def tick(self):
51
78
        self.show()
52
79
        self.pulse()
53
80
 
 
81
    @main_iteration
54
82
    def update(self, msg=None, current_cnt=None, total_cnt=None):
55
83
        self.show()
56
84
        if current_cnt is not None:
60
88
        if msg is not None:
61
89
            self.set_text(msg)
62
90
        if None not in (self.current, self.total):
63
 
            self.fraction = float(self.current) / self.total
64
 
            if self.fraction < 0.0 or self.fraction > 1.0:
65
 
                raise AssertionError
66
 
            self.set_fraction(self.fraction)
67
 
        while gtk.events_pending():
68
 
            gtk.main_iteration()
69
 
 
70
 
    def finished(self):
71
 
        self.hide()
72
 
 
73
 
    def clear(self):
74
 
        self.hide()
75
 
 
76
 
 
77
 
class ProgressBarWindow(gtk.Window):
 
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):
78
128
 
79
129
    def __init__(self):
80
 
        super(ProgressBarWindow, self).__init__(type=gtk.WINDOW_TOPLEVEL)
 
130
        super(ProgressBarWindow, self).__init__(type=Gtk.WindowType.TOPLEVEL)
81
131
        self.set_border_width(0)
82
132
        self.set_title("Progress")
83
 
        self.set_position(gtk.WIN_POS_CENTER_ALWAYS)
 
133
        self.set_position(Gtk.WindowPosition.CENTER_ALWAYS)
84
134
        self.pb = GtkProgressBar()
85
135
        self.add(self.pb)
86
136
        self.resize(250, 15)
87
137
        self.set_resizable(False)
88
138
 
89
 
    def tick(self, *args, **kwargs):
90
 
        self.show_all()
91
 
        self.pb.tick(*args, **kwargs)
92
 
 
93
 
    def update(self, *args, **kwargs):
94
 
        self.show_all()
95
 
        self.pb.update(*args, **kwargs)
96
 
 
97
 
    def finished(self):
98
 
        self.pb.finished()
99
 
        self.hide_all()
100
 
        self.destroy()
101
 
 
102
 
    def clear(self):
103
 
        self.pb.clear()
104
 
        self.hide_all()
105
 
 
106
 
 
107
 
class ProgressPanel(gtk.HBox):
 
139
 
 
140
class ProgressPanel(ProgressContainerMixin, Gtk.Box):
108
141
 
109
142
    def __init__(self):
110
 
        super(ProgressPanel, self).__init__()
111
 
        image_loading = gtk.image_new_from_stock(gtk.STOCK_REFRESH,
112
 
                                                 gtk.ICON_SIZE_BUTTON)
 
143
        super(ProgressPanel, self).__init__(Gtk.Orientation.HORIZONTAL, 5)
 
144
        image_loading = Gtk.Image.new_from_stock(Gtk.STOCK_REFRESH,
 
145
                                                 Gtk.IconSize.BUTTON)
113
146
        image_loading.show()
114
147
 
115
148
        self.pb = GtkProgressBar()
116
 
        self.set_spacing(5)
117
149
        self.set_border_width(5)
118
 
        self.pack_start(image_loading, False, False)
119
 
        self.pack_start(self.pb, True, True)
120
 
 
121
 
    def tick(self, *args, **kwargs):
122
 
        self.show_all()
123
 
        self.pb.tick(*args, **kwargs)
124
 
 
125
 
    def update(self, *args, **kwargs):
126
 
        self.show_all()
127
 
        self.pb.update(*args, **kwargs)
128
 
 
129
 
    def finished(self):
130
 
        self.pb.finished()
131
 
        self.hide_all()
132
 
 
133
 
    def clear(self):
134
 
        self.pb.clear()
135
 
        self.hide_all()
136
 
 
137
 
 
138
 
class PasswordDialog(gtk.Dialog):
 
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):
139
155
    """ Prompt the user for a password. """
140
156
 
141
157
    def __init__(self, prompt):
142
 
        gtk.Dialog.__init__(self)
143
 
 
144
 
        label = gtk.Label(prompt)
145
 
        self.vbox.pack_start(label, padding=10)
146
 
 
147
 
        self.entry = gtk.Entry()
 
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()
148
164
        self.entry.set_visibility(False)
149
 
        self.vbox.pack_end(self.entry, padding=10)
150
 
 
151
 
        self.vbox.show_all()
152
 
 
153
 
        self.add_buttons(gtk.STOCK_OK, gtk.RESPONSE_OK,
154
 
                         gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
 
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)
155
171
 
156
172
    def _get_passwd(self):
157
173
        return self.entry.get_text()
175
191
        dialog = PromptDialog(prompt)
176
192
        response = dialog.run()
177
193
        dialog.destroy()
178
 
        return (response == gtk.RESPONSE_YES)
 
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)
179
219
 
180
220
    def get_password(self, prompt='', **kwargs):
181
221
        """Prompt the user for a password.
184
224
        :param kwargs: Arguments which will be expanded into the prompt.
185
225
                       This lets front ends display different things if
186
226
                       they so choose.
187
 
        :return: The password string, return None if the user 
 
227
        :return: The password string, return None if the user
188
228
                 canceled the request.
189
229
        """
190
230
        dialog = PasswordDialog(prompt % kwargs)
191
231
        response = dialog.run()
192
232
        passwd = dialog.passwd
193
233
        dialog.destroy()
194
 
        if response == gtk.RESPONSE_OK:
 
234
        if response == Gtk.ResponseType.OK:
195
235
            return passwd
196
236
        else:
197
237
            return None
198
238
 
199
239
    def _progress_all_finished(self):
200
 
        """See UIFactory._progress_all_finished"""
 
240
        """See UIFactory._progress_all_finished."""
201
241
        pbw = self._progress_bar_widget
202
242
        if pbw:
203
243
            pbw.finished()
204
244
 
205
 
    def _progress_updated(self, task):
206
 
        """See UIFactory._progress_updated"""
 
245
    def _ensure_progress_widget(self):
207
246
        if self._progress_bar_widget is None:
208
 
            # 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
248
            # progress.
210
249
            self.set_progress_bar_widget(ProgressBarWindow())
 
250
 
 
251
    def _progress_updated(self, task):
 
252
        """See UIFactory._progress_updated."""
 
253
        self._ensure_progress_widget()
211
254
        self._progress_bar_widget.update(task.msg,
212
255
                                         task.current_cnt, task.total_cnt)
213
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()