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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 """ 22 23 import XSLForms.Prepare 24 import XSLForms.Resources.Common 25 import qt, qtui, qtxmldom 26 import os 27 28 class XSLFormsResource(XSLForms.Resources.Common.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 class Factory: 48 49 "A widget factory helper class." 50 51 def __init__(self, ui_filename): 52 self.ui_filename = ui_filename 53 self.ui_doc = qtxmldom.parse(ui_filename) 54 55 def connect(self, widget, obj): 56 57 for connection in self.ui_doc.getElementsByTagName("connection"): 58 sender_name = self.get_text(connection.getElementsByTagName("sender")[0]).encode("utf-8") 59 signal_name = self.get_text(connection.getElementsByTagName("signal")[0]).encode("utf-8") 60 slot_name = self.get_text(connection.getElementsByTagName("slot")[0]).encode("utf-8") 61 62 if widget.name() == sender_name: 63 senders = [widget] 64 else: 65 senders = self.find_widgets(widget, sender_name) 66 67 slot = slot_name.split("(")[0] 68 if hasattr(obj, slot): 69 signal = qt.SIGNAL(signal_name) 70 for sender in senders: 71 qt.QObject.connect(sender, signal, getattr(obj, slot)) 72 73 def get_text(self, node): 74 node.normalize() 75 return node.childNodes[0].nodeValue 76 77 def find_widgets(self, widget, name): 78 widgets = [] 79 found = widget.child(name) 80 if found: 81 widgets.append(found) 82 for child in widget.children(): 83 widgets += self.find_widgets(child, name) 84 return widgets 85 86 # vim: tabstop=4 expandtab shiftwidth=4