# HG changeset patch # User paulb # Date 1130177822 0 # Node ID 7b20e53fed9db980f3d2b4b3b3024703d6c83047 # Parent 693cb44470024ff303c03f464935d36f0c73fabc [project @ 2005-10-24 18:17:02 by paulb] Fixed tab page removal. Improved tab page addition by using the Factory class to establish connections in the added widgets. Added automatic user interface fragment preparation. Improved the Factory class to provide support for finding widgets in the user interface (renaming the old find_widget method which is used on the Qt Designer file) and establishing connections between widgets. diff -r 693cb4447002 -r 7b20e53fed9d examples/Common/QtConfigurator/__init__.py --- a/examples/Common/QtConfigurator/__init__.py Mon Oct 24 18:13:53 2005 +0000 +++ b/examples/Common/QtConfigurator/__init__.py Mon Oct 24 18:17:02 2005 +0000 @@ -1,5 +1,6 @@ import QtConfigurator.Forms -#import factory +import XSLForms.Prepare +import factory import qtui import os @@ -8,46 +9,54 @@ 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") def __init__(self, *args, **kw): QtConfigurator.Forms.Configurator.__init__(self, *args, **kw) - #self.factory = factory.Factory(self.ui_filename) + 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") def baseSystemChanged(self): print self.base_system.currentItem() def addHardDisk(self): hard_disks = qtui.QWidgetFactory.create(self.ui_hard_disks_filename) - print str(hard_disks.name()) - #top = qtui.QWidgetFactory.create(self.ui_filename) - #print [str(c.name()) for c in top.children()] - #hard_disks = top.child("hard_disks") - print [str(c.name()) for c in hard_disks.children()] tab_pages = hard_disks.child("tab pages") - print [str(c.name()) for c in tab_pages.children()] tab = tab_pages.child("tab") self.hard_disks.addTab(tab, hard_disks.tabLabel(tab)) + self.factory.connect(tab, self) def addMemoryUnit(self): - print "configuration.addMemoryUnit(): Not implemented yet" + memory_units = qtui.QWidgetFactory.create(self.ui_memory_units_filename) + 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) def addStorageUnit(self): - print "configuration.addStorageUnit(): Not implemented yet" + storage_units = qtui.QWidgetFactory.create(self.ui_storage_units_filename) + 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) def removeHardDisk(self): page = self.hard_disks.currentPage() self.hard_disks.removePage(page) - del page + page.deleteLater() def removeMemoryUnit(self): page = self.memory_units.currentPage() self.memory_units.removePage(page) - del page + page.deleteLater() def removeStorageUnit(self): page = self.storage_units.currentPage() self.storage_units.removePage(page) - del page + page.deleteLater() def updateConfig(self): print "configuration.updateConfig(): Not implemented yet" diff -r 693cb4447002 -r 7b20e53fed9d examples/Common/QtConfigurator/factory.py --- a/examples/Common/QtConfigurator/factory.py Mon Oct 24 18:13:53 2005 +0000 +++ b/examples/Common/QtConfigurator/factory.py Mon Oct 24 18:17:02 2005 +0000 @@ -1,22 +1,56 @@ #!/usr/bin/env python from qt import * +import qtui import qtxmldom class Factory: def __init__(self, ui_filename): - self.ui = qtxmldom.parse(ui_filename) + self.ui_filename = ui_filename + self.ui_doc = qtxmldom.parse(ui_filename) + + def connect(self, widget, obj): + + for connection in self.ui_doc.getElementsByTagName("connection"): + sender_name = self.get_text(connection.getElementsByTagName("sender")[0]).encode("utf-8") + signal_name = self.get_text(connection.getElementsByTagName("signal")[0]).encode("utf-8") + slot_name = self.get_text(connection.getElementsByTagName("slot")[0]).encode("utf-8") + + if widget.name() == sender_name: + sender = widget + else: + sender = self.find_widget(widget, sender_name) + + if sender: + signal = SIGNAL(signal_name) + slot = slot_name.split("(")[0] + + if hasattr(obj, slot): + QObject.connect(sender, signal, getattr(obj, slot)) - def findWidget(self, widget_class, name): - for widget in self.ui.getElementsByTagName("widget"): - if widget.getAttribute("class") == widget_class: - for property in widget.getElementsByTagName("property"): - if property.getAttribute("name") == "name": - for cstring in property.getElementsByTagName("cstring"): - cstring.normalize() - found_name = cstring.childNodes[0].nodeValue - if found_name == name: - return widget + def find_widget(self, widget, name): + 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 + + def get_text(self, node): + node.normalize() + return node.childNodes[0].nodeValue + + def find_widget_element(self, name): + for widget in self.ui_doc.getElementsByTagName("widget"): + for property in widget.getElementsByTagName("property"): + if property.getAttribute("name") == "name": + for cstring in property.getElementsByTagName("cstring"): + found_name = self.get_text(cstring) + if found_name == name: + return widget return None # vim: tabstop=4 expandtab shiftwidth=4