This repo is not current. Development has moved from Hg to Git. For the latest code use the "Source Code" tab above to go to the "Thun" git repo or navigate to:
https://osdn.net/projects/joypy/scm/git/Thun
Revisão | 294446e938e8030f017d1b9f3e6594c967938ff4 (tree) |
---|---|
Hora | 2020-06-06 13:34:28 |
Autor | Simon Forman <sforman@hush...> |
Commiter | Simon Forman |
dnd utility function.
@@ -173,6 +173,52 @@ | ||
173 | 173 | ## return expression |
174 | 174 | |
175 | 175 | |
176 | +def dnd(stack, from_index, to_index): | |
177 | + ''' | |
178 | + Given a stack and two indices return a rearranged stack. | |
179 | + First remove the item at from_index and then insert it at to_index, | |
180 | + the second index is relative to the stack after removal of the item | |
181 | + at from_index. | |
182 | + | |
183 | + This function reuses all of the items and as much of the stack as it | |
184 | + can. It's meant to be used by remote clients to support drag-n-drop | |
185 | + rearranging of the stack from e.g. the StackListbox. | |
186 | + ''' | |
187 | + assert 0 <= from_index | |
188 | + assert 0 <= to_index | |
189 | + if from_index == to_index: | |
190 | + return stack | |
191 | + head, n = [], from_index | |
192 | + while True: | |
193 | + item, stack = stack | |
194 | + n -= 1 | |
195 | + if n < 0: | |
196 | + break | |
197 | + head.append(item) | |
198 | + assert len(head) == from_index | |
199 | + # now we have two cases: | |
200 | + diff = from_index - to_index | |
201 | + if diff < 0: | |
202 | + # from < to | |
203 | + # so the destination index is still in the stack | |
204 | + while diff: | |
205 | + h, stack = stack | |
206 | + head.append(h) | |
207 | + diff += 1 | |
208 | + stack = item, stack | |
209 | + while head: | |
210 | + stack = head.pop(), stack | |
211 | + else: | |
212 | + # from > to | |
213 | + # so the destination is in the head list | |
214 | + while head: | |
215 | + stack = head.pop(), stack | |
216 | + from_index -= 1 | |
217 | + if from_index == to_index: | |
218 | + stack = item, stack | |
219 | + return stack | |
220 | + | |
221 | + | |
176 | 222 | |
177 | 223 | def pick(stack, n): |
178 | 224 | ''' |