1.1 --- a/XSLForms/Resources/Common.py Thu Oct 27 16:31:26 2005 +0000
1.2 +++ b/XSLForms/Resources/Common.py Thu Oct 27 16:33:53 2005 +0000
1.3 @@ -48,6 +48,14 @@
1.4 design_resources = {}
1.5
1.6 def get_document(self, document_identifier):
1.7 +
1.8 + """
1.9 + Return a DOM-style document retrieved using the given
1.10 + 'document_identifier'.
1.11 +
1.12 + Each implementation is free to choose its own DOM library.
1.13 + """
1.14 +
1.15 raise NotImplementedError, "get_document"
1.16
1.17 def get_elements(self, document_identifier):
1.18 @@ -58,4 +66,33 @@
1.19 filename = self.design_resources[design_identifier]
1.20 return os.path.abspath(os.path.join(self.resource_dir, filename))
1.21
1.22 + def populate_list(self, field, elements):
1.23 +
1.24 + "Populate the given 'field' using a list of DOM 'elements'."
1.25 +
1.26 + current_text = field.currentText()
1.27 + while field.count() > 0:
1.28 + field.removeItem(0)
1.29 + item = 0
1.30 + set = 0
1.31 + for element in elements:
1.32 + text = element.getAttribute("value")
1.33 + field.insertItem(text)
1.34 + if text == current_text:
1.35 + field.setCurrentItem(item)
1.36 + set = 1
1.37 + item += 1
1.38 + if not set:
1.39 + field.setCurrentItem(0)
1.40 +
1.41 + def reset_collection(self, field):
1.42 +
1.43 + "Empty the given collection 'field'."
1.44 +
1.45 + layout = field.layout()
1.46 + for child in field.children():
1.47 + if child is not layout:
1.48 + layout.remove(child)
1.49 + child.deleteLater()
1.50 +
1.51 # vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/XSLForms/Resources/PyQtResources.py Thu Oct 27 16:31:26 2005 +0000
2.2 +++ b/XSLForms/Resources/PyQtResources.py Thu Oct 27 16:33:53 2005 +0000
2.3 @@ -31,6 +31,9 @@
2.4
2.5 widget_resources = {}
2.6
2.7 + def __init__(self, design_identifier):
2.8 + self.factory = Factory(self.prepare_design(design_identifier))
2.9 +
2.10 def get_document(self, document_identifier):
2.11 return qtxmldom.parse(self.prepare_document(document_identifier))
2.12
2.13 @@ -41,29 +44,6 @@
2.14 XSLForms.Prepare.ensure_qt_fragment(design_path, fragment_path, widget_name)
2.15 return qtui.QWidgetFactory.create(fragment_path, None, parent)
2.16
2.17 - def populate_list(self, field, elements):
2.18 - current_text = field.currentText()
2.19 - while field.count() > 0:
2.20 - field.removeItem(0)
2.21 - item = 0
2.22 - set = 0
2.23 - for element in elements:
2.24 - text = element.getAttribute("value")
2.25 - field.insertItem(text)
2.26 - if text == current_text:
2.27 - field.setCurrentItem(item)
2.28 - set = 1
2.29 - item += 1
2.30 - if not set:
2.31 - field.setCurrentItem(0)
2.32 -
2.33 - def reset_collection(self, field):
2.34 - layout = field.layout()
2.35 - for child in field.children():
2.36 - if child is not layout:
2.37 - layout.remove(child)
2.38 - child.deleteLater()
2.39 -
2.40 class Factory:
2.41
2.42 "A widget factory helper class."
2.43 @@ -90,6 +70,10 @@
2.44 for sender in senders:
2.45 qt.QObject.connect(sender, signal, getattr(obj, slot))
2.46
2.47 + def get_text(self, node):
2.48 + node.normalize()
2.49 + return node.childNodes[0].nodeValue
2.50 +
2.51 def find_widgets(self, widget, name):
2.52 widgets = []
2.53 found = widget.child(name)
2.54 @@ -99,18 +83,4 @@
2.55 widgets += self.find_widgets(child, name)
2.56 return widgets
2.57
2.58 - def get_text(self, node):
2.59 - node.normalize()
2.60 - return node.childNodes[0].nodeValue
2.61 -
2.62 - def find_widget_element(self, name):
2.63 - for widget in self.ui_doc.getElementsByTagName("widget"):
2.64 - for property in widget.getElementsByTagName("property"):
2.65 - if property.getAttribute("name") == "name":
2.66 - for cstring in property.getElementsByTagName("cstring"):
2.67 - found_name = self.get_text(cstring)
2.68 - if found_name == name:
2.69 - return widget
2.70 - return None
2.71 -
2.72 # vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/XSLForms/Resources/PyQtWebResources.py Thu Oct 27 16:31:26 2005 +0000
3.2 +++ b/XSLForms/Resources/PyQtWebResources.py Thu Oct 27 16:33:53 2005 +0000
3.3 @@ -23,6 +23,7 @@
3.4 import XSLForms.Prepare
3.5 import XSLForms.Resources.Common
3.6 import XSLForms.Resources.WebResources
3.7 +import WebStack.Generic
3.8 import os
3.9 import libxml2dom
3.10
3.11 @@ -35,6 +36,42 @@
3.12
3.13 widget_resources = {}
3.14
3.15 + def __init__(self, design_identifier):
3.16 + self.factory = Factory()
3.17 + self.default_design = design_identifier
3.18 +
3.19 + # NOTE: Filename extended by string concatenation.
3.20 +
3.21 + self.template_resources = {}
3.22 + for design_identifier, design_name in self.design_resources.items():
3.23 + self.template_resources[design_identifier] = (design_name + ".xhtml", design_name + ".xsl")
3.24 +
3.25 + # Resource methods.
3.26 +
3.27 + def prepare_output(self, design_identifier):
3.28 +
3.29 + """
3.30 + Prepare the output stylesheets using the given 'design_identifier' to
3.31 + indicate which templates and stylesheets are to be employed in the
3.32 + production of output from the resource.
3.33 +
3.34 + The 'design_identifier' is used as a key to the 'design_resources' and
3.35 + 'template_resources' dictionary attributes.
3.36 +
3.37 + Return the full path to the output stylesheet for use with 'send_output'
3.38 + or 'get_result'.
3.39 + """
3.40 +
3.41 + design_path = self.prepare_design(design_identifier)
3.42 + template_filename, output_filename = self.template_resources[output_identifier]
3.43 + output_path = os.path.abspath(os.path.join(self.resource_dir, output_filename))
3.44 + template_path = os.path.abspath(os.path.join(self.resource_dir, template_filename))
3.45 + XSLForms.Prepare.ensure_qt_template(design_path, template_path)
3.46 + XSLForms.Prepare.ensure_stylesheet(template_path, output_path)
3.47 + return output_path
3.48 +
3.49 + # PyQt compatibility methods.
3.50 +
3.51 def get_document(self, document_identifier):
3.52 return libxml2dom.parse(self.prepare_document(document_identifier))
3.53
3.54 @@ -46,50 +83,107 @@
3.55 # NOTE: Implement the equivalent here!
3.56 return qtui.QWidgetFactory.create(fragment_path, None, parent)
3.57
3.58 - def populate_list(self, field, elements):
3.59 - # NOTE: Support this!
3.60 - current_text = field.currentText()
3.61 - while field.count() > 0:
3.62 - field.removeItem(0)
3.63 - item = 0
3.64 - set = 0
3.65 - for element in elements:
3.66 - text = element.getAttribute("value")
3.67 - field.insertItem(text)
3.68 - if text == current_text:
3.69 - field.setCurrentItem(item)
3.70 - set = 1
3.71 - item += 1
3.72 - if not set:
3.73 - field.setCurrentItem(0)
3.74 + def child(self, name):
3.75 + return self.doc.child(name)
3.76 +
3.77 + # PyQt structural methods.
3.78 +
3.79 + def form_init(self):
3.80 + raise NotImplementedError, "form_init"
3.81 +
3.82 + def form_refresh(self):
3.83 + raise NotImplementedError, "form_refresh"
3.84 +
3.85 + # Standard XSLFormsResource method, overridden to handle presentation.
3.86 +
3.87 + def respond_to_form(self, trans, form):
3.88 +
3.89 + """
3.90 + Respond to the request described by the given transaction 'trans', using
3.91 + the given 'form' object to conveniently retrieve field (request
3.92 + parameter) information and structured form information (as DOM-style XML
3.93 + documents).
3.94 + """
3.95 +
3.96 + # Remember the document since it is accessed independently elsewhere.
3.97 +
3.98 + doc = form.get_document(self.default_design)
3.99 + if doc is None:
3.100 + self.doc = UINode(form.new_document(self.default_design))
3.101 + else:
3.102 + self.doc = UINode(doc)
3.103 +
3.104 + self.form_init()
3.105 +
3.106 + # NOTE: Updates happen here.
3.107 +
3.108 + self.form_refresh()
3.109 +
3.110 + trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding))
3.111 + design_xsl = self.prepare_output(self.default_design)
3.112 + self.send_output(trans, [design_xsl], doc._node)
3.113 +
3.114 +class UINode:
3.115
3.116 - def reset_collection(self, field):
3.117 - # NOTE: Support this!
3.118 - layout = field.layout()
3.119 - for child in field.children():
3.120 - if child is not layout:
3.121 - layout.remove(child)
3.122 - child.deleteLater()
3.123 + "A PyQt widget tree emulation node."
3.124 +
3.125 + def __init__(self, node):
3.126 + self._node = node
3.127 +
3.128 + def child(self, name):
3.129 + nodes = self._node.xpath(name)
3.130 + if len(nodes) > 0:
3.131 + return UINode(nodes[0])
3.132 + else:
3.133 + return None
3.134 +
3.135 + def children(self):
3.136 + return [UINode(node) for node in self._node.childNodes]
3.137 +
3.138 + def count(self):
3.139 + return len(self._node.childNodes)
3.140 +
3.141 + def currentText(self):
3.142 + return self.getAttribute("value")
3.143 +
3.144 + def currentItem(self):
3.145 + found = self._node.xpath("*[@value=current()/@value]")
3.146 + if found:
3.147 + return int(found.xpath("count(preceding-sibling::*)"))
3.148 + else:
3.149 + return 0
3.150 +
3.151 + def insertItem(self, item, position=-1):
3.152 +
3.153 + def parent(self):
3.154 + return UINode(self._node.parentNode)
3.155 +
3.156 + def removeItem(self, item):
3.157 +
3.158 + def remove(self, item):
3.159 +
3.160 + def layout(self):
3.161 + return self
3.162 +
3.163 + def setCurrentItem(self):
3.164 +
3.165 + def deleteLater(self):
3.166 + self._node.parentNode.removeChild(self._node)
3.167
3.168 class Factory:
3.169
3.170 "A widget factory helper class."
3.171
3.172 - def __init__(self, ui_filename):
3.173 - self.ui_filename = ui_filename
3.174 - self.ui_doc = libxml2dom.parse(ui_filename)
3.175 -
3.176 def connect(self, widget, obj):
3.177 pass
3.178
3.179 def find_widgets(self, widget, name):
3.180 - # NOTE: Support this!
3.181 - widgets = []
3.182 - found = widget.child(name)
3.183 - if found:
3.184 - widgets.append(found)
3.185 - for child in widget.children():
3.186 - widgets += self.find_widgets(child, name)
3.187 - return widgets
3.188 +
3.189 + """
3.190 + Find within the given 'widget' (a DOM node) the widget with the given
3.191 + 'name'.
3.192 + """
3.193 +
3.194 + return widget.getElementsByTagName(name)
3.195
3.196 # vim: tabstop=4 expandtab shiftwidth=4