Added ability to move graphics items in the diagram via the cursor keys and to limit the item position to the scene.

Tue, 11 Sep 2012 18:46:46 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 11 Sep 2012 18:46:46 +0200
changeset 2038
72557ef75de1
parent 2037
7a3470ae170e
child 2039
fa41ccd7f7bc

Added ability to move graphics items in the diagram via the cursor keys and to limit the item position to the scene.

APIs/Python3/eric5.api file | annotate | diff | comparison | revisions
Documentation/Help/source.qch file | annotate | diff | comparison | revisions
Documentation/Help/source.qhp file | annotate | diff | comparison | revisions
Documentation/Source/eric5.Graphics.UMLGraphicsView.html file | annotate | diff | comparison | revisions
Graphics/UMLGraphicsView.py file | annotate | diff | comparison | revisions
Graphics/UMLItem.py file | annotate | diff | comparison | revisions
--- a/APIs/Python3/eric5.api	Tue Sep 11 18:26:47 2012 +0200
+++ b/APIs/Python3/eric5.api	Tue Sep 11 18:46:46 2012 +0200
@@ -1720,6 +1720,7 @@
 eric5.Graphics.UMLGraphicsView.UMLGraphicsView.getItemId?4()
 eric5.Graphics.UMLGraphicsView.UMLGraphicsView.getPersistenceData?4()
 eric5.Graphics.UMLGraphicsView.UMLGraphicsView.initToolBar?4()
+eric5.Graphics.UMLGraphicsView.UMLGraphicsView.keyPressEvent?4(evt)
 eric5.Graphics.UMLGraphicsView.UMLGraphicsView.parsePersistenceData?4(version, data)
 eric5.Graphics.UMLGraphicsView.UMLGraphicsView.printDiagram?4()
 eric5.Graphics.UMLGraphicsView.UMLGraphicsView.printPreviewDiagram?4()
Binary file Documentation/Help/source.qch has changed
--- a/Documentation/Help/source.qhp	Tue Sep 11 18:26:47 2012 +0200
+++ b/Documentation/Help/source.qhp	Tue Sep 11 18:46:46 2012 +0200
@@ -10668,6 +10668,7 @@
       <keyword name="UMLGraphicsView.__incHeight" id="UMLGraphicsView.__incHeight" ref="eric5.Graphics.UMLGraphicsView.html#UMLGraphicsView.__incHeight" />
       <keyword name="UMLGraphicsView.__incWidth" id="UMLGraphicsView.__incWidth" ref="eric5.Graphics.UMLGraphicsView.html#UMLGraphicsView.__incWidth" />
       <keyword name="UMLGraphicsView.__initActions" id="UMLGraphicsView.__initActions" ref="eric5.Graphics.UMLGraphicsView.html#UMLGraphicsView.__initActions" />
+      <keyword name="UMLGraphicsView.__itemsBoundingRect" id="UMLGraphicsView.__itemsBoundingRect" ref="eric5.Graphics.UMLGraphicsView.html#UMLGraphicsView.__itemsBoundingRect" />
       <keyword name="UMLGraphicsView.__printPreviewPrint" id="UMLGraphicsView.__printPreviewPrint" ref="eric5.Graphics.UMLGraphicsView.html#UMLGraphicsView.__printPreviewPrint" />
       <keyword name="UMLGraphicsView.__relayout" id="UMLGraphicsView.__relayout" ref="eric5.Graphics.UMLGraphicsView.html#UMLGraphicsView.__relayout" />
       <keyword name="UMLGraphicsView.__sceneChanged" id="UMLGraphicsView.__sceneChanged" ref="eric5.Graphics.UMLGraphicsView.html#UMLGraphicsView.__sceneChanged" />
@@ -10681,6 +10682,7 @@
       <keyword name="UMLGraphicsView.getItemId" id="UMLGraphicsView.getItemId" ref="eric5.Graphics.UMLGraphicsView.html#UMLGraphicsView.getItemId" />
       <keyword name="UMLGraphicsView.getPersistenceData" id="UMLGraphicsView.getPersistenceData" ref="eric5.Graphics.UMLGraphicsView.html#UMLGraphicsView.getPersistenceData" />
       <keyword name="UMLGraphicsView.initToolBar" id="UMLGraphicsView.initToolBar" ref="eric5.Graphics.UMLGraphicsView.html#UMLGraphicsView.initToolBar" />
