/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
1
# Copyright (C) 2006 Szilveszter Farkas <szilveszter.farkas@gmail.com>
225 by Jelmer Vernooij
Add docstrings, remove unused code.
2
# Copyright (C) 2007 Jelmer Vernooij <jelmer@samba.org>
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
3
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
19
"""GTK UI
20
"""
21
776.2.2 by Curtis Hovey
Revert change.
22
from gi.repository import Gtk
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
23
24
from bzrlib.ui import UIFactory
25
26
776.1.2 by Curtis Hovey
Semi-stable push progress.
27
def main_iteration(function):
28
    def with_main_iteration(self, *args, **kwargs):
776.1.34 by Curtis Hovey
Added test for main_iteration() and fixed decorator to return the result.
29
        result = function(self, *args, **kwargs)
776.1.2 by Curtis Hovey
Semi-stable push progress.
30
        while Gtk.events_pending():
776.1.3 by Curtis Hovey
Abandon effort to make the progress bar spin. bzrlib lives to block
31
            Gtk.main_iteration_do(False)
776.1.34 by Curtis Hovey
Added test for main_iteration() and fixed decorator to return the result.
32
        return result
776.1.2 by Curtis Hovey
Semi-stable push progress.
33
    return with_main_iteration
34
35
776.2.6 by Curtis Hovey
Change PromptDialog to extend GtkMessageDialog that provides icons and text handling for
36
class PromptDialog(Gtk.MessageDialog):
724 by Jelmer Vernooij
Fix formatting, imports.
37
    """Prompt the user for a yes/no answer."""
38
776.2.6 by Curtis Hovey
Change PromptDialog to extend GtkMessageDialog that provides icons and text handling for
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)
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
43
44
776.2.7 by Curtis Hovey
Added InfoDialog and implemented show_message().
45
class InfoDialog(Gtk.MessageDialog):
46
    """Show the user an informational message."""
47
776.2.8 by Curtis Hovey
Added WarningDialog and implementd show_warning.
48
    MESSAGE_TYPE = Gtk.MessageType.INFO
49
776.2.7 by Curtis Hovey
Added InfoDialog and implemented show_message().
50
    def __init__(self, prompt, parent=None):
