1 #!/usr/bin/env python 2 3 import os 4 5 class ConfiguratorResource: 6 7 # Standard attributes. 8 9 resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") 10 11 design_resources = { 12 "configuration" : "config.ui" 13 } 14 15 widget_resources = { 16 "hard-disk" : ("config_hard_disk.ui", "hard_disk"), 17 "memory-unit" : ("config_memory_unit.ui", "memory_unit"), 18 "storage-unit" : ("config_storage_unit.ui", "storage_unit") 19 #"hard-disks" : ("config_hard_disks.ui", "hard_disks"), 20 #"memory-units" : ("config_memory_units.ui", "memory_units"), 21 #"storage-units" : ("config_storage_units.ui", "storage_units") 22 } 23 24 document_resources = { 25 "base-system" : "config_base_system.xml", 26 "cpu" : "config_cpu.xml", 27 "hard-disk" : "config_hard_disk.xml", 28 "keyboard" : "config_keyboard.xml", 29 "memory-unit" : "config_memory_unit.xml", 30 "mouse" : "config_mouse.xml", 31 "screen" : "config_screen.xml", 32 "storage-unit" : "config_storage_unit.xml" 33 } 34 35 # Initialisation. 36 37 def __init__(self, *args, **kw): 38 pass 39 40 def form_init(self): 41 42 self.reset_collection(self.child("hard_disks")) 43 self.reset_collection(self.child("memory_units")) 44 self.reset_collection(self.child("storage_units")) 45 46 def form_populate(self): 47 48 # Get field data. 49 # NOTE: This would be done for whole page updates in a Web application. 50 51 self.populate_list(self.child("base_system"), self.get_elements("base-system")) 52 self.populate_list(self.child("keyboard"), self.get_elements("keyboard")) 53 self.populate_list(self.child("mouse"), self.get_elements("mouse")) 54 self.populate_list(self.child("screen"), self.get_elements("screen")) 55 56 # General functionality. 57 58 def form_refresh(self, current_text=None): 59 60 # Ensure consistency. 61 # NOTE: This would be done for whole page updates in a Web application. 62 # NOTE: This would also be done for page updates where the information 63 # NOTE: involved was important. 64 65 current_text = current_text or self.child("base_system").currentText() 66 67 # Find the CPU socket and the interface of the current base system. 68 cpu_socket = None 69 interface = None 70 for element in self.get_elements("base-system"): 71 text = element.getAttribute("value") 72 if text == current_text: 73 cpu_socket = element.getAttribute("cpu-socket") 74 interface = element.getAttribute("interface") 75 76 # Find all valid CPUs. 77 valid = [] 78 for element in self.get_elements("cpu"): 79 if not element.hasAttribute("cpu-socket") or element.getAttribute("cpu-socket") == cpu_socket: 80 valid.append(element) 81 self.populate_list(self.child("cpu"), valid) 82 83 # Find all valid hard disks. 84 valid = [] 85 for element in self.get_elements("hard-disk"): 86 if not element.hasAttribute("interface") or element.getAttribute("interface") == interface: 87 valid.append(element) 88 for hard_disk_value in self.factory.find_widgets(self, "hard_disk_value"): 89 self.populate_list(hard_disk_value, valid) 90 91 # Find memory units. 92 for memory_unit_value in self.factory.find_widgets(self, "memory_unit_value"): 93 self.populate_list(memory_unit_value, self.get_elements("memory-unit")) 94 95 # Find storage units. 96 for storage_unit_value in self.factory.find_widgets(self, "storage_unit_value"): 97 self.populate_list(storage_unit_value, self.get_elements("storage-unit")) 98 99 # Slots. 100 101 def baseSystemChanged(self, current_text): 102 self.request_refresh(current_text) 103 104 def addHardDisk(self): 105 #hard_disks = self.prepare_widget("configuration", "hard-disks") 106 #tab_pages = hard_disks.child("tab pages") 107 #tab = tab_pages.child("tab") 108 #self.child("hard_disks").addTab(tab, hard_disks.tabLabel(tab)) 109 #self.factory.connect(tab, self) 110 hard_disk = self.prepare_widget("configuration", "hard-disk", self.child("hard_disks")) 111 self.child("hard_disks").layout().add(hard_disk) 112 hard_disk.show() 113 self.factory.connect(hard_disk, self) 114 115 # Perform the consistency check. 116 # NOTE: This is not as efficient as it could be since the general check 117 # NOTE: refreshes all fields, not just newly added ones. 118 self.request_refresh() 119 120 def addMemoryUnit(self): 121 #memory_units = self.prepare_widget("configuration", "memory-units") 122 #tab_pages = memory_units.child("tab pages") 123 #tab = tab_pages.child("tab") 124 #self.child("memory_units").addTab(tab, memory_units.tabLabel(tab)) 125 #self.factory.connect(tab, self) 126 memory_unit = self.prepare_widget("configuration", "memory-unit", self.child("memory_units")) 127 self.child("memory_units").layout().add(memory_unit) 128 memory_unit.show() 129 self.factory.connect(memory_unit, self) 130 131 # Perform the consistency check. 132 # NOTE: This is not as efficient as it could be since the general check 133 # NOTE: refreshes all fields, not just newly added ones. 134 self.request_refresh() 135 136 def addStorageUnit(self): 137 #storage_units = self.prepare_widget("configuration", "storage-units") 138 #tab_pages = storage_units.child("tab pages") 139 #tab = tab_pages.child("tab") 140 #self.child("storage_units").addTab(tab, storage_units.tabLabel(tab)) 141 #self.factory.connect(tab, self) 142 storage_unit = self.prepare_widget("configuration", "storage-unit", self.child("storage_units")) 143 self.child("storage_units").layout().add(storage_unit) 144 storage_unit.show() 145 self.factory.connect(storage_unit, self) 146 147 # Perform the consistency check. 148 # NOTE: This is not as efficient as it could be since the general check 149 # NOTE: refreshes all fields, not just newly added ones. 150 self.request_refresh() 151 152 def removeHardDisk(self): 153 #page = self.hard_disks.currentPage() 154 #self.hard_disks.removePage(page) 155 #page.deleteLater() 156 remove_hard_disk = self.sender() 157 hard_disk = remove_hard_disk.parent() 158 self.child("hard_disks").layout().remove(hard_disk) 159 hard_disk.deleteLater() 160 161 def removeMemoryUnit(self): 162 #page = self.memory_units.currentPage() 163 #self.memory_units.removePage(page) 164 #page.deleteLater() 165 remove_memory_unit = self.sender() 166 memory_unit = remove_memory_unit.parent() 167 self.child("memory_units").layout().remove(memory_unit) 168 memory_unit.deleteLater() 169 170 def removeStorageUnit(self): 171 #page = self.storage_units.currentPage() 172 #self.storage_units.removePage(page) 173 #page.deleteLater() 174 remove_storage_unit = self.sender() 175 storage_unit = remove_storage_unit.parent() 176 self.child("storage_units").layout().remove(storage_unit) 177 storage_unit.deleteLater() 178 179 def updateConfig(self): 180 self.form_refresh() 181 182 def exportConfig(self): 183 print "configuration.exportConfig(): Not implemented yet" 184 185 def get_resource(resource_type, *args, **kw): 186 187 if resource_type == "PyQt": 188 import XSLForms.Resources.PyQtResources 189 import QtConfigurator.Forms 190 class Configurator(ConfiguratorResource, QtConfigurator.Forms.Configurator, XSLForms.Resources.PyQtResources.XSLFormsResource): 191 def __init__(self, *args, **kw): 192 QtConfigurator.Forms.Configurator.__init__(self, *args, **kw) 193 XSLForms.Resources.PyQtResources.XSLFormsResource.__init__(self, "configuration") 194 ConfiguratorResource.__init__(self, *args, **kw) 195 resource = Configurator(*args, **kw) 196 resource.form_init() 197 resource.form_populate() 198 199 else: 200 import XSLForms.Resources.PyQtWebResources 201 class Configurator(ConfiguratorResource, XSLForms.Resources.PyQtWebResources.XSLFormsResource): 202 def __init__(self, *args, **kw): 203 XSLForms.Resources.PyQtWebResources.XSLFormsResource.__init__(self, "configuration") 204 ConfiguratorResource.__init__(self, *args, **kw) 205 resource = Configurator(*args, **kw) 206 # Do not initialise or populate the resource here: both happen when a 207 # Web request is received (initialisation when no form document is 208 # found; population when a form document is prepared for output). 209 210 return resource 211 212 # vim: tabstop=4 expandtab shiftwidth=4