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