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