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