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