| 1 | # ============================================================================ |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2007-2008 Conceptive Engineering bvba. All rights reserved. |
|---|
| 4 | # www.conceptive.be / project-camelot@conceptive.be |
|---|
| 5 | # |
|---|
| 6 | # This file is part of the Camelot Library. |
|---|
| 7 | # |
|---|
| 8 | # This file may be used under the terms of the GNU General Public |
|---|
| 9 | # License version 2.0 as published by the Free Software Foundation |
|---|
| 10 | # and appearing in the file LICENSE.GPL included in the packaging of |
|---|
| 11 | # this file. Please review the following information to ensure GNU |
|---|
| 12 | # General Public Licensing requirements will be met: |
|---|
| 13 | # http://www.trolltech.com/products/qt/opensource.html |
|---|
| 14 | # |
|---|
| 15 | # If you are unsure which license is appropriate for your use, please |
|---|
| 16 | # review the following information: |
|---|
| 17 | # http://www.trolltech.com/products/qt/licensing.html or contact |
|---|
| 18 | # project-camelot@conceptive.be. |
|---|
| 19 | # |
|---|
| 20 | # This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
|---|
| 21 | # WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
|---|
| 22 | # |
|---|
| 23 | # For use of this library in commercial applications, please contact |
|---|
| 24 | # project-camelot@conceptive.be |
|---|
| 25 | # |
|---|
| 26 | # ============================================================================ |
|---|
| 27 | |
|---|
| 28 | from PyQt4 import QtGui, QtCore |
|---|
| 29 | |
|---|
| 30 | from camelot.core.utils import ugettext as _ |
|---|
| 31 | from camelot.view.art import Icon |
|---|
| 32 | from camelot.view.model_thread import post |
|---|
| 33 | |
|---|
| 34 | class FormPage(QtGui.QWizardPage): |
|---|
| 35 | """FormPage is a generic wizard page that displays a form for an object |
|---|
| 36 | in a wizard page, subclass this class to use it. The class attribute 'Data' |
|---|
| 37 | should be the class of the object to be used to store the form information. |
|---|
| 38 | |
|---|
| 39 | To access the data stored by the wizard form into a data object, use its |
|---|
| 40 | get_data method. |
|---|
| 41 | |
|---|
| 42 | The 'Next' button will only be activated when the form is complete. |
|---|
| 43 | """ |
|---|
| 44 | |
|---|
| 45 | icon = Icon('tango/32x32/mimetypes/x-office-spreadsheet.png') |
|---|
| 46 | title = None |
|---|
| 47 | sub_title = None |
|---|
| 48 | Data = None |
|---|
| 49 | Admin = None |
|---|
| 50 | |
|---|
| 51 | def __init__(self, parent=None): |
|---|
| 52 | |
|---|
| 53 | from camelot.view.controls.formview import FormWidget |
|---|
| 54 | from camelot.view.proxy.collection_proxy import CollectionProxy |
|---|
| 55 | |
|---|
| 56 | super(FormPage, self).__init__(parent) |
|---|
| 57 | assert self.Data |
|---|
| 58 | self.setTitle(unicode(self.get_title())) |
|---|
| 59 | self.setSubTitle(unicode(self.get_sub_title())) |
|---|
| 60 | self.setPixmap(QtGui.QWizard.LogoPixmap, self.get_icon().getQPixmap()) |
|---|
| 61 | self._data = self.Data() |
|---|
| 62 | self._complete = False |
|---|
| 63 | |
|---|
| 64 | admin = self.get_admin() |
|---|
| 65 | self._model = CollectionProxy(admin, lambda:[self._data], admin.get_fields) |
|---|
| 66 | |
|---|
| 67 | layout = QtGui.QVBoxLayout() |
|---|
| 68 | form = FormWidget(admin) |
|---|
| 69 | self.connect(form, FormWidget.changed_signal, self._form_changed) |
|---|
| 70 | form.set_model(self._model) |
|---|
| 71 | layout.addWidget(form) |
|---|
| 72 | self.setLayout(layout) |
|---|
| 73 | |
|---|
| 74 | def _form_changed(self): |
|---|
| 75 | |
|---|
| 76 | def is_valid(): |
|---|
| 77 | return self._model.get_validator().isValid(0) |
|---|
| 78 | |
|---|
| 79 | post(is_valid, self._change_complete) |
|---|
| 80 | |
|---|
| 81 | def _change_complete(self, complete): |
|---|
| 82 | self._complete = complete |
|---|
| 83 | self.emit(QtCore.SIGNAL('completeChanged ()')) |
|---|
| 84 | |
|---|
| 85 | def isComplete(self): |
|---|
| 86 | return self._complete |
|---|
| 87 | |
|---|
| 88 | def get_admin(self): |
|---|
| 89 | from camelot.view.application_admin import get_application_admin |
|---|
| 90 | app_admin = get_application_admin() |
|---|
| 91 | if self.Admin: |
|---|
| 92 | return self.Admin(app_admin, self.Data) |
|---|
| 93 | return app_admin.get_entity_admin(self.Data) |
|---|
| 94 | |
|---|
| 95 | def get_title(self): |
|---|
| 96 | return self.title or self.get_admin().get_verbose_name() |
|---|
| 97 | |
|---|
| 98 | def get_sub_title(self): |
|---|
| 99 | return self.sub_title or _('Please complete') |
|---|
| 100 | |
|---|
| 101 | def get_icon(self): |
|---|
| 102 | return self.icon |
|---|
| 103 | |
|---|
| 104 | def get_data(self): |
|---|
| 105 | return self._data |
|---|