1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/XSLForms/PyQt.py Tue Oct 25 17:27:43 2005 +0000
1.3 @@ -0,0 +1,107 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +import XSLForms.Prepare
1.7 +import qt
1.8 +import qtui
1.9 +import qtxmldom
1.10 +import os
1.11 +
1.12 +class XSLFormsResource:
1.13 +
1.14 + "An XSLForms resource for use with PyQt."
1.15 +
1.16 + template_resources = {}
1.17 + widget_resources = {}
1.18 + document_resources = {}
1.19 +
1.20 + # Helper methods.
1.21 + # NOTE: Should be moved to a superclass.
1.22 +
1.23 + def prepare_document(self, document_identifier):
1.24 + filename = self.document_resources[document_identifier]
1.25 + return os.path.abspath(os.path.join(self.resource_dir, filename))
1.26 +
1.27 + def get_document(self, document_identifier):
1.28 + return qtxmldom.parse(self.prepare_document(document_identifier))
1.29 +
1.30 + def get_elements(self, document_identifier):
1.31 + doc = self.get_document(document_identifier)
1.32 + return doc.getElementsByTagName(document_identifier + "-enum")
1.33 +
1.34 + def prepare_template(self, template_identifier):
1.35 + filename = self.template_resources[template_identifier]
1.36 + return os.path.abspath(os.path.join(self.resource_dir, filename))
1.37 +
1.38 + def prepare_widget(self, template_identifier, widget_identifier, parent=None):
1.39 + template_path = self.prepare_template(template_identifier)
1.40 + fragment_name, widget_name = self.widget_resources[widget_identifier]
1.41 + fragment_path = os.path.abspath(os.path.join(self.resource_dir, fragment_name))
1.42 + XSLForms.Prepare.ensure_qt_fragment(template_path, fragment_path, widget_name)
1.43 + return qtui.QWidgetFactory.create(fragment_path, None, parent)
1.44 +
1.45 + def populate_list(self, field, elements):
1.46 + current_text = field.currentText()
1.47 + while field.count() > 0:
1.48 + field.removeItem(0)
1.49 + item = 0
1.50 + set = 0
1.51 + for element in elements:
1.52 + text = element.getAttribute("value")
1.53 + field.insertItem(text)
1.54 + if text == current_text:
1.55 + field.setCurrentItem(item)
1.56 + set = 1
1.57 + item += 1
1.58 + if not set:
1.59 + field.setCurrentItem(0)
1.60 +
1.61 +class Factory:
1.62 +
1.63 + "A widget factory helper class."
1.64 +
1.65 + def __init__(self, ui_filename):
1.66 + self.ui_filename = ui_filename
1.67 + self.ui_doc = qtxmldom.parse(ui_filename)
1.68 +
1.69 + def connect(self, widget, obj):
1.70 +
1.71 + for connection in self.ui_doc.getElementsByTagName("connection"):
1.72 + sender_name = self.get_text(connection.getElementsByTagName("sender")[0]).encode("utf-8")
1.73 + signal_name = self.get_text(connection.getElementsByTagName("signal")[0]).encode("utf-8")
1.74 + slot_name = self.get_text(connection.getElementsByTagName("slot")[0]).encode("utf-8")
1.75 +
1.76 + if widget.name() == sender_name:
1.77 + senders = [widget]
1.78 + else:
1.79 + senders = self.find_widgets(widget, sender_name)
1.80 +
1.81 + slot = slot_name.split("(")[0]
1.82 + if hasattr(obj, slot):
1.83 + signal = qt.SIGNAL(signal_name)
1.84 + for sender in senders:
1.85 + qt.QObject.connect(sender, signal, getattr(obj, slot))
1.86 +
1.87 + def find_widgets(self, widget, name):
1.88 + widgets = []
1.89 + found = widget.child(name)
1.90 + if found:
1.91 + widgets.append(found)
1.92 + for child in widget.children():
1.93 + widgets += self.find_widgets(child, name)
1.94 + return widgets
1.95 +
1.96 + def get_text(self, node):
1.97 + node.normalize()
1.98 + return node.childNodes[0].nodeValue
1.99 +
1.100 + def find_widget_element(self, name):
1.101 + for widget in self.ui_doc.getElementsByTagName("widget"):
1.102 + for property in widget.getElementsByTagName("property"):
1.103 + if property.getAttribute("name") == "name":
1.104 + for cstring in property.getElementsByTagName("cstring"):
1.105 + found_name = self.get_text(cstring)
1.106 + if found_name == name:
1.107 + return widget
1.108 + return None
1.109 +
1.110 +# vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/examples/Common/QtConfigurator/__init__.py Tue Oct 25 15:52:43 2005 +0000
2.2 +++ b/examples/Common/QtConfigurator/__init__.py Tue Oct 25 17:27:43 2005 +0000
2.3 @@ -1,13 +1,10 @@
2.4 #!/usr/bin/env python
2.5
2.6 +import XSLForms.PyQt
2.7 import QtConfigurator.Forms
2.8 -import XSLForms.Prepare
2.9 -import factory
2.10 -import qtui
2.11 -import qtxmldom
2.12 import os
2.13
2.14 -class Configurator(QtConfigurator.Forms.Configurator):
2.15 +class Configurator(QtConfigurator.Forms.Configurator, XSLForms.PyQt.XSLFormsResource):
2.16
2.17 # Standard attributes.
2.18
2.19 @@ -35,52 +32,11 @@
2.20 "screen" : "config_screen.xml"
2.21 }
2.22
2.23 - # Helper methods.
2.24 - # NOTE: Should be moved to a superclass.
2.25 -
2.26 - def prepare_document(self, document_identifier):
2.27 - filename = self.document_resources[document_identifier]
2.28 - return os.path.abspath(os.path.join(self.resource_dir, filename))
2.29 -
2.30 - def get_document(self, document_identifier):
2.31 - return qtxmldom.parse(self.prepare_document(document_identifier))
2.32 -
2.33 - def get_elements(self, document_identifier):
2.34 - doc = self.get_document(document_identifier)
2.35 - return doc.getElementsByTagName(document_identifier + "-enum")
2.36 -
2.37 - def populate_list(self, field, elements):
2.38 - current_text = field.currentText()
2.39 - while field.count() > 0:
2.40 - field.removeItem(0)
2.41 - item = 0
2.42 - set = 0
2.43 - for element in elements:
2.44 - text = element.getAttribute("value")
2.45 - field.insertItem(text)
2.46 - if text == current_text:
2.47 - field.setCurrentItem(item)
2.48 - set = 1
2.49 - item += 1
2.50 - if not set:
2.51 - field.setCurrentItem(0)
2.52 -
2.53 - def prepare_template(self, template_identifier):
2.54 - filename = self.template_resources[template_identifier]
2.55 - return os.path.abspath(os.path.join(self.resource_dir, filename))
2.56 -
2.57 - def prepare_widget(self, template_identifier, widget_identifier, parent=None):
2.58 - template_path = self.prepare_template(template_identifier)
2.59 - fragment_name, widget_name = self.widget_resources[widget_identifier]
2.60 - fragment_path = os.path.abspath(os.path.join(self.resource_dir, fragment_name))
2.61 - XSLForms.Prepare.ensure_qt_fragment(template_path, fragment_path, widget_name)
2.62 - return qtui.QWidgetFactory.create(fragment_path, None, parent)
2.63 -
2.64 # Initialisation.
2.65
2.66 def __init__(self, *args, **kw):
2.67 QtConfigurator.Forms.Configurator.__init__(self, *args, **kw)
2.68 - self.factory = factory.Factory(self.prepare_template("configuration"))
2.69 + self.factory = XSLForms.PyQt.Factory(self.prepare_template("configuration"))
2.70
2.71 # Get field data.
2.72 # NOTE: This would be done for whole page updates in a Web application.
3.1 --- a/examples/Common/QtConfigurator/factory.py Tue Oct 25 15:52:43 2005 +0000
3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
3.3 @@ -1,53 +0,0 @@
3.4 -#!/usr/bin/env python
3.5 -
3.6 -from qt import *
3.7 -import qtui
3.8 -import qtxmldom
3.9 -
3.10 -class Factory:
3.11 - def __init__(self, ui_filename):
3.12 - self.ui_filename = ui_filename
3.13 - self.ui_doc = qtxmldom.parse(ui_filename)
3.14 -
3.15 - def connect(self, widget, obj):
3.16 -
3.17 - for connection in self.ui_doc.getElementsByTagName("connection"):
3.18 - sender_name = self.get_text(connection.getElementsByTagName("sender")[0]).encode("utf-8")
3.19 - signal_name = self.get_text(connection.getElementsByTagName("signal")[0]).encode("utf-8")
3.20 - slot_name = self.get_text(connection.getElementsByTagName("slot")[0]).encode("utf-8")
3.21 -
3.22 - if widget.name() == sender_name:
3.23 - senders = [widget]
3.24 - else:
3.25 - senders = self.find_widgets(widget, sender_name)
3.26 -
3.27 - slot = slot_name.split("(")[0]
3.28 - if hasattr(obj, slot):
3.29 - signal = SIGNAL(signal_name)
3.30 - for sender in senders:
3.31 - QObject.connect(sender, signal, getattr(obj, slot))
3.32 -
3.33 - def find_widgets(self, widget, name):
3.34 - widgets = []
3.35 - found = widget.child(name)
3.36 - if found:
3.37 - widgets.append(found)
3.38 - for child in widget.children():
3.39 - widgets += self.find_widgets(child, name)
3.40 - return widgets
3.41 -
3.42 - def get_text(self, node):
3.43 - node.normalize()
3.44 - return node.childNodes[0].nodeValue
3.45 -
3.46 - def find_widget_element(self, name):
3.47 - for widget in self.ui_doc.getElementsByTagName("widget"):
3.48 - for property in widget.getElementsByTagName("property"):
3.49 - if property.getAttribute("name") == "name":
3.50 - for cstring in property.getElementsByTagName("cstring"):
3.51 - found_name = self.get_text(cstring)
3.52 - if found_name == name:
3.53 - return widget
3.54 - return None
3.55 -
3.56 -# vim: tabstop=4 expandtab shiftwidth=4