Collection of tools useful for audio production
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.

215 lines
7.3KB

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