Implement business logic of split

pull/281/head
Christoph Stelz 2021-06-23 10:26:20 +02:00
parent c3bbd8488d
commit 6656ea4687
3 changed files with 65 additions and 6 deletions

View File

@ -106,6 +106,31 @@ class Config(GObject.Object):
self._drawings.append(json.loads(json_string))
self.notify('drawings')
def replace_drawing(self, timestamp, json_string):
'''Replace the drawing JSON identified by the timestamp in the backend
storage. This will update self.drawings.'''
self.base_path.mkdir(parents=True, exist_ok=True)
path = Path(self.base_path, f'{timestamp}.json')
if not path.exists():
return
replaced_drawing = list(filter(lambda d: d["timestamp"] == timestamp,
self._drawings))
if len(replaced_drawing) <= 0:
return
else:
replaced_drawing = replaced_drawing[0]
with open(path, 'w') as fd:
fd.write(json_string)
self._drawings.remove(replaced_drawing)
self._drawings.append(json.loads(json_string))
self.notify('drawings')
def delete_drawing(self, timestamp):
# We don't delete json files immediately, we just rename them
# so we can resurrect them in the future if need be.

View File

@ -15,6 +15,7 @@ from gettext import gettext as _
import xdg.BaseDirectory
import os
import json
from pathlib import Path
from .config import Config
from .splitter import Splitter
@ -164,8 +165,38 @@ class Drawing(Gtk.EventBox):
dialog = Splitter(self)
response = dialog.run()
if response == Gtk.ResponseType.OK:
self._save_split_drawings(*dialog.split_drawings)
dialog.destroy()
def _save_split_drawings(self, json1, json2):
timestamp1 = json1["timestamp"]
timestamp2 = json2["timestamp"]
if timestamp2 in map(lambda d: d["timestamp"], Config().drawings):
error_dialog = Gtk.MessageDialog(
transient_for=self,
flags=0,
message_type=Gtk.MessageType.ERROR,
buttons=Gtk.ButtonsType.OK,
text="Error while splitting drawing"
)
error_dialog.format_secondary_text(
f"A drawing with timestamp {timestamp} already exists. Cannot proceed to save split drawing, otherwise data loss might occur"
)
error_dialog.run()
error_dialog.destroy()
Config().replace_drawing(timestamp1, json.dumps(json1))
Config().add_drawing(timestamp2, json.dumps(json2))
# Force redraw of this drawing
os.remove(self.svg.filename)
self.process_svg()
self.redraw()
@Gtk.Template.Callback('_on_delete_button_clicked')
def _on_delete_button_clicked(self, button):
Config().delete_drawing(self.timestamp)

View File

@ -13,10 +13,9 @@ import os
import math
from pathlib import Path
from .config import Config
from tuhi.export import JsonPartialSvg
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import GObject, Gtk, GdkPixbuf, Gdk # NOQA
from gi.repository import GObject, Gtk
import cairo
@ -46,10 +45,6 @@ class Splitter(Gtk.Dialog):
self.adjustment.set_value(self.num_strokes)
self.drawing_area.connect("draw", self._on_draw_image)
@Gtk.Template.Callback('_on_cancel')
def _on_cancel(self, button):
super().response(Gtk.ResponseType.CANCEL)
@ -59,6 +54,14 @@ class Splitter(Gtk.Dialog):
def _on_ok(self, button):
super().response(Gtk.ResponseType.OK)
print("OK")
json_data1 = self.json_data
json_data2 = self.json_data.copy()
json_data1["strokes"] = json_data1["strokes"][:self.max_strokes]
json_data2["strokes"] = json_data2["strokes"][self.max_strokes:]
json_data2["timestamp"] += 1
self.split_drawings = [json_data1, json_data2]
def _on_split_value_changed(self, adjustment):
self.max_strokes = int(adjustment.get_value())