/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-02-26 15:45:59 UTC
  • mto: (776.2.1 ui-factory)
  • mto: This revision was merged to the branch mainline in revision 779.
  • Revision ID: sinzui.is@verizon.net-20120226154559-vshgd90658a0rk67
Added a test module for ui.
Updated setup.py check to accept a -m argument to run a specific test module.

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
from gi.repository import (
 
23
    Gtk,
 
24
    )
23
25
 
24
26
from bzrlib.ui import UIFactory
25
27
 
26
28
 
27
29
def main_iteration(function):
28
30
    def with_main_iteration(self, *args, **kwargs):
29
 
        result = function(self, *args, **kwargs)
 
31
        function(self, *args, **kwargs)
30
32
        while Gtk.events_pending():
31
33
            Gtk.main_iteration_do(False)
32
 
        return result
33
34
    return with_main_iteration
34
35
 
35
36
 
36
 
class PromptDialog(Gtk.MessageDialog):
 
37
class PromptDialog(Gtk.Dialog):
37
38
    """Prompt the user for a yes/no answer."""
38
39
 
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
 
40
    def __init__(self, prompt):
 
41
        super(PromptDialog, self).__init__()
 
42
 
 
43
        label = Gtk.Label(label=prompt)
 
44
        self.get_content_area().pack_start(label, True, True, 10)
 
45
        self.get_content_area().show_all()
 
46
 
 
47
        self.add_buttons(Gtk.STOCK_YES, Gtk.ResponseType.YES, Gtk.STOCK_NO,
 
48
                         Gtk.ResponseType.NO)
66
49
 
67
50
 
68
51
class GtkProgressBar(Gtk.ProgressBar):
88
71
        if msg is not None:
89
72
            self.set_text(msg)
90
73
        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)
 
74
            self.fraction = float(self.current) / self.total
 
75
            if self.fraction < 0.0 or self.fraction > 1.0:
 
76
                raise AssertionError
 
77
            self.set_fraction(self.fraction)
95
78
 
96
79
    @main_iteration
97
80
    def finished(self):
98
 
        self.set_fraction(0.0)
99
81
        self.current = None
100
82
        self.total = None
101
83
        self.hide()
104
86
        self.finished()
105
87
 
106
88
 
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):
 
89
class ProgressBarWindow(Gtk.Window):
128
90
 
129
91
    def __init__(self):
130
92
        super(ProgressBarWindow, self).__init__(type=Gtk.WindowType.TOPLEVEL)
136
98
        self.resize(250, 15)
137
99
        self.set_resizable(False)
138
100
 
139
 
 
140
 
class ProgressPanel(ProgressContainerMixin, Gtk.Box):
 
101
    def tick(self, *args, **kwargs):
 
102
        self.show_all()
 
103
        self.pb.tick(*args, **kwargs)
 
104
 
 
105
    def update(self, *args, **kwargs):
 
106
        self.show_all()
 
107
        self.pb.update(*args, **kwargs)
 
108
 
 
109
    def finished(self):
 
110
        self.pb.finished()
 
111
        self.hide()
 
112
        self.destroy()
 
113
 
 
114
    def clear(self):
 
115
        self.pb.clear()
 
116
        self.hide()
 
117
 
 
118
 
 
119
class ProgressPanel(Gtk.HBox):
141
120
 
142
121
    def __init__(self):
143
 
        super(ProgressPanel, self).__init__(Gtk.Orientation.HORIZONTAL, 5)
 
122
        super(ProgressPanel, self).__init__()
144
123
        image_loading = Gtk.Image.new_from_stock(Gtk.STOCK_REFRESH,
145
124
                                                 Gtk.IconSize.BUTTON)
146
125
        image_loading.show()
147
126
 
148
127
        self.pb = GtkProgressBar()
 
128
        self.set_spacing(5)
149
129
        self.set_border_width(5)
150
130
        self.pack_start(image_loading, False, False, 0)
151
131
        self.pack_start(self.pb, True, True, 0)
152
132
 
 
133
    def tick(self, *args, **kwargs):
 
134
        self.show_all()
 
135
        self.pb.tick(*args, **kwargs)
 
136
 
 
137
    def update(self, *args, **kwargs):
 
138
        self.show_all()
 
139
        self.pb.update(*args, **kwargs)
 
140
 
 
141
    def finished(self):
 
142
        self.hide()
 
143
        self.pb.finished()
 
144
 
 
145
    def clear(self):
 
146
        self.hide()
 
147
        self.pb.clear()
 
148
 
153
149
 
154
150
class PasswordDialog(Gtk.Dialog):
155
151
    """ Prompt the user for a password. """
193
189
        dialog.destroy()
194
190
        return (response == Gtk.ResponseType.YES)
195
191
 
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
192
    def get_password(self, prompt='', **kwargs):
221
193
        """Prompt the user for a password.
222
194
 
237
209
            return None
238
210
 
239
211
    def _progress_all_finished(self):
240
 
        """See UIFactory._progress_all_finished."""
 
212
        """See UIFactory._progress_all_finished"""
241
213
        pbw = self._progress_bar_widget
242
214
        if pbw:
243
215
            pbw.finished()
244
216
 
245
 
    def _ensure_progress_widget(self):
 
217
    def _progress_updated(self, task):
 
218
        """See UIFactory._progress_updated"""
246
219
        if self._progress_bar_widget is None:
247
 
            # Default to a window since nobody gave us a better means to report
 
220
            # Default to a window since nobody gave us a better mean to report
248
221
            # progress.
249
222
            self.set_progress_bar_widget(ProgressBarWindow())
250
 
 
251
 
    def _progress_updated(self, task):
252
 
        """See UIFactory._progress_updated."""
253
 
        self._ensure_progress_widget()
254
223
        self._progress_bar_widget.update(task.msg,
255
224
                                         task.current_cnt, task.total_cnt)
256
225
 
257
226
    def report_transport_activity(self, transport, byte_count, direction):
258
 
        """See UIFactory.report_transport_activity."""
259
 
        self._ensure_progress_widget()
 
227
        """See UIFactory.report_transport_activity"""
260
228
        self._progress_bar_widget.tick()