Source code for jdxi_editor.ui.editors.helpers.widgets

"""
Helpers for editors
"""

from typing import Any

from PySide6.QtGui import QPixmap
from PySide6.QtWidgets import (
    QButtonGroup,
    QComboBox,
    QHBoxLayout,
    QLabel,
    QPushButton,
    QSizePolicy,
    QSlider,
    QSpinBox,
    QWidget,
)

from jdxi_editor.ui.common import JDXi, QWidget
from jdxi_editor.ui.style import JDXiUIDimensions, JDXiUIStyle
from jdxi_editor.ui.widgets.label_click import make_label_clickable
from picoui.specs.widgets import ButtonSpec


[docs] def set_widget_value_safely(widget: Any, value: int) -> None: """ Block signals for the widget, set its value, then unblock signals. :param widget: The widget whose value is to be set. :param value: The value to set on the widget. """ widget.blockSignals(True) if isinstance(widget, QSlider): widget.setValue(value) elif isinstance(widget, QComboBox): widget.setCurrentIndex(value) elif isinstance(widget, QSpinBox): widget.setValue(value) # Add other widget types as needed widget.blockSignals(False)
[docs] def get_icon_pixmap(icon_name) -> QPixmap | None: """get icon pixmap for given name""" icon_pixmap = JDXi.UI.Icon.get_icon_pixmap( icon_name, color=JDXi.UI.Style.FOREGROUND, size=20 ) return icon_pixmap
[docs] def create_jdxi_button_with_label_from_spec( spec: ButtonSpec, checkable: bool = True ) -> tuple[QWidget, QPushButton]: """create jdxi button from spec""" button = create_jdxi_button_from_spec(spec, checkable=checkable) classify_tracks_icon_pixmap = get_icon_pixmap(icon_name=spec.icon) row, _ = create_jdxi_row(spec.label, icon_pixmap=classify_tracks_icon_pixmap) make_label_clickable(row, button) return row, button
[docs] def create_jdxi_button_from_spec( spec: ButtonSpec, button_group: QButtonGroup = None, checkable: bool = True ) -> QPushButton: """Create a round JD-Xi styled button with optional tooltip.""" button = create_jdxi_button() tooltip = getattr(spec, "tooltip", "") or "" if not isinstance(tooltip, str): tooltip = "" button.setToolTip(tooltip) button.setCheckable(checkable) if getattr(spec, "slot", None) is not None: button.clicked.connect(spec.slot) if getattr(spec, "grouped", False) and button_group is not None: button_group.addButton(button) return button
[docs] def _create_jdxi_button() -> QPushButton: from jdxi_editor.ui.style.factory import active_round_button_style button = QPushButton() button.setStyleSheet(active_round_button_style()) button.setFixedWidth(JDXiUIDimensions.BUTTON_ROUND.WIDTH) button.setFixedHeight(JDXiUIDimensions.BUTTON_ROUND.HEIGHT) return button
[docs] def create_small_sequencer_square_for_channel(ch: int) -> QPushButton: """create small sequencer square""" btn = QPushButton(f"{ch}") btn.setFixedWidth(JDXiUIDimensions.SEQUENCER.SQUARE_SIZE) btn.setFixedHeight(JDXiUIDimensions.SEQUENCER.SQUARE_SIZE) btn.setStyleSheet(JDXiUIStyle.BUTTON_SEQUENCER_SMALL) return btn
[docs] def create_jdxi_button(tooltip: str = "") -> QPushButton: """Create a round JD-Xi styled button with optional tooltip.""" from jdxi_editor.ui.style.factory import active_round_button_style button = QPushButton() button.setStyleSheet(active_round_button_style()) button.setFixedWidth(JDXiUIDimensions.BUTTON_ROUND.WIDTH) button.setFixedHeight(JDXiUIDimensions.BUTTON_ROUND.HEIGHT) button.setToolTip(tooltip) return button
[docs] def create_jdxi_row( label: str = "", icon_pixmap: QPixmap | None = None ) -> tuple[QWidget, QLabel]: """Create Row""" label_row = QWidget() label_layout = QHBoxLayout(label_row) label_layout.setContentsMargins(0, 0, 0, 0) label_layout.setSpacing(4) if icon_pixmap and not icon_pixmap.isNull(): icon_label = QLabel() icon_label.setPixmap(icon_pixmap) label_layout.addWidget(icon_label) label = QLabel(label) label.setStyleSheet(JDXi.UI.Style.STYLE_FOREGROUND) label_layout.addWidget(label) # A click on the label clicks the round button beside it, if there is one make_label_clickable(label_row) return label_row, label
[docs] def create_round_button_with_label( text: str, icon: str, slot: Any = None, tooltip: str = "", checkable: bool = False, ) -> tuple[QWidget, QPushButton]: """ A round JD-Xi button with its icon and label beside it, as one widget. Use in place of a plain text button so editor actions share the panel look (as Edit Tone Name, the transport, ...). :return: (widget to add to a layout, the button) """ spec = ButtonSpec(label=text, icon=icon, tooltip=tooltip, slot=slot) label_row, button = create_jdxi_button_with_label_from_spec(spec, checkable=checkable) label_row.setToolTip(tooltip) widget = QWidget() layout = QHBoxLayout(widget) layout.setContentsMargins(0, 0, 0, 0) layout.setSpacing(4) layout.addWidget(button) layout.addWidget(label_row) # Keep the label next to its button when the row has room to spare widget.setSizePolicy(QSizePolicy.Policy.Maximum, QSizePolicy.Policy.Fixed) return widget, button
[docs] def create_panel(*items: Any): """ One faceplate holding several widgets or layouts, stacked. The panel is a :class:`~jdxi_editor.ui.widgets.rack.panel.RackPanel`, so it wears the active skin, cheeks included. Group boxes inside it draw as flat sections with a title and a thin rule (see AppEditorStyle). """ from PySide6.QtWidgets import QLayout from jdxi_editor.ui.widgets.rack.panel import RackPanel panel = RackPanel() layout = panel.layout() layout.setSpacing(JDXi.UI.MidiPlayer.SPACING_RELATED) for item in items: if isinstance(item, QLayout): layout.addLayout(item) else: layout.addWidget(item) return panel