decorators: Use memoize() to implement all factory functions
[git-cola.git] / cola / models / selection.py
blob98d396fe8ab4dd6040ea59b3c7bc4bf6ced98f43
1 """Provides a selection model to handle selection."""
3 from cola.models.observable import ObservableModel
4 from cola.decorators import memoize
7 @memoize
8 def selection_model():
9 """Provides access to a static SelectionModel instance."""
10 return SelectionModel()
13 def selection():
14 """Return the current selection."""
15 model = selection_model()
16 return (model.staged, model.modified, model.unmerged, model.untracked)
19 def single_selection():
20 """Scan across staged, modified, etc. and return a single item."""
21 staged, modified, unmerged, untracked = selection()
22 s = None
23 m = None
24 um = None
25 ut = None
26 if staged:
27 s = staged[0]
28 elif modified:
29 m = modified[0]
30 elif unmerged:
31 um = unmerged[0]
32 elif untracked:
33 ut = untracked[0]
34 return s, m, um, ut
37 def filename():
38 s, m, um, ut = single_selection()
39 if s:
40 return s
41 if m:
42 return m
43 if um:
44 return um
45 if ut:
46 return ut
47 return None
50 class SelectionModel(ObservableModel):
51 """Provides information about selected file paths."""
52 # Notification message sent out when selection changes
53 message_selection_changed = 'selection_changed'
55 # These properties wrap the individual selection items
56 # to provide higher-level pseudo-selections.
57 unstaged = property(lambda self: self.modified +
58 self.unmerged +
59 self.untracked)
61 all = property(lambda self: self.staged +
62 self.modified +
63 self.unmerged +
64 self.untracked)
66 def __init__(self):
67 ObservableModel.__init__(self)
68 self.staged = []
69 self.modified = []
70 self.unmerged = []
71 self.untracked = []
73 def set_selection(self, staged, modified, unmerged, untracked):
74 """Set the new selection."""
75 self.set_staged(staged)
76 self.set_modified(modified)
77 self.set_unmerged(unmerged)
78 self.set_untracked(untracked)
79 self.notify_message_observers(self.message_selection_changed)