1 #!/usr/bin/env python 2 3 """ 4 Common resource class functionality. 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 os 24 25 class CommonResource: 26 27 "Common resource methods." 28 29 document_resources = {} 30 resource_dir = None 31 32 def prepare_document(self, document_identifier): 33 34 """ 35 Prepare a document using the given 'document_identifier'. 36 37 Return the full path of the document for use either as the source 38 document or as a reference with 'send_output' or 'get_result'. 39 """ 40 41 filename = self.document_resources[document_identifier] 42 return os.path.abspath(os.path.join(self.resource_dir, filename)) 43 44 class PyQtCommonResource(CommonResource): 45 46 "Common PyQt-compatible resource methods." 47 48 design_resources = {} 49 50 def get_document(self, document_identifier): 51 52 """ 53 Return a DOM-style document retrieved using the given 54 'document_identifier'. 55 56 Each implementation is free to choose its own DOM library. 57 """ 58 59 raise NotImplementedError, "get_document" 60 61 def get_elements(self, document_identifier): 62 doc = self.get_document(document_identifier) 63 64 # NOTE: Using special suffix. 65 66 return doc.getElementsByTagName(document_identifier + "-enum") 67 68 def prepare_design(self, design_identifier): 69 filename = self.design_resources[design_identifier] 70 return os.path.abspath(os.path.join(self.resource_dir, filename)) 71 72 def populate_list(self, field, elements): 73 74 "Populate the given 'field' using a list of DOM 'elements'." 75 76 current_text = field.currentText() 77 while field.count() > 0: 78 field.removeItem(0) 79 item = 0 80 set = 0 81 for element in elements: 82 text = element.getAttribute("value") 83 field.insertItem(text) 84 if text == current_text: 85 field.setCurrentItem(item) 86 set = 1 87 item += 1 88 if not set: 89 field.setCurrentItem(0) 90 91 def reset_collection(self, field): 92 93 "Empty the given collection 'field'." 94 95 layout = field.layout() 96 for child in field.children(): 97 if child is not layout: 98 layout.remove(child) 99 child.deleteLater() 100 101 # vim: tabstop=4 expandtab shiftwidth=4