Home | Trees | Indices | Help |
|
---|
|
1 """Class definition for editing the detail view.""" 2 3 # ***** BEGIN LICENSE BLOCK ***** 4 # 5 # Copyright (c) 2007-2011, Python File Format Interface 6 # All rights reserved. 7 # 8 # Redistribution and use in source and binary forms, with or without 9 # modification, are permitted provided that the following conditions 10 # are met: 11 # 12 # * Redistributions of source code must retain the above copyright 13 # notice, this list of conditions and the following disclaimer. 14 # 15 # * Redistributions in binary form must reproduce the above 16 # copyright notice, this list of conditions and the following 17 # disclaimer in the documentation and/or other materials provided 18 # with the distribution. 19 # 20 # * Neither the name of the Python File Format Interface 21 # project nor the names of its contributors may be used to endorse 22 # or promote products derived from this software without specific 23 # prior written permission. 24 # 25 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 26 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 27 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 28 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 29 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 30 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 31 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 32 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 33 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 35 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 # POSSIBILITY OF SUCH DAMAGE. 37 # 38 # ***** END LICENSE BLOCK ***** 39 40 from PyQt4 import QtCore, QtGui 41 42 # each delegate type corresponds to a QtGui delegate type 43 # (see _checkValidEditor for more details) 44 from pyffi.object_models.editable import EditableComboBox # -> QComboBox 45 from pyffi.object_models.editable import EditableFloatSpinBox # -> QDoubleSpinBox 46 from pyffi.object_models.editable import EditableSpinBox # -> QSpinBox 47 from pyffi.object_models.editable import EditableTextEdit # -> QTextEdit 48 from pyffi.object_models.editable import EditableLineEdit # -> QLineEdit 49 50 # implementation details: 51 # http://doc.trolltech.com/4.3/model-view-delegate.html 52 # http://doc.trolltech.com/4.3/qitemdelegate.html#details54 """Defines an editor for data in the detail view.""" 5519957 """This function checks that the delegate class has the correct editor. 58 If data and editor do not correspond to one another, then a ValueError is 59 raised. 60 61 All functions checking for delegate base classes should respect 62 the order in this function, because a class may derive from more than 63 one delegate class. So this function determines which editor is 64 preferred if this happens. The order is: 65 66 - ComboBox 67 - FloatSpinBox 68 - SpinBox 69 - TextEdit 70 - LineEdit 71 72 This function is only used for internal debugging purposes. 73 """ 74 # the general idea is to check complex instances (that derive from 75 # simpler ones) first 76 77 # check combo box 78 # (some combo types may also derive from spin box such as bools, 79 # in that case prefer the combo box representation) 80 if isinstance(data, EditableComboBox): 81 isvalid = isinstance(editor, QtGui.QComboBox) 82 # check float spin box 83 elif isinstance(data, EditableFloatSpinBox): 84 isvalid = isinstance(editor, QtGui.QDoubleSpinBox) 85 # check spin box 86 elif isinstance(data, EditableSpinBox): 87 isvalid = isinstance(editor, QtGui.QSpinBox) 88 # check text editor 89 elif isinstance(data, EditableTextEdit): 90 isvalid = isinstance(editor, QtGui.QTextEdit) 91 # check line editor 92 elif isinstance(data, EditableLineEdit): 93 isvalid = isinstance(editor, QtGui.QLineEdit) 94 else: 95 # data has no delegate class, which is classified as invalid 96 isvalid = False 97 98 # if invalid, raise ValueError 99 if not isvalid: 100 raise ValueError("data %s has bad editor %s" 101 % (data.__class__.__name__, 102 editor.__class__.__name__))103105 """Returns the widget used to change data.""" 106 # check if index is valid 107 if not index.isValid(): 108 return None 109 # get the data 110 node = index.internalPointer().data.node 111 # determine editor by checking for delegate base classes 112 # (see _checkValidEditor for the correct delegate preference order) 113 if isinstance(node, EditableComboBox): 114 # a general purpose combo box 115 editor = QtGui.QComboBox(parent) 116 for key in node.get_editor_keys(): 117 editor.addItem(key) 118 elif isinstance(node, EditableFloatSpinBox): 119 # a spinbox for floats 120 editor = QtGui.QDoubleSpinBox(parent) 121 editor.setMinimum(node.get_editor_minimum()) 122 editor.setMaximum(node.get_editor_maximum()) 123 editor.setDecimals(node.get_editor_decimals()) 124 elif isinstance(node, EditableSpinBox): 125 # an integer spin box 126 editor = QtGui.QSpinBox(parent) 127 editor.setMinimum(node.get_editor_minimum()) 128 # work around a qt "bug": maximum must be C type "int" 129 # so cannot be larger than 0x7fffffff 130 editor.setMaximum(min(node.get_editor_maximum(), 0x7fffffff)) 131 elif isinstance(node, EditableTextEdit): 132 # a text editor 133 editor = QtGui.QTextEdit(parent) 134 elif isinstance(node, EditableLineEdit): 135 # a line editor 136 editor = QtGui.QLineEdit(parent) 137 else: 138 return None 139 # check validity 140 self._checkValidEditor(node, editor) 141 # return the editor 142 return editor143145 """Provides the widget with data to manipulate.""" 146 # check if index is valid 147 if not index.isValid(): 148 return None 149 # determine the data and its value 150 node = index.internalPointer().data.node 151 editorvalue = node.get_editor_value() 152 # check validity of editor 153 self._checkValidEditor(node, editor) 154 # set editor node 155 # (see _checkValidEditor for the correct delegate preference order) 156 if isinstance(node, EditableComboBox): 157 # a combo box: set the index 158 editor.setCurrentIndex(editorvalue) 159 elif isinstance(node, EditableSpinBox): 160 # a (possibly float) spinbox: simply set the value 161 editor.setValue(editorvalue) 162 elif isinstance(node, EditableLineEdit): 163 # a text editor: set the text 164 editor.setText(editorvalue)165167 """Ensures that the editor is displayed correctly with respect to the 168 item view.""" 169 editor.setGeometry(option.rect)170172 """Returns updated data to the model.""" 173 # check if index is valid 174 if not index.isValid(): 175 return None 176 # get the data 177 node = index.internalPointer().data.node 178 # check validity of editor 179 self._checkValidEditor(node, editor) 180 # set model data from editor value 181 # (see _checkValidEditor for the correct delegate preference order) 182 if isinstance(node, EditableComboBox): 183 # a combo box: get the value from the current index 184 editorvalue = editor.currentIndex() 185 elif isinstance(node, EditableSpinBox): 186 # a regular (float) spin box 187 editorvalue = editor.value() 188 elif isinstance(node, EditableLineEdit): 189 # a text editor 190 editorvalue = editor.text() 191 else: 192 # should not happen: no editor 193 print("WARNING: cannot set model data for type %s" 194 % node.__class__.__name__) 195 return 196 # set the model data 197 # EditRole ensures that setData uses set_editor_value to set the data 198 model.setData(index, QtCore.QVariant(editorvalue), QtCore.Qt.EditRole)
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Mon Oct 10 19:04:05 2011 | http://epydoc.sourceforge.net |