# HG changeset patch # User paulb # Date 1130202796 0 # Node ID b8872088a1d0e14d584bb61d3df177b804772247 # Parent 837527dca4f6b3491ca7df4de7698dc6644f3203 [project @ 2005-10-25 01:13:16 by paulb] Reorganised, moving various PyQt-specific things into their own methods. Added support for the base system field and the effects it has on other fields. Changed the Factory class's find_widget method to find_widgets in order to support changes to many widgets of the same name. diff -r 837527dca4f6 -r b8872088a1d0 examples/Common/QtConfigurator/__init__.py --- a/examples/Common/QtConfigurator/__init__.py Mon Oct 24 21:35:13 2005 +0000 +++ b/examples/Common/QtConfigurator/__init__.py Tue Oct 25 01:13:16 2005 +0000 @@ -1,48 +1,169 @@ +#!/usr/bin/env python + import QtConfigurator.Forms import XSLForms.Prepare import factory import qtui +import qtxmldom import os class Configurator(QtConfigurator.Forms.Configurator): + # Standard attributes. + resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") - ui_filename = os.path.join(resource_dir, "config.ui") - ui_hard_disks_filename = os.path.join(resource_dir, "config_hard_disks.ui") - ui_memory_units_filename = os.path.join(resource_dir, "config_memory_units.ui") - ui_storage_units_filename = os.path.join(resource_dir, "config_storage_units.ui") + + template_resources = { + "configuration" : "config.ui" + } + + widget_resources = { + "hard-disks" : ("config_hard_disks.ui", "hard_disks"), + "memory-units" : ("config_memory_units.ui", "memory_units"), + "storage-units" : ("config_storage_units.ui", "storage_units") + } + + document_resources = { + "base-system" : "config_base_system.xml", + "cpu" : "config_cpu.xml", + "hard-disk" : "config_hard_disk.xml", + "keyboard" : "config_keyboard.xml", + "mouse" : "config_mouse.xml", + "screen" : "config_screen.xml" + } + + # Helper methods. + # NOTE: Should be moved to a superclass. + + def prepare_document(self, document_identifier): + filename = self.document_resources[document_identifier] + return os.path.abspath(os.path.join(self.resource_dir, filename)) + + def get_document(self, document_identifier): + return qtxmldom.parse(self.prepare_document(document_identifier)) + + def get_elements(self, document_identifier): + doc = self.get_document(document_identifier) + return doc.getElementsByTagName(document_identifier + "-enum") + + def populate_list(self, field, elements): + current_text = field.currentText() + while field.count() > 0: + field.removeItem(0) + item = 0 + set = 0 + for element in elements: + text = element.getAttribute("value") + field.insertItem(text) + if text == current_text: + field.setCurrentItem(item) + set = 1 + item += 1 + if not set: + field.setCurrentItem(0) + + def prepare_template(self, template_identifier): + filename = self.template_resources[template_identifier] + return os.path.abspath(os.path.join(self.resource_dir, filename)) + + def prepare_widget(self, template_identifier, widget_identifier): + template_path = self.prepare_template(template_identifier) + fragment_name, widget_name = self.widget_resources[widget_identifier] + fragment_path = os.path.abspath(os.path.join(self.resource_dir, fragment_name)) + XSLForms.Prepare.ensure_qt_fragment(template_path, fragment_path, widget_name) + return qtui.QWidgetFactory.create(fragment_path) + + # Initialisation. def __init__(self, *args, **kw): QtConfigurator.Forms.Configurator.__init__(self, *args, **kw) - self.factory = factory.Factory(self.ui_filename) - XSLForms.Prepare.ensure_qt_fragment(self.ui_filename, self.ui_hard_disks_filename, "hard_disks") - XSLForms.Prepare.ensure_qt_fragment(self.ui_filename, self.ui_memory_units_filename, "memory_units") - XSLForms.Prepare.ensure_qt_fragment(self.ui_filename, self.ui_storage_units_filename, "storage_units") + self.factory = factory.Factory(self.prepare_template("configuration")) + + # Get field data. + # NOTE: This would be done for whole page updates in a Web application. + + self.populate_list(self.base_system, self.get_elements("base-system")) + self.populate_list(self.keyboard, self.get_elements("keyboard")) + self.populate_list(self.mouse, self.get_elements("mouse")) + self.populate_list(self.screen, self.get_elements("screen")) + + # General functionality. + + def refresh(self, current_text=None): + + # Ensure consistency. + # NOTE: This would be done for whole page updates in a Web application. + # NOTE: This would also be done for page updates where the information + # NOTE: involved was important. + + current_text = current_text or self.base_system.currentText() - def baseSystemChanged(self): - print self.base_system.currentItem() + # Find the CPU socket and the interface of the current base system. + cpu_socket = None + interface = None + for element in self.get_elements("base-system"): + text = element.getAttribute("value") + if text == current_text: + cpu_socket = element.getAttribute("cpu-socket") + interface = element.getAttribute("interface") + + # Find all valid CPUs. + valid = [] + for element in self.get_elements("cpu"): + if not element.hasAttribute("cpu-socket") or element.getAttribute("cpu-socket") == cpu_socket: + valid.append(element) + self.populate_list(self.cpu, valid) + + # Find all valid hard disks. + valid = [] + for element in self.get_elements("hard-disk"): + if not element.hasAttribute("interface") or element.getAttribute("interface") == interface: + valid.append(element) + for hard_disk_value in self.factory.find_widgets(self, "hard_disk_value"): + self.populate_list(hard_disk_value, valid) + + # Slots. + + def baseSystemChanged(self, current_text): + self.refresh(current_text) def addHardDisk(self): - hard_disks = qtui.QWidgetFactory.create(self.ui_hard_disks_filename) + print self.sender() + hard_disks = self.prepare_widget("configuration", "hard-disks") tab_pages = hard_disks.child("tab pages") tab = tab_pages.child("tab") self.hard_disks.addTab(tab, hard_disks.tabLabel(tab)) self.factory.connect(tab, self) + # Perform the consistency check. + # NOTE: This is not as efficient as it could be since the general check + # NOTE: refreshes all fields, not just newly added ones. + self.refresh() + def addMemoryUnit(self): - memory_units = qtui.QWidgetFactory.create(self.ui_memory_units_filename) + memory_units = self.prepare_widget("configuration", "memory-units") tab_pages = memory_units.child("tab pages") tab = tab_pages.child("tab") self.memory_units.addTab(tab, memory_units.tabLabel(tab)) self.factory.connect(tab, self) + # Perform the consistency check. + # NOTE: This is not as efficient as it could be since the general check + # NOTE: refreshes all fields, not just newly added ones. + self.refresh() + def addStorageUnit(self): - storage_units = qtui.QWidgetFactory.create(self.ui_storage_units_filename) + storage_units = self.prepare_widget("configuration", "storage-units") tab_pages = storage_units.child("tab pages") tab = tab_pages.child("tab") self.storage_units.addTab(tab, storage_units.tabLabel(tab)) self.factory.connect(tab, self) + # Perform the consistency check. + # NOTE: This is not as efficient as it could be since the general check + # NOTE: refreshes all fields, not just newly added ones. + self.refresh() + def removeHardDisk(self): page = self.hard_disks.currentPage() self.hard_disks.removePage(page) @@ -59,7 +180,7 @@ page.deleteLater() def updateConfig(self): - print "configuration.updateConfig(): Not implemented yet" + self.refresh() def exportConfig(self): print "configuration.exportConfig(): Not implemented yet" diff -r 837527dca4f6 -r b8872088a1d0 examples/Common/QtConfigurator/factory.py --- a/examples/Common/QtConfigurator/factory.py Mon Oct 24 21:35:13 2005 +0000 +++ b/examples/Common/QtConfigurator/factory.py Tue Oct 25 01:13:16 2005 +0000 @@ -17,27 +17,24 @@ slot_name = self.get_text(connection.getElementsByTagName("slot")[0]).encode("utf-8") if widget.name() == sender_name: - sender = widget + senders = [widget] else: - sender = self.find_widget(widget, sender_name) + senders = self.find_widgets(widget, sender_name) - if sender: + slot = slot_name.split("(")[0] + if hasattr(obj, slot): signal = SIGNAL(signal_name) - slot = slot_name.split("(")[0] - - if hasattr(obj, slot): + for sender in senders: QObject.connect(sender, signal, getattr(obj, slot)) - def find_widget(self, widget, name): + def find_widgets(self, widget, name): + widgets = [] found = widget.child(name) if found: - return found - else: - for child in widget.children(): - found = self.find_widget(child, name) - if found: - return found - return None + widgets.append(found) + for child in widget.children(): + widgets += self.find_widgets(child, name) + return widgets def get_text(self, node): node.normalize()