-
Notifications
You must be signed in to change notification settings - Fork 35
Extract memo information from PNG file Comment tag and display in Knitting Progress window #779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 21 commits
330304b
542ee01
a1531f1
02fbb4d
b3d74f4
e4d8271
a86dc9e
c0b4c96
aea8e89
2836dde
b6f5537
3b7c243
8793fed
9abe187
7822017
764d261
2340df0
80d09cb
12b877c
14a2246
12e40c3
03b6404
593551c
6f192ab
2958126
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
# Andreas Müller, Christian Gerbrandt | ||
# https://github.com/AllYarnsAreBeautiful/ayab-desktop | ||
|
||
|
||
from __future__ import annotations | ||
import logging | ||
from math import ceil | ||
|
@@ -26,7 +27,7 @@ | |
from PySide6.QtCore import QCoreApplication | ||
from PySide6.QtWidgets import QInputDialog, QDialog, QFileDialog | ||
|
||
from .transforms import Transform, Mirrors | ||
from .transforms import ImageTransform, Mirrors | ||
from .signal_sender import SignalSender | ||
from .utils import display_blocking_popup | ||
from .machine import Machine | ||
|
@@ -55,6 +56,7 @@ def __init__(self, parent: GuiMain): | |
super().__init__(parent.signal_receiver) | ||
self.__parent = parent | ||
self.image: Image.Image = None # type: ignore | ||
self.memos: list[str] = [] | ||
t0mpr1c3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.filename: Optional[str] = None | ||
self.filename_input = self.__parent.ui.filename_lineedit | ||
|
||
|
@@ -87,12 +89,12 @@ def __load(self, filename: str) -> None: | |
display_blocking_popup( | ||
QCoreApplication.translate("Image", "Unable to load image file"), | ||
"error", | ||
) # FIXME translate | ||
) | ||
logging.error("Unable to load " + str(filename)) | ||
except Exception as e: | ||
display_blocking_popup( | ||
QCoreApplication.translate("Image", "Error loading image file"), "error" | ||
) # FIXME translate | ||
) | ||
logging.error("Error loading image: " + str(e)) | ||
raise | ||
else: | ||
|
@@ -101,6 +103,7 @@ def __load(self, filename: str) -> None: | |
self.__parent.engine.config.refresh() | ||
|
||
def __open(self, filename: str) -> None: | ||
self.memos = [] | ||
# check for files that need conversion | ||
suffix = filename[-4:].lower() | ||
if suffix == ".pat": | ||
|
@@ -111,12 +114,25 @@ def __open(self, filename: str) -> None: | |
self.image = CutPatternConverter().pattern2im(filename) | ||
else: | ||
self.image = Image.open(filename) | ||
if suffix == ".png": | ||
# check metadata for memo information | ||
self.image.load() | ||
if "Comment" in self.image.info and len(str(self.image.info["Comment"])) > 0: | ||
comment = str(self.image.info["Comment"]) | ||
if comment.startswith("AYAB:"): | ||
# update memo information | ||
for i in range(len(comment) - 5): | ||
self.memos.append(comment[i + 5]) | ||
# report metadata | ||
logging.info("File metadata Comment tag: " + comment) | ||
logging.info("File memo information: " + str(self.memos)) | ||
self.image = self.image.convert("RGBA") | ||
self.emit_got_image_flag() | ||
self.emit_image_resizer() | ||
|
||
def invert(self) -> None: | ||
self.apply_transform(Transform.invert) | ||
self.apply_transform(ImageTransform.invert) | ||
# memos unchanged | ||
|
||
def repeat(self) -> None: | ||
machine_width = Machine(self.__parent.prefs.value("machine")).width | ||
|
@@ -131,7 +147,8 @@ def repeat(self) -> None: | |
minValue=1, | ||
maxValue=ceil(machine_width / self.image.width), | ||
) | ||
self.apply_transform(Transform.repeat, v[0], h[0]) | ||
self.apply_transform(ImageTransform.repeat, v[0], h[0]) | ||
self.memos = self.memos * v[0] | ||
|
||
def stretch(self) -> None: | ||
machine_width = Machine(self.__parent.prefs.value("machine")).width | ||
|
@@ -146,24 +163,30 @@ def stretch(self) -> None: | |
minValue=1, | ||
maxValue=ceil(machine_width / self.image.width), | ||
) | ||
self.apply_transform(Transform.stretch, v[0], h[0]) | ||
self.apply_transform(ImageTransform.stretch, v[0], h[0]) | ||
self.memos = [] | ||
Comment on lines
+166
to
+167
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider preserving memo data for safe transforms The current implementation clears memos for several transforms (stretch, reflect, vflip, rotate_left, rotate_right). According to the PR discussion, some of these might be safe in specific circumstances. For user experience consistency, consider adding a warning dialog when a transform would clear memo data, giving users the option to proceed or cancel. def _clear_memos_with_warning(self, transform_name):
"""Display warning and clear memos if user confirms."""
result = display_warning_dialog(
f"The {transform_name} transform will clear memo data. Proceed?",
buttons=["Yes", "No"]
)
if result == "Yes":
self.memos = []
return True
return False
# Then in transform methods like stretch:
def stretch(self):
# ... existing code ...
if self.memos and not self._clear_memos_with_warning("Stretch"):
return # User canceled
self.apply_transform(ImageTransform.stretch, v[0], h[0]) Also applies to: 172-173, 180-181, 184-185, 188-189 |
||
|
||
def reflect(self) -> None: | ||
m = Mirrors() | ||
if m.result == QDialog.DialogCode.Accepted: | ||
self.apply_transform(Transform.reflect, m.mirrors) | ||
self.apply_transform(ImageTransform.reflect, m.mirrors) | ||
self.memos = [] | ||
|
||
def hflip(self) -> None: | ||
self.apply_transform(Transform.hflip) | ||
self.apply_transform(ImageTransform.hflip) | ||
# memos unchanged | ||
|
||
def vflip(self) -> None: | ||
self.apply_transform(Transform.vflip) | ||
self.apply_transform(ImageTransform.vflip) | ||
self.memos = [] | ||
|
||
def rotate_left(self) -> None: | ||
self.apply_transform(Transform.rotate_left) | ||
self.apply_transform(ImageTransform.rotate_left) | ||
self.memos = [] | ||
|
||
def rotate_right(self) -> None: | ||
self.apply_transform(Transform.rotate_right) | ||
self.apply_transform(ImageTransform.rotate_right) | ||
self.memos = [] | ||
|
||
def zoom_in(self) -> None: | ||
self.__parent.scene.set_zoom(+1) | ||
|
@@ -178,10 +201,6 @@ def apply_transform( | |
) -> None: | ||
"""Executes an image transform specified by function and args.""" | ||
self.image = transform(self.image, args) | ||
try: | ||
pass # self.image = transform(self.image, args) | ||
except Exception as e: | ||
logging.error("Error while executing image transform: " + repr(e)) | ||
|
||
# Update the view | ||
self.emit_image_resizer() | ||
|
Uh oh!
There was an error while loading. Please reload this page.