1
#-------------------------------------------------------------------------------
3
# Copyright(c) 2009, Enthought, Inc.
6
# This software is provided without warranty under the terms of the BSD
7
# license included in enthought/LICENSE.txt and may be redistributed only
8
# under the conditions described in the aforementioned license. The license
9
# is also available online at http://www.enthought.com/licenses/BSD.txt
11
# Thanks for using Enthought open source!
13
# Author: Evan Patterson
16
#-------------------------------------------------------------------------------
18
""" Defines a text editor which displays a text field and maintains a history
19
of previously entered values.
22
#-------------------------------------------------------------------------------
24
#-------------------------------------------------------------------------------
26
from pyface.qt import QtCore, QtGui
28
from editor import Editor
30
#-------------------------------------------------------------------------------
31
# '_HistoryEditor' class:
32
#-------------------------------------------------------------------------------
34
class _HistoryEditor(Editor):
35
""" Simple style text editor, which displays a text field and maintains a
36
history of previously entered values, the maximum number of which is
37
specified by the 'entries' trait of the HistoryEditor factory.
40
#---------------------------------------------------------------------------
42
#---------------------------------------------------------------------------
44
def init(self, parent):
45
""" Finishes initializing the editor by creating the underlying toolkit
48
self.control = control = QtGui.QComboBox()
49
control.setEditable(True)
50
control.setInsertPolicy(QtGui.QComboBox.InsertAtTop)
52
if self.factory.entries > 0:
53
signal = QtCore.SIGNAL('rowsInserted(const QModelIndex&, int, int)')
54
QtCore.QObject.connect(control.model(), signal, self._truncate)
56
if self.factory.auto_set:
57
signal = QtCore.SIGNAL('editTextChanged(QString)')
59
signal = QtCore.SIGNAL('activated(QString)')
60
QtCore.QObject.connect(control, signal, self.update_object)
64
def update_object(self, text):
65
""" Handles the user entering input data in the edit control.
67
if not self._no_update:
68
self.value = unicode(text)
70
def update_editor(self):
71
""" Updates the editor when the object trait changes externally to the
74
self._no_update = True
75
self.control.setEditText(self.str_value)
76
self._no_update = False
78
#-- UI preference save/restore interface -----------------------------------
80
def restore_prefs(self, prefs):
81
""" Restores any saved user preference information associated with the
84
history = prefs.get('history')
86
self._no_update = True
87
self.control.addItems(history[:self.factory.entries])
89
# Adding items sets the edit text, so we reset it afterwards:
90
self.control.setEditText(self.str_value)
92
self._no_update = False
95
""" Returns any user preference information associated with the editor.
97
history = [ str(self.control.itemText(index))
98
for index in xrange(self.control.count()) ]
100
# If the view closed successfully, update the history with the current
101
# editor value, as long it is different from the current object value:
103
current = str(self.control.currentText())
104
if current != self.str_value:
105
history.insert(0, current)
107
return { 'history': history }
109
#---------------------------------------------------------------------------
110
# '_HistoryEditor' private interface:
111
#---------------------------------------------------------------------------
113
def _truncate(self, parent, start, end):
114
""" Handle items being added to the combo box. If there are too many,
115
remove items at the end.
117
diff = self.control.count() - self.factory.entries
119
for i in xrange(diff):
120
self.control.removeItem(self.factory.entries)