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