Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-photo

Source of release 0.1.1

0.1.1 / src / properties.luc

## The Properties pane: one column about the selected photo. In Library mode
## what the camera recorded and its marks (stars, flag, label: on the whole
## selection); in the other modes the mode's develop controls
## (develop_controls.luc) on the lead photo. The histogram and the photo's name
## head both.
from luce_ui.ui import VStack, HStack, Text, Button, Font, Panel, Size, ScrollView, Spacer, Rule, Command, Widget, Histogram
from interop import Connection
from photo_library import PhotoLibrary
from actions import Actions
from develop_controls import DevelopControls
import modes
import presets
import preset_marks

pub class PropertiesPane:
    pub let panel: Panel
    pub let histogram: Histogram
    pub let develop: DevelopControls
    let library: PhotoLibrary
    let title: Text
    let camera: Text
    let exposure: Text
    let captured: Text
    let file: Text
    let marks: Text
    ## The photo's preset, over its exposed parameters in Library mode.
    let preset: Text
    ## Stands in for the develop controls while no photo is selected.
    let unselected: Text
    ## The ratings, flags and labels, shown with a photo to give them to.
    let marking: VStack
    ## The camera's facts, shown in Library mode.
    let facts: VStack
    ## The mode shown (modes.luc).
    var mode: int = 0
    var selected_photo: bool = false
    var connections: list[Connection] = []

    pub func init(self, photos: PhotoLibrary, actions: Actions, font: Font) -> unit!:
        self.library = photos
        self.title = Text("No photo selected", font = font)
        self.camera = Text("", font = font, muted = true)
        self.exposure = Text("", font = font, muted = true)
        self.captured = Text("", font = font, muted = true)
        self.file = Text("", font = font, muted = true)
        self.marks = Text("", font = font)
        self.preset = Text("", font = font, muted = true)
        self.develop = DevelopControls(font)
        self.histogram = Histogram()
        var stars: list[Widget] = []
        for command in actions.ratings:
            stars.append(self.command_button(command, "0" if stars.length == 0 else f"{stars.length}★", font))
        let flags = HStack([self.icon_button(actions.pick, "flag", font), self.icon_button(actions.reject, "close", font),
                            self.command_button(actions.unflag, "Unflag", font)], spacing = 4.0)
        var labels: list[Widget] = []
        for (at, command) in actions.labels.indexed():
            labels.append(self.command_button(command, ["Red", "Yellow", "Green", "Blue", "None"][at], font))
        self.unselected = Text("Select a photo to develop it.", font = font, muted = true)
        # What the photo is, right under its name: camera and lens, exposure, when, file.
        self.marking = VStack([Rule(), Text("MARKS", font = font, muted = true), self.marks, HStack(stars, spacing = 4.0), flags,
                               HStack(labels, spacing = 4.0)], spacing = 6.0)
        self.facts = VStack([self.camera, self.exposure, self.captured, self.file, Rule()], spacing = 6.0)
        let rows = VStack([self.histogram, self.title, self.facts, self.unselected, self.preset, self.develop.body, self.marking, Spacer()], spacing = 6.0, padding = 6.0)
        self.panel = Panel("Properties", ScrollView(rows), minimum = Size(260.0, 200.0), closable = false, id = "properties")
        self.show(-1, 0)

    func command_button(self, command: Command, text: str, font: Font) -> Button!:
        let button = Button(text, font = font)
        self.connections.append(button.on_click(func () -> unit!:
            command.trigger()))
        return button

    func icon_button(self, command: Command, icon: str, font: Font) -> Button!:
        let button = Button(icon = icon, font = font)
        self.connections.append(button.on_click(func () -> unit!:
            command.trigger()))
        return button

    ## Show the photo at `at` of the library's index (-1 for none), and how many
    ## photos are selected.
    pub func show(self, at: int, selected: int) -> unit!:
        let chosen = at >= 0
        self.selected_photo = chosen
        self.show_parts()
        if chosen:
            self.show_preset(at)
        if not chosen:
            self.title.set_text("No photo selected")
            self.marks.set_text("")
            return
        let index = self.library.index
        self.title.set_text(index.name(at) if selected <= 1 else f"{index.name(at)} and {selected - 1} more")
        self.captured.set_text(index.captured(at).replace("T", "  "))
        let camera = camera_name(index.make(at), index.model(at))
        let lens = index.lens(at)
        self.camera.set_text(f"{camera}  ·  {lens}" if camera != "" and lens != "" else (camera if camera != "" else lens))
        var parts: list[str] = []
        if index.shutter(at) != "":
            parts.append(index.shutter(at))
        if index.aperture(at) > 0.0:
            parts.append(f"f/{decimal(index.aperture(at))}")
        if index.iso(at) > 0:
            parts.append(f"ISO {index.iso(at)}")
        if index.focal(at) > 0.0:
            parts.append(f"{decimal(index.focal(at))} mm")
        self.exposure.set_text(parts.join("  "))
        self.file.set_text(f"{index.kind(at)}  {index.width(at)}×{index.height(at)}  {megabytes(index.bytes(at))} MB")
        let flag = ["Rejected", "", "Picked"][index.flag(at) + 1]
        let stars = "★".repeat(index.rating(at)) if index.rating(at) > 0 else "No rating"
        self.marks.set_text(f"{stars}  {flag}".trim())

    ## The photo at `at`'s preset: its name, and the parameters the Library
    ## mode shows (Exposure and White Balance without one).
    func show_preset(self, at: int) -> unit!:
        let saved = self.library.presets
        let mark = self.library.index.preset(at)
        let found = saved.position_of(preset_marks.saved_of(mark))
        let name = preset_marks.name_of(saved, mark)
        self.preset.set_text(f"PRESET · {name.upper()}" if name != "" else "NO PRESET")
        let exposed = saved.exposed(found) if found >= 0 else presets.default_exposed
        self.develop.exposed = exposed.split("|")
        if self.mode == modes.library:
            self.develop.show_mode(modes.library)

    ## Show mode `mode`'s parts: the facts and marks in Library, the mode's
    ## controls in the others.
    pub func set_mode(self, mode: int) -> unit!:
        self.mode = mode
        self.develop.show_mode(mode)
        self.show_parts()

    func show_parts(self) -> unit!:
        let chosen = self.selected_photo
        let library = self.mode == modes.library
        self.unselected.layout().set_visible(not chosen)
        self.develop.body.layout().set_visible(chosen)
        self.preset.layout().set_visible(chosen and library)
        self.marking.layout().set_visible(chosen and library)
        self.facts.layout().set_visible(chosen and library)
        # The histogram measures the live develop, which the Library mode does not run.
        self.histogram.layout().set_visible(chosen and not library)

    pub func close(self):
        self.develop.close()
        for connection in self.connections:
            connection.disconnect()
        self.connections.clear()

