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.3KB

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