~ubuntu-branches/ubuntu/utopic/python-traitsui/utopic

« back to all changes in this revision

Viewing changes to traitsui/qt4/history_editor.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-09 13:57:39 UTC
  • Revision ID: james.westby@ubuntu.com-20110709135739-x5u20q86huissmn1
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#-------------------------------------------------------------------------------
 
2
#
 
3
#  Copyright(c) 2009, Enthought, Inc.
 
4
#  All rights reserved.
 
5
#
 
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
 
10
#
 
11
#  Thanks for using Enthought open source!
 
12
#
 
13
#  Author: Evan Patterson
 
14
#  Date:   08/21/2009
 
15
#
 
16
#-------------------------------------------------------------------------------
 
17
 
 
18
""" Defines a text editor which displays a text field and maintains a history
 
19
    of previously entered values.
 
20
"""
 
21
 
 
22
#-------------------------------------------------------------------------------
 
23
#  Imports:
 
24
#-------------------------------------------------------------------------------
 
25
 
 
26
from pyface.qt import QtCore, QtGui
 
27
 
 
28
from editor import Editor
 
29
 
 
30
#-------------------------------------------------------------------------------
 
31
#  '_HistoryEditor' class:
 
32
#-------------------------------------------------------------------------------
 
33
 
 
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.
 
38
    """
 
39
 
 
40
    #---------------------------------------------------------------------------
 
41
    #  'Editor' interface:
 
42
    #---------------------------------------------------------------------------
 
43
 
 
44
    def init(self, parent):
 
45
        """ Finishes initializing the editor by creating the underlying toolkit
 
46
            widget.
 
47
        """
 
48
        self.control = control = QtGui.QComboBox()
 
49
        control.setEditable(True)
 
50
        control.setInsertPolicy(QtGui.QComboBox.InsertAtTop)
 
51
 
 
52
        if self.factory.entries > 0:
 
53
            signal = QtCore.SIGNAL('rowsInserted(const QModelIndex&, int, int)')
 
54
            QtCore.QObject.connect(control.model(), signal, self._truncate)
 
55
 
 
56
        if self.factory.auto_set:
 
57
            signal = QtCore.SIGNAL('editTextChanged(QString)')
 
58
        else:
 
59
            signal = QtCore.SIGNAL('activated(QString)')
 
60
        QtCore.QObject.connect(control, signal, self.update_object)
 
61
 
 
62
        self.set_tooltip()
 
63
 
 
64
    def update_object(self, text):
 
65
        """ Handles the user entering input data in the edit control.
 
66
        """
 
67
        if not self._no_update:
 
68
            self.value = unicode(text)
 
69
 
 
70
    def update_editor(self):
 
71
        """ Updates the editor when the object trait changes externally to the
 
72
            editor.
 
73
        """
 
74
        self._no_update = True
 
75
        self.control.setEditText(self.str_value)
 
76
        self._no_update = False
 
77
 
 
78
    #-- UI preference save/restore interface -----------------------------------
 
79
 
 
80
    def restore_prefs(self, prefs):
 
81
        """ Restores any saved user preference information associated with the
 
82
            editor.
 
83
        """
 
84
        history = prefs.get('history')
 
85
        if history:
 
86
            self._no_update = True
 
87
            self.control.addItems(history[:self.factory.entries])
 
88
 
 
89
            # Adding items sets the edit text, so we reset it afterwards:
 
90
            self.control.setEditText(self.str_value)
 
91
 
 
92
            self._no_update = False
 
93
 
 
94
    def save_prefs(self):
 
95
        """ Returns any user preference information associated with the editor.
 
96
        """
 
97
        history = [ str(self.control.itemText(index))
 
98
                    for index in xrange(self.control.count()) ]
 
99
 
 
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:
 
102
        if self.ui.result:
 
103
            current = str(self.control.currentText())
 
104
            if current != self.str_value:
 
105
                history.insert(0, current)
 
106
 
 
107
        return { 'history': history }
 
108
 
 
109
    #---------------------------------------------------------------------------
 
110
    #  '_HistoryEditor' private interface:
 
111
    #---------------------------------------------------------------------------
 
112
 
 
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.
 
116
        """
 
117
        diff = self.control.count() - self.factory.entries
 
118
        if diff > 0:
 
119
            for i in xrange(diff):
 
120
                self.control.removeItem(self.factory.entries)