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