Package Camelot :: Package camelot :: Package view :: Package wizard :: Module update_value |
[frames] | no frames]

# Source Code for Module Camelot.camelot.view.wizard.update_value

      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  """A wizard to update a field on a collection of objects"""
     29
     30  from PyQt4 import QtGui, QtCore
     31
     32  from camelot.view.controls.editors import ChoicesEditor
     33  from camelot.view.controls import delegates
     34  from camelot.core.utils import ugettext_lazy as _
     35  from camelot.view.model_thread import post
     36  from camelot.view.proxy import ValueLoading
     37  from camelot.view.wizard.pages.update_entities_page import UpdateEntitiesPage
     38  from camelot.view.controls.editors.customeditor import editingFinished
     39
     40 -class ReplaceContentsData(object):
     41
     42 -    def __init__(self):
     43          self.field = None
     44          self.value = None
     45
     46 -class SelectValuePage(QtGui.QWizardPage):
     47      """Page to select a value to update"""
     48
     49      title = _('Replace field contents')
     50      sub_title = _('Select the field to update and enter its new value')
     51
     52 -    def __init__(self, parent, admin, data):
     53          super(SelectValuePage, self).__init__(parent)
     54          self.setTitle( unicode(self.title) )
     55          self.setSubTitle( unicode(self.sub_title) )
     56
     57          self.editor = ChoicesEditor()
     58          layout = QtGui.QVBoxLayout()
     59          layout.addWidget(self.editor)
     60          self.setLayout(layout)
     61          self._fields = {}
     62          self._data = data
     63          self.connect(self.editor, QtCore.SIGNAL('currentIndexChanged(int)'), self.field_changed)
     64          self._value_editor = None
     65
     66          post(admin.get_all_fields_and_attributes, self.set_fields)
     67
     68 -    def set_fields(self, fields):
     69          self._fields = fields
     70
     71          def filter(attributes):
     72              if not attributes['editable']:
     73                  return False
     74              if attributes['delegate'] in (delegates.One2ManyDelegate, delegates.ManyToManyDelegate):
     75                  return False
     76              return True
     77
     78          choices = [(field, attributes['name']) for field, attributes in fields.items() if filter(attributes)]
     79          self.editor.set_choices(choices)
     80          self.editor.set_value((choices+[(None,None)])[1][0])
     81          self.field_changed(0)
     82
     83 -    def value_changed(self):
     84          if self._value_editor:
     85              self._data.value = self._value_editor.get_value()
     86
     87 -    def field_changed(self, index):
     88          if self._value_editor:
     89              self.layout().removeWidget(self._value_editor)
     90              self._value_editor.deleteLater()
     91              self._value_editor = None
     92          selected_field = self.editor.get_value()
     93          if selected_field!=ValueLoading:
     94              self._data.field = selected_field
     95              self._data.value = None
     96              field_attributes = self._fields[selected_field]
     97              delegate = field_attributes['delegate'](**field_attributes)
     98              option = QtGui.QStyleOptionViewItem()
     99              option.version = 5
    100              self._value_editor = delegate.createEditor( self, option, None )
    101              self.connect(self._value_editor, editingFinished, self.value_changed)
    102              self.layout().addWidget(self._value_editor)
    103              if isinstance(delegate, delegates.Many2OneDelegate):
    104                  self._value_editor.set_value(lambda:None)
    105              else:
    106                  self._value_editor.set_value(None)
    107
    108 -class ReplaceContentsPage(UpdateEntitiesPage):
    109
    110      title = _('Replace field contents')
    111
    112 -    def __init__(self, parent, collection_getter, data):
    113          super(ReplaceContentsPage, self).__init__(parent=parent, collection_getter=collection_getter)
    114          self._data = data
    115
    116 -    def update_entity(self, obj):
    117          setattr(obj, self._data.field, self._data.value)
    118
    119 -class UpdateValueWizard(QtGui.QWizard):
    120      """This wizard presents the user with a selection of the possible fields to
    121      update and a new value.  Then this field is changed for all objects in a given
    122      collection"""
    123
    124      select_value_page = SelectValuePage
    125
    126      window_title = _('Replace')
    127
    128 -    def __init__(self, parent=None, selection_getter=None, admin=None):
    129          """:param model: a collection proxy on which to replace the field contents"""
    130          super(UpdateValueWizard, self).__init__(parent)
    131          self.setWindowTitle( unicode(self.window_title) )
    132          data = ReplaceContentsData()
    133          assert selection_getter
    134          assert admin
    135          self.addPage(SelectValuePage(parent=self, admin=admin, data=data))
    136          self.addPage(ReplaceContentsPage(parent=self, collection_getter=selection_getter, data=data))
    137