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