Moved the tape editor widgets to another file

master
Jaka Perovšek 2023-12-15 23:39:35 +01:00
parent 1fce5f2d69
commit 6981f710be
2 changed files with 328 additions and 284 deletions

View File

@ -1,25 +1,12 @@
import sys import sys
import os import os
from PySide6.QtCore import QSize, Qt, Slot, QRect, QFileSystemWatcher from tape_editor_widgets import *
from PySide6.QtGui import QIcon, QAction, QShortcut, QKeySequence, QPainter, QColor, QTextFormat, QPixmap
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QWidget, QLabel, QVBoxLayout, QHBoxLayout, \
QGroupBox, QLineEdit, QCheckBox, QSpinBox, QPlainTextEdit, QSizePolicy, QGridLayout, QTextEdit, QScrollArea, QFrame, \
QDoubleSpinBox
from widgets import *
from pathlib import Path from pathlib import Path
import yaml import yaml
def change_font_size(label: QLabel | QLineEdit, factor: float) -> QLabel:
f = label.font()
f.setPointSizeF(f.pointSizeF() * factor)
label.setFont(f)
return label
class OptionsWidget(QWidget): class OptionsWidget(QWidget):
def __init__(self, parent: QWidget): def __init__(self, parent: QWidget):
super().__init__() super().__init__()
@ -141,6 +128,7 @@ class EditorWidget(QWidget):
self.transfer_to_validator_pushbutton = QPushButton() self.transfer_to_validator_pushbutton = QPushButton()
self.transfer_to_validator_pushbutton.setIcon(QIcon("./icons/ic_keyboard_double_arrow_right_24px.svg")) self.transfer_to_validator_pushbutton.setIcon(QIcon("./icons/ic_keyboard_double_arrow_right_24px.svg"))
self.transfer_to_validator_pushbutton.setToolTip("Apply the code.") self.transfer_to_validator_pushbutton.setToolTip("Apply the code.")
self.transfer_to_validator_pushbutton.clicked.connect(self.apply_code)
self.status_same = f"<b>Status:</b> yaml setup matches {self.filename_string} file" self.status_same = f"<b>Status:</b> yaml setup matches {self.filename_string} file"
self.status_external_file_changed = f"<b>Status:</b> {self.filename_string} file was edited externally" self.status_external_file_changed = f"<b>Status:</b> {self.filename_string} file was edited externally"
@ -209,253 +197,9 @@ class EditorWidget(QWidget):
if not self.check_if_setups_match(): if not self.check_if_setups_match():
self.status_label.setText(self.status_editor_code_changed) self.status_label.setText(self.status_editor_code_changed)
def apply_code(self):
class ProcessEditWidget(QWidget): print("applying code")
def __init__(self, tape: dict): pass
super().__init__()
self.tape = tape
self.cbox_clip_start = QCheckBox("Clip at start")
self.cbox_clip_end = QCheckBox("Clip at end")
self.cbox_volume_factor = QCheckBox("Volume factor")
self.clip_start = TimeEditWithoutWheel()
self.clip_start.setDisplayFormat("hh:mm:ss.z")
self.clip_end = TimeEditWithoutWheel()
self.clip_end.setDisplayFormat("hh:mm:ss.z")
self.volume_factor = SpinBoxWithoutWheel()
self.volume_factor.setSuffix(" dB")
self.volume_factor.setMinimum(-30)
self.volume_factor.setMaximum(30)
layout = QGridLayout(self)
layout.setContentsMargins(5, 0, 5, 0)
layout.setColumnStretch(3, 1)
layout.addWidget(self.cbox_clip_start, 0, 0)
layout.addWidget(self.cbox_clip_end, 1, 0)
layout.addWidget(self.cbox_volume_factor, 2, 0)
layout.addWidget(self.clip_start, 0, 1)
layout.addWidget(self.clip_end, 1, 1)
layout.addWidget(self.volume_factor, 2, 1)
self.load()
self.clip_start.timeChanged.connect(self.save)
def load(self):
clip_start, clip_end = self.tape["process"]["start"], self.tape["process"]["end"]
start, end = QTime(), QTime()
start_time = datetime.datetime.strptime(clip_start, "%H:%M:%S.%f")
end_time = datetime.datetime.strptime(clip_end, "%H:%M:%S.%f")
start.setHMS(start_time.hour, start_time.minute, start_time.second, ms=start_time.microsecond * 1e-3)
end.setHMS(end_time.hour, end_time.minute, end_time.second, ms=end_time.microsecond * 1e-3)
self.clip_start.setTime(start)
self.clip_end.setTime(end)
self.cbox_clip_start.setChecked(clip_start != "00:00:00.0")
self.cbox_clip_end.setChecked(clip_end != "00:00:00.0")
volume_factor = self.tape["process"]["volume"]
self.volume_factor.setValue(volume_factor)
self.cbox_volume_factor.setChecked(volume_factor != 0)
def save(self):
start = self.clip_start.time()
end = self.clip_end.time()
self.tape["process"]["start"] = f"{start.hour():02}:{start.minute():02}:{start.second():02}.{start.msec():03}"
self.tape["process"]["end"] = f"{end.hour():02}:{end.minute():02}:{end.second():02}.{end.msec():03}"
self.tape["process"]["volume"] = self.volume_factor.value()
class EconomyEditWidget(QWidget):
def __init__(self, tape: dict):
super().__init__()
self.tape = tape
self.spinbox_price = SpinBoxWithoutWheel()
self.spinbox_price.setRange(10, 100000)
self.spinbox_price.setSuffix(" mk")
self.spinbox_price.setSizePolicy(QSizePolicy.Policy.Maximum, QSizePolicy.Policy.Maximum)
layout = QGridLayout(self)
layout.setContentsMargins(5, 0, 5, 0)
layout.addWidget(self.spinbox_price, 1, 0)
layout.setColumnStretch(4, 1)
for i, text in enumerate(["Price", "Sold in", "Factor", " Local price"]):
layout.addWidget(change_font_size(QLabel(text), 0.8), 0, i)
self.economy = {"outpost": {},
"city": {},
"research": {},
"military": {},
"mine": {}}
for i, (location, editors) in enumerate(self.economy.items()):
editors["cbox"] = QCheckBox(location.capitalize())
editors["float"] = DoubleSpinBoxWithoutWheel()
editors["float"].setSingleStep(0.05)
editors["label"] = QLabel()
editors["label"].setFont("consolas")
layout.addWidget(editors["cbox"], int(i) + 1, 1)
layout.addWidget(editors["float"], int(i) + 1, 2)
layout.addWidget(editors["label"], int(i) + 1, 3)
self.load()
self.update_labels()
self.spinbox_price.valueChanged.connect(self.save)
for editors in self.economy.values():
editors["cbox"].toggled.connect(self.save)
editors["float"].valueChanged.connect(self.save)
def load(self):
self.spinbox_price.setValue(self.tape["price"])
for locale in self.tape["economy"]:
self.economy[locale["location"]]["cbox"].setChecked(locale["sold"])
self.economy[locale["location"]]["float"].setValue(locale["factor"])
def save(self):
self.tape["price"] = self.spinbox_price.value()
self.tape["economy"] = [{"location": location,
"factor": editors["float"].value(),
"sold": editors["cbox"].isChecked()} for location, editors in self.economy.items()]
self.update_labels()
def update_labels(self):
for editors in self.economy.values():
editors["label"].setText(f" {self.tape['price'] * editors['float'].value():4.0f} mk")
class SpawnEditWidget(QWidget):
def __init__(self, tape: dict):
super().__init__()
self.tape = tape
layout = QGridLayout(self)
layout.setContentsMargins(5, 0, 5, 0)
layout.setColumnStretch(2, 1)
for i, text in enumerate(["Place", "Probability"]):
layout.addWidget(change_font_size(QLabel(text), 0.8), 0, i)
self.spawn = {"outpostcrewcabinet": {"label": QLabel("Outpost Crew Cabinet"),
"float": ProbabilitySpinBox()},
"wreckstorage": {"label": QLabel("Wreck Storage"),
"float": ProbabilitySpinBox()},
"abandonedcrewcab": {"label": QLabel("Abandoned Crew Cabinet"),
"float": ProbabilitySpinBox()},
"abandonedstoragecab": {"label": QLabel("Abandoned Storage Cabinet"),
"float": ProbabilitySpinBox()}}
for i, (location, editors) in enumerate(self.spawn.items()):
layout.addWidget(editors["label"], int(i) + 1, 0)
layout.addWidget(editors["float"], int(i) + 1, 1)
self.load()
for i, (location, editors) in enumerate(self.spawn.items()):
editors["float"].valueChanged.connect(self.save)
def load(self):
for locale in self.tape["spawn"]:
self.spawn[locale["location"]]["float"].setValue(locale["probability"])
def save(self):
self.tape["spawn"] = [{"location": location,
"probability": editors["float"].value()} for location, editors in self.spawn.items() if
editors["float"].value() > 0]
# TODO: afflictions with shorter range for walkman songs.
class CraftingEditWidget(QWidget):
def __init__(self, tape: dict):
super().__init__()
self.tape = tape
self.int_uses = SpinBoxWithoutWheel()
self.int_uses.setMinimum(1)
self.int_uses.setMaximum(1000)
self.description = QPlainTextEdit("One use is duration of the tape")
self.description.setEnabled(False)
self.description.setMaximumHeight(QFontMetrics(self.description.font()).height() * 1.7)
layout = QHBoxLayout(self)
layout.addWidget(QLabel("Number of uses:"))
layout.addWidget(self.int_uses)
layout.addWidget(self.description)
layout.addStretch()
layout.setContentsMargins(5, 0, 5, 0)
self.load()
self.int_uses.valueChanged.connect(self.save)
def load(self):
self.int_uses.setValue(self.tape["no_of_uses"])
def save(self):
self.tape["no_of_uses"] = self.int_uses.value()
class AfflictionsEditWidget(QWidget):
def __init__(self, tape: dict):
super().__init__()
self.tape = tape
self.description = QPlainTextEdit("Factor 1.0 means that the affliction will reach the "
"highest level after being exposed to the sound of the "
"tape for its play duration.")
self.description.setEnabled(False)
self.description.setMaximumWidth(212)
layout = QGridLayout(self)
layout.setContentsMargins(5, 0, 5, 0)
layout.addWidget(change_font_size(QLabel("Affliction"), 0.8), 0, 0)
layout.addWidget(change_font_size(QLabel("Factor"), 0.8), 0, 1)
layout.addWidget(self.description, 1, 2, 3, 1)
layout.setColumnStretch(3, 1)
self.afflictions = {"strengthen": {},
"haste": {},
"psychosis": {}}
for i, (affliction, editors) in enumerate(self.afflictions.items()):
editors["cbox"] = QCheckBox(affliction.capitalize())
editors["float"] = DoubleSpinBoxWithoutWheel()
editors["float"].setSingleStep(0.01)
layout.addWidget(editors["cbox"], int(i) + 1, 0)
layout.addWidget(editors["float"], int(i) + 1, 1)
self.load()
for editors in self.afflictions.values():
editors["cbox"].toggled.connect(self.save)
editors["float"].valueChanged.connect(self.save)
def load(self):
if self.tape["afflictions"]:
for affliction in self.tape["afflictions"]:
self.afflictions[affliction["name"]]["cbox"].setChecked(True)
self.afflictions[affliction["name"]]["float"].setValue(affliction["factor"])
def save(self):
self.tape["afflictions"] = [{"name": affliction, "factor": editors["float"].value()} for affliction, editors in
self.afflictions.items() if editors["cbox"].isChecked()]
class TapeEditWidget(QWidget): class TapeEditWidget(QWidget):
@ -467,30 +211,12 @@ class TapeEditWidget(QWidget):
self.label_nr = QLabel(f"#{number}") self.label_nr = QLabel(f"#{number}")
self.label_nr.setAlignment(Qt.AlignmentFlag.AlignCenter) self.label_nr.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.label_nr.setStyleSheet("background-color: #cfd8dc;") self.label_nr.setStyleSheet("background-color: #cfd8dc;")
self.edit_identifier = QLineEdit()
self.edit_identifier.setFont("Consolas")
self.edit_identifier.setStyleSheet("font-weight: bold; background-color: #cfd8dc;")
self.edit_identifier.setTextMargins(2, 0, 2, 0)
self.edit_identifier.setFrame(QFrame.Shape.NoFrame)
self.edit_identifier.setText(self.tape["identifier"])
change_font_size(self.edit_identifier, 1.41)
change_font_size(self.label_nr, 1.41) change_font_size(self.label_nr, 1.41)
pix = QPixmap(f"../source/images/{self.tape['identifier']}.png") self.identifier = IdentifierEditWidget(self.tape)
self.pixmap = pix.scaled(350, 350, self.cover = CoverEditWidget(self.tape)
Qt.AspectRatioMode.KeepAspectRatio, self.edit_name = NameEditWidget(self.tape)
Qt.TransformationMode.SmoothTransformation) self.edit_source = SourceEditWidget(self.tape)
self.cover = QLabel()
self.cover.resize(350, 0)
self.cover.setPixmap(self.pixmap)
self.edit_name = QLineEdit()
self.edit_name.setText(self.tape["name"])
source = self.tape["source"]
self.edit_source = UrlWidget(source)
self.process = ProcessEditWidget(self.tape) self.process = ProcessEditWidget(self.tape)
self.economy = EconomyEditWidget(self.tape) self.economy = EconomyEditWidget(self.tape)
self.spawn = SpawnEditWidget(self.tape) self.spawn = SpawnEditWidget(self.tape)
@ -500,7 +226,7 @@ class TapeEditWidget(QWidget):
layout = QGridLayout(self) layout = QGridLayout(self)
layout.setContentsMargins(0, 0, 0, 5) layout.setContentsMargins(0, 0, 0, 5)
for i, (text, widget) in enumerate([(f"", self.edit_identifier), for i, (text, widget) in enumerate([(f"", self.identifier),
("Cover", self.cover), ("Cover", self.cover),
("Name", self.edit_name), ("Name", self.edit_name),
("Source", self.edit_source), ("Source", self.edit_source),

View File

@ -0,0 +1,318 @@
from widgets import *
def change_font_size(label: QLabel | QLineEdit, factor: float) -> QLabel:
f = label.font()
f.setPointSizeF(f.pointSizeF() * factor)
label.setFont(f)
return label
class IdentifierEditWidget(QLineEdit):
def __init__(self, tape: dict):
super().__init__()
self.tape = tape
self.setFont("Consolas")
self.setStyleSheet("font-weight: bold; background-color: #cfd8dc;")
self.setTextMargins(2, 0, 2, 0)
self.setFrame(QFrame.Shape.NoFrame)
self.setText(self.tape["identifier"])
change_font_size(self, 1.41)
self.load()
def load(self):
self.setText(self.tape["identifier"])
def save(self):
self.tape["identifier"] = self.text()
class CoverEditWidget(QLabel):
def __init__(self, tape: dict):
super().__init__()
self.tape = tape
pix = QPixmap(f"../source/images/{self.tape['identifier']}.png")
self.pixmap = pix.scaled(350, 350,
Qt.AspectRatioMode.KeepAspectRatio,
Qt.TransformationMode.SmoothTransformation)
self.resize(350, 0)
self.setPixmap(self.pixmap)
class NameEditWidget(QLineEdit):
def __init__(self, tape: dict):
super().__init__()
self.tape = tape
self.load()
def load(self):
self.setText(self.tape["name"])
def save(self):
self.tape["name"] = self.text()
class SourceEditWidget(UrlWidget):
def __init__(self, tape: dict):
super().__init__()
self.tape = tape
self.load()
def load(self):
self.url_lineedit.setText(self.tape["source"])
def save(self):
self.tape["source"] = self.url_lineedit.text()
class ProcessEditWidget(QWidget):
def __init__(self, tape: dict):
super().__init__()
self.tape = tape
self.cbox_clip_start = QCheckBox("Clip at start")
self.cbox_clip_end = QCheckBox("Clip at end")
self.cbox_volume_factor = QCheckBox("Volume factor")
self.clip_start = TimeEditWithoutWheel()
self.clip_start.setDisplayFormat("hh:mm:ss.z")
self.clip_end = TimeEditWithoutWheel()
self.clip_end.setDisplayFormat("hh:mm:ss.z")
self.volume_factor = SpinBoxWithoutWheel()
self.volume_factor.setSuffix(" dB")
self.volume_factor.setMinimum(-30)
self.volume_factor.setMaximum(30)
layout = QGridLayout(self)
layout.setContentsMargins(5, 0, 5, 0)
layout.setColumnStretch(3, 1)
layout.addWidget(self.cbox_clip_start, 0, 0)
layout.addWidget(self.cbox_clip_end, 1, 0)
layout.addWidget(self.cbox_volume_factor, 2, 0)
layout.addWidget(self.clip_start, 0, 1)
layout.addWidget(self.clip_end, 1, 1)
layout.addWidget(self.volume_factor, 2, 1)
self.load()
self.clip_start.timeChanged.connect(self.save)
def load(self):
clip_start, clip_end = self.tape["process"]["start"], self.tape["process"]["end"]
start, end = QTime(), QTime()
start_time = datetime.datetime.strptime(clip_start, "%H:%M:%S.%f")
end_time = datetime.datetime.strptime(clip_end, "%H:%M:%S.%f")
start.setHMS(start_time.hour, start_time.minute, start_time.second, ms=start_time.microsecond * 1e-3)
end.setHMS(end_time.hour, end_time.minute, end_time.second, ms=end_time.microsecond * 1e-3)
self.clip_start.setTime(start)
self.clip_end.setTime(end)
self.cbox_clip_start.setChecked(clip_start != "00:00:00.0")
self.cbox_clip_end.setChecked(clip_end != "00:00:00.0")
volume_factor = self.tape["process"]["volume"]
self.volume_factor.setValue(volume_factor)
self.cbox_volume_factor.setChecked(volume_factor != 0)
def save(self):
start = self.clip_start.time()
end = self.clip_end.time()
self.tape["process"]["start"] = f"{start.hour():02}:{start.minute():02}:{start.second():02}.{start.msec():03}"
self.tape["process"]["end"] = f"{end.hour():02}:{end.minute():02}:{end.second():02}.{end.msec():03}"
self.tape["process"]["volume"] = self.volume_factor.value()
class EconomyEditWidget(QWidget):
def __init__(self, tape: dict):
super().__init__()
self.tape = tape
self.spinbox_price = SpinBoxWithoutWheel()
self.spinbox_price.setRange(10, 100000)
self.spinbox_price.setSuffix(" mk")
self.spinbox_price.setSizePolicy(QSizePolicy.Policy.Maximum, QSizePolicy.Policy.Maximum)
layout = QGridLayout(self)
layout.setContentsMargins(5, 0, 5, 0)
layout.addWidget(self.spinbox_price, 1, 0)
layout.setColumnStretch(4, 1)
for i, text in enumerate(["Price", "Sold in", "Factor", " Local price"]):
layout.addWidget(change_font_size(QLabel(text), 0.8), 0, i)
self.economy = {"outpost": {},
"city": {},
"research": {},
"military": {},
"mine": {}}
for i, (location, editors) in enumerate(self.economy.items()):
editors["cbox"] = QCheckBox(location.capitalize())
editors["float"] = DoubleSpinBoxWithoutWheel()
editors["float"].setSingleStep(0.05)
editors["label"] = QLabel()
editors["label"].setFont("consolas")
layout.addWidget(editors["cbox"], int(i) + 1, 1)
layout.addWidget(editors["float"], int(i) + 1, 2)
layout.addWidget(editors["label"], int(i) + 1, 3)
self.load()
self.update_labels()
self.spinbox_price.valueChanged.connect(self.save)
for editors in self.economy.values():
editors["cbox"].toggled.connect(self.save)
editors["float"].valueChanged.connect(self.save)
def load(self):
self.spinbox_price.setValue(self.tape["price"])
for locale in self.tape["economy"]:
self.economy[locale["location"]]["cbox"].setChecked(locale["sold"])
self.economy[locale["location"]]["float"].setValue(locale["factor"])
def save(self):
self.tape["price"] = self.spinbox_price.value()
self.tape["economy"] = [{"location": location,
"factor": editors["float"].value(),
"sold": editors["cbox"].isChecked()} for location, editors in self.economy.items()]
self.update_labels()
def update_labels(self):
for editors in self.economy.values():
editors["label"].setText(f" {self.tape['price'] * editors['float'].value():4.0f} mk")
class SpawnEditWidget(QWidget):
def __init__(self, tape: dict):
super().__init__()
self.tape = tape
layout = QGridLayout(self)
layout.setContentsMargins(5, 0, 5, 0)
layout.setColumnStretch(2, 1)
for i, text in enumerate(["Place", "Probability"]):
layout.addWidget(change_font_size(QLabel(text), 0.8), 0, i)
self.spawn = {"outpostcrewcabinet": {"label": QLabel("Outpost Crew Cabinet"),
"float": ProbabilitySpinBox()},
"wreckstorage": {"label": QLabel("Wreck Storage"),
"float": ProbabilitySpinBox()},
"abandonedcrewcab": {"label": QLabel("Abandoned Crew Cabinet"),
"float": ProbabilitySpinBox()},
"abandonedstoragecab": {"label": QLabel("Abandoned Storage Cabinet"),
"float": ProbabilitySpinBox()}}
for i, (location, editors) in enumerate(self.spawn.items()):
layout.addWidget(editors["label"], int(i) + 1, 0)
layout.addWidget(editors["float"], int(i) + 1, 1)
self.load()
for i, (location, editors) in enumerate(self.spawn.items()):
editors["float"].valueChanged.connect(self.save)
def load(self):
for locale in self.tape["spawn"]:
self.spawn[locale["location"]]["float"].setValue(locale["probability"])
def save(self):
self.tape["spawn"] = [{"location": location,
"probability": editors["float"].value()} for location, editors in self.spawn.items() if
editors["float"].value() > 0]
# TODO: afflictions with shorter range for walkman songs.
class CraftingEditWidget(QWidget):
def __init__(self, tape: dict):
super().__init__()
self.tape = tape
self.int_uses = SpinBoxWithoutWheel()
self.int_uses.setMinimum(1)
self.int_uses.setMaximum(1000)
self.description = QPlainTextEdit("One use is duration of the tape")
self.description.setEnabled(False)
self.description.setMaximumHeight(QFontMetrics(self.description.font()).height() * 1.7)
layout = QHBoxLayout(self)
layout.addWidget(QLabel("Number of uses:"))
layout.addWidget(self.int_uses)
layout.addWidget(self.description)
layout.addStretch()
layout.setContentsMargins(5, 0, 5, 0)
self.load()
self.int_uses.valueChanged.connect(self.save)
def load(self):
self.int_uses.setValue(self.tape["no_of_uses"])
def save(self):
self.tape["no_of_uses"] = self.int_uses.value()
class AfflictionsEditWidget(QWidget):
def __init__(self, tape: dict):
super().__init__()
self.tape = tape
self.description = QPlainTextEdit("Factor 1.0 means that the affliction will reach the "
"highest level after being exposed to the sound of the "
"tape for its play duration.")
self.description.setEnabled(False)
self.description.setMaximumWidth(212)
layout = QGridLayout(self)
layout.setContentsMargins(5, 0, 5, 0)
layout.addWidget(change_font_size(QLabel("Affliction"), 0.8), 0, 0)
layout.addWidget(change_font_size(QLabel("Factor"), 0.8), 0, 1)
layout.addWidget(self.description, 1, 2, 3, 1)
layout.setColumnStretch(3, 1)
self.afflictions = {"strengthen": {},
"haste": {},
"psychosis": {}}
for i, (affliction, editors) in enumerate(self.afflictions.items()):
editors["cbox"] = QCheckBox(affliction.capitalize())
editors["float"] = DoubleSpinBoxWithoutWheel()
editors["float"].setSingleStep(0.01)
layout.addWidget(editors["cbox"], int(i) + 1, 0)
layout.addWidget(editors["float"], int(i) + 1, 1)
self.load()
for editors in self.afflictions.values():
editors["cbox"].toggled.connect(self.save)
editors["float"].valueChanged.connect(self.save)
def load(self):
if self.tape["afflictions"]:
for affliction in self.tape["afflictions"]:
self.afflictions[affliction["name"]]["cbox"].setChecked(True)
self.afflictions[affliction["name"]]["float"].setValue(affliction["factor"])
def save(self):
self.tape["afflictions"] = [{"name": affliction, "factor": editors["float"].value()} for affliction, editors in
self.afflictions.items() if editors["cbox"].isChecked()]