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

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