1.1 --- a/XSLForms/Resources/PyQtWebResources.py Thu Oct 27 16:31:26 2005 +0000
1.2 +++ b/XSLForms/Resources/PyQtWebResources.py Thu Oct 27 16:33:53 2005 +0000
1.3 @@ -23,6 +23,7 @@
1.4 import XSLForms.Prepare
1.5 import XSLForms.Resources.Common
1.6 import XSLForms.Resources.WebResources
1.7 +import WebStack.Generic
1.8 import os
1.9 import libxml2dom
1.10
1.11 @@ -35,6 +36,42 @@
1.12
1.13 widget_resources = {}
1.14
1.15 + def __init__(self, design_identifier):
1.16 + self.factory = Factory()
1.17 + self.default_design = design_identifier
1.18 +
1.19 + # NOTE: Filename extended by string concatenation.
1.20 +
1.21 + self.template_resources = {}
1.22 + for design_identifier, design_name in self.design_resources.items():
1.23 + self.template_resources[design_identifier] = (design_name + ".xhtml", design_name + ".xsl")
1.24 +
1.25 + # Resource methods.
1.26 +
1.27 + def prepare_output(self, design_identifier):
1.28 +
1.29 + """
1.30 + Prepare the output stylesheets using the given 'design_identifier' to
1.31 + indicate which templates and stylesheets are to be employed in the
1.32 + production of output from the resource.
1.33 +
1.34 + The 'design_identifier' is used as a key to the 'design_resources' and
1.35 + 'template_resources' dictionary attributes.
1.36 +
1.37 + Return the full path to the output stylesheet for use with 'send_output'
1.38 + or 'get_result'.
1.39 + """
1.40 +
1.41 + design_path = self.prepare_design(design_identifier)
1.42 + template_filename, output_filename = self.template_resources[output_identifier]
1.43 + output_path = os.path.abspath(os.path.join(self.resource_dir, output_filename))
1.44 + template_path = os.path.abspath(os.path.join(self.resource_dir, template_filename))
1.45 + XSLForms.Prepare.ensure_qt_template(design_path, template_path)
1.46 + XSLForms.Prepare.ensure_stylesheet(template_path, output_path)
1.47 + return output_path
1.48 +
1.49 + # PyQt compatibility methods.
1.50 +
1.51 def get_document(self, document_identifier):
1.52 return libxml2dom.parse(self.prepare_document(document_identifier))
1.53
1.54 @@ -46,50 +83,107 @@
1.55 # NOTE: Implement the equivalent here!
1.56 return qtui.QWidgetFactory.create(fragment_path, None, parent)
1.57
1.58 - def populate_list(self, field, elements):
1.59 - # NOTE: Support this!
1.60 - current_text = field.currentText()
1.61 - while field.count() > 0:
1.62 - field.removeItem(0)
1.63 - item = 0
1.64 - set = 0
1.65 - for element in elements:
1.66 - text = element.getAttribute("value")
1.67 - field.insertItem(text)
1.68 - if text == current_text:
1.69 - field.setCurrentItem(item)
1.70 - set = 1
1.71 - item += 1
1.72 - if not set:
1.73 - field.setCurrentItem(0)
1.74 + def child(self, name):
1.75 + return self.doc.child(name)
1.76 +
1.77 + # PyQt structural methods.
1.78 +
1.79 + def form_init(self):
1.80 + raise NotImplementedError, "form_init"
1.81 +
1.82 + def form_refresh(self):
1.83 + raise NotImplementedError, "form_refresh"
1.84 +
1.85 + # Standard XSLFormsResource method, overridden to handle presentation.
1.86 +
1.87 + def respond_to_form(self, trans, form):
1.88 +
1.89 + """
1.90 + Respond to the request described by the given transaction 'trans', using
1.91 + the given 'form' object to conveniently retrieve field (request
1.92 + parameter) information and structured form information (as DOM-style XML
1.93 + documents).
1.94 + """
1.95 +
1.96 + # Remember the document since it is accessed independently elsewhere.
1.97 +
1.98 + doc = form.get_document(self.default_design)
1.99 + if doc is None:
1.100 + self.doc = UINode(form.new_document(self.default_design))
1.101 + else:
1.102 + self.doc = UINode(doc)
1.103 +
1.104 + self.form_init()
1.105 +
1.106 + # NOTE: Updates happen here.
1.107 +
1.108 + self.form_refresh()
1.109 +
1.110 + trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding))
1.111 + design_xsl = self.prepare_output(self.default_design)
1.112 + self.send_output(trans, [design_xsl], doc._node)
1.113 +
1.114 +class UINode:
1.115
1.116 - def reset_collection(self, field):
1.117 - # NOTE: Support this!
1.118 - layout = field.layout()
1.119 - for child in field.children():
1.120 - if child is not layout:
1.121 - layout.remove(child)
1.122 - child.deleteLater()
1.123 + "A PyQt widget tree emulation node."
1.124 +
1.125 + def __init__(self, node):
1.126 + self._node = node
1.127 +
1.128 + def child(self, name):
1.129 + nodes = self._node.xpath(name)
1.130 + if len(nodes) > 0:
1.131 + return UINode(nodes[0])
1.132 + else:
1.133 + return None
1.134 +
1.135 + def children(self):
1.136 + return [UINode(node) for node in self._node.childNodes]
1.137 +
1.138 + def count(self):
1.139 + return len(self._node.childNodes)
1.140 +
1.141 + def currentText(self):
1.142 + return self.getAttribute("value")
1.143 +
1.144 + def currentItem(self):
1.145 + found = self._node.xpath("*[@value=current()/@value]")
1.146 + if found:
1.147 + return int(found.xpath("count(preceding-sibling::*)"))
1.148 + else:
1.149 + return 0
1.150 +
1.151 + def insertItem(self, item, position=-1):
1.152 +
1.153 + def parent(self):
1.154 + return UINode(self._node.parentNode)
1.155 +
1.156 + def removeItem(self, item):
1.157 +
1.158 + def remove(self, item):
1.159 +
1.160 + def layout(self):
1.161 + return self
1.162 +
1.163 + def setCurrentItem(self):
1.164 +
1.165 + def deleteLater(self):
1.166 + self._node.parentNode.removeChild(self._node)
1.167
1.168 class Factory:
1.169
1.170 "A widget factory helper class."
1.171
1.172 - def __init__(self, ui_filename):
1.173 - self.ui_filename = ui_filename
1.174 - self.ui_doc = libxml2dom.parse(ui_filename)
1.175 -
1.176 def connect(self, widget, obj):
1.177 pass
1.178
1.179 def find_widgets(self, widget, name):
1.180 - # NOTE: Support this!
1.181 - widgets = []
1.182 - found = widget.child(name)
1.183 - if found:
1.184 - widgets.append(found)
1.185 - for child in widget.children():
1.186 - widgets += self.find_widgets(child, name)
1.187 - return widgets
1.188 +
1.189 + """
1.190 + Find within the given 'widget' (a DOM node) the widget with the given
1.191 + 'name'.
1.192 + """
1.193 +
1.194 + return widget.getElementsByTagName(name)
1.195
1.196 # vim: tabstop=4 expandtab shiftwidth=4