src/extras/examples/GUI_example_3.py
changeset 0 ca70ae20a155
equal deleted inserted replaced
-1:000000000000 0:ca70ae20a155
       
     1 # Advanced GUI example
       
     2 
       
     3 # This nonfunctional sample code is based on a simple application for
       
     4 # accessing a to-do list.  The details of that particular application
       
     5 # have been edited out.
       
     6 #
       
     7 # Copyright (c) 2005 Nokia Corporation
       
     8 #
       
     9 # Licensed under the Apache License, Version 2.0 (the "License");
       
    10 # you may not use this file except in compliance with the License.
       
    11 # You may obtain a copy of the License at
       
    12 #
       
    13 #     http://www.apache.org/licenses/LICENSE-2.0
       
    14 #
       
    15 # Unless required by applicable law or agreed to in writing, software
       
    16 # distributed under the License is distributed on an "AS IS" BASIS,
       
    17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    18 # See the License for the specific language governing permissions and
       
    19 # limitations under the License.
       
    20 
       
    21 
       
    22 import e32
       
    23 import appuifw
       
    24 
       
    25 from MyDataAccess import MyDataAccess
       
    26 
       
    27 e32.ao_yield()
       
    28 
       
    29 def format(item):
       
    30     # Format the item as a short unicode string.
       
    31     return u""                          # omitted
       
    32 
       
    33 class MyApp:
       
    34     def __init__(self):
       
    35         self.lock = e32.Ao_lock()
       
    36 
       
    37         self.old_title = appuifw.app.title
       
    38         appuifw.app.title = u"My Application"
       
    39 
       
    40         self.exit_flag = False
       
    41         appuifw.app.exit_key_handler = self.abort
       
    42 
       
    43         self.data = []
       
    44         appuifw.app.body = appuifw.Listbox([u"Loading..."], self.handle_modify)
       
    45 
       
    46         self.menu_add = (u"Add", self.handle_add)
       
    47         self.menu_del = (u"Delete", self.handle_delete)
       
    48         appuifw.app.menu = [] # First call to refresh() will fill in the menu.
       
    49 
       
    50     def connect(self, host):
       
    51         self.db = MyDataAccess(host)
       
    52         self.db.listen(self.notify) # Set up callback for change notifications.
       
    53 
       
    54     def loop(self):
       
    55         try:
       
    56             self.lock.wait()
       
    57             while not self.exit_flag:
       
    58                 self.refresh()
       
    59                 self.lock.wait()
       
    60         finally:
       
    61             self.db.close()
       
    62 
       
    63     def close(self):
       
    64         appuifw.app.menu = []
       
    65         appuifw.app.body = None
       
    66         appuifw.app.exit_key_handler = None
       
    67         appuifw.app.title = self.old_title
       
    68 
       
    69     def abort(self):
       
    70         # Exit-key handler.
       
    71         self.exit_flag = True
       
    72         self.lock.signal()
       
    73 
       
    74     def notify(self, in_sync):
       
    75         # Handler for database change notifications.
       
    76         if in_sync:
       
    77             self.lock.signal()
       
    78 
       
    79     def refresh(self):
       
    80         # Note selected item.
       
    81         current_item = self.get_current_item()
       
    82 
       
    83         # Get updated data.
       
    84         self.data = self.db.get_data()
       
    85 
       
    86         if not self.data:
       
    87             content = [u"(Empty)"]
       
    88         else:
       
    89             content = [format(item) for item in self.data]
       
    90 
       
    91         if current_item in self.data:
       
    92             # Update the displayed data, retaining the previous selection.
       
    93             index = self.data.index(current_item)
       
    94             appuifw.app.body.set_list(content, index)
       
    95         else:
       
    96             # Previously selected item is no longer present, so allow
       
    97             # the selection to be reset.
       
    98             appuifw.app.body.set_list(content)
       
    99 
       
   100         if not self.data:
       
   101             appuifw.app.menu = [self.menu_add]
       
   102         else:
       
   103             appuifw.app.menu = [self.menu_add, self.menu_del]
       
   104 
       
   105     def handle_modify(self):
       
   106         item = self.get_current_item()
       
   107         if item is not None:
       
   108             # Display data in Form for user to edit.
       
   109             # Save modified record in database.
       
   110             pass                        # omitted
       
   111 
       
   112     def handle_add(self):
       
   113         new_item = self.edit_item(ToDoItem())
       
   114         if new_item is not None:
       
   115             # User enters new data into Form.
       
   116             # Save new record in database.
       
   117             pass                        # omitted
       
   118 
       
   119     def handle_delete(self):
       
   120         item = self.get_current_item()
       
   121         if item is not None:
       
   122             # Remove record from database.
       
   123             pass                        # omitted
       
   124 
       
   125     def get_current_item(self):
       
   126         # Return currently selected item, or None if the list is empty.
       
   127         if not self.data:
       
   128             return None
       
   129         else:
       
   130             current = appuifw.app.body.current()
       
   131             return self.data[current]
       
   132 
       
   133 def main():
       
   134     app = MyApp()
       
   135     try:
       
   136         hosts = [u"some.foo.com", u"other.foo.com"]
       
   137         i = appuifw.popup_menu(hosts, u"Select server:")
       
   138         if i is not None:
       
   139             app.connect(hosts[i])
       
   140             app.loop()
       
   141     finally:
       
   142         app.close()
       
   143 
       
   144 if __name__ == "__main__":
       
   145     main()