## The camera as people name it: the maker's first word once, then the model.
## "NIKON CORPORATION" + "NIKON D800" is "NIKON D800"; "FUJIFILM" + "X-T2" is
## "FUJIFILM X-T2"; "Canon" + "Canon EOS R5" is "Canon EOS R5".
pub func camera_name(make: str, model: str) -> str:
    let maker = make.trim().split(" ")[0]
    if maker == "":
        return model.trim()
    if model.lower().starts_with(maker.lower()):
        return model.trim()
    return f"{maker} {model.trim()}".trim()

## `value` with one decimal, or none when it is whole: 2.8, 35.
func decimal(value: float) -> str:
    let tenths = int(value * 10.0 + 0.5)
    return f"{tenths // 10}" if tenths % 10 == 0 else f"{tenths // 10}.{tenths % 10}"

func megabytes(count: int) -> str:
    return decimal(float(count) / 1048576.0)

test "cameras are named as people name them":
    assert(camera_name("NIKON CORPORATION", "NIKON D800") == "NIKON D800")
    assert(camera_name("FUJIFILM", "X-T2") == "FUJIFILM X-T2")
    assert(camera_name("Canon", "Canon EOS R5") == "Canon EOS R5")
    assert(camera_name("OLYMPUS IMAGING CORP.", "E-M10") == "OLYMPUS E-M10")
    assert(camera_name("", "iPhone 8") == "iPhone 8")