Skip to content
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

WIP: Keep shift with layers #270

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 22 additions & 7 deletions inkcut/job/models.py
Expand Up @@ -20,7 +20,7 @@
)
from contextlib import contextmanager
from enaml.qt.QtGui import QPainterPath, QTransform
from enaml.qt.QtCore import QPointF
from enaml.qt.QtCore import QPointF, QRectF
from enaml.colors import ColorMember
from inkcut.core.api import Model, AreaBase
from inkcut.core.svg import QtSvgDoc
Expand Down Expand Up @@ -167,6 +167,10 @@ class Job(Model):
align_center = ContainerList(Bool(),
default=[False, False]).tag(config=True)

# Shifting of original file
auto_shift = Bool(True).tag(config=True, help="shift to start at origin")
copy_bbox = Instance(QRectF)

rotation = Float(0).tag(config=True)
auto_rotate = Bool(False).tag(
config=True, help="automatically rotate if it saves space")
Expand Down Expand Up @@ -198,6 +202,7 @@ def _default_order(self):
filters = ContainerList(filters.JobFilter)

#: Original path parsed from the source document
doc = Instance(QtSvgDoc)
path = Instance(QtSvgDoc)

#: Path filtered by layers/colors and ordered according to the order
Expand Down Expand Up @@ -227,9 +232,9 @@ def _observe_document(self, change):
#: Only load from stdin when explicitly changed to it (when doing
#: open from the cli) otherwise when restoring state this hangs
#: startup
self.path = QtSvgDoc(sys.stdin, **self.document_kwargs)
self.doc = self.path = QtSvgDoc(sys.stdin, **self.document_kwargs)
elif self.document and os.path.exists(self.document):
self.path = QtSvgDoc(self.document, **self.document_kwargs)
self.doc = self.path = QtSvgDoc(self.document, **self.document_kwargs)

# Recreate available filters when the document changes
self.filters = self._default_filters()
Expand Down Expand Up @@ -289,6 +294,8 @@ def _create_copy(self):
self.scale[1] * (self.mirror[1] and -1 or 1),
)

self.copy_bbox = t.mapRect(bbox)

# Rotate about center
if self.rotation != 0:
c = bbox.center()
Expand Down Expand Up @@ -320,10 +327,12 @@ def _create_copy(self):
s = min(sx, sy) # Fit to the smaller of the two
path = optimized_path * QTransform.fromScale(s, s)

# Move to bottom left
p = path.boundingRect().bottomRight()
# Save original bbox
bbox = path.boundingRect()

path = path * QTransform.fromTranslate(-p.x(), -p.y())
# Move to bottom left
br = bbox.bottomRight()
path = path * QTransform.fromTranslate(-br.x(), -br.y())

return path

Expand All @@ -343,7 +352,7 @@ def events_suppressed(self):
'copy_spacing', 'copy_weedline', 'copy_weedline_padding',
'plot_weedline', 'plot_weedline_padding', 'feed_to_end',
'feed_after', 'material', 'material.size', 'material.padding',
'auto_copies')
'auto_copies', 'auto_shift')
def update_document(self, change=None):
""" Recreate an instance of of the plot using the current settings

Expand Down Expand Up @@ -426,6 +435,12 @@ def create(self, swap_xy=False, scale=None):
p = bbox.bottomLeft()
tx, ty = -p.x(), -p.y()

if not self.auto_shift:
# Re-add original shift
bbox = self.copy_bbox
tx += -bbox.right() if self.mirror[0] else bbox.left()
ty += bbox.bottom() if self.mirror[1] else -bbox.top()

# If swapped, make sure padding is still correct
if swap_xy:
px, py = -py, -px
Expand Down
5 changes: 4 additions & 1 deletion inkcut/job/view.enaml
Expand Up @@ -493,6 +493,10 @@ enamldef MaterialDockItem(DockItem):

Label:
text = QApplication.translate("job", "Plot Alignment")
CheckBox:
text = QApplication.translate("job", 'Shift to origin')
icon = load_icon('shape_align_left')
checked := job.auto_shift
CheckBox:
text = QApplication.translate("job", 'Align center horizontally')
icon = load_icon('shape_align_center')
Expand All @@ -501,7 +505,6 @@ enamldef MaterialDockItem(DockItem):
text = QApplication.translate("job", 'Align center vertically')
icon = load_icon('shape_align_middle')
checked := job.align_center[1]

Container:
padding = 0
constraints = [
Expand Down