+      <keyword name="UMLGraphicsView.keyPressEvent" id="UMLGraphicsView.keyPressEvent" ref="eric5.Graphics.UMLGraphicsView.html#UMLGraphicsView.keyPressEvent" />
       <keyword name="UMLGraphicsView.parsePersistenceData" id="UMLGraphicsView.parsePersistenceData" ref="eric5.Graphics.UMLGraphicsView.html#UMLGraphicsView.parsePersistenceData" />
       <keyword name="UMLGraphicsView.printDiagram" id="UMLGraphicsView.printDiagram" ref="eric5.Graphics.UMLGraphicsView.html#UMLGraphicsView.printDiagram" />
       <keyword name="UMLGraphicsView.printPreviewDiagram" id="UMLGraphicsView.printPreviewDiagram" ref="eric5.Graphics.UMLGraphicsView.html#UMLGraphicsView.printPreviewDiagram" />
--- a/Documentation/Source/eric5.Graphics.UMLGraphicsView.html	Tue Sep 11 18:26:47 2012 +0200
+++ b/Documentation/Source/eric5.Graphics.UMLGraphicsView.html	Tue Sep 11 18:46:46 2012 +0200
@@ -92,6 +92,9 @@
 <td><a href="#UMLGraphicsView.__initActions">__initActions</a></td>
 <td>Private method to initialize the view actions.</td>
 </tr><tr>
+<td><a href="#UMLGraphicsView.__itemsBoundingRect">__itemsBoundingRect</a></td>
+<td>Private method to calculate the bounding rectangle of the given items.</td>
+</tr><tr>
 <td><a href="#UMLGraphicsView.__printPreviewPrint">__printPreviewPrint</a></td>
 <td>Private slot to generate a print preview.</td>
 </tr><tr>
@@ -131,6 +134,9 @@
 <td><a href="#UMLGraphicsView.initToolBar">initToolBar</a></td>
 <td>Public method to populate a toolbar with our actions.</td>
 </tr><tr>
+<td><a href="#UMLGraphicsView.keyPressEvent">keyPressEvent</a></td>
+<td>Protected method handling key press events.</td>
+</tr><tr>
 <td><a href="#UMLGraphicsView.parsePersistenceData">parsePersistenceData</a></td>
 <td>Public method to parse persisted data.</td>
 </tr><tr>
@@ -218,7 +224,22 @@
 <b>__initActions</b>(<i></i>)
 <p>
         Private method to initialize the view actions.
-</p><a NAME="UMLGraphicsView.__printPreviewPrint" ID="UMLGraphicsView.__printPreviewPrint"></a>
+</p><a NAME="UMLGraphicsView.__itemsBoundingRect" ID="UMLGraphicsView.__itemsBoundingRect"></a>
+<h4>UMLGraphicsView.__itemsBoundingRect</h4>
+<b>__itemsBoundingRect</b>(<i>items</i>)
+<p>
+        Private method to calculate the bounding rectangle of the given items.
+</p><dl>
+<dt><i>items</i></dt>
+<dd>
+list of items to operate on (list of UMLItem)
+</dd>
+</dl><dl>
+<dt>Returns:</dt>
+<dd>
+bounding rectangle (QRectF)
+</dd>
+</dl><a NAME="UMLGraphicsView.__printPreviewPrint" ID="UMLGraphicsView.__printPreviewPrint"></a>
 <h4>UMLGraphicsView.__printPreviewPrint</h4>
 <b>__printPreviewPrint</b>(<i>printer</i>)
 <p>
@@ -348,6 +369,16 @@
 <dd>
 the populated toolBar (QToolBar)
 </dd>
+</dl><a NAME="UMLGraphicsView.keyPressEvent" ID="UMLGraphicsView.keyPressEvent"></a>
+<h4>UMLGraphicsView.keyPressEvent</h4>
+<b>keyPressEvent</b>(<i>evt</i>)
+<p>
+        Protected method handling key press events.
+</p><dl>
+<dt><i>evt</i></dt>
+<dd>
+reference to the key event (QKeyEvent)
+</dd>
 </dl><a NAME="UMLGraphicsView.parsePersistenceData" ID="UMLGraphicsView.parsePersistenceData"></a>
 <h4>UMLGraphicsView.parsePersistenceData</h4>
 <b>parsePersistenceData</b>(<i>version, data</i>)
