Graphics/UMLGraphicsView.py

changeset 2038
72557ef75de1
parent 2034
8de0fc1f7fef
child 2039
fa41ccd7f7bc
equal deleted inserted replaced
2037:7a3470ae170e 2038:72557ef75de1
5 5
6 """ 6 """
7 Module implementing a subclass of E5GraphicsView for our diagrams. 7 Module implementing a subclass of E5GraphicsView for our diagrams.
8 """ 8 """
9 9
10 from PyQt4.QtCore import pyqtSignal, Qt, QSignalMapper, QFileInfo, QEvent 10 from PyQt4.QtCore import pyqtSignal, Qt, QSignalMapper, QFileInfo, QEvent, QRectF
11 from PyQt4.QtGui import QGraphicsView, QAction, QToolBar, QDialog, QPrinter, QPrintDialog 11 from PyQt4.QtGui import QGraphicsView, QAction, QToolBar, QDialog, QPrinter, QPrintDialog
12 12
13 from E5Graphics.E5GraphicsView import E5GraphicsView 13 from E5Graphics.E5GraphicsView import E5GraphicsView
14 14
15 from E5Gui import E5MessageBox, E5FileDialog 15 from E5Gui import E5MessageBox, E5FileDialog
189 self.deleteShapeAct.setEnabled(False) 189 self.deleteShapeAct.setEnabled(False)
190 190
191 sceneRect = self.scene().sceneRect() 191 sceneRect = self.scene().sceneRect()
192 newWidth = width = sceneRect.width() 192 newWidth = width = sceneRect.width()
193 newHeight = height = sceneRect.height() 193 newHeight = height = sceneRect.height()
194 rect = self._getDiagramRect(10) 194 rect = self.scene().itemsBoundingRect()
195 if width < rect.width(): 195 # calculate with 10 pixel border on each side
196 newWidth = rect.width() 196 if sceneRect.right() - 10 < rect.right():
197 if height < rect.height(): 197 newWidth = rect.right() + 10
198 newHeight = rect.height() 198 if sceneRect.bottom() - 10 < rect.bottom():
199 newHeight = rect.bottom() + 10
199 200
200 if newHeight != height or newWidth != width: 201 if newHeight != height or newWidth != width:
201 self.setSceneSize(newWidth, newHeight) 202 self.setSceneSize(newWidth, newHeight)
202 self.__checkSizeActions() 203 self.__checkSizeActions()
203 204
513 yOffset = (rect.y() + rect.height() // 2) - \ 514 yOffset = (rect.y() + rect.height() // 2) - \
514 (itemrect.y() + itemrect.height() // 2) 515 (itemrect.y() + itemrect.height() // 2)
515 item.moveBy(xOffset, yOffset) 516 item.moveBy(xOffset, yOffset)
516 517
517 self.scene().update() 518 self.scene().update()
519
520 def __itemsBoundingRect(self, items):
521 """
522 Private method to calculate the bounding rectangle of the given items.
523
524 @param items list of items to operate on (list of UMLItem)
525 @return bounding rectangle (QRectF)
526 """
527 rect = self.scene().sceneRect()
528 right = rect.left()
529 bottom = rect.top()
530 left = rect.right()
531 top = rect.bottom()
532 for item in items:
533 rect = item.sceneBoundingRect()
534 left = min(rect.left(), left)
535 right = max(rect.right(), right)
536 top = min(rect.top(), top)
537 bottom = max(rect.bottom(), bottom)
538 return QRectF(left, top, right - left, bottom - top)
539
540 def keyPressEvent(self, evt):
541 """
542 Protected method handling key press events.
543
544 @param evt reference to the key event (QKeyEvent)
545 """
546 key = evt.key()
547 if key in [Qt.Key_Up, Qt.Key_Down, Qt.Key_Left, Qt.Key_Right]:
548 items = self.filteredItems(self.scene().selectedItems())
549 if items:
550 if evt.modifiers() & Qt.ControlModifier:
551 stepSize = 50
552 else:
553 stepSize = 5
554 if key == Qt.Key_Up:
555 dx = 0
556 dy = -stepSize
557 elif key == Qt.Key_Down:
558 dx = 0
559 dy = stepSize
560 elif key == Qt.Key_Left:
561 dx = -stepSize
562 dy = 0
563 else:
564 dx = stepSize
565 dy = 0
566 for item in items:
567 item.moveBy(dx, dy)
568 evt.accept()
569 return
570
571 super().keyPressEvent(evt)
518 572
519 def wheelEvent(self, evt): 573 def wheelEvent(self, evt):
520 """ 574 """
521 Protected method to handle wheel events. 575 Protected method to handle wheel events.
522 576

eric ide

mercurial