| 94 | | |
|---|
| 95 | | canvas.py: |
|---|
| 96 | | Canvas: |
|---|
| 97 | | add() and remove() |
|---|
| 98 | | |
|---|
| 99 | | item.py: |
|---|
| 100 | | Handle: |
|---|
| | 93 | >>> undo_list |
|---|
| | 94 | [] |
|---|
| | 95 | |
|---|
| | 96 | canvas.py: Canvas |
|---|
| | 97 | ----------------- |
|---|
| | 98 | |
|---|
| | 99 | On the canvas only add() and remove() are monitored: |
|---|
| | 100 | |
|---|
| | 101 | >>> from gaphas import Canvas, Item |
|---|
| | 102 | >>> canvas = Canvas() |
|---|
| | 103 | >>> canvas.get_all_items() |
|---|
| | 104 | [] |
|---|
| | 105 | >>> item = Item() |
|---|
| | 106 | >>> canvas.add(item) |
|---|
| | 107 | >>> canvas.get_all_items() # doctest: +ELLIPSIS |
|---|
| | 108 | [<gaphas.item.Item object at 0x...>] |
|---|
| | 109 | >>> item.canvas is canvas |
|---|
| | 110 | True |
|---|
| | 111 | >>> undo() |
|---|
| | 112 | >>> canvas.get_all_items() |
|---|
| | 113 | [] |
|---|
| | 114 | >>> item.canvas is None |
|---|
| | 115 | True |
|---|
| | 116 | >>> canvas.add(item) |
|---|
| | 117 | >>> del undo_list[:] |
|---|
| | 118 | >>> canvas.remove(item) |
|---|
| | 119 | >>> canvas.get_all_items() |
|---|
| | 120 | [] |
|---|
| | 121 | >>> undo() |
|---|
| | 122 | >>> canvas.get_all_items() # doctest: +ELLIPSIS |
|---|
| | 123 | [<gaphas.item.Item object at 0x...>] |
|---|
| | 124 | >>> undo_list |
|---|
| | 125 | [] |
|---|
| | 126 | |
|---|
| | 127 | The request_update() method is observed too: |
|---|
| | 128 | |
|---|
| | 129 | >>> canvas.request_update(item) |
|---|
| | 130 | >>> len(undo_list) |
|---|
| | 131 | 1 |
|---|
| | 132 | |
|---|
| | 133 | NOTE: If remove() is invoked recursively (e.i. when the to-be removed item has |
|---|
| | 134 | children), the remove items can not simply be applied in reverse order. |
|---|
| | 135 | |
|---|
| | 136 | >>> child = Item() |
|---|
| | 137 | >>> canvas.add(child, parent=item) |
|---|
| | 138 | >>> canvas.get_all_items() # doctest: +ELLIPSIS |
|---|
| | 139 | [<gaphas.item.Item object at 0x...>, <gaphas.item.Item object at 0x...>] |
|---|
| | 140 | >>> del undo_list[:] |
|---|
| | 141 | >>> canvas.remove(item) |
|---|
| | 142 | >>> canvas.get_all_items() |
|---|
| | 143 | [] |
|---|
| | 144 | |
|---|
| | 145 | |
|---|
| | 146 | item.py: Handle |
|---|
| | 147 | --------------- |
|---|
| 102 | | Item: |
|---|
| 103 | | canvas and matric properties |
|---|
| 104 | | Element: |
|---|
| 105 | | min_height and min_width properties |
|---|
| 106 | | Line: |
|---|
| 107 | | line_width, fuzzyness, orthogonal and horizontal properties; |
|---|
| 108 | | split_segment() and merge_segment() |
|---|
| 109 | | |
|---|
| 110 | | solver.py: |
|---|
| 111 | | Variable: |
|---|
| 112 | | strength and value properties |
|---|
| 113 | | solver.py: |
|---|
| 114 | | Solver: |
|---|
| 115 | | add_constraint() and remove_constraint() |
|---|
| 116 | | |
|---|
| 117 | | |
|---|
| | 149 | Changing the Handle's position is reversible: |
|---|
| | 150 | |
|---|
| | 151 | >>> from gaphas import Handle |
|---|
| | 152 | >>> handle = Handle() |
|---|
| | 153 | >>> handle.x, handle.y = 10, 12 |
|---|
| | 154 | >>> handle.pos |
|---|
| | 155 | (Variable(10, 20), Variable(12, 20)) |
|---|
| | 156 | >>> undo() |
|---|
| | 157 | >>> handle.pos |
|---|
| | 158 | (Variable(0, 20), Variable(0, 20)) |
|---|
| | 159 | |
|---|
| | 160 | As are all other properties: |
|---|
| | 161 | |
|---|
| | 162 | >>> handle.connectable, handle.movable, handle.visible, handle.connected_to |
|---|
| | 163 | (False, True, True, None) |
|---|
| | 164 | >>> handle.disconnect # doctest: +ELLIPSIS |
|---|
| | 165 | <function <lambda> at 0x...> |
|---|
| | 166 | >>> handle.connectable = True |
|---|
| | 167 | >>> handle.movable = False |
|---|
| | 168 | >>> handle.visible = False |
|---|
| | 169 | >>> handle.connected_to = item |
|---|
| | 170 | >>> def my_fancy_disconnect(): pass |
|---|
| | 171 | >>> handle.disconnect = my_fancy_disconnect |
|---|
| | 172 | >>> handle.connectable, handle.movable, handle.visible |
|---|
| | 173 | (True, False, False) |
|---|
| | 174 | >>> handle.connected_to # doctest: +ELLIPSIS |
|---|
| | 175 | <gaphas.item.Item object at 0x...> |
|---|
| | 176 | >>> handle.disconnect # doctest: +ELLIPSIS |
|---|
| | 177 | <function my_fancy_disconnect at 0x...> |
|---|
| | 178 | |
|---|
| | 179 | And now undo the whole lot at once: |
|---|
| | 180 | |
|---|
| | 181 | >>> undo() |
|---|
| | 182 | >>> handle.connectable, handle.movable, handle.visible, handle.connected_to |
|---|
| | 183 | (False, True, True, None) |
|---|
| | 184 | >>> handle.disconnect # doctest: +ELLIPSIS |
|---|
| | 185 | <function <lambda> at 0x...> |
|---|
| | 186 | |
|---|
| | 187 | item.py: Item |
|---|
| | 188 | ------------- |
|---|
| | 189 | |
|---|
| | 190 | The basic Item properties are canvas and matrix. Canvas has been tested before, |
|---|
| | 191 | while testing the Canvas class. |
|---|
| | 192 | |
|---|
| | 193 | The Matrix has been tested in section matrix.y: Matrix. |
|---|
| | 194 | |
|---|
| | 195 | item.py: Element |
|---|
| | 196 | ---------------- |
|---|
| | 197 | |
|---|
| | 198 | An element has min_height and min_width properties. |
|---|
| | 199 | |
|---|
| | 200 | >>> from gaphas import Element |
|---|
| | 201 | >>> e = Element() |
|---|
| | 202 | >>> e.min_height, e.min_width |
|---|
| | 203 | (10, 10) |
|---|
| | 204 | >>> e.min_height, e.min_width = 30, 40 |
|---|
| | 205 | >>> e.min_height, e.min_width |
|---|
| | 206 | (30, 40) |
|---|
| | 207 | |
|---|
| | 208 | >>> undo() |
|---|
| | 209 | >>> e.min_height, e.min_width |
|---|
| | 210 | (10, 10) |
|---|
| | 211 | |
|---|
| | 212 | |
|---|
| | 213 | item.py: Line |
|---|
| | 214 | ------------- |
|---|
| | 215 | |
|---|
| | 216 | A line has the following properties: line_width, fuzzyness, orthogonal and |
|---|
| | 217 | horizontal. Each one of then is observed for changes: |
|---|
| | 218 | |
|---|
| | 219 | >>> from gaphas import Line |
|---|
| | 220 | >>> l = Line() |
|---|
| | 221 | >>> l.line_width, l.fuzzyness, l.orthogonal, l.horizontal |
|---|
| | 222 | (2, 0, False, False) |
|---|
| | 223 | |
|---|
| | 224 | Now change the properties: |
|---|
| | 225 | |
|---|
| | 226 | >>> l.line_width = 4 |
|---|
| | 227 | >>> l.fuzzyness = 2 |
|---|
| | 228 | >>> l.orthogonal = True |
|---|
| | 229 | >>> l.horizontal = True |
|---|
| | 230 | >>> l.line_width, l.fuzzyness, l.orthogonal, l.horizontal |
|---|
| | 231 | (4, 2, True, True) |
|---|
| | 232 | |
|---|
| | 233 | And undo the changes: |
|---|
| | 234 | |
|---|
| | 235 | >>> undo() |
|---|
| | 236 | >>> l.line_width, l.fuzzyness, l.orthogonal, l.horizontal |
|---|
| | 237 | (2, 0, False, False) |
|---|
| | 238 | |
|---|
| | 239 | In addition to those properties, line segments can be split and merged. |
|---|
| | 240 | |
|---|
| | 241 | >>> l.handles()[1].pos = 10, 10 |
|---|
| | 242 | >>> l.handles() |
|---|
| | 243 | [<Handle object on (0, 0)>, <Handle object on (10, 10)>] |
|---|
| | 244 | |
|---|
| | 245 | >>> l.split_segment(0) |
|---|
| | 246 | >>> l.handles() |
|---|
| | 247 | [<Handle object on (0, 0)>, <Handle object on (5, 5)>, <Handle object on (10, 10)>] |
|---|
| | 248 | |
|---|
| | 249 | The opposite operation is performed with the merge_segment() method: |
|---|
| | 250 | |
|---|
| | 251 | >>> undo() |
|---|
| | 252 | >>> l.handles() |
|---|
| | 253 | [<Handle object on (0, 0)>, <Handle object on (10, 10)>] |
|---|
| | 254 | |
|---|
| | 255 | solver.py: Variable |
|---|
| | 256 | ------------------- |
|---|
| | 257 | |
|---|
| | 258 | Variable's strength and value properties are observed: |
|---|
| | 259 | |
|---|
| | 260 | >>> from gaphas.solver import Variable |
|---|
| | 261 | >>> v = Variable() |
|---|
| | 262 | >>> v.value = 10 |
|---|
| | 263 | >>> v.strength = 100 |
|---|
| | 264 | >>> v |
|---|
| | 265 | Variable(10, 100) |
|---|
| | 266 | >>> undo() |
|---|
| | 267 | >>> v |
|---|
| | 268 | Variable(0, 20) |
|---|
| | 269 | |
|---|
| | 270 | solver.py: Solver |
|---|
| | 271 | ----------------- |
|---|
| | 272 | |
|---|
| | 273 | Solvers add_constraint() and remove_constraint() are observed. |
|---|
| | 274 | |
|---|
| | 275 | >>> from gaphas.solver import Solver |
|---|
| | 276 | >>> from gaphas.constraint import EquationConstraint |
|---|
| | 277 | >>> s = Solver() |
|---|
| | 278 | >>> a, b = Variable(1.0), Variable(2.0) |
|---|
| | 279 | >>> s.add_constraint(EquationConstraint(lambda a,b: a+b, a=a, b=b)) |
|---|
| | 280 | EquationConstraint(<lambda>, a=Variable(1, 20), b=Variable(2, 20)) |
|---|
| | 281 | >>> list(s.constraints_with_variable(a)) |
|---|
| | 282 | [EquationConstraint(<lambda>, a=Variable(1, 20), b=Variable(2, 20))] |
|---|
| | 283 | |
|---|
| | 284 | >>> undo() |
|---|
| | 285 | >>> list(s.constraints_with_variable(a)) |
|---|
| | 286 | [] |
|---|
| | 287 | |
|---|