51
        super(InfoDialog, self).__init__(
52
            parent, Gtk.DialogFlags.DESTROY_WITH_PARENT,
776.2.8 by Curtis Hovey
Added WarningDialog and implementd show_warning.
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
776.2.7 by Curtis Hovey
Added InfoDialog and implemented show_message().
60
61
776.2.10 by Curtis Hovey
Added ErrorDialog and implented show_error.
62
class ErrorDialog(InfoDialog):
63
    """Show the user a warning message."""
64
65
    MESSAGE_TYPE = Gtk.MessageType.ERROR
66
67
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
68
class GtkProgressBar(Gtk.ProgressBar):
645.1.1 by Vincent Ladeuil
Fix #385191 by using the new progress reporting API.
69
70
    def __init__(self):
734.1.51 by Curtis Hovey
Fix the initializer for many classes.
71
        super(GtkProgressBar, self).__init__()
174 by Jelmer Vernooij
Implement simple GTK+ progress bars.
72
        self.set_fraction(0.0)
475.2.1 by Chad MILLER
Make "vizualize" use the GUI progress bar defined in the parent 'ui' module.
73
        self.current = None
74
        self.total = None
174 by Jelmer Vernooij
Implement simple GTK+ progress bars.
75
776.1.2 by Curtis Hovey
Semi-stable push progress.
76
    @main_iteration
174 by Jelmer Vernooij
Implement simple GTK+ progress bars.
77
    def tick(self):
645.1.2 by Vincent Ladeuil
Address Jelmer's review.
78
        self.show()
174 by Jelmer Vernooij
Implement simple GTK+ progress bars.
79
        self.pulse()
80
776.1.2 by Curtis Hovey
Semi-stable push progress.
81
    @main_iteration
511.5.6 by Jelmer Vernooij
Simplify progress bar code, use embedded progress bar inside viz window.
82
    def update(self, msg=None, current_cnt=None, total_cnt=None):
645.1.1 by Vincent Ladeuil
Fix #385191 by using the new progress reporting API.
83
        self.show()
533.10.1 by Jelmer Vernooij
Consider 0 a valid value for progress bars.
84
        if current_cnt is not None:
511.5.6 by Jelmer Vernooij
Simplify progress bar code, use embedded progress bar inside viz window.
85
            self.current = current_cnt
533.10.1 by Jelmer Vernooij
Consider 0 a valid value for progress bars.
86
        if total_cnt is not None:
511.5.6 by Jelmer Vernooij
Simplify progress bar code, use embedded progress bar inside viz window.
87
            self.total = total_cnt
174 by Jelmer Vernooij
Implement simple GTK+ progress bars.
88
        if msg is not None:
89
            self.set_text(msg)
475.2.1 by Chad MILLER
Make "vizualize" use the GUI progress bar defined in the parent 'ui' module.
90
        if None not in (self.current, self.total):
776.1.21 by Curtis Hovey
Added a test for the common case of update().
91
            fraction = float(self.current) / self.total
92
            if fraction < 0.0 or fraction > 1.0:
776.1.22 by Curtis Hovey
Added tests for partial and insane update() data.
93
                raise ValueError
776.1.21 by Curtis Hovey
Added a test for the common case of update().
94
            self.set_fraction(fraction)
174 by Jelmer Vernooij
Implement simple GTK+ progress bars.
95
776.1.2 by Curtis Hovey
Semi-stable push progress.
96
    @main_iteration
645.1.1 by Vincent Ladeuil
Fix #385191 by using the new progress reporting API.
97
    def finished(self):
776.1.23 by Curtis Hovey
Restore the check that progress bar ui methods clear the pending events to update the UI.
98
        self.set_fraction(0.0)
776.1.2 by Curtis Hovey
Semi-stable push progress.
99
        self.current = None
100
        self.total = None
645.1.1 by Vincent Ladeuil
Fix #385191 by using the new progress reporting API.
101
        self.hide()
102
103
    def clear(self):
776.1.2 by Curtis Hovey
Semi-stable push progress.
104
        self.finished()
645.1.1 by Vincent Ladeuil
Fix #385191 by using the new progress reporting API.
105
174 by Jelmer Vernooij
Implement simple GTK+ progress bars.
106
776.1.30 by Curtis Hovey
Use extracted ProgressContainerMixin to provide common GtkProgressBar methods.
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):
645.1.1 by Vincent Ladeuil
Fix #385191 by using the new progress reporting API.
128
129
    def __init__(self):
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
130
        super(ProgressBarWindow, self).__init__(type=Gtk.WindowType.TOPLEVEL)
174 by Jelmer Vernooij
Implement simple GTK+ progress bars.
131
        self.set_border_width(0)
132
        self.set_title("Progress")
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
133
        self.set_position(Gtk.WindowPosition.CENTER_ALWAYS)
645.1.1 by Vincent Ladeuil
Fix #385191 by using the new progress reporting API.
134
        self.pb = GtkProgressBar()
511.5.6 by Jelmer Vernooij
Simplify progress bar code, use embedded progress bar inside viz window.
135
        self.add(self.pb)
136
        self.resize(250, 15)
174 by Jelmer Vernooij
Implement simple GTK+ progress bars.
137
        self.set_resizable(False)
645.1.1 by Vincent Ladeuil
Fix #385191 by using the new progress reporting API.
138
511.5.6 by Jelmer Vernooij
Simplify progress bar code, use embedded progress bar inside viz window.
139
776.1.33 by Curtis Hovey
Updated ProgressPanel to Gtk 3.0 Gtk.Box.
140
class ProgressPanel(ProgressContainerMixin, Gtk.Box):
645.1.1 by Vincent Ladeuil
Fix #385191 by using the new progress reporting API.
141
511.5.6 by Jelmer Vernooij
Simplify progress bar code, use embedded progress bar inside viz window.
142
    def __init__(self):
