From ca359f9cd0ba09eb937405cf68cfdb50d5a79d41 Mon Sep 17 00:00:00 2001 From: falkTX Date: Sun, 2 Feb 2014 06:52:11 +0000 Subject: [PATCH] Implement custom canvas sizes in the preview frame --- resources/ui/carla_host.ui | 2 +- source/carla | 8 ++ source/carla_patchbay.py | 5 +- source/widgets/canvaspreviewframe.py | 139 ++++++++++++--------------- 4 files changed, 75 insertions(+), 79 deletions(-) diff --git a/resources/ui/carla_host.ui b/resources/ui/carla_host.ui index 4fbb053a6..7894ec15f 100644 --- a/resources/ui/carla_host.ui +++ b/resources/ui/carla_host.ui @@ -153,7 +153,7 @@ - 210 + 16777215 162 diff --git a/source/carla b/source/carla index f597b2325..6e47a2aaa 100755 --- a/source/carla +++ b/source/carla @@ -148,6 +148,14 @@ class CarlaMultiW(QTabWidget): self.fPatchbay.resize(self.fRack.size()) self.fPatchbay.slot_miniCanvasCheckSize() + #def paintEvent(self, event): + #QTabWidget.paintEvent(self, event) + + #painter = QPainter(self) + #painter.setBrush(QColor(36, 36, 36)) + #painter.setPen(QColor(62, 62, 62)) + #painter.drawRect(1, self.height()/2, self.width()-3, self.height()-self.height()/2-1) + def resizeEvent(self, event): QTabWidget.resizeEvent(self, event) diff --git a/source/carla_patchbay.py b/source/carla_patchbay.py index bf5742b61..2743b0fb2 100644 --- a/source/carla_patchbay.py +++ b/source/carla_patchbay.py @@ -339,8 +339,8 @@ class CarlaPatchbayW(QGraphicsView): @pyqtSlot(float, float) def slot_miniCanvasMoved(self, xp, yp): self.fMovingViaMiniCanvas = True - self.horizontalScrollBar().setValue(xp * self.fCanvasWidth) - self.verticalScrollBar().setValue(yp * self.fCanvasHeight) + self.horizontalScrollBar().setValue(xp * self.horizontalScrollBar().maximum()) + self.verticalScrollBar().setValue(yp * self.verticalScrollBar().maximum()) self.fMovingViaMiniCanvas = False self.updateCanvasInitialPos() @@ -451,6 +451,7 @@ class CarlaPatchbayW(QGraphicsView): self.setupCanvas() self.fParent.updateContainer(self.themeData) + self.slot_miniCanvasCheckAll() if Carla.host.is_engine_running(): Carla.host.patchbay_refresh() diff --git a/source/widgets/canvaspreviewframe.py b/source/widgets/canvaspreviewframe.py index 3aa14577a..c177b3311 100644 --- a/source/widgets/canvaspreviewframe.py +++ b/source/widgets/canvaspreviewframe.py @@ -36,43 +36,42 @@ iHeight = 3 class CanvasPreviewFrame(QFrame): miniCanvasMoved = pyqtSignal(float, float) + # x = 2 + # y = 2 + # w = width-4 + # h = height-3 + # bounds -1 px + + kInternalWidth = 210-6 # -4 for width + -1px*2 bounds + kInternalHeight = 162-5 # -3 for height + -1px*2 bounds + def __init__(self, parent): QFrame.__init__(self, parent) + #self.setMinimumWidth(210) + #self.setMinimumHeight(162) + #self.setMaximumHeight(162) + + self.fRealParent = None + + self.fScene = None + self.fRenderSource = QRectF(0.0, 0.0, 0.0, 0.0) + self.fRenderTarget = QRectF(0.0, 0.0, 0.0, 0.0) self.fUseCustomPaint = False - self.fMouseDown = False + self.fInitialX = 0.0 + self.fScale = 1.0 self.fViewBg = QColor(0, 0, 0) self.fViewBrush = QBrush(QColor(75, 75, 255, 30)) self.fViewPen = QPen(Qt.blue, 1) + self.fViewRect = [3.0, 3.0, 10.0, 10.0] - self.fScale = 1.0 - self.fScene = None - - self.fRealParent = None - self.fFakeWidth = 0.0 - self.fFakeHeight = 0.0 - - self.fRenderSource = self.getRenderSource() - self.fRenderTarget = QRectF(0, 0, 0, 0) - - self.fViewPadX = 0.0 - self.fViewPadY = 0.0 - self.fViewRect = [0.0, 0.0, 10.0, 10.0] + self.fMouseDown = False def init(self, scene, realWidth, realHeight, useCustomPaint = False): - padding = 6 - self.fScene = scene - self.fFakeWidth = float(realWidth) / 15.0 - self.fFakeHeight = float(realHeight) / 15.0 - - self.setMinimumSize(self.fFakeWidth+padding, self.fFakeHeight+padding) - self.setMaximumSize(self.fFakeWidth*4+padding, self.fFakeHeight+padding) - - self.fRenderTarget.setWidth(realWidth) - self.fRenderTarget.setHeight(realHeight) + self.fRenderSource = QRectF(0.0, 0.0, float(realWidth), float(realHeight)) if self.fUseCustomPaint != useCustomPaint: self.fUseCustomPaint = useCustomPaint @@ -81,21 +80,12 @@ class CanvasPreviewFrame(QFrame): def setRealParent(self, parent): self.fRealParent = parent - def getRenderSource(self): - xPadding = (float(self.width()) - self.fFakeWidth) / 2.0 - yPadding = (float(self.height()) - self.fFakeHeight) / 2.0 - return QRectF(xPadding, yPadding, self.fFakeWidth, self.fFakeHeight) - def setViewPosX(self, xp): - x = self.fFakeWidth*xp - xRatio = (x / self.fFakeWidth) * self.fViewRect[iWidth] / self.fScale - self.fViewRect[iX] = x - xRatio + self.fRenderSource.x() + self.fViewRect[iX] = (xp * self.kInternalWidth) - (xp * (self.fViewRect[iWidth]/self.fScale)) + xp self.update() def setViewPosY(self, yp): - y = self.fFakeHeight*yp - yRatio = (y / self.fFakeHeight) * self.fViewRect[iHeight] / self.fScale - self.fViewRect[iY] = y - yRatio + self.fRenderSource.y() + self.fViewRect[iY] = (yp * self.kInternalHeight) - (yp * (self.fViewRect[iHeight]/self.fScale)) + yp self.update() def setViewScale(self, scale): @@ -105,8 +95,8 @@ class CanvasPreviewFrame(QFrame): QTimer.singleShot(0, self.fRealParent.slot_miniCanvasCheckAll) def setViewSize(self, width, height): - self.fViewRect[iWidth] = width * self.fFakeWidth - self.fViewRect[iHeight] = height * self.fFakeHeight + self.fViewRect[iWidth] = width * self.kInternalWidth + self.fViewRect[iHeight] = height * self.kInternalHeight self.update() def setViewTheme(self, bgColor, brushColor, penColor): @@ -117,85 +107,81 @@ class CanvasPreviewFrame(QFrame): self.fViewPen = QPen(penColor, 1) def handleMouseEvent(self, eventX, eventY): - x = float(eventX) - self.fRenderSource.x() - (self.fViewRect[iWidth] / self.fScale / 2) - y = float(eventY) - self.fRenderSource.y() - (self.fViewRect[iHeight] / self.fScale / 2) - - maxWidth = self.fViewRect[iWidth] / self.fScale - maxHeight = self.fViewRect[iHeight] / self.fScale - - if maxWidth > self.fFakeWidth: - maxWidth = self.fFakeWidth - if maxHeight > self.fFakeHeight: - maxHeight = self.fFakeHeight + x = float(eventX) - self.fInitialX + y = float(eventY) - 3.0 if x < 0.0: x = 0.0 - elif x > self.fFakeWidth - maxWidth: - x = self.fFakeWidth - maxWidth + elif x > self.kInternalWidth: + x = float(self.kInternalWidth) if y < 0.0: y = 0.0 - elif y > self.fFakeHeight - maxHeight: - y = self.fFakeHeight - maxHeight + elif y > self.kInternalHeight: + y = float(self.kInternalHeight) + + xp = x/self.kInternalWidth + yp = y/self.kInternalHeight - self.fViewRect[iX] = x + self.fRenderSource.x() - self.fViewRect[iY] = y + self.fRenderSource.y() + self.fViewRect[iX] = x - (xp * self.fViewRect[iWidth]/self.fScale) + xp + self.fViewRect[iY] = y - (yp * self.fViewRect[iHeight]/self.fScale) + yp self.update() - self.miniCanvasMoved.emit(x * self.fScale / self.fFakeWidth, y * self.fScale / self.fFakeHeight) + self.miniCanvasMoved.emit(xp, yp) def mousePressEvent(self, event): if event.button() == Qt.LeftButton: self.fMouseDown = True self.setCursor(QCursor(Qt.SizeAllCursor)) self.handleMouseEvent(event.x(), event.y()) - event.accept() + return event.accept() + QFrame.mouseMoveEvent(self, event) def mouseMoveEvent(self, event): if self.fMouseDown: self.handleMouseEvent(event.x(), event.y()) - event.accept() + return event.accept() + QFrame.mouseMoveEvent(self, event) def mouseReleaseEvent(self, event): if self.fMouseDown: self.setCursor(QCursor(Qt.ArrowCursor)) - self.fMouseDown = False + self.fMouseDown = False + return event.accept() QFrame.mouseReleaseEvent(self, event) def paintEvent(self, event): painter = QPainter(self) if self.fUseCustomPaint: - painter.setBrush(self.fViewBg) - painter.setPen(QColor(12, 12, 12)) - painter.drawRect(0, 0, self.width(), self.height()-2) - painter.setBrush(QColor(36, 36, 36)) painter.setPen(QColor(62, 62, 62)) - painter.drawRect(1, 1, self.width()-2, self.height()-4) - - painter.setBrush(self.fViewBg) - painter.setPen(self.fViewBg) - painter.drawRect(2, 3, self.width()-5, self.height()-7) + painter.drawRect(2, 2, self.width()-4, self.height()-3) + #painter.setBrush(self.fViewBg) + #painter.setPen(self.fViewBg) + painter.setBrush(QColor(62, 62, 255)) + painter.setPen(QColor(62, 62, 255)) + painter.drawRect(3, 3, self.width()-6, self.height()-6) else: painter.setBrush(self.fViewBg) painter.setPen(self.fViewBg) - painter.drawRoundedRect(2, 2, self.width()-6, self.height()-6, 3, 3) + painter.drawRoundedRect(3, 3, self.width()-6, self.height()-6, 3, 3) - self.fScene.render(painter, self.fRenderSource, self.fRenderTarget, Qt.KeepAspectRatio) + self.fScene.render(painter, self.fRenderTarget, self.fRenderSource, Qt.KeepAspectRatio) - maxWidth = self.fViewRect[iWidth] / self.fScale - maxHeight = self.fViewRect[iHeight] / self.fScale + width = self.fViewRect[iWidth]/self.fScale + height = self.fViewRect[iHeight]/self.fScale - if maxWidth > self.fFakeWidth: - maxWidth = self.fFakeWidth - if maxHeight > self.fFakeHeight: - maxHeight = self.fFakeHeight + if width > self.kInternalWidth: + width = self.kInternalWidth + if height > self.kInternalHeight: + height = self.kInternalHeight + # cursor painter.setBrush(self.fViewBrush) painter.setPen(self.fViewPen) - painter.drawRect(self.fViewRect[iX], self.fViewRect[iY], maxWidth, maxHeight) + painter.drawRect(self.fViewRect[iX]+self.fInitialX, self.fViewRect[iY]+3, width, height) if self.fUseCustomPaint: event.accept() @@ -203,7 +189,8 @@ class CanvasPreviewFrame(QFrame): QFrame.paintEvent(self, event) def resizeEvent(self, event): - self.fRenderSource = self.getRenderSource() + self.fInitialX = float(self.width())/2.0 - float(self.kInternalWidth)/2.0 + self.fRenderTarget = QRectF(self.fInitialX, 3.0, float(self.kInternalWidth), float(self.kInternalHeight)) if self.fRealParent is not None: QTimer.singleShot(0, self.fRealParent.slot_miniCanvasCheckAll)