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.

179 lines
6.1KB

  1. #!/usr/bin/env python
  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 modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # 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 COPYING 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.fMouseDown = False
  33. self.fViewBrush = QBrush(QColor(75, 75, 255, 30))
  34. self.fViewPen = QPen(Qt.blue, 1)
  35. self.fScale = 1.0
  36. self.fScene = None
  37. self.fRealParent = None
  38. self.fFakeWidth = 0
  39. self.fFakeHeight = 0
  40. self.fRenderSource = self.getRenderSource()
  41. self.fRenderTarget = QRectF(0, 0, 0, 0)
  42. self.fViewPadX = 0.0
  43. self.fViewPadY = 0.0
  44. self.fViewRect = [0.0, 0.0, 10.0, 10.0]
  45. def init(self, scene, realWidth, realHeight):
  46. padding = 6
  47. self.fScene = scene
  48. self.fFakeWidth = float(realWidth) / 15
  49. self.fFakeHeight = float(realHeight) / 15
  50. self.setMinimumSize(self.fFakeWidth+padding, self.fFakeHeight+padding)
  51. self.setMaximumSize(self.fFakeWidth*4+padding, self.fFakeHeight+padding)
  52. self.fRenderTarget.setWidth(realWidth)
  53. self.fRenderTarget.setHeight(realHeight)
  54. def setRealParent(self, parent):
  55. self.fRealParent = parent
  56. def getRenderSource(self):
  57. xPadding = (self.width() - self.fFakeWidth) / 2
  58. yPadding = (self.height() - self.fFakeHeight) / 2
  59. return QRectF(xPadding, yPadding, self.fFakeWidth, self.fFakeHeight)
  60. def setViewPosX(self, xp):
  61. x = xp * self.fFakeWidth
  62. xRatio = (x / self.fFakeWidth) * self.fViewRect[iWidth] / self.fScale
  63. self.fViewRect[iX] = x - xRatio + self.fRenderSource.x()
  64. self.update()
  65. def setViewPosY(self, yp):
  66. y = yp * self.fFakeHeight
  67. yRatio = (y / self.fFakeHeight) * self.fViewRect[iHeight] / self.fScale
  68. self.fViewRect[iY] = y - yRatio + self.fRenderSource.y()
  69. self.update()
  70. def setViewScale(self, scale):
  71. self.fScale = scale
  72. QTimer.singleShot(0, self.fRealParent, SLOT("slot_miniCanvasCheckAll()"))
  73. def setViewSize(self, width, height):
  74. self.fViewRect[iWidth] = width * self.fFakeWidth
  75. self.fViewRect[iHeight] = height * self.fFakeHeight
  76. self.update()
  77. def setViewTheme(self, brushColor, penColor):
  78. brushColor.setAlpha(40)
  79. penColor.setAlpha(100)
  80. self.fViewBrush = QBrush(brushColor)
  81. self.fViewPen = QPen(penColor, 1)
  82. def handleMouseEvent(self, event_x, event_y):
  83. x = float(event_x) - self.fRenderSource.x() - (self.fViewRect[iWidth] / self.fScale / 2)
  84. y = float(event_y) - self.fRenderSource.y() - (self.fViewRect[iHeight] / self.fScale / 2)
  85. maxWidth = self.fViewRect[iWidth] / self.fScale
  86. maxHeight = self.fViewRect[iHeight] / self.fScale
  87. if maxWidth > self.fFakeWidth:
  88. maxWidth = self.fFakeWidth
  89. if maxHeight > self.fFakeHeight:
  90. maxHeight = self.fFakeHeight
  91. if x < 0.0:
  92. x = 0.0
  93. elif x > self.fFakeWidth - maxWidth:
  94. x = self.fFakeWidth - maxWidth
  95. if y < 0.0:
  96. y = 0.0
  97. elif y > self.fFakeHeight - maxHeight:
  98. y = self.fFakeHeight - maxHeight
  99. self.fViewRect[iX] = x + self.fRenderSource.x()
  100. self.fViewRect[iY] = y + self.fRenderSource.y()
  101. self.update()
  102. self.emit(SIGNAL("miniCanvasMoved(double, double)"), x * self.fScale / self.fFakeWidth, y * self.fScale / self.fFakeHeight)
  103. def mousePressEvent(self, event):
  104. if event.button() == Qt.LeftButton:
  105. self.fMouseDown = True
  106. self.setCursor(QCursor(Qt.SizeAllCursor))
  107. self.handleMouseEvent(event.x(), event.y())
  108. event.accept()
  109. def mouseMoveEvent(self, event):
  110. if self.fMouseDown:
  111. self.handleMouseEvent(event.x(), event.y())
  112. event.accept()
  113. def mouseReleaseEvent(self, event):
  114. if self.fMouseDown:
  115. self.setCursor(QCursor(Qt.ArrowCursor))
  116. self.fMouseDown = False
  117. QFrame.mouseReleaseEvent(self, event)
  118. def paintEvent(self, event):
  119. painter = QPainter(self)
  120. painter.setBrush(QBrush(Qt.darkBlue, Qt.DiagCrossPattern))
  121. painter.drawRect(0, 0, self.width(), self.height())
  122. self.fScene.render(painter, self.fRenderSource, self.fRenderTarget, Qt.KeepAspectRatio)
  123. maxWidth = self.fViewRect[iWidth] / self.fScale
  124. maxHeight = self.fViewRect[iHeight] / self.fScale
  125. if maxWidth > self.fFakeWidth:
  126. maxWidth = self.fFakeWidth
  127. if maxHeight > self.fFakeHeight:
  128. maxHeight = self.fFakeHeight
  129. painter.setBrush(self.fViewBrush)
  130. painter.setPen(self.fViewPen)
  131. painter.drawRect(self.fViewRect[iX], self.fViewRect[iY], maxWidth, maxHeight)
  132. QFrame.paintEvent(self, event)
  133. def resizeEvent(self, event):
  134. self.fRenderSource = self.getRenderSource()
  135. if self.fRealParent:
  136. QTimer.singleShot(0, self.fRealParent, SLOT("slot_miniCanvasCheckAll()"))
  137. QFrame.resizeEvent(self, event)