776.1.33 by Curtis Hovey
Updated ProgressPanel to Gtk 3.0 Gtk.Box.
143
        super(ProgressPanel, self).__init__(Gtk.Orientation.HORIZONTAL, 5)
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
144
        image_loading = Gtk.Image.new_from_stock(Gtk.STOCK_REFRESH,
145
                                                 Gtk.IconSize.BUTTON)
511.5.6 by Jelmer Vernooij
Simplify progress bar code, use embedded progress bar inside viz window.
146
        image_loading.show()
645.1.1 by Vincent Ladeuil
Fix #385191 by using the new progress reporting API.
147
148
        self.pb = GtkProgressBar()
149
        self.set_border_width(5)
734.1.24 by Curtis Hovey
Updated most of BranchView to gtk3.
150
        self.pack_start(image_loading, False, False, 0)
151
        self.pack_start(self.pb, True, True, 0)
511.5.6 by Jelmer Vernooij
Simplify progress bar code, use embedded progress bar inside viz window.
152
174 by Jelmer Vernooij
Implement simple GTK+ progress bars.
153
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
154
class PasswordDialog(Gtk.Dialog):
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
155
    """ Prompt the user for a password. """
645.1.2 by Vincent Ladeuil
Address Jelmer's review.
156
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
157
    def __init__(self, prompt):
734.1.51 by Curtis Hovey
Fix the initializer for many classes.
158
        super(PasswordDialog, self).__init__()
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
159
160
        label = Gtk.Label(label=prompt)
734.1.51 by Curtis Hovey
Fix the initializer for many classes.
161
        self.get_content_area().pack_start(label, True, True, 10)
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
162
163
        self.entry = Gtk.Entry()
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
164
        self.entry.set_visibility(False)
734.1.53 by Curtis Hovey
Fixed packing args.
165
        self.get_content_area().pack_end(self.entry, False, False, 10)
645.1.1 by Vincent Ladeuil
Fix #385191 by using the new progress reporting API.
166
734.1.51 by Curtis Hovey
Fix the initializer for many classes.
167
        self.get_content_area().show_all()
645.1.1 by Vincent Ladeuil
Fix #385191 by using the new progress reporting API.
168
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
169
        self.add_buttons(Gtk.STOCK_OK, Gtk.ResponseType.OK,
170
                         Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL)
645.1.1 by Vincent Ladeuil
Fix #385191 by using the new progress reporting API.
171
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
172
    def _get_passwd(self):
173
        return self.entry.get_text()
174
175
    passwd = property(_get_passwd)
176
177
178
class GtkUIFactory(UIFactory):
0.13.12 by Jelmer Vernooij
Bunch of other small updates, add more items to
179
    """A UI factory for GTK user interfaces."""
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
180
645.1.1 by Vincent Ladeuil
Fix #385191 by using the new progress reporting API.
181
    def __init__(self):
182
        """Create a GtkUIFactory"""
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
183
        super(GtkUIFactory, self).__init__()
645.1.2 by Vincent Ladeuil
Address Jelmer's review.
184
        self.set_progress_bar_widget(None)
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
185
645.1.3 by Vincent Ladeuil
Cleanup.
186
    def set_progress_bar_widget(self, widget):
187
        self._progress_bar_widget = widget
188
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
189
    def get_boolean(self, prompt):
190
        """GtkDialog with yes/no answers"""
191
        dialog = PromptDialog(prompt)
192
        response = dialog.run()
193
        dialog.destroy()
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
194
        return (response == Gtk.ResponseType.YES)
645.1.1 by Vincent Ladeuil
Fix #385191 by using the new progress reporting API.
195
776.2.7 by Curtis Hovey
Added InfoDialog and implemented show_message().
196
    def show_message(self, msg):
