/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/UI/selection.py
ViewVC logotype

Contents of /branches/WIP-pyshapelib-bramz/Thuban/UI/selection.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2734 - (show annotations)
Thu Mar 1 12:42:59 2007 UTC (18 years ago) by bramz
File MIME type: text/x-python
File size: 5756 byte(s)
made a copy
1 # Copyright (c) 2001, 2003 by Intevation GmbH
2 # Authors:
3 # Bernhard Herzog <[email protected]>
4 #
5 # This program is free software under the GPL (>=v2)
6 # Read the file COPYING coming with Thuban for details.
7
8 """
9 Selection handling
10 """
11
12 __version__ = "$Revision$"
13 # $Source$
14 # $Id$
15
16 from Thuban.Lib.connector import Publisher
17
18 from messages import LAYER_SELECTED, SHAPES_SELECTED
19
20
21 class Selection(Publisher):
22
23 """Manage the selection
24
25 The selection consists of a list of shape ids and the layer that
26 contains those shapes.
27
28 Messages issued by the selection:
29
30 LAYER_SELECTED -- A different layer has been selected or there was a
31 layer selected and the selection has been cleared.
32
33 Arguments: The newly selected layer or None if no
34 layer is selected.
35
36 SHAPES_SELECTED -- The set of selected shapes has changed.
37
38 Arguments: The newly selected layer or None if no
39 layer is selected and a list with ids of the
40 selected shapes. The list is empty if no shapes
41 are selected.
42 """
43
44 def __init__(self):
45 """Initialize the selection
46
47 Initially nothing is selected so the selected layer is None and
48 the list of selected shapes is empty.
49 """
50 self.layer = None
51 self.shapes = []
52
53 def issue_messages(self, issue_layer, issue_shape):
54 """Internal: Issue SHAPES_SELECTED and LAYER_SELECTED messages
55
56 If the issue_layer argument is true, issue a LAYER_SELECTED
57 message. If issue_shape is true issue a SHAPES_SELECTED message.
58 """
59 if issue_layer:
60 self.issue(LAYER_SELECTED, self.layer)
61 if issue_shape:
62 self.issue(SHAPES_SELECTED, self.layer, self.SelectedShapes())
63
64 def ClearSelection(self):
65 """Unselect the currently selected shapes if any.
66
67 If there was a layer selected before the call, send a
68 LAYER_SELECTED message. If there were selected shapes before the
69 call, send a SHAPES_SELECTED message as well.
70 """
71 issue_layer = self.layer is not None
72 issue_shape = len(self.shapes) > 0
73
74 self.layer = None
75 self.shapes = []
76 self.issue_messages(issue_layer, issue_shape)
77
78 def SelectedLayer(self):
79 """Return the selected layer. If no layer is selected return None"""
80 return self.layer
81
82 def HasSelectedLayer(self):
83 """Return true iff a layer is currently selected"""
84 return self.layer is not None
85
86 def SelectedShapes(self):
87 """Return the ids of the selected shapes as a list.
88
89 The list is sorted in ascending order. If no shapes are selected
90 the list is empty.
91 """
92 return self.shapes
93
94 def HasSelectedShapes(self):
95 """Return true iff at least one shape is selected."""
96 return len(self.shapes) > 0
97
98 def SelectLayer(self, layer):
99 """Select the given layer.
100
101 If the layer is the currently selected layer, do nothing.
102 Ortherwise, issue a LAYER_SELECTED message and if there were
103 shapes selected preciously issue a SHAPES_SELECTED message as
104 well.
105 """
106 if self.layer is not layer:
107 self.layer = layer
108 issue_shape = len(self.shapes) > 0
109 self.shapes = []
110 self.issue_messages(1, issue_shape)
111
112 def SelectShapes(self, layer, shapes, add = 0):
113 """Select the shapes given in the list shapes in the given layer.
114
115 If the optional keyword parameter add is true, add the selected
116 shapes to the currently selected shapes if layer is the already
117 selected layer if any. If add is false (the default) make the
118 shapes in shapes the selected shapes.
119
120 If a different layer is selected afterwards issue a
121 LAYER_SELECTED message. If the set of selected shapes has
122 changed afterwards issue a SHAPES_SELECTED message.
123 """
124 # Turn the shapes explicitly into a list. This main purpose
125 # is that we get a mutable sequence that we can modify in place.
126 shapes = list(shapes)
127 shapes.sort()
128
129 if self.layer is not layer:
130 # If the layer is differen we can ignore the add parameter
131 # and we have to issue both messages.
132 self.layer = layer
133 self.shapes = shapes
134 issue_layer = issue_shape = 1
135 else:
136 # If the layer is differen we have to merge the ids if add
137 # is true, and we won't have to issue the LAYER_SELECTED
138 # message.
139 issue_layer = 0
140 if add:
141 # Merge the ids by creating a dict with the ids in
142 # self.shapes and in shapes as the keys. The use the sorted
143 # keys of the dict as the new shapes list.
144 set = {}
145 for i in self.shapes:
146 set[i] = 1
147 for i in shapes:
148 set[i] = 1
149 shapes = set.keys()
150 shapes.sort()
151 if self.shapes != shapes:
152 self.shapes = shapes
153 issue_shape = 1
154 else:
155 issue_shape = 0
156
157 self.issue_messages(issue_layer, issue_shape)
158
159 def AddShapes(self, layer, shapes):
160 """Add the shapes on layer to the selection.
161
162 If the layer is the already selected layer, add the shape ids in
163 shapes to the already selected shapes.
164
165 If the layer is not the already selected layer, make the layer
166 and shapes the selected layer and shapes.
167 """
168 self.SelectShapes(layer, shapes)

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26