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

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