2019-07-10 01:51:41 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
2019-07-16 13:48:37 +02:00
|
|
|
|
|
|
|
from gettext import gettext as _
|
2019-07-19 10:09:13 +02:00
|
|
|
from gi.repository import GObject, Gtk, GdkPixbuf, Gdk
|
2019-07-10 01:51:41 +02:00
|
|
|
|
2019-07-16 06:40:37 +02:00
|
|
|
from .config import Config
|
2019-07-16 13:06:32 +02:00
|
|
|
from .svg import JsonSvg
|
2019-07-16 06:40:37 +02:00
|
|
|
|
2019-07-10 01:51:41 +02:00
|
|
|
import gi
|
|
|
|
gi.require_version("Gtk", "3.0")
|
|
|
|
|
2019-07-16 07:28:51 +02:00
|
|
|
|
2019-07-17 13:10:36 +02:00
|
|
|
@Gtk.Template(resource_path='/org/freedesktop/Tuhi/ui/Drawing.ui')
|
2019-07-19 10:09:13 +02:00
|
|
|
class Drawing(Gtk.EventBox):
|
2019-07-10 01:51:41 +02:00
|
|
|
__gtype_name__ = "Drawing"
|
|
|
|
|
2019-07-19 03:08:54 +02:00
|
|
|
box_toolbar = Gtk.Template.Child()
|
2019-07-10 01:51:41 +02:00
|
|
|
image_svg = Gtk.Template.Child()
|
2019-07-16 13:29:03 +02:00
|
|
|
btn_rotate_left = Gtk.Template.Child()
|
|
|
|
btn_rotate_right = Gtk.Template.Child()
|
2019-07-10 01:51:41 +02:00
|
|
|
|
2019-07-16 13:06:32 +02:00
|
|
|
def __init__(self, json_data, *args, **kwargs):
|
2019-07-10 01:51:41 +02:00
|
|
|
super().__init__()
|
2019-07-16 13:15:11 +02:00
|
|
|
self.orientation = Config.instance().orientation
|
2019-07-18 10:32:11 +02:00
|
|
|
Config.instance().connect('notify::orientation', self._on_orientation_changed)
|
2019-07-16 13:15:11 +02:00
|
|
|
|
2019-07-16 13:06:32 +02:00
|
|
|
self.json_data = json_data
|
2019-07-19 08:41:17 +02:00
|
|
|
self._zoom = 0
|
2019-07-19 03:17:19 +02:00
|
|
|
self.refresh() # sets self.svg
|
2019-07-15 08:44:24 +02:00
|
|
|
|
2019-07-19 03:17:19 +02:00
|
|
|
self.timestamp = self.svg.timestamp
|
2019-07-19 10:09:13 +02:00
|
|
|
self.box_toolbar.set_opacity(0)
|
2019-07-10 01:51:41 +02:00
|
|
|
|
2019-07-18 10:32:11 +02:00
|
|
|
def _on_orientation_changed(self, config, pspec):
|
|
|
|
self.orientation = config.orientation
|
|
|
|
self.refresh()
|
|
|
|
|
2019-07-16 13:06:32 +02:00
|
|
|
def refresh(self):
|
2019-07-19 08:41:17 +02:00
|
|
|
self.svg = JsonSvg(self.json_data, self.orientation)
|
2019-08-08 06:39:28 +02:00
|
|
|
width, height = -1, -1
|
|
|
|
if 'portrait' in self.orientation:
|
|
|
|
height = 1000
|
|
|
|
else:
|
|
|
|
width = 1000
|
2019-07-19 08:41:17 +02:00
|
|
|
self.pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=self.svg.filename,
|
2019-08-08 06:39:28 +02:00
|
|
|
width=width,
|
|
|
|
height=height,
|
2019-07-19 08:41:17 +02:00
|
|
|
preserve_aspect_ratio=True)
|
|
|
|
self.redraw()
|
|
|
|
|
|
|
|
def redraw(self):
|
2019-08-08 06:39:28 +02:00
|
|
|
ratio = self.pixbuf.get_height() / self.pixbuf.get_width()
|
|
|
|
base = 250 + self.zoom * 50
|
|
|
|
if 'portrait' in self.orientation:
|
|
|
|
width = base / ratio
|
|
|
|
height = base
|
|
|
|
else:
|
|
|
|
width = base
|
|
|
|
height = base * ratio
|
|
|
|
pb = self.pixbuf.scale_simple(width, height, GdkPixbuf.InterpType.BILINEAR)
|
2019-07-19 08:41:17 +02:00
|
|
|
self.image_svg.set_from_pixbuf(pb)
|
2019-07-16 13:06:32 +02:00
|
|
|
|
2019-07-10 01:51:41 +02:00
|
|
|
@GObject.Property
|
|
|
|
def name(self):
|
|
|
|
return "drawing"
|
|
|
|
|
2019-07-19 08:41:17 +02:00
|
|
|
@GObject.Property
|
|
|
|
def zoom(self):
|
|
|
|
return self._zoom
|
|
|
|
|
|
|
|
@zoom.setter
|
|
|
|
def zoom(self, zoom):
|
|
|
|
if zoom == self._zoom:
|
|
|
|
return
|
|
|
|
self._zoom = zoom
|
|
|
|
self.redraw()
|
|
|
|
|
2019-07-10 01:51:41 +02:00
|
|
|
@Gtk.Template.Callback('_on_download_button_clicked')
|
|
|
|
def _on_download_button_clicked(self, button):
|
2019-07-16 13:48:37 +02:00
|
|
|
dialog = Gtk.FileChooserDialog(_('Please choose a file'),
|
2019-07-10 01:51:41 +02:00
|
|
|
None,
|
|
|
|
Gtk.FileChooserAction.SAVE,
|
|
|
|
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
|
|
|
|
Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
|
|
|
|
|
|
|
|
dialog.set_do_overwrite_confirmation(True)
|
2019-07-16 13:48:37 +02:00
|
|
|
# Translators: the default filename to save to
|
|
|
|
dialog.set_current_name(_('untitled.svg'))
|
2019-07-10 01:51:41 +02:00
|
|
|
|
|
|
|
filter_any = Gtk.FileFilter()
|
2019-07-16 13:48:37 +02:00
|
|
|
# Translators: filter name to show all/any files
|
|
|
|
filter_any.set_name(_('Any files'))
|
2019-07-10 01:51:41 +02:00
|
|
|
filter_any.add_pattern('*')
|
|
|
|
filter_svg = Gtk.FileFilter()
|
2019-07-16 13:48:37 +02:00
|
|
|
# Translators: filter to show svg files only
|
|
|
|
filter_svg.set_name(_('SVG files'))
|
2019-07-10 01:51:41 +02:00
|
|
|
filter_svg.add_pattern('*.svg')
|
|
|
|
dialog.add_filter(filter_svg)
|
|
|
|
dialog.add_filter(filter_any)
|
|
|
|
|
|
|
|
response = dialog.run()
|
|
|
|
if response == Gtk.ResponseType.OK:
|
|
|
|
import shutil
|
|
|
|
file = dialog.get_filename()
|
|
|
|
shutil.copyfile(self.svg.filename, file)
|
|
|
|
# FIXME: error handling
|
|
|
|
|
|
|
|
dialog.destroy()
|
2019-07-16 06:40:37 +02:00
|
|
|
|
|
|
|
@Gtk.Template.Callback('_on_delete_button_clicked')
|
|
|
|
def _on_delete_button_clicked(self, button):
|
2019-07-16 13:09:02 +02:00
|
|
|
Config.instance().delete_drawing(self.timestamp)
|
2019-07-16 13:17:09 +02:00
|
|
|
|
|
|
|
@Gtk.Template.Callback('_on_rotate_button_clicked')
|
|
|
|
def _on_rotate_button_clicked(self, button):
|
2019-07-16 13:29:03 +02:00
|
|
|
if button == self.btn_rotate_left:
|
|
|
|
advance = 1
|
|
|
|
else:
|
|
|
|
advance = 3
|
|
|
|
|
|
|
|
orientations = ['portrait', 'landscape', 'reverse-portrait', 'reverse-landscape'] * 3
|
|
|
|
o = orientations[orientations.index(self.orientation) + advance]
|
2019-07-16 13:17:09 +02:00
|
|
|
self.orientation = o
|
|
|
|
self.refresh()
|
2019-07-19 10:09:13 +02:00
|
|
|
|
|
|
|
@Gtk.Template.Callback('_on_enter')
|
|
|
|
def _on_enter(self, *args):
|
|
|
|
self.box_toolbar.set_opacity(100)
|
|
|
|
|
|
|
|
@Gtk.Template.Callback('_on_leave')
|
|
|
|
def _on_leave(self, drawing, event):
|
|
|
|
if event.detail == Gdk.NotifyType.INFERIOR:
|
|
|
|
return
|
|
|
|
self.box_toolbar.set_opacity(0)
|