--- a/Graphics/UMLGraphicsView.py	Tue Sep 11 18:26:47 2012 +0200
+++ b/Graphics/UMLGraphicsView.py	Tue Sep 11 18:46:46 2012 +0200
@@ -7,7 +7,7 @@
 Module implementing a subclass of E5GraphicsView for our diagrams.
 """
 
-from PyQt4.QtCore import pyqtSignal, Qt, QSignalMapper, QFileInfo, QEvent
+from PyQt4.QtCore import pyqtSignal, Qt, QSignalMapper, QFileInfo, QEvent, QRectF
 from PyQt4.QtGui import QGraphicsView, QAction, QToolBar, QDialog, QPrinter, QPrintDialog
 
 from E5Graphics.E5GraphicsView import E5GraphicsView
@@ -191,11 +191,12 @@
         sceneRect = self.scene().sceneRect()
         newWidth = width = sceneRect.width()
         newHeight = height = sceneRect.height()
-        rect = self._getDiagramRect(10)
-        if width < rect.width():
-            newWidth = rect.width()
-        if height < rect.height():
-            newHeight = rect.height()
+        rect = self.scene().itemsBoundingRect()
+        # calculate with 10 pixel border on each side
+        if sceneRect.right() - 10 < rect.right():
+            newWidth = rect.right() + 10
+        if sceneRect.bottom() - 10 < rect.bottom():
+            newHeight = rect.bottom() + 10
         
         if newHeight != height or newWidth != width:
             self.setSceneSize(newWidth, newHeight)
@@ -516,6 +517,59 @@
         
         self.scene().update()
     
+    def __itemsBoundingRect(self, items):
+        """
+        Private method to calculate the bounding rectangle of the given items.
+        
+        @param items list of items to operate on (list of UMLItem)
+        @return bounding rectangle (QRectF)
+        """
+        rect = self.scene().sceneRect()
+        right = rect.left()
+        bottom = rect.top()
+        left = rect.right()
+        top = rect.bottom()
+        for item in items:
+            rect = item.sceneBoundingRect()
+            left = min(rect.left(), left)
+            right = max(rect.right(), right)
+            top = min(rect.top(), top)
+            bottom = max(rect.bottom(), bottom)
+        return QRectF(left, top, right - left, bottom - top)
+    
+    def keyPressEvent(self, evt):
+        """
+        Protected method handling key press events.
+        
+        @param evt reference to the key event (QKeyEvent)
+        """
+        key = evt.key()
+        if key in [Qt.Key_Up, Qt.Key_Down, Qt.Key_Left, Qt.Key_Right]:
+            items = self.filteredItems(self.scene().selectedItems())
+            if items:
+                if evt.modifiers() & Qt.ControlModifier:
+                    stepSize = 50
+                else:
+                    stepSize = 5
+                if key == Qt.Key_Up:
+                    dx = 0
+                    dy = -stepSize
+                elif key == Qt.Key_Down:
+                    dx = 0
+                    dy = stepSize
+                elif key == Qt.Key_Left:
+                    dx = -stepSize
+                    dy = 0
+                else:
+                    dx = stepSize
+                    dy = 0
+                for item in items:
+                    item.moveBy(dx, dy)
+                evt.accept()
+                return
+        
+        super().keyPressEvent(evt)
+    
     def wheelEvent(self, evt):
         """
         Protected method to handle wheel events.
--- a/Graphics/UMLItem.py	Tue Sep 11 18:26:47 2012 +0200
+++ b/Graphics/UMLItem.py	Tue Sep 11 18:46:46 2012 +0200
@@ -126,7 +126,17 @@
         @return adjusted values
         """
         if change == QGraphicsItem.ItemPositionChange:
+            # 1. remember to adjust associations
             self.shouldAdjustAssociations = True
+            
+            # 2. ensure the new position is inside the scene
+            rect = self.scene().sceneRect()
+            if not rect.contains(value):
+                # keep the item inside the scene
+                value.setX(min(rect.right(), max(value.x(), rect.left())))
+                value.setY(min(rect.bottom(), max(value.y(), rect.top())))
+                return value
+            
         return QGraphicsItem.itemChange(self, change, value)
         
     def paint(self, painter, option, widget=None):

eric ide

mercurial