00001 ############################################################################## 00002 # 00003 # Copyright (c) 2008 Albert Cervera i Areny <albert@nan-tic.com> 00004 # 00005 # WARNING: This program as such is intended to be used by professional 00006 # programmers who take the whole responsability of assessing all potential 00007 # consequences resulting from its eventual inadequacies and bugs 00008 # End users who are looking for a ready-to-use solution with commercial 00009 # garantees and support are strongly adviced to contract a Free Software 00010 # Service Company 00011 # 00012 # This program is Free Software; you can redistribute it and/or 00013 # modify it under the terms of the GNU General Public License 00014 # as published by the Free Software Foundation; either version 2 00015 # of the License, or (at your option) any later version. 00016 # 00017 # This program is distributed in the hope that it will be useful, 00018 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 # GNU General Public License for more details. 00021 # 00022 # You should have received a copy of the GNU General Public License 00023 # along with this program; if not, write to the Free Software 00024 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00025 # 00026 ############################################################################## 00027 00028 ## @brief This class encapsulates the view types and ids handling for Screen. 00029 # 00030 # Model view definitions have an unintuitive way of handling which views should 00031 # be shown. There are two ways of specifying it: view types and ids. The problem 00032 # is that they can be combined. For example: 00033 # types = ['form','tree'] and ids = [24, False] 00034 # What this class will do is convert those two lists into one more intuitive list: 00035 # views = [24, 'tree'] 00036 # 00037 # You can initialize this class with values comming from a view definition using 00038 # the setup function and then pickup each view. Example of usage: 00039 # queue = ViewQueue() 00040 # queue.setup( ['form','tree'], [24, False] ) 00041 # if queue.isType(): 00042 # useViewAsAType( queue.next() ) 00043 00044 class ViewQueue: 00045 def __init__(self): 00046 self._mixed = [] 00047 00048 ## @brief Initializes the queue with types and ids view definitions 00049 def setup(self, types, ids): 00050 if types == None: 00051 types = ['form', 'tree'] 00052 if ids == None: 00053 ids = [] 00054 # Merge lists 00055 self._views = [] 00056 self._mixed = [] 00057 for x in range(max(len(types),len(ids))): 00058 if x < len(ids) and ids[x]: 00059 self._views.append( ids[x] ) 00060 elif x < len(types): 00061 self._views.append( types[x] ) 00062 00063 if x < len(ids): 00064 id = ids[x] 00065 else: 00066 id = False 00067 if x < len(types): 00068 type = types[x] 00069 else: 00070 type = False 00071 self._mixed.append( (id, type) ) 00072 00073 ## @brief Initializes the queue with a given list of view types. 00074 # If types is None then the list is initialized to the default ['form', 'tree'] 00075 def setViewTypes(self, types): 00076 if types == None: 00077 self._views = ['form', 'tree'] 00078 else: 00079 self._views = types 00080 self._mixed = [ (False, x) for x in self._views ] 00081 00082 ## @brief Initializes the queue with a given list of view ids. 00083 def setViewIds(self, ids): 00084 self._views = ids 00085 self._mixed = [ (x, False) for x in self._views ] 00086 00087 ## @brief Returns True if the next element is a view type. 00088 def isType(self): 00089 v = self._views[0] 00090 if isinstance(v,int): 00091 return False 00092 else: 00093 return True 00094 00095 ## @brief Returns True if the next element is a view id. 00096 def isId(self): 00097 return not self.isType() 00098 00099 ## @brief Returns True if the queue is empty. 00100 def isEmpty(self): 00101 return len(self._views) == 0 00102 00103 ## @brief Returns the next element of the queue. 00104 # 00105 # If the queue is already empty, it will rise an exception. 00106 def next(self): 00107 self._views.pop(0) 00108 return self._mixed.pop(0) 00109 00110 def viewFromType(self, type): 00111 for view in self._mixed: 00112 if view[1] == type: 00113 return view 00114 return None, None 00115 00116 def indexFromType(self, type): 00117 index = 0 00118 for view in self._mixed: 00119 if view[1] == type: 00120 return index 00121 index += 1 00122 return -1 00123 00124 def typeFromIndex(self, index): 00125 return self._mixed[ index ][1] 00126 00127 def count(self): 00128 return len(self._mixed) 00129 00130 def addViewType(self, type): 00131 self._mixed.append( (False, type) ) 00132 00133 def typeExists(self, type): 00134 return self.indexFromType( type ) != -1 00135 00136 # vim:noexpandtab: