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.

canvaspreviewframe.py 7.1KB

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