Improving move of elements using arrow keys in Viewer
authorlaurent
Sun, 01 Apr 2012 15:51:39 +0200
changeset 667 baab9eb5b8ad
parent 666 d4bb66691248
child 668 e858ff2f7862
Improving move of elements using arrow keys in Viewer
Viewer.py
graphics/GraphicCommons.py
--- a/Viewer.py	Sat Mar 31 15:08:18 2012 +0200
+++ b/Viewer.py	Sun Apr 01 15:51:39 2012 +0200
@@ -23,6 +23,7 @@
 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 import math
+import time
 from types import TupleType
 from threading import Lock
 
@@ -542,6 +543,7 @@
         self.InstancePath = instancepath
         self.StartMousePos = None
         self.StartScreenPos = None
+        self.Buffering = False
         
         # Initialize Cursors
         ResetCursors()
@@ -881,9 +883,24 @@
     # Buffer the last model state
     def RefreshBuffer(self):
         self.Controler.BufferProject()
-        self.ParentWindow.RefreshTitle()
-        self.ParentWindow.RefreshFileMenu()
-        self.ParentWindow.RefreshEditMenu()
+        if self.ParentWindow:
+            self.ParentWindow.RefreshTitle()
+            self.ParentWindow.RefreshFileMenu()
+            self.ParentWindow.RefreshEditMenu()
+    
+    def StartBuffering(self):
+        if not self.Buffering:
+            self.Buffering = True
+            self.Controler.StartBuffering()
+            if self.ParentWindow:
+                self.ParentWindow.RefreshTitle()
+                self.ParentWindow.RefreshFileMenu()
+                self.ParentWindow.RefreshEditMenu()
+    
+    def ResetBuffer(self):
+        if self.Buffering:
+            self.Controler.EndBuffering()
+            self.Buffering = False
     
     def GetBufferState(self):
         if not self.Debug:
@@ -976,6 +993,7 @@
         # Start by reseting Viewer
         self.Flush()
         self.ResetView()
+        self.ResetBuffer()
         instance = {}
         # List of ids of already loaded blocks
         ids = []
@@ -1439,6 +1457,7 @@
 
     def OnViewerMouseEvent(self, event):
         if not event.Entering():
+            self.ResetBuffer()
             element = None
             if not event.Leaving() and not event.LeftUp() and not event.LeftDClick():
                 element = self.FindElement(event, True, False)
@@ -1808,6 +1827,13 @@
 #                          Keyboard event functions
 #-------------------------------------------------------------------------------
 
+    ARROW_KEY_MOVE = {
+        wx.WXK_LEFT: (-1, 0),
+        wx.WXK_RIGHT: (1, 0),
+        wx.WXK_UP: (0, -1),
+        wx.WXK_DOWN: (0, 1),
+    }
+
     def OnChar(self, event):
         xpos, ypos = self.GetScrollPos(wx.HORIZONTAL), self.GetScrollPos(wx.VERTICAL)
         xmax = self.GetScrollRange(wx.HORIZONTAL) - self.GetScrollThumb(wx.HORIZONTAL)
@@ -1827,62 +1853,29 @@
             self.RefreshRect(self.GetScrolledRect(rect), False)
         elif not self.Debug and keycode == wx.WXK_RETURN and self.SelectedElement is not None:
             self.SelectedElement.OnLeftDClick(event, self.GetLogicalDC(), self.Scaling)
-        elif keycode == wx.WXK_LEFT:
+        elif self.ARROW_KEY_MOVE.has_key(keycode):
+            move = self.ARROW_KEY_MOVE[keycode]
             if event.ControlDown() and event.ShiftDown():
-                self.Scroll(0, ypos)
+                self.Scroll({-1: 0, 0: xpos, 1: xmax}[move[0]],
+                            {-1: 0, 0: ypos, 1: ymax}[move[1]])
                 self.RefreshVisibleElements()
             elif event.ControlDown():
-                self.Scroll(xpos - 1, ypos)
+                self.Scroll(xpos + move[0], ypos + move[1])
                 self.RefreshScrollBars()
                 self.RefreshVisibleElements()
             elif not self.Debug and self.SelectedElement is not None:
