Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
6.3KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Custom Mini Canvas Preview, a custom Qt4 widget
  4. # Copyright (C) 2011-2013 Filipe Coelho <falktx@falktx.com>
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License as
  8. # published by the Free Software Foundation; either version 2 of
  9. # the License, or any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # For a full copy of the GNU General Public License see the GPL.txt file
  17. # ------------------------------------------------------------------------------------------------------------
  18. # Imports (Global)
  19. from PyQt4.QtCore import Qt, QRectF, QTimer, SIGNAL, SLOT
  20. from PyQt4.QtGui import QBrush, QColor, QCursor, QFrame, QPainter, QPen
  21. # ------------------------------------------------------------------------------------------------------------
  22. # Static Variables
  23. iX = 0
  24. iY = 1
  25. iWidth = 2
  26. iHeight = 3
  27. # ------------------------------------------------------------------------------------------------------------
  28. # Widget Class
  29. class CanvasPreviewFrame(QFrame):
  30. def __init__(self, parent):
  31. QFrame.__init__(self, parent)
  32. self.fMouseDown = False
  33. self.fViewBg = QColor(0, 0, 0)
  34. self.fViewBrush = QBrush(QColor(75, 75, 255, 30))
  35. self.fViewPen = QPen(Qt.blue, 1)
  36. self.fScale = 1.0
  37. self.fScene = None
  38. self.fRealParent = None
  39. self.fFakeWidth = 0.0
  40. self.fFakeHeight = 0.0
  41. self.fRenderSource = self.getRenderSource()
  42. self.fRenderTarget = QRectF(0, 0, 0, 0)
  43. self.fViewPadX = 0.0
  44. self.fViewPadY = 0.0
  45. self.fViewRect = [0.0, 0.0, 10.0, 10.0]
  46. def init(self, scene, realWidth, realHeight):
  47. padding = 6
  48. self.fScene = scene
  49. self.fFakeWidth = float(realWidth) / 15
  50. self.fFakeHeight = float(realHeight) / 15
  51. self.setMinimumSize(self.fFakeWidth+padding, self.fFakeHeight+padding)
  52. self.setMaximumSize(self.fFakeWidth*4+padding, self.fFakeHeight+padding)
  53. self.fRenderTarget.setWidth(realWidth)
  54. self.fRenderTarget.setHeight(realHeight)
  55. def setRealParent(self, parent):
  56. self.fRealParent = parent
  57. def getRenderSource(self):
  58. xPadding = (float(self.width()) - self.fFakeWidth) / 2.0
  59. yPadding = (float(self.height()) - self.fFakeHeight) / 2.0
  60. return QRectF(xPadding, yPadding, self.fFakeWidth, self.fFakeHeight)
  61. def setViewPosX(self, xp):
  62. x = self.fFakeWidth*xp
  63. xRatio = (x / self.fFakeWidth) * self.fViewRect[iWidth] / self.fScale
  64. self.fViewRect[iX] = x - xRatio + self.fRenderSource.x()
  65. self.update()
  66. def setViewPosY(self, yp):
  67. y = self.fFakeHeight*yp
  68. yRatio = (y / self.fFakeHeight) * self.fViewRect[iHeight] / self.fScale
  69. self.fViewRect[iY] = y - yRatio + self.fRenderSource.y()
  70. self.update()
  71. def setViewScale(self, scale):
  72. self.fScale = scale
  73. QTimer.singleShot(0, self.fRealParent, SLOT("slot_miniCanvasCheckAll()"))
  74. def setViewSize(self, width, height):
  75. self.fViewRect[iWidth] = width * self.fFakeWidth
  76. self.fViewRect[iHeight] = height * self.fFakeHeight
  77. self.update()
  78. def setViewTheme(self, bgColor, brushColor, penColor):
  79. brushColor.setAlpha(40)
  80. penColor.setAlpha(100)
  81. self.fViewBg = bgColor
  82. self.fViewBrush = QBrush(brushColor)
  83. self.fViewPen = QPen(penColor, 1)
  84. def handleMouseEvent(self, eventX, eventY):
  85. x = float(eventX) - self.fRenderSource.x() - (self.fViewRect[iWidth] / self.fScale / 2)
  86. y = float(eventY) - self.fRenderSource.y() - (self.fViewRect[iHeight] / self.fScale / 2)
  87. maxWidth = self.fViewRect[iWidth] / self.fScale
  88. maxHeight = self.fViewRect[iHeight] / self.fScale
  89. if maxWidth > self.fFakeWidth:
  90. maxWidth = self.fFakeWidth
  91. if maxHeight > self.fFakeHeight:
  92. maxHeight = self.fFakeHeight
  93. if x < 0.0:
  94. x = 0.0
  95. elif x > self.fFakeWidth - maxWidth:
  96. x = self.fFakeWidth - maxWidth
  97. if y < 0.0:
  98. y = 0.0
  99. elif y > self.fFakeHeight - maxHeight:
  100. y = self.fFakeHeight - maxHeight
  101. self.fViewRect[iX] = x + self.fRenderSource.x()
  102. self.fViewRect[iY] = y + self.fRenderSource.y()
  103. self.update()
  104. self.emit(SIGNAL("miniCanvasMoved(double, double)"), x * self.fScale / self.fFakeWidth, y * self.fScale / self.fFakeHeight)
  105. def mousePressEvent(self, event):
  106. if event.button() == Qt.LeftButton:
  107. self.fMouseDown = True
  108. self.setCursor(QCursor(Qt.SizeAllCursor))
  109. self.handleMouseEvent(event.x(), event.y())
  110. event.accept()
  111. def mouseMoveEvent(self, event):
  112. if self.fMouseDown:
  113. self.handleMouseEvent(event.x(), event.y())
  114. event.accept()
  115. def mouseReleaseEvent(self, event):
  116. if self.fMouseDown:
  117. self.setCursor(QCursor(Qt.ArrowCursor))
  118. self.fMouseDown = False
  119. QFrame.mouseReleaseEvent(self, event)
  120. def paintEvent(self, event):
  121. painter = QPainter(self)
  122. painter.setBrush(self.fViewBg)
  123. painter.setPen(self.fViewBg)
  124. painter.drawRoundRect(2, 2, self.width()-6, self.height()-6, 3, 3)
  125. self.fScene.render(painter, self.fRenderSource, self.fRenderTarget, Qt.KeepAspectRatio)
  126. maxWidth = self.fViewRect[iWidth] / self.fScale
  127. maxHeight = self.fViewRect[iHeight] / self.fScale
  128. if maxWidth > self.fFakeWidth:
  129. maxWidth = self.fFakeWidth
  130. if maxHeight > self.fFakeHeight:
  131. maxHeight = self.fFakeHeight
  132. painter.setBrush(self.fViewBrush)
  133. painter.setPen(self.fViewPen)
  134. painter.drawRect(self.fViewRect[iX], self.fViewRect[iY], maxWidth, maxHeight)
  135. QFrame.paintEvent(self, event)
  136. def resizeEvent(self, event):
  137. self.fRenderSource = self.getRenderSource()
  138. if self.fRealParent:
  139. QTimer.singleShot(0, self.fRealParent, SLOT("slot_miniCanvasCheckAll()"))
  140. QFrame.resizeEvent(self, event)