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@354 | 24 | import XSLForms.Resources.Common |
paulb@354 | 25 | import qt, qtui, qtxmldom |
paulb@353 | 26 | import os |
paulb@353 | 27 | |
paulb@354 | 28 | class XSLFormsResource(XSLForms.Resources.Common.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@353 | 34 | def get_document(self, document_identifier): |
paulb@353 | 35 | return qtxmldom.parse(self.prepare_document(document_identifier)) |
paulb@353 | 36 | |
paulb@354 | 37 | def prepare_widget(self, design_identifier, widget_identifier, parent=None): |
paulb@354 | 38 | design_path = self.prepare_design(design_identifier) |
paulb@353 | 39 | fragment_name, widget_name = self.widget_resources[widget_identifier] |
paulb@353 | 40 | fragment_path = os.path.abspath(os.path.join(self.resource_dir, fragment_name)) |
paulb@354 | 41 | XSLForms.Prepare.ensure_qt_fragment(design_path, fragment_path, widget_name) |
paulb@353 | 42 | return qtui.QWidgetFactory.create(fragment_path, None, parent) |
paulb@353 | 43 | |
paulb@353 | 44 | def populate_list(self, field, elements): |
paulb@353 | 45 | current_text = field.currentText() |
paulb@353 | 46 | while field.count() > 0: |
paulb@353 | 47 | field.removeItem(0) |
paulb@353 | 48 | item = 0 |
paulb@353 | 49 | set = 0 |
paulb@353 | 50 | for element in elements: |
paulb@353 | 51 | text = element.getAttribute("value") |
paulb@353 | 52 | field.insertItem(text) |
paulb@353 | 53 | if text == current_text: |
paulb@353 | 54 | field.setCurrentItem(item) |
paulb@353 | 55 | set = 1 |
paulb@353 | 56 | item += 1 |
paulb@353 | 57 | if not set: |
paulb@353 | 58 | field.setCurrentItem(0) |
paulb@353 | 59 | |
paulb@353 | 60 | def reset_collection(self, field): |
paulb@353 | 61 | layout = field.layout() |
paulb@353 | 62 | for child in field.children(): |
paulb@353 | 63 | if child is not layout: |
paulb@353 | 64 | layout.remove(child) |
paulb@353 | 65 | child.deleteLater() |
paulb@353 | 66 | |
paulb@353 | 67 | class Factory: |
paulb@353 | 68 | |
paulb@353 | 69 | "A widget factory helper class." |
paulb@353 | 70 | |
paulb@353 | 71 | def __init__(self, ui_filename): |
paulb@353 | 72 | self.ui_filename = ui_filename |
paulb@353 | 73 | self.ui_doc = qtxmldom.parse(ui_filename) |
paulb@353 | 74 | |
paulb@353 | 75 | def connect(self, widget, obj): |
paulb@353 | 76 | |
paulb@353 | 77 | for connection in self.ui_doc.getElementsByTagName("connection"): |
paulb@353 | 78 | sender_name = self.get_text(connection.getElementsByTagName("sender")[0]).encode("utf-8") |
paulb@353 | 79 | signal_name = self.get_text(connection.getElementsByTagName("signal")[0]).encode("utf-8") |
paulb@353 | 80 | slot_name = self.get_text(connection.getElementsByTagName("slot")[0]).encode("utf-8") |
paulb@353 | 81 | |
paulb@353 | 82 | if widget.name() == sender_name: |
paulb@353 | 83 | senders = [widget] |
paulb@353 | 84 | else: |
paulb@353 | 85 | senders = self.find_widgets(widget, sender_name) |
paulb@353 | 86 | |
paulb@353 | 87 | slot = slot_name.split("(")[0] |
paulb@353 | 88 | if hasattr(obj, slot): |
paulb@353 | 89 | signal = qt.SIGNAL(signal_name) |
paulb@353 | 90 | for sender in senders: |
paulb@353 | 91 | qt.QObject.connect(sender, signal, getattr(obj, slot)) |
paulb@353 | 92 | |
paulb@353 | 93 | def find_widgets(self, widget, name): |
paulb@353 | 94 | widgets = [] |
paulb@353 | 95 | found = widget.child(name) |
paulb@353 | 96 | if found: |
paulb@353 | 97 | widgets.append(found) |
paulb@353 | 98 | for child in widget.children(): |
paulb@353 | 99 | widgets += self.find_widgets(child, name) |
paulb@353 | 100 | return widgets |
paulb@353 | 101 | |
paulb@353 | 102 | def get_text(self, node): |
paulb@353 | 103 | node.normalize() |
paulb@353 | 104 | return node.childNodes[0].nodeValue |
paulb@353 | 105 | |
paulb@353 | 106 | def find_widget_element(self, name): |
paulb@353 | 107 | for widget in self.ui_doc.getElementsByTagName("widget"): |
paulb@353 | 108 | for property in widget.getElementsByTagName("property"): |
paulb@353 | 109 | if property.getAttribute("name") == "name": |
paulb@353 | 110 | for cstring in property.getElementsByTagName("cstring"): |
paulb@353 | 111 | found_name = self.get_text(cstring) |
paulb@353 | 112 | if found_name == name: |
paulb@353 | 113 | return widget |
paulb@353 | 114 | return None |
paulb@353 | 115 | |
paulb@353 | 116 | # vim: tabstop=4 expandtab shiftwidth=4 |