Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ dependencies = [
"prettytable>=3.7.0",
"python-dotenv>=1.0.1",
"pypdf>=6.0.0",
"PySide6>=6.8.0",
"PySide6_Addons>=6.8.0",
"PySide6_Essentials>=6.8.0",
"PySide6==6.9.3", # PySide 6.10.2 has a problem displaying scores (see issue #411)
"PySide6_Addons==6.9.3",
"PySide6_Essentials==6.9.3",
"sentry-sdk>=2.21.0",
"soundfile~=0.13.1",
"TiLiA",
Expand Down
Binary file added tests/resources/example.pdf
Binary file not shown.
10 changes: 5 additions & 5 deletions tests/timelines/pdf/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import pytest

from tests.mock import Serve
from tilia.requests import Get, post, Post
from tilia.requests import post, Post
from tilia.timelines.component_kinds import ComponentKind
from tilia.timelines.pdf.components import PdfMarker
from tilia.timelines.pdf.timeline import PdfTimeline
Expand Down Expand Up @@ -32,9 +31,10 @@ def pdf_tlui(pdf_tl, tluis):


@pytest.fixture
def pdf_tl(tls):
with Serve(Get.FROM_USER_RETRY_PDF_PATH, (False)):
tl: PdfTimeline = tls.create_timeline(TlKind.PDF_TIMELINE, path="")
def pdf_tl(tls, resources):
tl: PdfTimeline = tls.create_timeline(
TlKind.PDF_TIMELINE, path=(resources / "example.pdf").resolve().__str__()
)
tl.clear() # delete initial pdf marker
tl.create_pdf_marker = functools.partial(
tl.create_component, ComponentKind.PDF_MARKER
Expand Down
1 change: 1 addition & 0 deletions tests/timelines/pdf/test_pdf_timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def test_marker_at_same_time_fails(self, pdf_tlui):

class TestPageTotal:
def test_page_total_is_zero_with_invalid_pdf(self, pdf_tl):
pdf_tl.path = "invalid_path"
assert pdf_tl.get_data("page_total") == 0


Expand Down
36 changes: 36 additions & 0 deletions tests/ui/timelines/pdf/test_pdf_marker_ui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from typing import cast

from tilia.ui import commands
from tilia.ui.format import format_media_time
from tilia.ui.qtui import QtUI
from tilia.ui.windows import WindowKind
from tilia.ui.windows.inspect import Inspect


def get_inspect_window(qtui: QtUI) -> Inspect | None:
window = qtui._windows[WindowKind.INSPECT]
if not window:
return
return cast(Inspect, window)


def get_inspect_widget(qtui: QtUI, field_name: str) -> None:
window = get_inspect_window(qtui)
if not window:
raise ValueError("Inspect window not found.")

try:
return window.field_name_to_widgets[field_name][1]
except KeyError:
raise KeyError("Field name not found in inspect window.")


class TestInspect:
def test_inspect(self, pdf_tlui, qtui, resources):
commands.execute("timeline.pdf.add", time=10, page_number=5)
pdf_tlui.select_element(pdf_tlui[0])
commands.execute("timeline.element.inspect")
time = get_inspect_widget(qtui, "Time").text()
page_number = get_inspect_widget(qtui, "Page number").value()
assert time == format_media_time(10)
assert page_number == 5
41 changes: 41 additions & 0 deletions tests/ui/timelines/pdf/test_pdf_timeline_ui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from typing import Any

from tilia.ui import commands
from tilia.ui.timelines.pdf import PdfTimelineUI


def assert_pdf_marker(tlui: PdfTimelineUI, index: int, attr: str, value: Any) -> None:
assert tlui[index].get_data(attr) == value


class TestAddMarker:
def test_no_args(self, pdf_tlui, tilia_state):
tilia_state.current_time = 10
commands.execute("timeline.pdf.add")
tilia_state.current_time = 11
commands.execute("timeline.pdf.add")

assert len(pdf_tlui.timeline) == 2
assert_pdf_marker(pdf_tlui, 0, "time", 10)
assert_pdf_marker(pdf_tlui, 0, "page_number", 1)
assert_pdf_marker(pdf_tlui, 1, "time", 11)
assert_pdf_marker(pdf_tlui, 1, "page_number", 2)

def test_pass_time(self, pdf_tlui):
commands.execute("timeline.pdf.add", time=10)

assert len(pdf_tlui.timeline) == 1
assert_pdf_marker(pdf_tlui, 0, "time", 10)

def test_pass_page_number(self, pdf_tlui):
commands.execute("timeline.pdf.add", page_number=5)

assert len(pdf_tlui.timeline) == 1
assert_pdf_marker(pdf_tlui, 0, "page_number", 5)

def test_pass_time_and_page_number(self, pdf_tlui):
commands.execute("timeline.pdf.add", time=10, page_number=5)

assert len(pdf_tlui.timeline) == 1
assert_pdf_marker(pdf_tlui, 0, "time", 10)
assert_pdf_marker(pdf_tlui, 0, "page_number", 5)
14 changes: 10 additions & 4 deletions tilia/ui/timelines/pdf/timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,22 @@ def register_commands(cls, collection: TimelineUIs):
icon="pdf_add",
)

def on_add(self, time: float | None = None):
def on_add(self, time: float | None = None, page_number: int | None = None):
if time is None:
time = get(Get.SELECTED_TIME)

page_number = min(
self.timeline.get_previous_page_number(time) + 1, self.timeline.page_total
# If no page number is provided, use next available page number
requested_page_number = (
page_number
if page_number is not None
else self.timeline.get_previous_page_number(time) + 1
)

# Limit page number to total number of pages in PDF
page_number = min(requested_page_number, self.timeline.page_total)

pdf_marker, reason = self.timeline.create_component(
ComponentKind.PDF_MARKER, get(Get.SELECTED_TIME), page_number
ComponentKind.PDF_MARKER, time, page_number
)
if not pdf_marker:
tilia.errors.display(tilia.errors.ADD_PDF_MARKER_FAILED, reason)
Expand Down
12 changes: 6 additions & 6 deletions tilia/ui/windows/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,20 +268,20 @@ def on_text_edit_changed(self, field_name, text_edit):
id(self),
)

def on_spin_box_changed(self, field_name, spin_box):
def on_spin_box_changed(self, field_name: str, value: int):
post(
Post.INSPECTOR_FIELD_EDITED,
field_name,
spin_box.value(),
value,
self.element_id,
id(self),
)

def on_combo_box_changed(self, field_name, combo_box):
def on_combo_box_changed(self, field_name: str, value: int):
post(
Post.INSPECTOR_FIELD_EDITED,
field_name,
combo_box.currentData(),
value,
self.element_id,
id(self),
)
Expand Down Expand Up @@ -309,14 +309,14 @@ def _get_widget_for_row(self, kind: InspectRowKind, name: str, kwargs):
widget.setMinimum(kwargs["min"])
widget.setMaximum(kwargs["max"])
widget.valueChanged.connect(
functools.partial(self.on_spin_box_changed, name, widget)
functools.partial(self.on_spin_box_changed, name)
)
case InspectRowKind.COMBO_BOX:
widget = QComboBox()
for value, data in kwargs["items"]:
widget.addItem(str(value), data)
widget.currentIndexChanged.connect(
functools.partial(self.on_combo_box_changed, name, widget)
functools.partial(self.on_combo_box_changed, name)
)
case _:
widget = None
Expand Down