1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
#
# Authors: see git history
#
# Copyright (c) 2023 Authors
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
import wx
import wx.adv
from ..i18n import _
from ..svg.tags import EMBROIDERABLE_TAGS
class GenerateSwatchesFrame(wx.Frame):
def __init__(self, *args, **kwargs):
self.extension = kwargs.pop("extension")
self.choices = kwargs.pop("choices")
wx.Frame.__init__(self, *args, **kwargs)
wx.Frame.__init__(self, None, wx.ID_ANY, _("Generate Swatches"), *args, **kwargs)
self.SetWindowStyle(wx.FRAME_FLOAT_ON_PARENT | wx.DEFAULT_FRAME_STYLE)
self.panel = wx.Panel(self, wx.ID_ANY)
main_sizer = wx.BoxSizer(wx.VERTICAL)
self.notebook = wx.Notebook(self.panel, wx.ID_ANY)
main_sizer.Add(self.notebook, 0, wx.ALL | wx.EXPAND, 10)
self.options = wx.Panel(self.notebook, wx.ID_ANY)
self.notebook.AddPage(self.options, _("Options"))
options_sizer = wx.BoxSizer(wx.VERTICAL)
grid_main_sizer = wx.FlexGridSizer(6, 2, 10, 10)
options_sizer.Add(grid_main_sizer, 0, wx.ALL, 10)
param_label = wx.StaticText(self.options, wx.ID_ANY, _("Param"))
grid_main_sizer.Add(param_label, 0, 0, 0)
self.param = wx.ComboBox(self.options, wx.ID_ANY, choices=[])
for choice in self.choices:
self.param.Append(choice.name, choice)
self.param.SetSelection(0)
grid_main_sizer.Add(self.param, 0, 0, 0)
start_value_label = wx.StaticText(self.options, wx.ID_ANY, _("Start Value"))
grid_main_sizer.Add(start_value_label, 0, 0, 0)
self.start_value = wx.SpinCtrlDouble(self.options, wx.ID_ANY, initial=2.5, min=0.0, max=500.0)
self.start_value.SetDigits(2)
grid_main_sizer.Add(self.start_value, 0, 0, 0)
step_label = wx.StaticText(self.options, wx.ID_ANY, _("Increase by"))
grid_main_sizer.Add(step_label, 0, 0, 0)
self.step = wx.SpinCtrlDouble(self.options, wx.ID_ANY, initial=0.5, min=0.01, max=500.0)
self.step.SetDigits(2)
grid_main_sizer.Add(self.step, 0, 0, 0)
cols_label = wx.StaticText(self.options, wx.ID_ANY, _("Columns"))
grid_main_sizer.Add(cols_label, 0, 0, 0)
self.columns = wx.SpinCtrl(self.options, wx.ID_ANY, "0", min=1, max=100)
grid_main_sizer.Add(self.columns, 0, 0, 0)
rows_label = wx.StaticText(self.options, wx.ID_ANY, _("Rows"))
grid_main_sizer.Add(rows_label, 0, 0, 0)
self.rows = wx.SpinCtrl(self.options, wx.ID_ANY, "0", min=1, max=100)
grid_main_sizer.Add(self.rows, 0, 0, 0)
spacing_label = wx.StaticText(self.options, wx.ID_ANY, _("Spacing"))
grid_main_sizer.Add(spacing_label, 0, 0, 0)
self.spacing = wx.SpinCtrlDouble(self.options, wx.ID_ANY, initial=5, min=0.0, max=100.0)
self.spacing.SetDigits(2)
grid_main_sizer.Add(self.spacing, 0, 0, 0)
self.info = wx.Panel(self.notebook, wx.ID_ANY)
self.notebook.AddPage(self.info, _("Help"))
info_sizer = wx.BoxSizer(wx.VERTICAL)
info_text_label = wx.StaticText(
self.info,
wx.ID_ANY,
_("This extension generates test swatches from a selection.\n"
"Test swatches help to find best stitch settings for your design.\n"
"Sew them out with the same thread and fabric as the final designs."),
style=wx.ALIGN_LEFT
)
info_text_label.Wrap(500)
info_sizer.Add(info_text_label, 0, wx.ALL, 8)
info_sizer.Add((20, 20), 0, 0, 0)
more_info_label = wx.StaticText(self.info, wx.ID_ANY, _("Get more information on our website"))
info_sizer.Add(more_info_label, 0, wx.ALL, 8)
self.help_link = wx.adv.HyperlinkCtrl(
self.info,
wx.ID_ANY,
_("https://inkstitch.org/docs/edit/#generate-test-swatches-from-selection"),
_("https://inkstitch.org/docs/edit/#generate-test-swatches-from-selection"),
style=wx.adv.HL_ALIGN_CENTRE
)
info_sizer.Add(self.help_link, 0, 0, 0)
button_sizer = wx.StdDialogButtonSizer()
main_sizer.Add(button_sizer, 1, wx.BOTTOM | wx.EXPAND, 10)
button_sizer.Add((0, 0), 1, 0, 0)
self.apply_button = wx.Button(self.panel, wx.ID_ANY, _("Apply"))
button_sizer.Add(self.apply_button, 0, wx.RIGHT, 10)
self.options.SetSizer(options_sizer)
self.info.SetSizer(info_sizer)
self.panel.SetSizer(main_sizer)
main_sizer.Fit(self)
self.Layout()
self.SetSizeHints(main_sizer.CalcMin())
self.param.Bind(wx.EVT_TEXT, self.on_text_input)
self.Bind(wx.EVT_BUTTON, self.apply_button_clicked, self.apply_button)
def on_text_input(self, event):
# this will allow us to catch manual text input, but
# we only want to catch it when we actually create the swatches
pass
def apply_button_clicked(self, event):
self.apply()
self.Destroy()
def apply(self):
num_cols = self.columns.GetValue()
num_rows = self.rows.GetValue()
spacing = self.spacing.GetValue()
start_value = self.start_value.GetValue()
step = self.step.GetValue()
choice_names = [choice.name for choice in self.choices]
if self.param.GetValue() not in choice_names:
# catch manual text input
param = self.param.GetValue()
if param.startswith("inkstitch:"):
param = param[10:]
else:
param = self.choices[self.param.GetSelection()].id
for element in self.extension.svg.selection:
dimensions = element.bounding_box()
param_value = start_value
for rows in range(0, num_rows):
for cols in range(0, num_cols):
new_element = element.duplicate()
translate_x = cols * dimensions.width + cols * spacing
translate_y = rows * dimensions.height + rows * spacing
new_element.transform.add_translate((translate_x, translate_y))
if new_element.TAG == "g":
for embroidery_element in new_element.iterdescendants(EMBROIDERABLE_TAGS):
# Since this won't effect functionality, we can simply ignore the fact
# that this will also set the value to patterns, guide lines etc.
self._set_param(embroidery_element, param, param_value)
else:
self._set_param(new_element, param, param_value)
param_value += step
# remove old element
element.getparent().remove(element)
def _set_param(self, element, param, value):
element.set(f'inkstitch:{ param }', value)
class GenerateSwatchesApp(wx.App):
def __init__(self, extension, choices):
self.extension = extension
self.choices = choices
super().__init__()
def OnInit(self):
self.frame = GenerateSwatchesFrame(extension=self.extension, choices=self.choices)
self.SetTopWindow(self.frame)
self.frame.Show()
return True
|