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