1 #!/usr/bin/env python 2 3 """ 4 Resources for use with PyQt. 5 6 Copyright (C) 2005 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU Lesser General Public License as published by the Free 10 Software Foundation; either version 3 of the License, or (at your option) any 11 later version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 details. 17 18 You should have received a copy of the GNU Lesser General Public License along 19 with this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 import XSLForms.Prepare 23 import XSLForms.Resources.PyQtCommon 24 import qt, qtui, qtxmldom 25 import os 26 27 class XSLFormsResource(XSLForms.Resources.PyQtCommon.PyQtCommonResource): 28 29 "An XSLForms resource for use with PyQt." 30 31 widget_resources = {} 32 33 def __init__(self, design_identifier): 34 self.factory = Factory(self.prepare_design(design_identifier)) 35 36 def get_document(self, document_identifier): 37 return qtxmldom.parse(self.prepare_document(document_identifier)) 38 39 def prepare_widget(self, design_identifier, widget_identifier, parent=None): 40 design_path = self.prepare_design(design_identifier) 41 fragment_name, widget_name = self.widget_resources[widget_identifier] 42 fragment_path = os.path.abspath(os.path.join(self.resource_dir, fragment_name)) 43 XSLForms.Prepare.ensure_qt_fragment(design_path, fragment_path, widget_name) 44 return qtui.QWidgetFactory.create(fragment_path, None, parent) 45 46 def request_refresh(self, *kw, **args): 47 self.form_refresh(*kw, **args) 48 49 class Factory: 50 51 "A widget factory helper class." 52 53 def __init__(self, ui_filename): 54 self.ui_filename = ui_filename 55 self.ui_doc = qtxmldom.parse(ui_filename) 56 57 def connect(self, widget, obj): 58 59 for connection in self.ui_doc.getElementsByTagName("connection"): 60 sender_name = self.get_text(connection.getElementsByTagName("sender")[0]).encode("utf-8") 61 signal_name = self.get_text(connection.getElementsByTagName("signal")[0]).encode("utf-8") 62 slot_name = self.get_text(connection.getElementsByTagName("slot")[0]).encode("utf-8") 63 64 if widget.name() == sender_name: 65 senders = [widget] 66 else: 67 senders = self.find_widgets(widget, sender_name) 68 69 slot = slot_name.split("(")[0] 70 if hasattr(obj, slot): 71 signal = qt.SIGNAL(signal_name) 72 for sender in senders: 73 qt.QObject.connect(sender, signal, getattr(obj, slot)) 74 75 def get_text(self, node): 76 node.normalize() 77 return node.childNodes[0].nodeValue 78 79 def find_widgets(self, widget, name): 80 widgets = [] 81 found = widget.child(name) 82 if found: 83 widgets.append(found) 84 for child in widget.children() or []: 85 widgets += self.find_widgets(child, name) 86 return widgets 87 88 # vim: tabstop=4 expandtab shiftwidth=4