-                self.SelectedElement.Move(-scaling[0], 0)
+                movex, movey = move
+                if not event.AltDown() or event.ShiftDown():
+                    movex *= scaling[0]
+                    movey *= scaling[1] 
+                    if event.ShiftDown() and not event.AltDown():
+                        movex *= 10
+                        movey *= 10
+                self.SelectedElement.Move(movex, movey)
+                self.StartBuffering()
                 self.SelectedElement.RefreshModel()
-                self.RefreshBuffer()
                 self.RefreshScrollBars()
-                self.RefreshRect(self.GetScrolledRect(self.SelectedElement.GetRedrawRect(-scaling[0], 0)), False)
-        elif keycode == wx.WXK_RIGHT:
-            if event.ControlDown() and event.ShiftDown():
-                self.Scroll(xmax, ypos)
-                self.RefreshVisibleElements()
-            elif event.ControlDown():
-                self.RefreshScrollBars(width_incr=max(0, xpos + 1 - xmax))
-                self.Scroll(xpos + 1, ypos)
-                self.RefreshVisibleElements()
-            elif not self.Debug and self.SelectedElement is not None:
-                self.SelectedElement.Move(scaling[0], 0)
-                self.SelectedElement.RefreshModel()
-                self.RefreshBuffer()
-                self.RefreshScrollBars()
-                self.RefreshRect(self.GetScrolledRect(self.SelectedElement.GetRedrawRect(scaling[0], 0)), False)
-        elif keycode == wx.WXK_UP:
-            if event.ControlDown() and event.ShiftDown():
-                self.Scroll(xpos, 0)
-                self.RefreshVisibleElements()
-            elif event.ControlDown():
-                self.Scroll(xpos, ypos - 1)
-                self.RefreshScrollBars()
-                self.RefreshVisibleElements()
-            elif not self.Debug and self.SelectedElement is not None:
-                self.SelectedElement.Move(0, -scaling[1])
-                self.SelectedElement.RefreshModel()
-                self.RefreshBuffer()
-                self.RefreshScrollBars()
-                self.RefreshRect(self.GetScrolledRect(self.SelectedElement.GetRedrawRect(0, -scaling[1])), False)
-        elif keycode == wx.WXK_DOWN:
-            if event.ControlDown() and event.ShiftDown():
-                self.Scroll(xpos, ymax)
-                self.RefreshVisibleElements()
-            elif event.ControlDown():
-                self.RefreshScrollBars(height_incr=max(0, ypos + 1 - ymax))
-                self.Scroll(xpos, ypos + 1)
-                self.RefreshVisibleElements()
-            elif not self.Debug and self.SelectedElement is not None:
-                self.SelectedElement.Move(0, scaling[1])
-                self.SelectedElement.RefreshModel()
-                self.RefreshBuffer()
-                self.RefreshScrollBars()
-                self.RefreshRect(self.GetScrolledRect(self.SelectedElement.GetRedrawRect(0, scaling[1])), False)
+                self.RefreshRect(self.GetScrolledRect(self.SelectedElement.GetRedrawRect(movex, movey)), False)
         elif not self.Debug and keycode == wx.WXK_SPACE and self.SelectedElement is not None and self.SelectedElement.Dragging:
             if self.IsBlock(self.SelectedElement) or self.IsComment(self.SelectedElement):
                 block = self.CopyBlock(self.SelectedElement, wx.Point(*self.SelectedElement.GetPosition()))
--- a/graphics/GraphicCommons.py	Sat Mar 31 15:08:18 2012 +0200
+++ b/graphics/GraphicCommons.py	Sun Apr 01 15:51:39 2012 +0200
@@ -933,8 +933,8 @@
 
     # Moves the element
     def Move(self, dx, dy, exclude = []):
-        self.Pos.x += dx
-        self.Pos.y += dy
+        self.Pos.x += max(-self.BoundingBox.x, dx)
+        self.Pos.y += max(-self.BoundingBox.y, dy)
         self.RefreshConnected(exclude)
         self.RefreshBoundingBox()
     
@@ -1311,6 +1311,8 @@
     
     # Move this group of elements
     def Move(self, movex, movey):
+        movex = max(-self.BoundingBox.x, movex)
+        movey = max(-self.BoundingBox.y, movey)
         # Move all the elements of the group
         for element in self.Elements:
             if not isinstance(element, Wire):