Skip to content

Commit

Permalink
fix image orientation (AllYarnsAreBeautiful#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
t0mpr1c3 authored Dec 25, 2023
1 parent 097a1d1 commit be93849
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 23 deletions.
4 changes: 3 additions & 1 deletion setup-environment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ pyuic5 src/main/python/ayab/engine/dock_gui.ui -o src/main/python/ayab/engine/do
pyuic5 src/main/python/ayab/engine/options_gui.ui -o src/main/python/ayab/engine/options_gui.py
pyuic5 src/main/python/ayab/engine/status_gui.ui -o src/main/python/ayab/engine/status_gui.py

# generate PyQt5 resource file
# generate PyQt5 resource filea
pyrcc5 src/main/python/ayab/ayab_logo_rc.qrc -o src/main/python/ayab/ayab_logo_rc.py
pyrcc5 src/main/python/ayab/engine/lowercase_e_rc.qrc -o src/main/python/ayab/engine/lowercase_e_rc.py
pyrcc5 src/main/python/ayab/engine/lowercase_e_reversed_rc.qrc -o src/main/python/ayab/engine/lowercase_e_reversed_rc.py

# generate translation files
cd src/main/resources/base/ayab/translations/
Expand Down
4 changes: 4 additions & 0 deletions src/main/python/ayab/ayab.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ def set_image_dimensions(self):
": {} x {}".format(width, height), False)
self.scene.refresh()

def reverse_image(self):
"""Flip scene horizontally."""
self.scene.reverse()

def update_start_row(self, start_row):
self.progbar.update(start_row)
self.scene.row_progress = start_row
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions src/main/python/ayab/engine/lowercase_e_rc.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>garamond-lowercase-e.png</file>
</qresource>
</RCC>
5 changes: 5 additions & 0 deletions src/main/python/ayab/engine/lowercase_e_reversed_rc.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>garamond-lowercase-e-reversed.png</file>
</qresource>
</RCC>
13 changes: 13 additions & 0 deletions src/main/python/ayab/engine/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@

from PyQt5.QtCore import Qt, QCoreApplication, QSettings
from PyQt5.QtWidgets import QWidget
from PyQt5.QtGui import QPixmap

from ayab.signal_sender import SignalSender
from .options_gui import Ui_Options
from .mode import Mode
from ayab.machine import Machine

import ayab.engine.lowercase_e_rc
import ayab.engine.lowercase_e_reversed_rc

# FIXME translations for UI
class OptionsTab(SignalSender, QWidget):
Expand Down Expand Up @@ -72,6 +75,7 @@ def __activate_ui(self):
self.update_needles)
self.ui.alignment_combo_box.currentIndexChanged.connect(
lambda: self.emit_alignment_updater(self.__read_alignment()))
self.ui.auto_mirror_checkbox.clicked.connect(self.__reverse_image)

def update_needles(self):
"""Sends the needles_updater signal."""
Expand All @@ -80,6 +84,13 @@ def update_needles(self):
stop_needle = NeedleColor.read_stop_needle(self.ui, self.machine)
self.emit_needles_updater(start_needle, stop_needle)

def __reverse_image(self):
if self.ui.auto_mirror_checkbox.isChecked():
self.ui.auto_mirror_icon.setPixmap(QPixmap(":/garamond-lowercase-e.png"))
else:
self.ui.auto_mirror_icon.setPixmap(QPixmap(":/garamond-lowercase-e-reversed.png"))
self.emit_image_reverser()

def __read_start_row(self):
return int(self.ui.start_row_edit.value()) - 1

Expand Down Expand Up @@ -127,8 +138,10 @@ def refresh(self):
self.ui.alignment_combo_box.setCurrentIndex(self.alignment.value)
if self.auto_mirror:
self.ui.auto_mirror_checkbox.setCheckState(Qt.Checked)
self.ui.auto_mirror_icon.setPixmap(QPixmap(":/garamond-lowercase-e.png"))
else:
self.ui.auto_mirror_checkbox.setCheckState(Qt.Unchecked)
self.ui.auto_mirror_icon.setPixmap(QPixmap(":/garamond-lowercase-e-reversed.png"))
# self.ui.continuous_reporting_checkbox

def as_dict(self):
Expand Down
15 changes: 14 additions & 1 deletion src/main/python/ayab/engine/options_gui.ui
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,17 @@
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="auto_mirror_icon">
<property name="pixmap">
<pixmap resource="lowercase_e_reversed_rc.qrc">:/garamond-lowercase-e-reversed.png</pixmap>
<size>
<width>32</width>
<height>32</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="continuous_reporting_checkbox">
<property name="text">
Expand All @@ -251,6 +262,8 @@
</layout>
</widget>
</widget>
<resources/>
<resources>
<include location="ayab.engine.lowercase_e_reversed.qrc"/>
</resources>
<connections/>
</ui>
73 changes: 52 additions & 21 deletions src/main/python/ayab/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from PyQt5.QtGui import QImage, QPixmap, QPen, QBrush, QColor
from PyQt5.QtWidgets import QGraphicsScene, QGraphicsRectItem, QGraphicsView

