1 #!/usr/bin/env python 2 3 """ 4 Common resource class functionality for PyQt-related applications. 5 6 Copyright (C) 2005 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU Lesser General Public License as published by the Free 10 Software Foundation; either version 3 of the License, or (at your option) any 11 later version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 details. 17 18 You should have received a copy of the GNU Lesser General Public License along 19 with this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 import os 23 from XSLForms.Resources.Common import CommonResource 24 25 class PyQtCommonResource(CommonResource): 26 27 "Common PyQt-compatible resource methods." 28 29 design_resources = {} 30 31 def get_document(self, document_identifier): 32 33 """ 34 Return a DOM-style document retrieved using the given 35 'document_identifier'. 36 37 Each implementation is free to choose its own DOM library. 38 """ 39 40 raise NotImplementedError, "get_document" 41 42 def get_elements(self, document_identifier): 43 doc = self.get_document(document_identifier) 44 45 # NOTE: Using special suffix. 46 47 return doc.getElementsByTagName(document_identifier + "-enum") 48 49 def prepare_design(self, design_identifier): 50 filename = self.design_resources[design_identifier] 51 return os.path.abspath(os.path.join(self.resource_dir, filename)) 52 53 def populate_list(self, field, elements): 54 55 "Populate the given 'field' using a list of DOM 'elements'." 56 57 current_text = field.currentText() 58 while field.count() > 0: 59 field.removeItem(0) 60 item = 0 61 set = 0 62 for element in elements: 63 text = element.getAttribute("value") 64 field.insertItem(text) 65 if text == current_text: 66 field.setCurrentItem(item) 67 set = 1 68 item += 1 69 if not set: 70 field.setCurrentItem(0) 71 72 def reset_collection(self, field): 73 74 "Empty the given collection 'field'." 75 76 layout = field.layout() 77 for child in field.children(): 78 if child is not layout: 79 layout.remove(child) 80 child.deleteLater() 81 82 # vim: tabstop=4 expandtab shiftwidth=4