~ubuntu-branches/ubuntu/trusty/groundcontrol/trusty-proposed

« back to all changes in this revision

Viewing changes to GroundControl/gtkcommon.py

  • Committer: Bazaar Package Importer
  • Author(s): Luke Faraone
  • Date: 2010-02-07 18:26:54 UTC
  • Revision ID: james.westby@ubuntu.com-20100207182654-u0n26lkazgfog4et
Tags: upstream-1.5
ImportĀ upstreamĀ versionĀ 1.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright 2010 Martin Owens
 
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 3 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, see <http://www.gnu.org/licenses/>
 
16
#
 
17
"""
 
18
Sets up some common gtk windows for generic use.
 
19
"""
 
20
 
 
21
import os
 
22
import time
 
23
import logging
 
24
 
 
25
from GroundControl.gtkviews import ThreadedWindow, GtkApp, Window, ChildWindow
 
26
 
 
27
class StatusWindow(ThreadedWindow):
 
28
    """Display a status bar to the user"""
 
29
    name = 'status_window'
 
30
 
 
31
    def load(self, *args, **kwargs):
 
32
        """Load our status window"""
 
33
        # hopefully filter out variables
 
34
        self.gapp.load_vars(args, kwargs)
 
35
        self.window.set_title(self.gapp.task_name)
 
36
        self.update()
 
37
        self._close_thread = False
 
38
        super(StatusWindow, self).load(*args, **kwargs)
 
39
 
 
40
    def inital_thread(self):
 
41
        """Pass on the action"""
 
42
        self.gapp.do_work()
 
43
 
 
44
    def thread_exited(self):
 
45
        """What to do when a thread exits"""
 
46
        self.done = True
 
47
        self.destroy_window()
 
48
 
 
49
    def update(self, value=None, name=None, transport=None):
 
50
        """Update gtk after some time"""
 
51
        # Update our progress bar and make sure the widgets still exist
 
52
        if name and self.widget('label'):
 
53
            self.widget('label').set_markup('<b>%s</b>' % name)
 
54
        if value != None and self.widget('progress'):
 
55
            self.widget('progress').set_fraction(value)
 
56
        if self.widget('transport'):
 
57
            if transport:
 
58
                self.widget('transport').set_text(transport)
 
59
            else:
 
60
                self.widget('transport').set_text('')
 
61
 
 
62
    def pop_error(self, error, title=None):
 
63
        """Popup a Gtk Error message box of some kind"""
 
64
        PopupApp(title=unicode(title), message=unicode(error))
 
65
 
 
66
 
 
67
class PromptWindow(ChildWindow):
 
68
    """A simple prompt window"""
 
69
    name = 'prompt'
 
70
 
 
71
    def load(self, title, label):
 
72
        """what to do when we load"""
 
73
        self.window.set_title(title)
 
74
        self.widget('label').set_text(label)
 
75
 
 
76
    def hold(self):
 
77
        """Hold the prompt until answered"""
 
78
        while not self.dead:
 
79
            time.sleep(0.5)
 
80
        return self.done
 
81
 
 
82
 
 
83
class StatusApp(GtkApp):
 
84
    """An application for showing any status"""
 
85
    gtkfile = 'common.glade'
 
86
    windows = [ StatusWindow, PromptWindow ]
 
87
    task_name = _("Unknown Action")
 
88
 
 
89
    def do_work(self, *args, **kwargs):
 
90
        """Replace this method in your own status updates"""
 
91
        raise NotImplimentedError("Replace do_work in your status window")
 
92
 
 
93
    def load_vars(self):
 
94
        """Strip out any useful variables for the status window"""
 
95
        # This is a bit of a juggling act, perhaps someone has a better idea
 
96
        # That we can move towards for this.
 
97
        pass
 
98
 
 
99
    @property
 
100
    def window(self):
 
101
        """Return the status window"""
 
102
        # Get the right window by name, more than one window could be loaded
 
103
        return self._loaded['status_window']
 
104
 
 
105
    def update(self, *args, **kwargs):
 
106
        """Pass an update call back to the window in a thread friendly way"""
 
107
        # We're going to make sure that updates don't collide
 
108
        kwargs['unique_call'] = True
 
109
        # Now call the threaded caller to do an update
 
110
        self.window.call('update', *args, **kwargs )
 
111
 
 
112
    def pop_error(self, title, error):
 
113
        """Call window's creation of popup windows"""
 
114
        return self.window.call('pop_error', error, title)
 
115
 
 
116
class PopupWindow(Window):
 
117
    """A simple popup warning window"""
 
118
    name = 'popup'
 
119
 
 
120
    def load(self, *args, **kwargs):
 
121
        """Load the title, icon"""
 
122
        self.window.set_title(kwargs.pop('title', _('Message!')))
 
123
        message = kwargs.pop('message')
 
124
        logging.error(message)
 
125
        self.widget('label').set_text(message)
 
126
        super(PopupWindow, self).load(*args, **kwargs)
 
127
 
 
128
 
 
129
class PopupApp(GtkApp):
 
130
    """An application for showing any status"""
 
131
    gtkfile = 'common-popup.glade'
 
132
    windows = [ PopupWindow ]
 
133