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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 doc/GPL.txt file.
  17. # ------------------------------------------------------------------------------------------------------------
  18. # Imports (Global)
  19. from PyQt4.QtCore import pyqtSignal, Qt, QRectF, QTimer
  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. miniCanvasMoved = pyqtSignal(float, float)
  31. def __init__(self, parent):
  32. QFrame.__init__(self, parent)
  33. self.fUseCustomPaint = False
  34. self.fMouseDown = False
  35. self.fViewBg = QColor(0, 0, 0)
  36. self.fViewBrush = QBrush(QColor(75, 75, 255, 30))
  37. self.fViewPen = QPen(Qt.blue, 1)
  38. self.fScale = 1.0
  39. self.fScene = None
  40. self.fRealParent = None
  41. self.fFakeWidth = 0.0
  42. self.fFakeHeight = 0.0
  43. self.fRenderSource = self.getRenderSource()
  44. self.fRenderTarget = QRectF(0, 0, 0, 0)
  45. self.fViewPadX = 0.0
  46. self.fViewPadY = 0.0
  47. self.fViewRect = [0.0, 0.0, 10.0, 10.0]
  48. def init(self, scene, realWidth, realHeight, useCustomPaint = False):
  49. padding = 6
  50. self.fScene = scene
  51. self.fFakeWidth = float(realWidth) / 15.0
  52. self.fFakeHeight = float(realHeight) / 15.0
  53. self.setMinimumSize(self.fFakeWidth+padding, self.fFakeHeight+padding)
  54. self.setMaximumSize(self.fFakeWidth*4+padding, self.fFakeHeight+padding)
  55. self.fRenderTarget.setWidth(realWidth)
  56. self.fRenderTarget.setHeight(realHeight)
  57. if self.fUseCustomPaint != useCustomPaint:
  58. self.fUseCustomPaint = useCustomPaint
  59. self.repaint()
  60. def setRealParent(self, parent):
  61. self.fRealParent = parent
  62. def getRenderSource(self):
  63. xPadding = (float(self.width()) - self.fFakeWidth) / 2.0
  64. yPadding = (float(self.height()) - self.fFakeHeight) / 2.0
  65. return QRectF(xPadding, yPadding, self.fFakeWidth, self.fFakeHeight)
  66. def setViewPosX(self, xp):
  67. x = self.fFakeWidth*xp
  68. xRatio = (x / self.fFakeWidth) * self.fViewRect[iWidth] / self.fScale
  69. self.fViewRect[iX] = x - xRatio + self.fRenderSource.x()
  70. self.update()
  71. def setViewPosY(self, yp):
  72. y = self.fFakeHeight*yp
  73. yRatio = (y / self.fFakeHeight) * self.fViewRect[iHeight] / self.fScale
  74. self.fViewRect[iY] = y - yRatio + self.fRenderSource.y()
  75. self.update()
  76. def setViewScale(self, scale):
  77. self.fScale = scale
  78. if self.fRealParent is not None:
  79. QTimer.singleShot(0, self.fRealParent.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.miniCanvasMoved.emit(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.drawRoundedRect(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 is not None:
  159. QTimer.singleShot(0, self.fRealParent.slot_miniCanvasCheckAll)
  160. QFrame.resizeEvent(self, event)