197
        """See UIFactory.show_message."""
198
        dialog = InfoDialog(msg)
199
        dialog.run()
200
        dialog.destroy()
201
776.2.8 by Curtis Hovey
Added WarningDialog and implementd show_warning.
202
    def show_warning(self, msg):
776.2.10 by Curtis Hovey
Added ErrorDialog and implented show_error.
203
        """See UIFactory.show_warning."""
776.2.8 by Curtis Hovey
Added WarningDialog and implementd show_warning.
204
        dialog = WarningDialog(msg)
205
        dialog.run()
206
        dialog.destroy()
207
776.2.10 by Curtis Hovey
Added ErrorDialog and implented show_error.
208
    def show_error(self, msg):
209
        """See UIFactory.show_error."""
210
        dialog = ErrorDialog(msg)
211
        dialog.run()
212
        dialog.destroy()
213
776.2.11 by Curtis Hovey
Added show_user_warning implementation.
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
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
220
    def get_password(self, prompt='', **kwargs):
221
        """Prompt the user for a password.
222
223
        :param prompt: The prompt to present the user
224
        :param kwargs: Arguments which will be expanded into the prompt.
225
                       This lets front ends display different things if
226
                       they so choose.
776.1.2 by Curtis Hovey
Semi-stable push progress.
227
        :return: The password string, return None if the user
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
228
                 canceled the request.
229
        """
230
        dialog = PasswordDialog(prompt % kwargs)
231
        response = dialog.run()
232
        passwd = dialog.passwd
233
        dialog.destroy()
734.1.1 by Curtis Hovey
Mechanical changes made by pygi.convert.sh.
234
        if response == Gtk.ResponseType.OK:
0.8.11 by Szilveszter Farkas (Phanatic)
2006-07-16 Szilveszter Farkas <Szilveszter.Farkas@gmail.com>
235
            return passwd
236
        else:
237
            return None
238
645.1.1 by Vincent Ladeuil
Fix #385191 by using the new progress reporting API.
239
    def _progress_all_finished(self):
776.2.3 by Curtis Hovey
Fixed text.
240
        """See UIFactory._progress_all_finished."""
645.1.1 by Vincent Ladeuil
Fix #385191 by using the new progress reporting API.
241
        pbw = self._progress_bar_widget
242
        if pbw:
243
            pbw.finished()
244
776.1.14 by Curtis Hovey
Added tests for report_transport_activity().
245
    def _ensure_progress_widget(self):
645.1.2 by Vincent Ladeuil
Address Jelmer's review.
246
        if self._progress_bar_widget is None:
776.1.14 by Curtis Hovey
Added tests for report_transport_activity().
247
            # Default to a window since nobody gave us a better means to report
645.1.2 by Vincent Ladeuil
Address Jelmer's review.
248
            # progress.
249
            self.set_progress_bar_widget(ProgressBarWindow())
776.1.14 by Curtis Hovey
Added tests for report_transport_activity().
250
251
    def _progress_updated(self, task):
776.2.3 by Curtis Hovey
Fixed text.
252
        """See UIFactory._progress_updated."""
776.1.14 by Curtis Hovey
Added tests for report_transport_activity().
253
        self._ensure_progress_widget()
645.1.2 by Vincent Ladeuil
Address Jelmer's review.
254
        self._progress_bar_widget.update(task.msg,
255
                                         task.current_cnt, task.total_cnt)
645.1.1 by Vincent Ladeuil
Fix #385191 by using the new progress reporting API.
256
776.1.2 by Curtis Hovey
Semi-stable push progress.
257
    def report_transport_activity(self, transport, byte_count, direction):
776.2.3 by Curtis Hovey
Fixed text.
258
        """See UIFactory.report_transport_activity."""
776.1.14 by Curtis Hovey
Added tests for report_transport_activity().
259
        self._ensure_progress_widget()
776.1.2 by Curtis Hovey
Semi-stable push progress.
260
        self._progress_bar_widget.tick()