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