paulb@353 | 1 | #!/usr/bin/env python |
paulb@353 | 2 | |
paulb@353 | 3 | import XSLForms.Prepare |
paulb@353 | 4 | import qt |
paulb@353 | 5 | import qtui |
paulb@353 | 6 | import qtxmldom |
paulb@353 | 7 | import os |
paulb@353 | 8 | |
paulb@353 | 9 | class XSLFormsResource: |
paulb@353 | 10 | |
paulb@353 | 11 | "An XSLForms resource for use with PyQt." |
paulb@353 | 12 | |
paulb@353 | 13 | template_resources = {} |
paulb@353 | 14 | widget_resources = {} |
paulb@353 | 15 | document_resources = {} |
paulb@353 | 16 | |
paulb@353 | 17 | # Helper methods. |
paulb@353 | 18 | # NOTE: Should be moved to a superclass. |
paulb@353 | 19 | |
paulb@353 | 20 | def prepare_document(self, document_identifier): |
paulb@353 | 21 | filename = self.document_resources[document_identifier] |
paulb@353 | 22 | return os.path.abspath(os.path.join(self.resource_dir, filename)) |
paulb@353 | 23 | |
paulb@353 | 24 | def get_document(self, document_identifier): |
paulb@353 | 25 | return qtxmldom.parse(self.prepare_document(document_identifier)) |
paulb@353 | 26 | |
paulb@353 | 27 | def get_elements(self, document_identifier): |
paulb@353 | 28 | doc = self.get_document(document_identifier) |
paulb@353 | 29 | return doc.getElementsByTagName(document_identifier + "-enum") |
paulb@353 | 30 | |
paulb@353 | 31 | def prepare_template(self, template_identifier): |
paulb@353 | 32 | filename = self.template_resources[template_identifier] |
paulb@353 | 33 | return os.path.abspath(os.path.join(self.resource_dir, filename)) |
paulb@353 | 34 | |
paulb@353 | 35 | def prepare_widget(self, template_identifier, widget_identifier, parent=None): |
paulb@353 | 36 | template_path = self.prepare_template(template_identifier) |
paulb@353 | 37 | fragment_name, widget_name = self.widget_resources[widget_identifier] |
paulb@353 | 38 | fragment_path = os.path.abspath(os.path.join(self.resource_dir, fragment_name)) |
paulb@353 | 39 | XSLForms.Prepare.ensure_qt_fragment(template_path, fragment_path, widget_name) |
paulb@353 | 40 | return qtui.QWidgetFactory.create(fragment_path, None, parent) |
paulb@353 | 41 | |
paulb@353 | 42 | def populate_list(self, field, elements): |
paulb@353 | 43 | current_text = field.currentText() |
paulb@353 | 44 | while field.count() > 0: |
paulb@353 | 45 | field.removeItem(0) |
paulb@353 | 46 | item = 0 |
paulb@353 | 47 | set = 0 |
paulb@353 | 48 | for element in elements: |
paulb@353 | 49 | text = element.getAttribute("value") |
paulb@353 | 50 | field.insertItem(text) |
paulb@353 | 51 | if text == current_text: |
paulb@353 | 52 | field.setCurrentItem(item) |
paulb@353 | 53 | set = 1 |
paulb@353 | 54 | item += 1 |
paulb@353 | 55 | if not set: |
paulb@353 | 56 | field.setCurrentItem(0) |
paulb@353 | 57 | |
paulb@353 | 58 | def reset_collection(self, field): |
paulb@353 | 59 | layout = field.layout() |
paulb@353 | 60 | for child in field.children(): |
paulb@353 | 61 | if child is not layout: |
paulb@353 | 62 | layout.remove(child) |
paulb@353 | 63 | child.deleteLater() |
paulb@353 | 64 | |
paulb@353 | 65 | class Factory: |
paulb@353 | 66 | |
paulb@353 | 67 | "A widget factory helper class." |
paulb@353 | 68 | |
paulb@353 | 69 | def __init__(self, ui_filename): |
paulb@353 | 70 | self.ui_filename = ui_filename |
paulb@353 | 71 | self.ui_doc = qtxmldom.parse(ui_filename) |
paulb@353 | 72 | |
paulb@353 | 73 | def connect(self, widget, obj): |
paulb@353 | 74 | |
paulb@353 | 75 | for connection in self.ui_doc.getElementsByTagName("connection"): |
paulb@353 | 76 | sender_name = self.get_text(connection.getElementsByTagName("sender")[0]).encode("utf-8") |
paulb@353 | 77 | signal_name = self.get_text(connection.getElementsByTagName("signal")[0]).encode("utf-8") |
paulb@353 | 78 | slot_name = self.get_text(connection.getElementsByTagName("slot")[0]).encode("utf-8") |
paulb@353 | 79 | |
paulb@353 | 80 | if widget.name() == sender_name: |
paulb@353 | 81 | senders = [widget] |
paulb@353 | 82 | else: |
paulb@353 | 83 | senders = self.find_widgets(widget, sender_name) |
paulb@353 | 84 | |
paulb@353 | 85 | slot = slot_name.split("(")[0] |
paulb@353 | 86 | if hasattr(obj, slot): |
paulb@353 | 87 | signal = qt.SIGNAL(signal_name) |
paulb@353 | 88 | for sender in senders: |
paulb@353 | 89 | qt.QObject.connect(sender, signal, getattr(obj, slot)) |
paulb@353 | 90 | |
paulb@353 | 91 | def find_widgets(self, widget, name): |
paulb@353 | 92 | widgets = [] |
paulb@353 | 93 | found = widget.child(name) |
paulb@353 | 94 | if found: |
paulb@353 | 95 | widgets.append(found) |
paulb@353 | 96 | for child in widget.children(): |
paulb@353 | 97 | widgets += self.find_widgets(child, name) |
paulb@353 | 98 | return widgets |
paulb@353 | 99 | |
paulb@353 | 100 | def get_text(self, node): |
paulb@353 | 101 | node.normalize() |
paulb@353 | 102 | return node.childNodes[0].nodeValue |
paulb@353 | 103 | |
paulb@353 | 104 | def find_widget_element(self, name): |
paulb@353 | 105 | for widget in self.ui_doc.getElementsByTagName("widget"): |
paulb@353 | 106 | for property in widget.getElementsByTagName("property"): |
paulb@353 | 107 | if property.getAttribute("name") == "name": |
paulb@353 | 108 | for cstring in property.getElementsByTagName("cstring"): |
paulb@353 | 109 | found_name = self.get_text(cstring) |
paulb@353 | 110 | if found_name == name: |
paulb@353 | 111 | return widget |
paulb@353 | 112 | return None |
paulb@353 | 113 | |
paulb@353 | 114 | # vim: tabstop=4 expandtab shiftwidth=4 |