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