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