from .image import AyabImage
from .image import AyabImage, Transform
from .engine.options import Alignment
from .machine import Machine

Expand All @@ -52,6 +52,7 @@ def __init__(self, parent):
self.ayabimage = AyabImage(parent)
self.__prefs = parent.prefs
default = self.__prefs.value("default_alignment")
self.__reversed = self.__prefs.value("default_knit_side_image")
self.__alignment = Alignment(default)
machine_width = Machine(self.__prefs.value("machine")).width
self.__start_needle = (machine_width // 2) - 20
Expand All @@ -62,12 +63,22 @@ def __init__(self, parent):
self.setDragMode(QGraphicsView.ScrollHandDrag)
self.__zoom = 3

def reverse(self):
'''Mirrors the graphics scene'''
self.__reversed = not self.__reversed
self.refresh()

def refresh(self):
'''Updates the graphics scene'''
qscene = QGraphicsScene()

width, height = self.ayabimage.image.size
data = self.ayabimage.image.convert("RGBA").tobytes("raw", "RGBA")
if self.__reversed:
im = Transform.hflip(self.ayabimage.image)
else:
im = self.ayabimage.image
data = im.convert("RGBA").tobytes("raw", "RGBA")
qim = QImage(data, width, height, QImage.Format_ARGB32)
pixmap = QPixmap.fromImage(qim)

Expand All @@ -83,38 +94,58 @@ def refresh(self):
else:
logging.warning("invalid alignment")
return
pattern.setPos(pos, 0)
if self.__reversed:
pattern.setPos(- pixmap.width() - pos, 0)
else:
pattern.setPos(pos, 0)

# draw "machine"
rect_orange = QGraphicsRectItem(
rect_L = QGraphicsRectItem(
-machine_width / 2 - 0.5,
-5.5,
machine_width / 2 + 0.5,
5)
rect_orange.setBrush(QBrush(QColor("orange")))
rect_green = QGraphicsRectItem(
rect_R = QGraphicsRectItem(
0,
-5.5,
machine_width / 2 + 0.5,
5)
rect_green.setBrush(QBrush(QColor("green")))

qscene.addItem(rect_orange)
qscene.addItem(rect_green)
if self.__reversed:
rect_L.setBrush(QBrush(QColor("green")))
rect_R.setBrush(QBrush(QColor("orange")))
else:
rect_L.setBrush(QBrush(QColor("orange")))
rect_R.setBrush(QBrush(QColor("green")))
qscene.addItem(rect_L)
qscene.addItem(rect_R)

# draw limiting lines (start/stop needle)
qscene.addItem(
QGraphicsRectItem(
self.__start_needle - machine_width / 2 - 0.5,
-5.5,
0,
pixmap.height() + 5.5))
qscene.addItem(
QGraphicsRectItem(
self.__stop_needle - machine_width / 2 + 1.5,
-5.5,
0,
pixmap.height() + 5.5))
if self.__reversed:
qscene.addItem(
QGraphicsRectItem(
-self.__start_needle + machine_width / 2 + 0.5,
-5.5,
0,
pixmap.height() + 5.5))
qscene.addItem(
QGraphicsRectItem(
-self.__stop_needle + machine_width / 2 - 1.5,
-5.5,
0,
pixmap.height() + 5.5))
else:
qscene.addItem(
QGraphicsRectItem(
self.__start_needle - machine_width / 2 - 0.5,
-5.5,
0,
pixmap.height() + 5.5))
qscene.addItem(
QGraphicsRectItem(
self.__stop_needle - machine_width / 2 + 1.5,
-5.5,
0,
pixmap.height() + 5.5))
qscene.addItem(
QGraphicsRectItem(
self.__start_needle - machine_width / 2 - 1,
Expand Down
2 changes: 2 additions & 0 deletions src/main/python/ayab/signal_receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class SignalReceiver(QObject):
needles_updater = pyqtSignal(int, int)
alignment_updater = pyqtSignal(Alignment)
image_resizer = pyqtSignal()
image_reverser = pyqtSignal()
got_image_flag = pyqtSignal()
new_image_flag = pyqtSignal()
bad_config_flag = pyqtSignal()
Expand Down Expand Up @@ -76,6 +77,7 @@ def activate_signals(self, parent):
self.needles_updater.connect(parent.scene.update_needles)
self.alignment_updater.connect(parent.scene.update_alignment)
self.image_resizer.connect(parent.set_image_dimensions)
self.image_reverser.connect(parent.reverse_image)
self.operation_finisher.connect(parent.finish_operation)
self.hw_test_starter.connect(parent.hw_test.open)
self.hw_test_writer.connect(parent.hw_test.output)

0 comments on commit be93849

Please sign in to comment.