File: tool_bar.py

package info (click to toggle)
raysession 0.17.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,168 kB
  • sloc: python: 44,371; sh: 1,538; makefile: 208; xml: 86
file content (57 lines) | stat: -rw-r--r-- 1,859 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

from types import NoneType
from typing import Optional

from qtpy import QT5
from qtpy.QtWidgets import QToolBar, QLabel
from qtpy.QtGui import QMouseEvent
from qtpy.QtCore import Qt, QPoint, Signal, QSize # type:ignore

from ..surclassed_widgets import SpacerWidget
from .hiddens_indicator import HiddensIndicator
from .bar_widget_canvas import BarWidgetCanvas
from .bar_widget_jack import BarWidgetJack
from .bar_widget_transport import BarWidgetTransport


class PatchbayToolBar(QToolBar):
    menu_asked = Signal(QPoint)
    
    def __init__(self, parent):
        super().__init__(parent)
        self.setContextMenuPolicy(Qt.ContextMenuPolicy.PreventContextMenu)
        self._min_width: Optional[int] = None

    def set_min_width(self, width: int | None):
        self._min_width = width

    def mousePressEvent(self, event: QMouseEvent) -> None:
        child_widget = self.childAt(event.pos())
        super().mousePressEvent(event)

        if (event.button() != Qt.MouseButton.RightButton
                or not isinstance(
                    child_widget,
                    (QLabel, BarWidgetCanvas,
                     BarWidgetJack, BarWidgetTransport,
                     NoneType, SpacerWidget, HiddensIndicator))):
            return
        
        # execute the menu, exit if no action
        if QT5:
            point = event.screenPos().toPoint() # type:ignore
        else:
            point = QPoint(int(event.scenePosition().x()), 0)
        point.setY(self.mapToGlobal(QPoint(0, self.height())).y())

        self.menu_asked.emit(point)
    
    def sizeHint(self) -> QSize:
        if self._min_width is None:
            return super().sizeHint()
        size = super().sizeHint()
        size.setWidth(self._min_width)
        return size
    
    def needed_width(self) -> int:
        return super().sizeHint().width()