From 1b3fd1e50827fea107bc92f76dfd51a3535bdbc0 Mon Sep 17 00:00:00 2001 From: Treeki Date: Mon, 14 Nov 2011 00:13:47 +0100 Subject: added object resizing --- src/editorui.py | 147 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) (limited to 'src') diff --git a/src/editorui.py b/src/editorui.py index 1d468c6..e89810c 100644 --- a/src/editorui.py +++ b/src/editorui.py @@ -54,6 +54,10 @@ class KPEditorObject(KPEditorItem): self._updatePosition() self._updateSize() + self.setAcceptHoverEvents(True) + + self.resizing = None + # I don't bother setting the ZValue because it doesn't quite matter: # only one layer's objects are ever clickable, and drawBackground takes # care of the layered drawing @@ -72,6 +76,8 @@ class KPEditorObject(KPEditorItem): self._boundingRect = QtCore.QRectF(0, 0, w*24, h*24) self._selectionRect = QtCore.QRectF(0, 0, w*24-1, h*24-1) + + self._resizerEndXY = (w*24-5, h*24-5) def paint(self, painter, option, widget): @@ -79,6 +85,147 @@ class KPEditorObject(KPEditorItem): painter.setPen(QtGui.QPen(Qt.white, 1, Qt.DotLine)) painter.drawRect(self._selectionRect) + + RESIZER_TOP_LEFT = 1 + RESIZER_TOP_RIGHT = 2 + RESIZER_BOTTOM_LEFT = 3 + RESIZER_BOTTOM_RIGHT = 4 + RESIZER_TOP = 5 + RESIZER_BOTTOM = 6 + RESIZER_LEFT = 7 + RESIZER_RIGHT = 8 + + def resizerPortionAt(self, x, y): + rightBound, bottomBound = self._resizerEndXY + + if y < 5: + if x < 5: return 1 # TOP_LEFT + elif x >= rightBound: return 2 # TOP_RIGHT + else: return 5 # TOP + + elif y >= bottomBound: + if x < 5: return 3 # BOTTOM_LEFT + elif x >= rightBound: return 4 # BOTTOM_RIGHT + else: return 6 # BOTTOM + + else: + if x < 5: return 7 # LEFT + elif x >= rightBound: return 8 # RIGHT + else: return None + + + def hoverMoveEvent(self, event): + pos = event.pos() + bit = self.resizerPortionAt(pos.x(), pos.y()) + + if bit == 1 or bit == 4: + self.setCursor(Qt.SizeFDiagCursor) + elif bit == 2 or bit == 3: + self.setCursor(Qt.SizeBDiagCursor) + elif bit == 7 or bit == 8: + self.setCursor(Qt.SizeHorCursor) + elif bit == 5 or bit == 6: + self.setCursor(Qt.SizeVerCursor) + else: + self.setCursor(Qt.ArrowCursor) + + + def mousePressEvent(self, event): + if event.button() == Qt.LeftButton: + pos = event.pos() + bit = self.resizerPortionAt(pos.x(), pos.y()) + + if bit: + event.accept() + + x, xSide, y, ySide = False, None, False, None + + if bit == 1 or bit == 7 or bit == 3: + x, xSide = True, 1 + elif bit == 2 or bit == 4 or bit == 8: + x, xSide = True, 0 + + if bit == 1 or bit == 2 or bit == 5: + y, ySide = True, 1 + elif bit == 3 or bit == 4 or bit == 6: + y, ySide = True, 0 + + self.resizing = (x, xSide, y, ySide) + return + + KPEditorItem.mousePressEvent(self, event) + + + def _tryAndResize(self, obj, axisIndex, mousePosition, stationarySide): + objPosition = obj.position[axisIndex] + objSize = obj.size[axisIndex] + + + if stationarySide == 0: + # Resize the right/bottom side + relativeMousePosition = mousePosition - objPosition + newSize = relativeMousePosition + 1 + if newSize == objSize or newSize < 1: + return False + + if axisIndex == 1: + obj.size = (obj.size[0], newSize) + else: + obj.size = (newSize, obj.size[1]) + + else: + # Resize the left/top side + rightSide = objPosition + objSize - 1 + newLeftSide = mousePosition + + newPosition = newLeftSide + newSize = rightSide - newLeftSide + 1 + + if newSize < 1: + return False + if newPosition == objPosition and newSize == objSize: + return False + + if axisIndex == 1: + obj.position = (obj.position[0], newPosition) + obj.size = (obj.size[0], newSize) + else: + obj.position = (newPosition, obj.position[1]) + obj.size = (newSize, obj.size[1]) + + return True + + + def mouseMoveEvent(self, event): + if self.resizing: + obj = self._objRef() + scenePos = event.scenePos() + + hasChanged = False + resizeX, xSide, resizeY, ySide = self.resizing + + if resizeX: + hasChanged |= self._tryAndResize(obj, 0, int(scenePos.x() / 24), xSide) + if resizeY: + hasChanged |= self._tryAndResize(obj, 1, int(scenePos.y() / 24), ySide) + + if hasChanged: + obj.updateCache() + self._layerRef().updateCache() + self._updatePosition() + self._updateSize() + + else: + KPEditorItem.mouseMoveEvent(self, event) + + + def mouseReleaseEvent(self, event): + if self.resizing and event.button() == Qt.LeftButton: + self.resizing = None + else: + KPEditorItem.mouseReleaseEvent(self, event) + + def _itemMoved(self, oldX, oldY, newX, newY): obj = self._objRef() obj.position = (newX/24, newY/24) -- cgit v1.2.3