1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
41
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
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):
137