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.

draggablegraphicsview.py 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Middle-click draggable QGraphicsView
  4. # Copyright (C) 2016-2019 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 PyQt5.QtCore import Qt
  20. from PyQt5.QtGui import QCursor, QMouseEvent
  21. from PyQt5.QtWidgets import QGraphicsView
  22. # ------------------------------------------------------------------------------------------------------------
  23. # Widget Class
  24. class DraggableGraphicsView(QGraphicsView):
  25. def __init__(self, parent):
  26. QGraphicsView.__init__(self, parent)
  27. self.fPanning = False
  28. self.fCtrlDown = False
  29. try:
  30. self.fMiddleButton = Qt.MiddleButton
  31. except:
  32. self.fMiddleButton = Qt.MidButton
  33. def mousePressEvent(self, event):
  34. if event.button() == self.fMiddleButton and not self.fCtrlDown:
  35. self.fPanning = True
  36. self.setDragMode(QGraphicsView.ScrollHandDrag)
  37. event = QMouseEvent(event.type(), event.pos(), Qt.LeftButton, Qt.LeftButton, event.modifiers())
  38. QGraphicsView.mousePressEvent(self, event)
  39. def mouseReleaseEvent(self, event):
  40. QGraphicsView.mouseReleaseEvent(self, event)
  41. if not self.fPanning:
  42. return
  43. self.fPanning = False
  44. self.setDragMode(QGraphicsView.NoDrag)
  45. self.setCursor(QCursor(Qt.ArrowCursor))
  46. def keyPressEvent(self, event):
  47. if event.key() == Qt.Key_Control:
  48. self.fCtrlDown = True
  49. QGraphicsView.keyPressEvent(self, event)
  50. def keyReleaseEvent(self, event):
  51. if event.key() == Qt.Key_Control:
  52. self.fCtrlDown = False
  53. QGraphicsView.keyReleaseEvent(self, event)