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 6.8KB

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