summaryrefslogtreecommitdiff
path: root/lidt/plotout.py
blob: cb3b8389d153c7d90543338c15565694dbd639f5 (plain)
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
# LIDT
# Copyright (C) 2016  Karel Kočí
#
# 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 3 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.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import math
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk

grid = None
grid_bot = None
step = 50
dia = 300
selectx = None
selecty = None
grid_h = 1

def __grid_draw(wd, cr):
    w = wd.get_allocated_width()
    h = wd.get_allocated_height()
    if w < h:
        s = w
    else:
        s = h
    cr.set_source_rgb(0, 0, 0)
    h = s / 2 # half of the size of drawing area
    global grid_h
    grid_h = h
    st = (h / dia) * step

    cr.rectangle(0, 0, s, s)
    cr.fill()

    cr.move_to(0, h)
    cr.arc(h, h, h, 0, 2 * math.pi)
    cr.set_source_rgb(1, 1, 1)
    cr.fill()

    cr.set_source_rgb(0, 0, 0)
    i = 0
    while i < h:
        cr.move_to(h + i, 0)
        cr.line_to(h + i, s)
        cr.move_to(h - i, 0)
        cr.line_to(h - i, s)
        cr.move_to(0, h + i)
        cr.line_to(s, h + i)
        cr.move_to(0, h - i)
        cr.line_to(s, h - i)
        i = i + st
    cr.set_line_width(1)
    cr.stroke()

    if selectx is not None:
        slx = (h / dia) * selectx
        sly = (h / dia) * selecty
        cr.move_to(h +slx - 5, h + sly)
        cr.arc(h + slx, h + sly, 5, 0, 2 * math.pi)
        cr.set_source_rgb(0, 1, 0)
        cr.fill()

def __grid_button(widg, button):
    global selectx
    global selecty
    selectx = (button.x - grid_h) * (dia / grid_h)
    selecty = (button.y - grid_h) * (dia / grid_h)
    selectx = round(selectx / step) * step
    selecty = round(selecty / step) * step
    grid_bot.set_text(str(selectx) + ':' + str(selecty))
    grid.queue_draw()

def test():
    grid_render(None, 1, 1)
    grid.connect("delete-event", Gtk.main_quit)
    Gtk.main()


def grid_render(win, st, di):
    global grid
    if not grid is None:
        grid.destroy()
    global step
    step = st
    global dia
    dia = di
    global selectx
    selectx = None
    global selecty
    selecty = None
    grid = Gtk.Window()
    grid.set_title("LIDT: Grid")
    grid.set_attached_to(win)
    grid.set_destroy_with_parent(True)

    box = Gtk.Box(orientation = Gtk.Orientation.VERTICAL)
    grid.add(box)

    drawarea = Gtk.DrawingArea()
    drawarea.set_size_request(512, 512)
    drawarea.set_events(drawarea.get_events() | Gdk.EventMask.BUTTON_PRESS_MASK)
    drawarea.connect('draw', __grid_draw)
    drawarea.connect('button-press-event', __grid_button)
    box.pack_start(drawarea, True, True, 0)

    global grid_bot
    grid_bot = Gtk.Label('')
    box.pack_start(grid_bot, False, False, 0)

    grid.show_all()