Changeset 1650
- Timestamp:
- 07/17/07 11:56:42 (1 year ago)
- Files:
-
- gaphas/branches/hw/gaphas/canvas.py (modified) (6 diffs)
- gaphas/branches/hw/gaphas/examples.py (modified) (5 diffs)
- gaphas/branches/hw/gaphas/item.py (modified) (1 diff)
- gaphas/branches/hw/gaphas/tests/test_canvas.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
gaphas/branches/hw/gaphas/canvas.py
r1649 r1650 62 62 Attributes: 63 63 - _cache: additional cache of item data 64 - _canvas_constraints: constraints set between canvas items 64 65 """ 65 66 … … 75 76 self.proj = CanvasProjector(self) 76 77 78 self._canvas_constraints = {} 79 77 80 solver = property(lambda s: s._solver) 78 81 … … 94 97 item.canvas = self 95 98 self._tree.add(item, parent) 96 97 99 self._cache[item] = CanvasBucket() 100 self._canvas_constraints[item] = {} 101 98 102 for v in self._registered_views: 99 103 self._cache[item].view[v] = ViewBucket() … … 125 129 self._tree.remove(item) 126 130 self.remove_connections_to_item(item) 131 del self._canvas_constraints[item] 127 132 self._update_views((item,)) 128 133 self._dirty_items.discard(item) … … 131 136 reversible_pair(add, remove, 132 137 bind1={'parent': lambda self, item: self.get_parent(item) }) 138 139 140 def add_canvas_constraint(self, item, handle, c): 141 """ 142 Add constraint between items. 143 144 Parameters: 145 - item: item holding constraint 146 - handle: handle holding constraint 147 - c: constraint between items 148 """ 149 if item not in self._canvas_constraints: 150 raise ValueError, 'Item not added to canvas' 151 152 i_cons = self._canvas_constraints[item] 153 if handle not in i_cons: 154 i_cons[handle] = set() 155 i_cons[handle].add(c) 156 self._solver.add_constraint(c) 157 158 159 def remove_canvas_constraint(self, item, handle, c=None): 160 """ 161 Remove constraint set between item. 162 163 If constraint is not set then all constraints are removed for given 164 item and handle. 165 166 Parameters: 167 - item: item holding constraint 168 - handle: handle holding constraint 169 - c: constraint between items 170 """ 171 if item not in self._canvas_constraints: 172 raise ValueError, 'Item not added to canvas' 173 174 i_cons = self._canvas_constraints[item] 175 176 if c is None: # remove all handle's constraints 177 h_cons = i_cons[handle] 178 for c in h_cons: 179 self._solver.remove_constraint(c) 180 h_cons.clear() 181 else: 182 # remove specific constraint 183 self._solver.remove_constraint(c) 184 i_cons[handle].remove(c) 185 186 187 def canvas_constraints(self, item): 188 """ 189 Get all constraints set between items for specific item. 190 """ 191 if item not in self._canvas_constraints: 192 raise ValueError, 'Item not added to canvas' 193 194 i_cons = self._canvas_constraints[item] 195 196 for cons in i_cons.values(): 197 for c in cons: 198 yield c 199 133 200 134 201 def remove_connections_to_item(self, item): … … 500 567 # Make sure handles are marked (for constraint solving) 501 568 request_resolve = self._solver.request_resolve 502 for c in item.iconstraints():569 for c in self.canvas_constraints(item): 503 570 request_resolve(c) 504 571 gaphas/branches/hw/gaphas/examples.py
r1648 r1650 215 215 def handle_disconnect(): 216 216 try: 217 item.remove_iconstraint(handle)217 view.canvas.remove_canvas_constraint(item, handle) 218 218 except KeyError: 219 219 pass # constraint was alreasy removed … … 226 226 if glue_item and glue_item is handle.connected_to: 227 227 try: 228 item.remove_iconstraint(handle)228 view.canvas.remove_canvas_constraint(item, handle) 229 229 except KeyError: 230 230 pass # constraint was already removed … … 241 241 view.canvas.proj(lc, xy=pdata, f=lc.update_ratio) 242 242 lc.update_ratio() 243 item.add_iconstraint(handle, lc)243 view.canvas.add_canvas_constraint(item, handle, lc) 244 244 245 245 handle.disconnect = handle_disconnect … … 265 265 view.canvas.proj(lc, xy=pdata, f=lc.update_ratio) 266 266 lc.update_ratio() 267 item.add_iconstraint(handle, lc)267 view.canvas.add_canvas_constraint(item, handle, lc) 268 268 269 269 handle.connected_to = glue_item … … 273 273 if handle.connected_to: 274 274 #print 'Handle.disconnect', view, item, handle 275 item.remove_iconstraint(handle)275 view.canvas.remove_canvas_constraint(item, handle) 276 276 277 277 gaphas/branches/hw/gaphas/item.py
r1645 r1650 127 127 self._handles = [] 128 128 self._constraints = [] 129 self._iconstraints = {}130 131 132 def add_iconstraint(self, h, c):133 if h not in self._iconstraints:134 self._iconstraints[h] = set()135 self._iconstraints[h].add(c)136 self._canvas.solver.add_constraint(c)137 138 139 def remove_iconstraint(self, h, c=None):140 if c is None: # remove all handle's constraints141 cons = self._iconstraints[h]142 for c in cons:143 self._canvas.solver.remove_constraint(c)144 cons.clear()145 else:146 # remove specific constraint147 self._canvas.solver.remove_constraint(c)148 self._iconstraints[h].remove(c)149 150 151 def iconstraints(self):152 for cons in self._iconstraints.values():153 for c in cons:154 yield c155 156 129 157 130 @observed gaphas/branches/hw/gaphas/tests/test_canvas.py
r1637 r1650 3 3 from gaphas.canvas import Canvas 4 4 from gaphas.examples import Box 5 from gaphas.item import Line 5 from gaphas.item import Line, Handle 6 6 from gaphas.constraint import BalanceConstraint, EqualsConstraint 7 7 … … 50 50 self.assertEquals(10, h2.x) 51 51 self.assertEquals(-10, h2.y) 52 53 54 55 class CanvasConstraintTestCase(unittest.TestCase): 56 def test_adding_constraint(self): 57 """Test adding canvas constraint""" 58 canvas = Canvas() 59 cons = canvas._canvas_constraints 60 61 l1 = Line() 62 canvas.add(l1) 63 64 h1, h2 = l1.handles() 65 h = Handle() 66 67 eq1 = EqualsConstraint(h1.x, h.x) 68 canvas.add_canvas_constraint(l1, h1, eq1) 69 self.assertTrue(l1 in cons) 70 self.assertTrue(h1 in cons[l1]) 71 self.assertTrue(eq1 in cons[l1][h1]) 72 73 l2 = Line() 74 canvas.add(l2) 75 76 h1, h2 = l2.handles() 77 h = Handle() 78 79 eq2 = EqualsConstraint(h1.x, h.x) 80 canvas.add_canvas_constraint(l2, h1, eq2) 81 self.assertTrue(l2 in cons) 82 self.assertTrue(h1 in cons[l2]) 83 self.assertTrue(eq2 in cons[l2][h1]) 84 85 86 def test_adding_constraint_ex(self): 87 """Test adding canvas constraint for non-existing item""" 88 canvas = Canvas() 89 l1 = Line() 90 h1, h2 = l1.handles() 91 h = Handle() 92 93 eq = EqualsConstraint(h1.x, h.x) 94 self.assertRaises(ValueError, canvas.add_canvas_constraint, l1, h1, eq) 95 96 97 def test_removing_constraint(self): 98 """Test removing canvas constraint""" 99 canvas = Canvas() 100 cons = canvas._canvas_constraints 101 102 l1 = Line() 103 canvas.add(l1) 104 105 h1, h2 = l1.handles() 106 h = Handle() 107 108 eq1 = EqualsConstraint(h1.x, h.x) 109 canvas.add_canvas_constraint(l1, h1, eq1) 110 111 # test preconditions 112 assert l1 in cons 113 assert h1 in cons[l1] 114 assert eq1 in cons[l1][h1] 115 116 canvas.remove_canvas_constraint(l1, h1, eq1) 117 self.assertTrue(l1 in cons) 118 self.assertTrue(h1 in cons[l1]) 119 self.assertFalse(eq1 in cons[l1][h1]) 120 121 eq1 = EqualsConstraint(h1.x, h.x) 122 eq2 = EqualsConstraint(h1.y, h.y) 123 canvas.add_canvas_constraint(l1, h1, eq1) 124 canvas.add_canvas_constraint(l1, h1, eq2) 125 126 # test preconditions 127 assert l1 in cons 128 assert h1 in cons[l1] 129 assert eq1 in cons[l1][h1] 130 assert eq2 in cons[l1][h1] 131 132 canvas.remove_canvas_constraint(l1, h1) 133 134 self.assertTrue(l1 in cons) 135 self.assertTrue(h1 in cons[l1]) 136 self.assertFalse(eq1 in cons[l1][h1]) 137 self.assertFalse(eq2 in cons[l1][h1]) 138 139 140 def test_fetching_constraints(self): 141 """Test fetching canvas constraints""" 142 canvas = Canvas() 143 cons = canvas._canvas_constraints 144 145 l1 = Line() 146 canvas.add(l1) 147 148 h1, h2 = l1.handles() 149 h = Handle() 150 151 eq1 = EqualsConstraint(h1.x, h.x) 152 eq2 = EqualsConstraint(h1.y, h.y) 153 canvas.add_canvas_constraint(l1, h1, eq1) 154 canvas.add_canvas_constraint(l1, h1, eq2) 155 156 # test preconditions 157 assert l1 in cons 158 assert h1 in cons[l1] 159 assert eq1 in cons[l1][h1] 160 assert eq2 in cons[l1][h1] 161 162 self.assertTrue(eq1 in canvas.canvas_constraints(l1)) 163 self.assertTrue(eq2 in canvas.canvas_constraints(l1))
