1.1 --- a/examples/Common/QtConfigurator/__init__.py Mon Oct 24 21:35:13 2005 +0000
1.2 +++ b/examples/Common/QtConfigurator/__init__.py Tue Oct 25 01:13:16 2005 +0000
1.3 @@ -1,48 +1,169 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 import QtConfigurator.Forms
1.7 import XSLForms.Prepare
1.8 import factory
1.9 import qtui
1.10 +import qtxmldom
1.11 import os
1.12
1.13 class Configurator(QtConfigurator.Forms.Configurator):
1.14
1.15 + # Standard attributes.
1.16 +
1.17 resource_dir = os.path.join(os.path.split(__file__)[0], "Resources")
1.18 - ui_filename = os.path.join(resource_dir, "config.ui")
1.19 - ui_hard_disks_filename = os.path.join(resource_dir, "config_hard_disks.ui")
1.20 - ui_memory_units_filename = os.path.join(resource_dir, "config_memory_units.ui")
1.21 - ui_storage_units_filename = os.path.join(resource_dir, "config_storage_units.ui")
1.22 +
1.23 + template_resources = {
1.24 + "configuration" : "config.ui"
1.25 + }
1.26 +
1.27 + widget_resources = {
1.28 + "hard-disks" : ("config_hard_disks.ui", "hard_disks"),
1.29 + "memory-units" : ("config_memory_units.ui", "memory_units"),
1.30 + "storage-units" : ("config_storage_units.ui", "storage_units")
1.31 + }
1.32 +
1.33 + document_resources = {
1.34 + "base-system" : "config_base_system.xml",
1.35 + "cpu" : "config_cpu.xml",
1.36 + "hard-disk" : "config_hard_disk.xml",
1.37 + "keyboard" : "config_keyboard.xml",
1.38 + "mouse" : "config_mouse.xml",
1.39 + "screen" : "config_screen.xml"
1.40 + }
1.41 +
1.42 + # Helper methods.
1.43 + # NOTE: Should be moved to a superclass.
1.44 +
1.45 + def prepare_document(self, document_identifier):
1.46 + filename = self.document_resources[document_identifier]
1.47 + return os.path.abspath(os.path.join(self.resource_dir, filename))
1.48 +
1.49 + def get_document(self, document_identifier):
1.50 + return qtxmldom.parse(self.prepare_document(document_identifier))
1.51 +
1.52 + def get_elements(self, document_identifier):
1.53 + doc = self.get_document(document_identifier)
1.54 + return doc.getElementsByTagName(document_identifier + "-enum")
1.55 +
1.56 + def populate_list(self, field, elements):
1.57 + current_text = field.currentText()
1.58 + while field.count() > 0:
1.59 + field.removeItem(0)
1.60 + item = 0
1.61 + set = 0
1.62 + for element in elements:
1.63 + text = element.getAttribute("value")
1.64 + field.insertItem(text)
1.65 + if text == current_text:
1.66 + field.setCurrentItem(item)
1.67 + set = 1
1.68 + item += 1
1.69 + if not set:
1.70 + field.setCurrentItem(0)
1.71 +
1.72 + def prepare_template(self, template_identifier):
1.73 + filename = self.template_resources[template_identifier]
1.74 + return os.path.abspath(os.path.join(self.resource_dir, filename))
1.75 +
1.76 + def prepare_widget(self, template_identifier, widget_identifier):
1.77 + template_path = self.prepare_template(template_identifier)
1.78 + fragment_name, widget_name = self.widget_resources[widget_identifier]
1.79 + fragment_path = os.path.abspath(os.path.join(self.resource_dir, fragment_name))
1.80 + XSLForms.Prepare.ensure_qt_fragment(template_path, fragment_path, widget_name)
1.81 + return qtui.QWidgetFactory.create(fragment_path)
1.82 +
1.83 + # Initialisation.
1.84
1.85 def __init__(self, *args, **kw):
1.86 QtConfigurator.Forms.Configurator.__init__(self, *args, **kw)
1.87 - self.factory = factory.Factory(self.ui_filename)
1.88 - XSLForms.Prepare.ensure_qt_fragment(self.ui_filename, self.ui_hard_disks_filename, "hard_disks")
1.89 - XSLForms.Prepare.ensure_qt_fragment(self.ui_filename, self.ui_memory_units_filename, "memory_units")
1.90 - XSLForms.Prepare.ensure_qt_fragment(self.ui_filename, self.ui_storage_units_filename, "storage_units")
1.91 + self.factory = factory.Factory(self.prepare_template("configuration"))
1.92 +
1.93 + # Get field data.
1.94 + # NOTE: This would be done for whole page updates in a Web application.
1.95 +
1.96 + self.populate_list(self.base_system, self.get_elements("base-system"))
1.97 + self.populate_list(self.keyboard, self.get_elements("keyboard"))
1.98 + self.populate_list(self.mouse, self.get_elements("mouse"))
1.99 + self.populate_list(self.screen, self.get_elements("screen"))
1.100 +
1.101 + # General functionality.
1.102 +
1.103 + def refresh(self, current_text=None):
1.104 +
1.105 + # Ensure consistency.
1.106 + # NOTE: This would be done for whole page updates in a Web application.
1.107 + # NOTE: This would also be done for page updates where the information
1.108 + # NOTE: involved was important.
1.109 +
1.110 + current_text = current_text or self.base_system.currentText()
1.111
1.112 - def baseSystemChanged(self):
1.113 - print self.base_system.currentItem()
1.114 + # Find the CPU socket and the interface of the current base system.
1.115 + cpu_socket = None
1.116 + interface = None
1.117 + for element in self.get_elements("base-system"):
1.118 + text = element.getAttribute("value")
1.119 + if text == current_text:
1.120 + cpu_socket = element.getAttribute("cpu-socket")
1.121 + interface = element.getAttribute("interface")
1.122 +
1.123 + # Find all valid CPUs.
1.124 + valid = []
1.125 + for element in self.get_elements("cpu"):
1.126 + if not element.hasAttribute("cpu-socket") or element.getAttribute("cpu-socket") == cpu_socket:
1.127 + valid.append(element)
1.128 + self.populate_list(self.cpu, valid)
1.129 +
1.130 + # Find all valid hard disks.
1.131 + valid = []
1.132 + for element in self.get_elements("hard-disk"):
1.133 + if not element.hasAttribute("interface") or element.getAttribute("interface") == interface:
1.134 + valid.append(element)
1.135 + for hard_disk_value in self.factory.find_widgets(self, "hard_disk_value"):
1.136 + self.populate_list(hard_disk_value, valid)
1.137 +
1.138 + # Slots.
1.139 +
1.140 + def baseSystemChanged(self, current_text):
1.141 + self.refresh(current_text)
1.142
1.143 def addHardDisk(self):
1.144 - hard_disks = qtui.QWidgetFactory.create(self.ui_hard_disks_filename)
1.145 + print self.sender()
1.146 + hard_disks = self.prepare_widget("configuration", "hard-disks")
1.147 tab_pages = hard_disks.child("tab pages")
1.148 tab = tab_pages.child("tab")
1.149 self.hard_disks.addTab(tab, hard_disks.tabLabel(tab))
1.150 self.factory.connect(tab, self)
1.151
1.152 + # Perform the consistency check.
1.153 + # NOTE: This is not as efficient as it could be since the general check
1.154 + # NOTE: refreshes all fields, not just newly added ones.
1.155 + self.refresh()
1.156 +
1.157 def addMemoryUnit(self):
1.158 - memory_units = qtui.QWidgetFactory.create(self.ui_memory_units_filename)
1.159 + memory_units = self.prepare_widget("configuration", "memory-units")
1.160 tab_pages = memory_units.child("tab pages")
1.161 tab = tab_pages.child("tab")
1.162 self.memory_units.addTab(tab, memory_units.tabLabel(tab))
1.163 self.factory.connect(tab, self)
1.164
1.165 + # Perform the consistency check.
1.166 + # NOTE: This is not as efficient as it could be since the general check
1.167 + # NOTE: refreshes all fields, not just newly added ones.
1.168 + self.refresh()
1.169 +
1.170 def addStorageUnit(self):
1.171 - storage_units = qtui.QWidgetFactory.create(self.ui_storage_units_filename)
1.172 + storage_units = self.prepare_widget("configuration", "storage-units")
1.173 tab_pages = storage_units.child("tab pages")
1.174 tab = tab_pages.child("tab")
1.175 self.storage_units.addTab(tab, storage_units.tabLabel(tab))
1.176 self.factory.connect(tab, self)
1.177
1.178 + # Perform the consistency check.
1.179 + # NOTE: This is not as efficient as it could be since the general check
1.180 + # NOTE: refreshes all fields, not just newly added ones.
1.181 + self.refresh()
1.182 +
1.183 def removeHardDisk(self):
1.184 page = self.hard_disks.currentPage()
1.185 self.hard_disks.removePage(page)
1.186 @@ -59,7 +180,7 @@
1.187 page.deleteLater()
1.188
1.189 def updateConfig(self):
1.190 - print "configuration.updateConfig(): Not implemented yet"
1.191 + self.refresh()
1.192
1.193 def exportConfig(self):
1.194 print "configuration.exportConfig(): Not implemented yet"