kopia lustrzana https://github.com/inkstitch/inkstitch
allow selection of a different thread palette
rodzic
c234d6ed2c
commit
8608508e02
|
@ -99,6 +99,7 @@ class PrintPreviewServer(Thread):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
self.html = kwargs.pop('html')
|
self.html = kwargs.pop('html')
|
||||||
self.metadata = kwargs.pop('metadata')
|
self.metadata = kwargs.pop('metadata')
|
||||||
|
self.stitch_plan = kwargs.pop('stitch_plan')
|
||||||
Thread.__init__(self, *args, **kwargs)
|
Thread.__init__(self, *args, **kwargs)
|
||||||
self.daemon = True
|
self.daemon = True
|
||||||
self.last_request_time = None
|
self.last_request_time = None
|
||||||
|
@ -178,6 +179,35 @@ class PrintPreviewServer(Thread):
|
||||||
save_defaults(request.json['value'])
|
save_defaults(request.json['value'])
|
||||||
return "OK"
|
return "OK"
|
||||||
|
|
||||||
|
@self.app.route('/palette', methods=['POST'])
|
||||||
|
def set_palette():
|
||||||
|
name = request.json['name']
|
||||||
|
catalog = ThreadCatalog()
|
||||||
|
palette = catalog.get_palette_by_name(name)
|
||||||
|
catalog.apply_palette(self.stitch_plan, palette)
|
||||||
|
|
||||||
|
# clear any saved color or thread names
|
||||||
|
for field in self.metadata:
|
||||||
|
if field.startswith('color-') or field.startswith('thread-'):
|
||||||
|
del self.metadata[field]
|
||||||
|
|
||||||
|
self.metadata['thread-palette'] = name
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
|
||||||
|
@self.app.route('/threads', methods=['GET'])
|
||||||
|
def get_threads():
|
||||||
|
threads = []
|
||||||
|
for color_block in self.stitch_plan:
|
||||||
|
threads.append({
|
||||||
|
'hex': color_block.color.hex_digits,
|
||||||
|
'name': color_block.color.name,
|
||||||
|
'manufacturer': color_block.color.manufacturer,
|
||||||
|
'number': color_block.color.number,
|
||||||
|
})
|
||||||
|
|
||||||
|
return jsonify(threads)
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
# for whatever reason, shutting down only seems possible in
|
# for whatever reason, shutting down only seems possible in
|
||||||
# the context of a flask request, so we'll just make one
|
# the context of a flask request, so we'll just make one
|
||||||
|
@ -291,7 +321,7 @@ class Print(InkstitchExtension):
|
||||||
|
|
||||||
patches = self.elements_to_patches(self.elements)
|
patches = self.elements_to_patches(self.elements)
|
||||||
stitch_plan = patches_to_stitch_plan(patches)
|
stitch_plan = patches_to_stitch_plan(patches)
|
||||||
palette = ThreadCatalog().match_and_apply_palette(stitch_plan)
|
palette = ThreadCatalog().match_and_apply_palette(stitch_plan, self.get_inkstitch_metadata()['thread-palette'])
|
||||||
render_stitch_plan(self.document.getroot(), stitch_plan)
|
render_stitch_plan(self.document.getroot(), stitch_plan)
|
||||||
|
|
||||||
self.strip_namespaces()
|
self.strip_namespaces()
|
||||||
|
@ -354,7 +384,7 @@ class Print(InkstitchExtension):
|
||||||
# metadata into it.
|
# metadata into it.
|
||||||
self.document = deepcopy(self.original_document)
|
self.document = deepcopy(self.original_document)
|
||||||
|
|
||||||
print_server = PrintPreviewServer(html=html, metadata=self.get_inkstitch_metadata())
|
print_server = PrintPreviewServer(html=html, metadata=self.get_inkstitch_metadata(), stitch_plan=stitch_plan)
|
||||||
print_server.start()
|
print_server.start()
|
||||||
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
|
@ -75,11 +75,11 @@ class InkStitchMetadata(MutableMapping):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return json.loads(item.text)
|
return json.loads(item.text)
|
||||||
except ValueError:
|
except (ValueError, TypeError):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def __delitem__(self, name):
|
def __delitem__(self, name):
|
||||||
item = self[name]
|
item = self._find_item(name)
|
||||||
|
|
||||||
if item:
|
if item:
|
||||||
self.metadata.remove(item)
|
self.metadata.remove(item)
|
||||||
|
|
|
@ -38,8 +38,11 @@ class _ThreadCatalog(Sequence):
|
||||||
|
|
||||||
return sum(1 for thread in threads if thread in palette)
|
return sum(1 for thread in threads if thread in palette)
|
||||||
|
|
||||||
def match_and_apply_palette(self, stitch_plan):
|
def match_and_apply_palette(self, stitch_plan, palette=None):
|
||||||
palette = self.match_palette(stitch_plan)
|
if palette is None:
|
||||||
|
palette = self.match_palette(stitch_plan)
|
||||||
|
else:
|
||||||
|
palette = self.get_palette_by_name(palette)
|
||||||
|
|
||||||
if palette is not None:
|
if palette is not None:
|
||||||
self.apply_palette(stitch_plan, palette)
|
self.apply_palette(stitch_plan, palette)
|
||||||
|
@ -75,6 +78,10 @@ class _ThreadCatalog(Sequence):
|
||||||
color_block.color.number = nearest.number
|
color_block.color.number = nearest.number
|
||||||
color_block.color.manufacturer = nearest.manufacturer
|
color_block.color.manufacturer = nearest.manufacturer
|
||||||
|
|
||||||
|
def get_palette_by_name(self, name):
|
||||||
|
for palette in self:
|
||||||
|
if palette.name == name:
|
||||||
|
return palette
|
||||||
|
|
||||||
_catalog = None
|
_catalog = None
|
||||||
|
|
||||||
|
|
|
@ -178,11 +178,11 @@ $(function() {
|
||||||
$('[data-field-name="' + field_name + '"]').each(function(i, item) {
|
$('[data-field-name="' + field_name + '"]').each(function(i, item) {
|
||||||
var item = $(item);
|
var item = $(item);
|
||||||
if (item.is(':checkbox')) {
|
if (item.is(':checkbox')) {
|
||||||
item.prop('checked', value).trigger('change');
|
item.prop('checked', value).trigger('initialize');
|
||||||
} else if (item.is('img')) {
|
} else if (item.is('img')) {
|
||||||
item.attr('src', value);
|
item.attr('src', value);
|
||||||
} else if (item.is('select')) {
|
} else if (item.is('select')) {
|
||||||
item.val(value).trigger('change');
|
item.val(value).trigger('initialize');
|
||||||
} else if (item.is('figure.inksimulation')) {
|
} else if (item.is('figure.inksimulation')) {
|
||||||
setSVGTransform(item, value);
|
setSVGTransform(item, value);
|
||||||
} else {
|
} else {
|
||||||
|
@ -249,10 +249,10 @@ $(function() {
|
||||||
/* Settings */
|
/* Settings */
|
||||||
|
|
||||||
// Paper Size
|
// Paper Size
|
||||||
$('select#printing-size').change(function(){
|
$('select#printing-size').on('change initialize', function(){
|
||||||
var size = $(this).find(':selected').val();
|
$('.page').toggleClass('a4', $(this).find(':selected').val() == 'a4');
|
||||||
$('.page').toggleClass('a4', size == 'a4');
|
}).on('change', function() {
|
||||||
$.postJSON('/settings/paper-size', {value: size});
|
$.postJSON('/settings/paper-size', {value: $(this).find(':selected').val()});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Thread Palette
|
// Thread Palette
|
||||||
|
@ -260,13 +260,26 @@ $(function() {
|
||||||
$('.modal').show();
|
$('.modal').show();
|
||||||
}).on('update', function() {
|
}).on('update', function() {
|
||||||
$(this).data('current-value', $(this).find(':selected').val());
|
$(this).data('current-value', $(this).find(':selected').val());
|
||||||
console.log("selected: " + $(this).data('current-value'));
|
|
||||||
}).trigger('update');
|
}).trigger('update');
|
||||||
|
|
||||||
$('#modal-yes').on('click', function(){
|
$('#modal-yes').on('click', function(){
|
||||||
// do shit with the newly-selected palette...
|
|
||||||
$("select#thread-palette").trigger("update");
|
$("select#thread-palette").trigger("update");
|
||||||
$('.modal').hide();
|
$('.modal').hide();
|
||||||
|
var body = {'name': $('select#thread-palette').find(':selected').val()};
|
||||||
|
$.postJSON('/palette', body, function() {
|
||||||
|
$.getJSON('/threads', function(threads) {
|
||||||
|
console.log("threads: " + JSON.stringify(threads));
|
||||||
|
$.each(threads, function(i, thread) {
|
||||||
|
console.log("doing: " + JSON.stringify(thread));
|
||||||
|
$('[data-field-name="color-' + thread.hex + '"]').text(thread.name);
|
||||||
|
var thread_description = thread.manufacturer;
|
||||||
|
if (thread.number) {
|
||||||
|
thread_description += " #" + thread.number;
|
||||||
|
}
|
||||||
|
$('[data-field-name="thread-' + thread.hex + '"]').text(thread_description);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
$('#modal-no').on('click', function(){
|
$('#modal-no').on('click', function(){
|
||||||
|
@ -276,14 +289,13 @@ $(function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
//Checkbox
|
//Checkbox
|
||||||
$(':checkbox').change(function() {
|
$(':checkbox').on('change initialize', function() {
|
||||||
var checked = $(this).prop('checked');
|
|
||||||
var field_name = $(this).attr('data-field-name');
|
var field_name = $(this).attr('data-field-name');
|
||||||
|
|
||||||
$('.' + field_name).toggle(checked);
|
$('.' + field_name).toggle($(this).prop('checked'));
|
||||||
setPageNumbers();
|
setPageNumbers();
|
||||||
|
}).on('change', function() {
|
||||||
$.postJSON('/settings/' + field_name, {value: checked});
|
$.postJSON('/settings/' + field_name, {value: $(this).prop('checked')});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Logo
|
// Logo
|
||||||
|
|
Ładowanie…
Reference in New Issue