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