Skip to content

toolbar

Classes:

PipetteToolbar

PipetteToolbar(main: MainWindow)

Bases: AbstractToolbar

Methods:

Attributes:

Source code
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def __init__(self, main: MainWindow) -> None:
    import ctypes

    super().__init__(main, PipetteSettings(self))

    self.setup_ui()
    self.src_max_val: float = 2**8 - 1
    self.pos_fmt = self.src_hex_fmt = self.src_dec_fmt = self.src_norm_fmt = ''
    self.outputs = WeakKeyDictionary[VideoOutput, vs.VideoNode]()
    self.tracking = False
    self._curr_frame_cache = WeakKeyDictionary[VideoOutput, tuple[int, vs.VideoFrame]]()
    self._curr_alphaframe_cache = WeakKeyDictionary[VideoOutput, tuple[int, vs.VideoFrame]]()
    self._mouse_is_subscribed = False

    self.last_pos: tuple[VideoOutput, QPoint] | None = None

    self.set_qobject_names()

    self.data_types = {
        vs.INTEGER: {
            1: ctypes.c_uint8,
            2: ctypes.c_uint16,
            4: ctypes.c_uint32,
        },
        vs.FLOAT: {
            2: ctypes.c_char,
            4: ctypes.c_float,
        }
    }

class_storable_attrs class-attribute instance-attribute

class_storable_attrs = tuple[str, ...](('settings', 'visibility'))

current_source_alpha_frame property

current_source_alpha_frame: VideoFrame

current_source_frame property

current_source_frame: VideoFrame

data_types instance-attribute

data_types = {
    INTEGER: {1: c_uint8, 2: c_uint16, 4: c_uint32},
    FLOAT: {2: c_char, 4: c_float},
}

hlayout instance-attribute

hlayout: HBoxLayout

is_notches_visible property

is_notches_visible: bool

labels class-attribute instance-attribute

labels = [
    "position",
    "rgb_label",
    "rgb_hex",
    "rgb_dec",
    "rgb_norm",
    "src_label",
    "src_hex",
    "src_dec",
    "src_norm",
]

last_pos instance-attribute

last_pos: tuple[VideoOutput, QPoint] | None = None

main instance-attribute

main: MainWindow = main

name instance-attribute

name: str = __name__[:-7]

notches_changed class-attribute instance-attribute

notches_changed = pyqtSignal(ExtendedWidget)

num_keys class-attribute instance-attribute

num_keys = [
    Key_1,
    Key_2,
    Key_3,
    Key_4,
    Key_5,
    Key_6,
    Key_7,
    Key_8,
    Key_9,
    Key_0,
]

outputs instance-attribute

outputs = WeakKeyDictionary[VideoOutput, VideoNode]()

pos_fmt instance-attribute

pos_fmt = ''

settings instance-attribute

settings: PipetteSettings

src_dec_fmt instance-attribute

src_dec_fmt = ''

src_hex_fmt instance-attribute

src_hex_fmt = ''

src_max_val instance-attribute

src_max_val: float = 2 ** 8 - 1

src_norm_fmt instance-attribute

src_norm_fmt = ''

storable_attrs class-attribute instance-attribute

storable_attrs = tuple[str, ...]()

toggle_button instance-attribute

toggle_button = PushButton(name, self, checkable=True, clicked=on_toggle)

tracking instance-attribute

tracking = False

visibility instance-attribute

visibility = False

vlayout instance-attribute

vlayout: VBoxLayout

extract_value

