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.

211 lines
7.2KB

  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. # TODO - SIGNAL, SLOT
  20. try:
  21. from PyQt5.QtCore import Qt, QRectF, QTimer
  22. from PyQt5.QtGui import QBrush, QColor, QCursor, QPainter, QPen
  23. from PyQt5.QtWidgets import QFrame
  24. except:
  25. from PyQt4.QtCore import Qt, QRectF, QTimer
  26. from PyQt4.QtGui import QBrush, QColor, QCursor, QFrame, QPainter, QPen
  27. # ------------------------------------------------------------------------------------------------------------
  28. # Static Variables
  29. iX = 0
  30. iY = 1
  31. iWidth = 2
  32. iHeight = 3
  33. # ------------------------------------------------------------------------------------------------------------
  34. # Widget Class
  35. class CanvasPreviewFrame(QFrame):
  36. def __init__(self, parent):
  37. QFrame.__init__(self, parent)
  38. self.fUseCustomPaint = False
  39. self.fMouseDown = False
  40. self.fViewBg = QColor(0, 0, 0)
  41. self.fViewBrush = QBrush(QColor(75, 75, 255, 30))
  42. self.fViewPen = QPen(Qt.blue, 1)
  43. self.fScale = 1.0
  44. self.fScene = None
  45. self.fRealParent = None
  46. self.fFakeWidth = 0.0
  47. self.fFakeHeight = 0.0
  48. self.fRenderSource = self.getRenderSource()
  49. self.fRenderTarget = QRectF(0, 0, 0, 0)
  50. self.fViewPadX = 0.0
  51. self.fViewPadY = 0.0
  52. self.fViewRect = [0.0, 0.0, 10.0, 10.0]
  53. def init(self, scene, realWidth, realHeight, useCustomPaint = False):
  54. padding = 6
  55. self.fScene = scene
  56. self.fFakeWidth = float(realWidth) / 15
  57. self.fFakeHeight = float(realHeight) / 15
  58. self.setMinimumSize(self.fFakeWidth+padding, self.fFakeHeight+padding)
  59. self.setMaximumSize(self.fFakeWidth*4+padding, self.fFakeHeight+padding)
  60. self.fRenderTarget.setWidth(realWidth)
  61. self.fRenderTarget.setHeight(realHeight)
  62. if self.fUseCustomPaint != useCustomPaint:
  63. self.fUseCustomPaint = useCustomPaint
  64. self.repaint()
  65. def setRealParent(self, parent):
  66. self.fRealParent = parent
  67. def getRenderSource(self):
  68. xPadding = (float(self.width()) - self.fFakeWidth) / 2.0
  69. yPadding = (float(self.height()) - self.fFakeHeight) / 2.0
  70. return QRectF(xPadding, yPadding, self.fFakeWidth, self.fFakeHeight)
  71. def setViewPosX(self, xp):
  72. x = self.fFakeWidth*xp
  73. xRatio = (x / self.fFakeWidth) * self.fViewRect[iWidth] / self.fScale
  74. self.fViewRect[iX] = x - xRatio + self.fRenderSource.x()
  75. self.update()
  76. def setViewPosY(self, yp):
  77. y = self.fFakeHeight*yp
  78. yRatio = (y / self.fFakeHeight) * self.fViewRect[iHeight] / self.fScale
  79. self.fViewRect[iY] = y - yRatio + self.fRenderSource.y()
  80. self.update()
  81. def setViewScale(self, scale):
  82. self.fScale = scale
  83. #QTimer.singleShot(0, self.fRealParent, SLOT("slot_miniCanvasCheckAll()"))
  84. def setViewSize(self, width, height):
  85. self.fViewRect[iWidth] = width * self.fFakeWidth
  86. self.fViewRect[iHeight] = height * self.fFakeHeight
  87. self.update()
  88. def setViewTheme(self, bgColor, brushColor, penColor):
  89. brushColor.setAlpha(40)
  90. penColor.setAlpha(100)
  91. self.fViewBg = bgColor
  92. self.fViewBrush = QBrush(brushColor)
  93. self.fViewPen = QPen(penColor, 1)
  94. def handleMouseEvent(self, eventX, eventY):
  95. x = float(eventX) - self.fRenderSource.x() - (self.fViewRect[iWidth] / self.fScale / 2)
  96. y = float(eventY) - self.fRenderSource.y() - (self.fViewRect[iHeight] / self.fScale / 2)
  97. maxWidth = self.fViewRect[iWidth] / self.fScale
  98. maxHeight = self.fViewRect[iHeight] / self.fScale
  99. if maxWidth > self.fFakeWidth:
  100. maxWidth = self.fFakeWidth
  101. if maxHeight > self.fFakeHeight:
  102. maxHeight = self.fFakeHeight
  103. if x < 0.0:
  104. x = 0.0
  105. elif x > self.fFakeWidth - maxWidth:
  106. x = self.fFakeWidth - maxWidth
  107. if y < 0.0:
  108. y = 0.0
  109. elif y > self.fFakeHeight - maxHeight:
  110. y = self.fFakeHeight - maxHeight
  111. self.fViewRect[iX] = x + self.fRenderSource.x()
  112. self.fViewRect[iY] = y + self.fRenderSource.y()
  113. self.update()
  114. #self.emit(SIGNAL("miniCanvasMoved(double, double)"), x * self.fScale / self.fFakeWidth, y * self.fScale / self.fFakeHeight)
  115. def mousePressEvent(self, event):
  116. if event.button() == Qt.LeftButton:
  117. self.fMouseDown = True
  118. self.setCursor(QCursor(Qt.SizeAllCursor))
  119. self.handleMouseEvent(event.x(), event.y())
  120. event.accept()
  121. def mouseMoveEvent(self, event):
  122. if self.fMouseDown:
  123. self.handleMouseEvent(event.x(), event.y())
  124. event.accept()
  125. def mouseReleaseEvent(self, event):
  126. if self.fMouseDown:
  127. self.setCursor(QCursor(Qt.ArrowCursor))
  128. self.fMouseDown = False
  129. QFrame.mouseReleaseEvent(self, event)
  130. def paintEvent(self, event):
  131. painter = QPainter(self)
  132. if self.fUseCustomPaint:
  133. painter.setBrush(self.fViewBg)
  134. painter.setPen(QColor(12, 12, 12))
  135. painter.drawRect(0, 0, self.width(), self.height()-2)
  136. painter.setBrush(QColor(36, 36, 36))
  137. painter.setPen(QColor(62, 62, 62))
  138. painter.drawRect(1, 1, self.width()-2, self.height()-4)
  139. painter.setBrush(self.fViewBg)
  140. painter.setPen(self.fViewBg)
  141. painter.drawRect(2, 3, self.width()-5, self.height()-7)
  142. else:
  143. painter.setBrush(self.fViewBg)
  144. painter.setPen(self.fViewBg)
  145. painter.drawRoundedRect(2, 2, self.width()-6, self.height()-6, 3, 3)
  146. self.fScene.render(painter, self.fRenderSource, self.fRenderTarget, Qt.KeepAspectRatio)
  147. maxWidth = self.fViewRect[iWidth] / self.fScale
  148. maxHeight = self.fViewRect[iHeight] / self.fScale
  149. if maxWidth > self.fFakeWidth:
  150. maxWidth = self.fFakeWidth
  151. if maxHeight > self.fFakeHeight:
  152. maxHeight = self.fFakeHeight
  153. painter.setBrush(self.fViewBrush)
  154. painter.setPen(self.fViewPen)
  155. painter.drawRect(self.fViewRect[iX], self.fViewRect[iY], maxWidth, maxHeight)
  156. if self.fUseCustomPaint:
  157. event.accept()
  158. else:
  159. QFrame.paintEvent(self, event)
  160. def resizeEvent(self, event):
  161. self.fRenderSource = self.getRenderSource()
  162. #if self.fRealParent:
  163. #QTimer.singleShot(0, self.fRealParent, SLOT("slot_miniCanvasCheckAll()"))
  164. #QFrame.resizeEvent(self, event)