extract_value(
    vs_frame: VideoFrame, pos: QPoint
) -> Generator[float, None, None]
Source code
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
def extract_value(self, vs_frame: vs.VideoFrame, pos: QPoint) -> Generator[float, None, None]:
    from ctypes import POINTER
    from ctypes import cast as ccast
    from struct import unpack

    fmt = vs_frame.format

    for plane in range(fmt.num_planes):
        stride = vs_frame.get_stride(plane)
        pointer = ccast(vs_frame.get_read_ptr(plane), POINTER(
            self.data_types[fmt.sample_type][fmt.bytes_per_sample] * (stride * vs_frame.height)  # type: ignore
        ))

        if fmt.sample_type == vs.FLOAT and fmt.bytes_per_sample == 2:
            offset = pos.y() * stride + pos.x() * fmt.bytes_per_sample
            yield cast(float, unpack('e', cast(bytearray, pointer.contents[
                slice(offset, offset + fmt.bytes_per_sample)
            ]))[0])
        else:
            yield cast(int, pointer.contents[pos.y() * (stride // fmt.bytes_per_sample) + pos.x()])

get_notches

get_notches() -> Notches
Source code
405
406
407
def get_notches(self) -> Notches:
    from .custom import Notches
    return Notches()

get_separator

get_separator(horizontal: bool = False) -> QFrame
Source code
318
319
320
321
322
def get_separator(self, horizontal: bool = False) -> QFrame:
    separator = QFrame(self)
    separator.setFrameShape(QFrame.Shape.HLine if horizontal else QFrame.Shape.VLine)
    separator.setFrameShadow(QFrame.Shadow.Sunken)
    return separator

init_notches

init_notches(main: MainWindow = ...) -> None
Source code
402
403
def init_notches(self, main: MainWindow = ...) -> None:
    self.notches_changed.connect(main.timeline.update_notches)

mouse_moved

mouse_moved(event: QMouseEvent) -> None
Source code
138
139
140
def mouse_moved(self, event: QMouseEvent) -> None:
    if self.tracking and not event.buttons():
        self.update_labels(event.pos())

mouse_pressed

mouse_pressed(event: QMouseEvent) -> None
Source code
142
143
144
145
146
147
def mouse_pressed(self, event: QMouseEvent) -> None:
    if event.buttons() == Qt.MouseButton.RightButton:
        self.tracking = not self.tracking

    if self.tracking:
        self.update_labels(event.pos())

mouse_released

mouse_released(event: QMouseEvent) -> None
Source code
149
150
def mouse_released(self, event: QMouseEvent) -> None:
    pass

on_copy_position_clicked

on_copy_position_clicked(checked: bool | None = None) -> None
Source code
262
263
264
def on_copy_position_clicked(self, checked: bool | None = None) -> None:
    self.main.clipboard.setText(self.position.text().strip())
    self.main.show_message('Coordinates successfully copied to clipboard')

on_current_frame_changed

on_current_frame_changed(frame: Frame) -> None
Source code
120
121
122
def on_current_frame_changed(self, frame: Frame) -> None:
    if self.last_pos and self.last_pos[0] is self.main.current_output:
        self.update_labels(self.last_pos[1])

on_current_output_changed

on_current_output_changed(index: int, prev_index: int) -> None
Source code
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
def on_current_output_changed(self, index: int, prev_index: int) -> None:
    super().on_current_output_changed(index, prev_index)

    if self.main.current_output not in self.outputs:
        self.outputs[self.main.current_output] = self.prepare_vs_output(self.main.current_output.source.clip)

    assert (src_fmt := self.outputs[self.main.current_output].format)

    has_alpha = bool(self.main.current_output.source.alpha)

    self.src_label.setText(f"Raw ({src_fmt.color_family.name}{' + Alpha' if has_alpha else ''}):")
    self.src_hex.setVisible(src_fmt.sample_type == vs.INTEGER)

    if src_fmt.sample_type == vs.INTEGER:
        self.src_max_val = 2**src_fmt.bits_per_sample - 1
    elif src_fmt.sample_type == vs.FLOAT:
        self.src_max_val = 1.0

    src_num_planes = src_fmt.num_planes + int(has_alpha)

    self.src_hex_fmt = ('{{:{w}X}},' * src_num_planes)[:-1].format(w=ceil(log(self.src_max_val, 16)))
    if src_fmt.sample_type == vs.INTEGER:
        self.src_dec_fmt = ('{{:{w}d}},' * src_num_planes)[:-1].format(w=ceil(log(self.src_max_val, 10)))
    elif src_fmt.sample_type == vs.FLOAT:
        self.src_dec_fmt = ('{: 0.5f},' * src_num_planes)[:-1]
    self.src_norm_fmt = ('{:0.5f},' * src_num_planes)[:-1]

    self.update_labels(self.main.graphics_view.mapFromGlobal(self.main.cursor().pos()))

on_toggle

on_toggle(new_state: bool) -> None
Source code
266
267
268
269
270
271
272
273
274
275
276
def on_toggle(self, new_state: bool) -> None:
    super().on_toggle(new_state)
    self.tracking = new_state
    self.main.graphics_view.setMouseTracking(new_state)

    if new_state:
        self.subscribe_on_mouse_events()
        self.main.graphics_view.setDragMode(QGraphicsView.DragMode.NoDrag)
    else:
        self.unsubscribe_from_mouse_events()
        self.main.graphics_view.setDragMode(QGraphicsView.DragMode.ScrollHandDrag)

prepare_vs_output staticmethod

prepare_vs_output(vs_output: VideoNode) -> VideoNode
Source code
278
279
280
281
282
283
284
285
286
287
288
289
@staticmethod
def prepare_vs_output(vs_output: vs.VideoNode) -> vs.VideoNode:
    assert (fmt := vs_output.format)

    if fmt.subsampling_w == fmt.subsampling_h == 0:
        return vs_output

    return vs.core.resize.Bicubic(
        vs_output, format=vs.core.query_video_format(
            fmt.color_family, fmt.sample_type, fmt.bits_per_sample, 0, 0
        ).id, dither_type='none'
    )

resize_main_window

resize_main_window(expanding: bool) -> None
Source code
481
482
483
484
485
486
487
488
489
def resize_main_window(self, expanding: bool) -> None:
    if self.main.windowState() in {Qt.WindowState.WindowMaximized, Qt.WindowState.WindowFullScreen}:
        return

    if expanding:
        self.main.resize(self.main.width(), self.main.height() + self.height() + round(6 * self.main.display_scale))
    if not expanding:
        self.main.resize(self.main.width(), self.main.height() - self.height() - round(6 * self.main.display_scale))
        self.main.timeline.update()

set_qobject_names

set_qobject_names() -> None
Source code
279
280
281
282
283
284
285
286
287
288
289
290
291
292
def set_qobject_names(self) -> None:
    if not hasattr(self, '__slots__'):
        return

    slots = list(self.__slots__)

    if isinstance(self, AbstractToolbar) and 'main' in slots:
        slots.remove('main')

    for attr_name in slots:
        attr = getattr(self, attr_name)
        if not isinstance(attr, QObject):
            continue
        attr.setObjectName(type(self).__name__ + '.' + attr_name)

setup_ui

setup_ui() -> None
Source code
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
def setup_ui(self) -> None:
    super().setup_ui()

    self.color_view = ColorView(self)
    self.color_view.setFixedSize(self.height() // 2, self.height() // 2)

    font = QFont('Consolas', 9)
    font.setStyleHint(QFont.StyleHint.Monospace)

    self.position = QLabel(self)

    self.rgb_label = QLabel('Rendered (RGB):', self)

    self.rgb_hex = QLabel(self)
    self.rgb_dec = QLabel(self)
    self.rgb_norm = QLabel(self)

    self.src_label = QLabel(self)

    self.src_hex = QLabel(self)
    self.src_dec = QLabel(self)
    self.src_norm = QLabel(self)

    self.rgb_hls = QLabel(self)
    self.rgb_hsv = QLabel(self)

    for label in [
        self.position,
        self.rgb_hex, self.rgb_dec, self.rgb_norm,
        self.src_hex, self.src_dec, self.src_norm
    ]:
        label.setFont(font)
        label.setTextInteractionFlags(Qt.TextInteractionFlag.TextSelectableByMouse)

    self.copy_position_button = PushButton('⎘', self, clicked=self.on_copy_position_clicked)

    self.hlayout.addWidgets([
        QFrame(),
        self.color_view, self.position, self.copy_position_button,
        self.get_separator(),
        self.rgb_label, self.rgb_hex, self.rgb_dec, self.rgb_norm,
        self.get_separator(),
        self.src_label, self.src_hex, self.src_dec, self.src_norm,
        self.get_separator(),
        QLabel('HLS:'), self.rgb_hls, QLabel('HSV:'), self.rgb_hsv,
    ])

    self.hlayout.addStretch()

subscribe_on_mouse_events

subscribe_on_mouse_events() -> None
Source code
124
125
126
127
128
129
def subscribe_on_mouse_events(self) -> None:
    if not self._mouse_is_subscribed:
        self.main.graphics_view.mouseMoved.connect(self.mouse_moved)
        self.main.graphics_view.mousePressed.connect(self.mouse_pressed)
        self.main.graphics_view.mouseReleased.connect(self.mouse_released)
    self._mouse_is_subscribed = True

unsubscribe_from_mouse_events

unsubscribe_from_mouse_events() -> None
Source code
131
132
133
134
135
136
def unsubscribe_from_mouse_events(self) -> None:
    if self._mouse_is_subscribed:
        self.main.graphics_view.mouseMoved.disconnect(self.mouse_moved)
        self.main.graphics_view.mousePressed.disconnect(self.mouse_pressed)
        self.main.graphics_view.mouseReleased.disconnect(self.mouse_released)
    self._mouse_is_subscribed = False

update_labels

update_labels(local_pos: QPoint) -> None
Source code
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
def update_labels(self, local_pos: QPoint) -> None:
    self.last_pos = (self.main.current_output, local_pos)

    pos_f = self.main.graphics_view.mapToScene(local_pos)

    if not self.main.current_scene.contains(pos_f):
        return

    pos = QPoint(floor(pos_f.x()), floor(pos_f.y()))
    color = self.main.current_scene.pixmap().toImage().pixelColor(pos)
    components = color.red(), color.green(), color.blue()
    components_float = tuple[float, ...](x / 255 for x in components)

    self.color_view.color = color
    self.position.setText('{:4d},{:4d}'.format(pos.x(), pos.y()))

    self.rgb_hex.setText('{:2X},{:2X},{:2X}'.format(*components))
    self.rgb_dec.setText('{:3d},{:3d},{:3d}'.format(*components))
    self.rgb_norm.setText('{:0.5f},{:0.5f},{:0.5f}'.format(*components_float))
    self.rgb_hls.setText('{}%,{}%,{}%'.format(*(int(x * 255) for x in rgb_to_hls(*components_float))))
    self.rgb_hsv.setText('{}%,{}%,{}%'.format(*(int(x * 255) for x in rgb_to_hsv(*components_float))))

    if not self.src_label.isVisible():
        return

    fmt = self.current_source_frame.format

    src_vals = list(self.extract_value(self.current_source_frame, pos))
    if self.main.current_output.source.alpha:
        src_vals.append(next(self.extract_value(self.current_source_alpha_frame, pos)))

    self.src_dec.setText(self.src_dec_fmt.format(*src_vals))
    if fmt.sample_type == vs.INTEGER:
        self.src_hex.setText(self.src_hex_fmt.format(*src_vals))
        self.src_norm.setText(self.src_norm_fmt.format(*[
            src_val / self.src_max_val for src_val in src_vals
        ]))
    elif fmt.sample_type == vs.FLOAT:
        self.src_norm.setText(self.src_norm_fmt.format(*[
            max(0.0, min(val, 1.0)) if i in {0, 3} else max(-.5, min(val, .5)) + .5
            for i, val in enumerate(src_vals)
        ]))