taolib 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of taolib might be problematic. Click here for more details.
- tao/__init__.py +2 -0
- tao/abc/__init__.py +1 -0
- tao/abc/profile.py +0 -0
- tao/abc/trace_code.py +62 -0
- tao/abc/validate.py +77 -0
- tao/algebra/__init__.py +2 -0
- tao/analysis/__init__.py +1 -0
- tao/download/__init__.py +22 -0
- tao/drlhp/__init__.py +3 -0
- tao/gui/__init__.py +0 -0
- tao/gui/qt/__init__.py +21 -0
- tao/gui/qt/qt5.py +8 -0
- tao/gui/qt/qt6.py +8 -0
- tao/gui/qt/widgets/__init__.py +0 -0
- tao/gui/qt/widgets/rect.py +169 -0
- tao/gui/tkinterx/__init__.py +0 -0
- tao/gui/tkinterx/graph/__init__.py +0 -0
- tao/gui/tkinterx/graph/base.py +264 -0
- tao/gui/tkinterx/graph/selector.py +122 -0
- tao/gui/tkinterx/graph/shape.py +92 -0
- tao/gui/tkinterx/graph/window.py +87 -0
- tao/gui/tkinterx/tips.py +121 -0
- tao/gui/tkinterx/tooltip.py +44 -0
- tao/quantum/__init__.py +2 -0
- tao/quantum/graph/__init__.py +2 -0
- tao/quantum/graph/tree.py +9 -0
- tao/tasks/__init__.py +1 -0
- tao/tasks/utils.py +67 -0
- tao/te/__init__.py +0 -0
- tao/te/tensor.py +37 -0
- tao/testing/__init__.py +0 -0
- tao/timeitx.py +57 -0
- tao/tools/__init__.py +2 -0
- tao/tools/dashstyle/__init__.py +0 -0
- tao/tools/dashstyle/design/__init__.py +0 -0
- tao/tools/dashstyle/design/template.py +7 -0
- tao/tools/dashstyle/server.py +97 -0
- tao/tools/dashstyle/utils/__init__.py +0 -0
- tao/tools/dashstyle/utils/nav.py +77 -0
- tao/tools/sanstyle/__init__.py +0 -0
- tao/tools/sanstyle/display/__init__.py +0 -0
- tao/tools/sanstyle/display/_html.py +13 -0
- tao/tools/sanstyle/display/html.py +38 -0
- tao/tools/sanstyle/display/template.py +49 -0
- tao/tools/sanstyle/github/__init__.py +0 -0
- tao/tools/sanstyle/github/file.py +11 -0
- tao/tools/sanstyle/utils.py +8 -0
- tao/tools/write/__init__.py +31 -0
- tao/tools/write/_docs.py +91 -0
- tao/tools/write/doc.py +65 -0
- tao/tools/write/release.py +12 -0
- tao/utils/__init__.py +1 -0
- tao/utils/data.py +30 -0
- tao/utils/file.py +14 -0
- tao/utils/log_config.py +48 -0
- tao/utils/unzip.py +37 -0
- taolib-0.2.0.dist-info/METADATA +301 -0
- taolib-0.2.0.dist-info/RECORD +61 -0
- taolib-0.2.0.dist-info/WHEEL +4 -0
- taolib-0.2.0.dist-info/entry_points.txt +4 -0
- taolib-0.2.0.dist-info/licenses/LICENSE +201 -0
tao/__init__.py
ADDED
tao/abc/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# d2py 抽象基类
|
tao/abc/profile.py
ADDED
|
File without changes
|
tao/abc/trace_code.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
from functools import update_wrapper
|
|
2
|
+
from bytecode import Bytecode, Instr
|
|
3
|
+
import logging
|
|
4
|
+
# logging.basicConfig(level=logging.INFO)
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class LoggedAccess:
|
|
8
|
+
def __set_name__(self, owner, name):
|
|
9
|
+
self.public_name = name
|
|
10
|
+
self.private_name = '_' + name
|
|
11
|
+
|
|
12
|
+
def __get__(self, obj, objtype=None):
|
|
13
|
+
value = getattr(obj, self.private_name)
|
|
14
|
+
logging.debug('Accessing %r giving %r', self.public_name, value)
|
|
15
|
+
return value
|
|
16
|
+
|
|
17
|
+
def __set__(self, obj, value):
|
|
18
|
+
logging.debug('Updating %r to %r', self.public_name, value)
|
|
19
|
+
setattr(obj, self.private_name, value)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class trace:
|
|
23
|
+
cache = {}
|
|
24
|
+
is_activate = False
|
|
25
|
+
variable = LoggedAccess()
|
|
26
|
+
def __init__(self, variable="result"):
|
|
27
|
+
self.variable = variable
|
|
28
|
+
|
|
29
|
+
def __call__(self, func):
|
|
30
|
+
if not type(self).is_activate:
|
|
31
|
+
return func
|
|
32
|
+
func = self.transform(func)
|
|
33
|
+
def wrapper(*args, **kwargs):
|
|
34
|
+
res, values = func(*args, **kwargs)
|
|
35
|
+
type(self).cache[func.__qualname__].append((self.variable, values))
|
|
36
|
+
return res
|
|
37
|
+
return update_wrapper(wrapper, func)
|
|
38
|
+
|
|
39
|
+
def transform(self, func):
|
|
40
|
+
type(self).cache[func.__qualname__] = []
|
|
41
|
+
c = Bytecode.from_code(func.__code__)
|
|
42
|
+
extra_code = [
|
|
43
|
+
Instr('STORE_FAST', '_res'),
|
|
44
|
+
Instr('LOAD_FAST', self.variable),
|
|
45
|
+
Instr('STORE_FAST', '_value'),
|
|
46
|
+
Instr('LOAD_FAST', '_res'),
|
|
47
|
+
Instr('LOAD_FAST', '_value'),
|
|
48
|
+
Instr('BUILD_TUPLE', 2),
|
|
49
|
+
Instr('STORE_FAST', '_result_tuple'),
|
|
50
|
+
Instr('LOAD_FAST', '_result_tuple'),
|
|
51
|
+
]
|
|
52
|
+
c[-1:-1] = extra_code
|
|
53
|
+
func.__code__ = c.to_code()
|
|
54
|
+
return func
|
|
55
|
+
|
|
56
|
+
@classmethod
|
|
57
|
+
def clear(cls):
|
|
58
|
+
cls.cache.clear()
|
|
59
|
+
|
|
60
|
+
@classmethod
|
|
61
|
+
def activate(cls):
|
|
62
|
+
cls.is_activate = True
|
tao/abc/validate.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Validator(ABC):
|
|
5
|
+
def __set_name__(self, owner, name):
|
|
6
|
+
self.public_name = name
|
|
7
|
+
self.private_name = '_' + name
|
|
8
|
+
|
|
9
|
+
def __get__(self, obj, objtype=None):
|
|
10
|
+
return getattr(obj, self.private_name)
|
|
11
|
+
|
|
12
|
+
def __set__(self, obj, value):
|
|
13
|
+
self.validate(value)
|
|
14
|
+
setattr(obj, self.private_name, value)
|
|
15
|
+
|
|
16
|
+
@abstractmethod
|
|
17
|
+
def validate(self, value):
|
|
18
|
+
NotImplemented
|
|
19
|
+
|
|
20
|
+
class OneOf(Validator):
|
|
21
|
+
def __init__(self, *options):
|
|
22
|
+
self.options = set(options)
|
|
23
|
+
|
|
24
|
+
def validate(self, value):
|
|
25
|
+
if value not in self.options:
|
|
26
|
+
raise ValueError(f'Expected {value!r} to be one of {self.options!r}')
|
|
27
|
+
|
|
28
|
+
class Number(Validator):
|
|
29
|
+
def __init__(self, minvalue=None, maxvalue=None):
|
|
30
|
+
self.minvalue = minvalue
|
|
31
|
+
self.maxvalue = maxvalue
|
|
32
|
+
|
|
33
|
+
def validate(self, value):
|
|
34
|
+
if not isinstance(value, (int, float)):
|
|
35
|
+
raise TypeError(f'Expected {value!r} to be an int or float')
|
|
36
|
+
if self.minvalue is not None and value < self.minvalue:
|
|
37
|
+
raise ValueError(
|
|
38
|
+
f'Expected {value!r} to be at least {self.minvalue!r}'
|
|
39
|
+
)
|
|
40
|
+
if self.maxvalue is not None and value > self.maxvalue:
|
|
41
|
+
raise ValueError(
|
|
42
|
+
f'Expected {value!r} to be no more than {self.maxvalue!r}'
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
class String(Validator):
|
|
46
|
+
def __init__(self, minsize=None, maxsize=None, predicate=None):
|
|
47
|
+
self.minsize = minsize
|
|
48
|
+
self.maxsize = maxsize
|
|
49
|
+
self.predicate = predicate
|
|
50
|
+
|
|
51
|
+
def validate(self, value):
|
|
52
|
+
if not isinstance(value, str):
|
|
53
|
+
raise TypeError(f'Expected {value!r} to be an str')
|
|
54
|
+
if self.minsize is not None and len(value) < self.minsize:
|
|
55
|
+
raise ValueError(
|
|
56
|
+
f'Expected {value!r} to be no smaller than {self.minsize!r}'
|
|
57
|
+
)
|
|
58
|
+
if self.maxsize is not None and len(value) > self.maxsize:
|
|
59
|
+
raise ValueError(
|
|
60
|
+
f'Expected {value!r} to be no bigger than {self.maxsize!r}'
|
|
61
|
+
)
|
|
62
|
+
if self.predicate is not None and not self.predicate(value):
|
|
63
|
+
raise ValueError(
|
|
64
|
+
f'Expected {self.predicate} to be true for {value!r}'
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
if __name__ == "__main__":
|
|
69
|
+
class Component:
|
|
70
|
+
name = String(minsize=3, maxsize=10, predicate=str.isupper)
|
|
71
|
+
kind = OneOf('wood', 'metal', 'plastic')
|
|
72
|
+
quantity = Number(minvalue=0)
|
|
73
|
+
|
|
74
|
+
def __init__(self, name, kind, quantity):
|
|
75
|
+
self.name = name
|
|
76
|
+
self.kind = kind
|
|
77
|
+
self.quantity = quantity
|
tao/algebra/__init__.py
ADDED
tao/analysis/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Mathematical Modern Analysis.
|
tao/download/__init__.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""下载一些东西"""
|
|
2
|
+
from github import Github
|
|
3
|
+
from io import BytesIO
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def get_github_content(user_name, repo_name, filename):
|
|
7
|
+
""""""
|
|
8
|
+
g = Github()
|
|
9
|
+
user = g.get_user(user_name)
|
|
10
|
+
repo = user.get_repo(repo_name)
|
|
11
|
+
contents = repo.get_contents(filename)
|
|
12
|
+
return contents.decoded_content.decode()
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def iter_github_bytes(user_name, repo_name, root_dir):
|
|
16
|
+
""""""
|
|
17
|
+
g = Github()
|
|
18
|
+
user = g.get_user(user_name)
|
|
19
|
+
repo = user.get_repo(repo_name)
|
|
20
|
+
contents = repo.get_contents(root_dir)
|
|
21
|
+
for content in contents:
|
|
22
|
+
yield BytesIO(content.decoded_content)
|
tao/drlhp/__init__.py
ADDED
tao/gui/__init__.py
ADDED
|
File without changes
|
tao/gui/qt/__init__.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
try:
|
|
2
|
+
from .qt5 import QtCore, QtGui, QtWidgets, Signal, Slot, QtPrintSupport, QtQml
|
|
3
|
+
except:
|
|
4
|
+
from .qt6 import QtCore, QtGui, QtWidgets, Signal, Slot, QtPrintSupport, QtQml
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def run(window_type, *args, **kwargs):
|
|
8
|
+
'''运行 GUI'''
|
|
9
|
+
#import sys
|
|
10
|
+
# 实例化一个QApplication对象,用于GUI的初始化
|
|
11
|
+
app = QtWidgets.QApplication([]) # QtWidgets.QApplication(sys.argv)
|
|
12
|
+
# 实例化一个 Window 对象,用于定义一个图形窗口
|
|
13
|
+
window = window_type(*args, **kwargs)
|
|
14
|
+
# # 适配 Retina 显示屏(选写).
|
|
15
|
+
# app.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
|
|
16
|
+
# app.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
|
|
17
|
+
window.show() # 显示主窗口
|
|
18
|
+
app.exec_() # 当GUI产生退出信号时Python程序结束
|
|
19
|
+
# 利用内置模块sys的exit()方法侦听GUI的退出信号,以便关闭Python程序。
|
|
20
|
+
#sys.exit(app.exec_())
|
|
21
|
+
|
tao/gui/qt/qt5.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
try:
|
|
2
|
+
# PySide2
|
|
3
|
+
from PySide2 import QtGui, QtWidgets, QtCore, QtPrintSupport, QtQml
|
|
4
|
+
from PySide2.QtCore import Signal, Slot
|
|
5
|
+
except ImportError:
|
|
6
|
+
# PyQt5
|
|
7
|
+
from PyQt5 import QtGui, QtWidgets, QtCore, QtPrintSupport, QtQml
|
|
8
|
+
from PyQt5.QtCore import pyqtSignal as Signal, pyqtSlot as Slot
|
tao/gui/qt/qt6.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
try:
|
|
2
|
+
# PySide6
|
|
3
|
+
from PySide6 import QtGui, QtWidgets, QtCore, QtPrintSupport, QtQml
|
|
4
|
+
from PySide6.QtCore import Signal, Slot
|
|
5
|
+
except ImportError:
|
|
6
|
+
# PyQt6
|
|
7
|
+
from PyQt6 import QtGui, QtWidgets, QtCore, QtPrintSupport, QtQml
|
|
8
|
+
from PyQt6.QtCore import pyqtSignal as Signal, pyqtSlot as Slot
|
|
File without changes
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
from .. import QtWidgets, QtGui, QtCore
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
Qt = QtCore.Qt
|
|
5
|
+
QGraphicsItem = QtWidgets.QGraphicsItem
|
|
6
|
+
QColor = QtGui.QColor
|
|
7
|
+
QRectF = QtCore.QRectF
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class RectHandle(QtWidgets.QGraphicsRectItem):
|
|
11
|
+
# handles 按照顺时针排列
|
|
12
|
+
handle_names = ('left_top', 'middle_top', 'right_top', 'right_middle',
|
|
13
|
+
'right_bottom', 'middle_bottom', 'left_bottom', 'left_middle')
|
|
14
|
+
# 设定在控制点上的光标形状
|
|
15
|
+
handle_cursors = {
|
|
16
|
+
0: Qt.SizeFDiagCursor,
|
|
17
|
+
1: Qt.SizeVerCursor,
|
|
18
|
+
2: Qt.SizeBDiagCursor,
|
|
19
|
+
3: Qt.SizeHorCursor,
|
|
20
|
+
4: Qt.SizeFDiagCursor,
|
|
21
|
+
5: Qt.SizeVerCursor,
|
|
22
|
+
6: Qt.SizeBDiagCursor,
|
|
23
|
+
7: Qt.SizeHorCursor
|
|
24
|
+
}
|
|
25
|
+
offset = 6.0 # 外边界框相对于内边界框的偏移量,也是控制点的大小
|
|
26
|
+
#min_size = 8 * offset # 矩形框的最小尺寸
|
|
27
|
+
|
|
28
|
+
def update_handles_pos(self):
|
|
29
|
+
"""
|
|
30
|
+
更新控制点的位置
|
|
31
|
+
"""
|
|
32
|
+
o = self.offset # 偏置量
|
|
33
|
+
s = o*2 # handle 的大小
|
|
34
|
+
b = self.rect() # 获取内边框
|
|
35
|
+
x1, y1 = b.left(), b.top() # 左上角坐标
|
|
36
|
+
offset_x = b.width()/2
|
|
37
|
+
offset_y = b.height()/2
|
|
38
|
+
# 设置 handles 的位置
|
|
39
|
+
self.handles[0] = QRectF(x1-o, y1-o, s, s)
|
|
40
|
+
self.handles[1] = self.handles[0].adjusted(offset_x, 0, offset_x, 0)
|
|
41
|
+
self.handles[2] = self.handles[1].adjusted(offset_x, 0, offset_x, 0)
|
|
42
|
+
self.handles[3] = self.handles[2].adjusted(0, offset_y, 0, offset_y)
|
|
43
|
+
self.handles[4] = self.handles[3].adjusted(0, offset_y, 0, offset_y)
|
|
44
|
+
self.handles[5] = self.handles[4].adjusted(-offset_x, 0, -offset_x, 0)
|
|
45
|
+
self.handles[6] = self.handles[5].adjusted(-offset_x, 0, -offset_x, 0)
|
|
46
|
+
self.handles[7] = self.handles[6].adjusted(0, -offset_y, 0, -offset_y)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class RectItem(RectHandle):
|
|
50
|
+
|
|
51
|
+
def __init__(self, *args, **kwargs):
|
|
52
|
+
super().__init__(*args, **kwargs)
|
|
53
|
+
self.handles = {} # 控制点的字典
|
|
54
|
+
self.setAcceptHoverEvents(True) # 设定为接受 hover 事件
|
|
55
|
+
self.setFlags(QGraphicsItem.ItemIsSelectable | # 设定矩形框为可选择的
|
|
56
|
+
QGraphicsItem.ItemSendsGeometryChanges | # 追踪图元改变的信息
|
|
57
|
+
QGraphicsItem.ItemIsFocusable | # 可聚焦
|
|
58
|
+
QGraphicsItem.ItemIsMovable) # 可移动
|
|
59
|
+
self.update_handles_pos() # 初始化控制点
|
|
60
|
+
self.reset_Ui() # 初始化 UI 变量
|
|
61
|
+
|
|
62
|
+
def reset_Ui(self):
|
|
63
|
+
'''初始化 UI 变量'''
|
|
64
|
+
self.handleSelected = None # 被选中的控制点
|
|
65
|
+
self.mousePressPos = None # 鼠标按下的位置
|
|
66
|
+
#self.mousePressRect = None # 鼠标按下的位置所在的图元的外边界框
|
|
67
|
+
|
|
68
|
+
def boundingRect(self):
|
|
69
|
+
"""
|
|
70
|
+
限制图元的可视化区域,且防止出现图元移动留下残影的情况
|
|
71
|
+
"""
|
|
72
|
+
o = self.offset
|
|
73
|
+
# 添加一个间隔为 o 的外边框
|
|
74
|
+
return self.rect().adjusted(-o, -o, o, o)
|
|
75
|
+
|
|
76
|
+
def paint(self, painter, option, widget=None):
|
|
77
|
+
"""
|
|
78
|
+
Paint the node in the graphic view.
|
|
79
|
+
"""
|
|
80
|
+
painter.setBrush(QtGui.QBrush(QColor(255, 0, 0, 100)))
|
|
81
|
+
painter.setPen(QtGui.QPen(QColor(0, 0, 0), 0, Qt.SolidLine))
|
|
82
|
+
painter.drawRect(self.rect())
|
|
83
|
+
|
|
84
|
+
painter.setRenderHint(QtGui.QPainter.Antialiasing)
|
|
85
|
+
painter.setBrush(QtGui.QBrush(QColor(255, 255, 0, 200)))
|
|
86
|
+
painter.setPen(QtGui.QPen(QColor(0, 0, 0, 255), 0,
|
|
87
|
+
Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
|
|
88
|
+
|
|
89
|
+
for shape in self.handles.values():
|
|
90
|
+
if self.isSelected():
|
|
91
|
+
painter.drawEllipse(shape)
|
|
92
|
+
|
|
93
|
+
def handle_at(self, point):
|
|
94
|
+
"""
|
|
95
|
+
返回给定 point 下的控制点 handle
|
|
96
|
+
"""
|
|
97
|
+
for k, v, in self.handles.items():
|
|
98
|
+
if v.contains(point):
|
|
99
|
+
return k
|
|
100
|
+
return
|
|
101
|
+
|
|
102
|
+
def hoverMoveEvent(self, event):
|
|
103
|
+
"""
|
|
104
|
+
当鼠标移到该 item(未按下)上时执行。
|
|
105
|
+
"""
|
|
106
|
+
super().hoverMoveEvent(event)
|
|
107
|
+
handle = self.handle_at(event.pos())
|
|
108
|
+
cursor = self.handle_cursors[handle] if handle in self.handles else Qt.ArrowCursor
|
|
109
|
+
self.setCursor(cursor)
|
|
110
|
+
|
|
111
|
+
def hoverLeaveEvent(self, event):
|
|
112
|
+
"""
|
|
113
|
+
当鼠标离开该形状(未按下)上时执行。
|
|
114
|
+
"""
|
|
115
|
+
super().hoverLeaveEvent(event)
|
|
116
|
+
self.setCursor(Qt.ArrowCursor) # 设定鼠标光标形状
|
|
117
|
+
|
|
118
|
+
def mousePressEvent(self, event):
|
|
119
|
+
"""
|
|
120
|
+
当在 item 上按下鼠标时执行。
|
|
121
|
+
"""
|
|
122
|
+
super().mousePressEvent(event)
|
|
123
|
+
self.handleSelected = self.handle_at(event.pos())
|
|
124
|
+
if self.handleSelected in self.handles:
|
|
125
|
+
self.mousePressPos = event.pos()
|
|
126
|
+
|
|
127
|
+
def mouseReleaseEvent(self, event):
|
|
128
|
+
"""
|
|
129
|
+
Executed when the mouse is released from the item.
|
|
130
|
+
"""
|
|
131
|
+
super().mouseReleaseEvent(event)
|
|
132
|
+
self.update()
|
|
133
|
+
self.reset_Ui()
|
|
134
|
+
|
|
135
|
+
def mouseMoveEvent(self, event):
|
|
136
|
+
"""
|
|
137
|
+
Executed when the mouse is being moved over the item while being pressed.
|
|
138
|
+
"""
|
|
139
|
+
if self.handleSelected in self.handles:
|
|
140
|
+
self.interactiveResize(event.pos())
|
|
141
|
+
else:
|
|
142
|
+
super().mouseMoveEvent(event)
|
|
143
|
+
|
|
144
|
+
def interactiveResize(self, mousePos):
|
|
145
|
+
"""
|
|
146
|
+
Perform shape interactive resize.
|
|
147
|
+
"""
|
|
148
|
+
rect = self.rect()
|
|
149
|
+
self.prepareGeometryChange()
|
|
150
|
+
# movePos = mousePos - self.mousePressPos
|
|
151
|
+
# move_x, move_y = movePos.x(), movePos.y()
|
|
152
|
+
if self.handleSelected == 0:
|
|
153
|
+
rect.setTopLeft(mousePos)
|
|
154
|
+
elif self.handleSelected == 1:
|
|
155
|
+
rect.setTop(mousePos.y())
|
|
156
|
+
elif self.handleSelected == 2:
|
|
157
|
+
rect.setTopRight(mousePos)
|
|
158
|
+
elif self.handleSelected == 3:
|
|
159
|
+
rect.setRight(mousePos.x())
|
|
160
|
+
elif self.handleSelected == 4:
|
|
161
|
+
rect.setBottomRight(mousePos)
|
|
162
|
+
elif self.handleSelected == 5:
|
|
163
|
+
rect.setBottom(mousePos.y())
|
|
164
|
+
elif self.handleSelected == 6:
|
|
165
|
+
rect.setBottomLeft(mousePos)
|
|
166
|
+
elif self.handleSelected == 7:
|
|
167
|
+
rect.setLeft(mousePos.x())
|
|
168
|
+
self.setRect(rect)
|
|
169
|
+
self.update_handles_pos()
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
'''Some of the actions related to the graph. see https://www.jianshu.com/p/011a318ffd1e'''
|
|
2
|
+
from tkinter import Canvas as _Canvas, StringVar, ttk
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Canvas(_Canvas):
|
|
6
|
+
'''Graphic elements are composed of line(segment), rectangle, ellipse, and arc.'''
|
|
7
|
+
|
|
8
|
+
def __init__(self, master=None, cnf={}, **kw):
|
|
9
|
+
'''The base class of all graphics frames.
|
|
10
|
+
|
|
11
|
+
Args:
|
|
12
|
+
master: a widget of tkinter or tkinter.ttk.
|
|
13
|
+
'''
|
|
14
|
+
super().__init__(master, cnf, **kw)
|
|
15
|
+
|
|
16
|
+
def draw_graph(self, graph_type, direction, color='blue', width=1, tags=None, **kwargs):
|
|
17
|
+
'''Draw basic graphic elements.
|
|
18
|
+
|
|
19
|
+
Args:
|
|
20
|
+
direction: Specifies the orientation of the graphic element.
|
|
21
|
+
Union[int, float] -> (x_0,y_0,x_,y_1), (x_0, y_0) refers to the starting point of
|
|
22
|
+
the reference brush (i.e., the left mouse button is pressed), and (x_1, y_1) refers to
|
|
23
|
+
the end position of the reference brush (i.e., release the left mouse button).
|
|
24
|
+
Multipoint sequences are supported for 'line' and 'polygon',
|
|
25
|
+
for example ((x_0, y_0), (x_1, y_1), (x_2, y_2)).
|
|
26
|
+
|
|
27
|
+
graph_type: Types of graphic elements.
|
|
28
|
+
(str) 'rectangle', 'oval', 'line', 'arc'(That is, segment), 'polygon'.
|
|
29
|
+
Note that 'line' can no longer pass in the parameter 'fill', and
|
|
30
|
+
the remaining graph_type cannot pass in the parameter 'outline'.
|
|
31
|
+
|
|
32
|
+
color: The color of the graphic element.
|
|
33
|
+
|
|
34
|
+
width: The width of the graphic element.(That is, center fill)
|
|
35
|
+
|
|
36
|
+
tags: The tags of the graphic element.
|
|
37
|
+
It cannot be a pure number (such as 1 or '1' or '1 2 3'), it can be a list, a tuple,
|
|
38
|
+
or a string separated by a space(is converted to String tupers separated by a blank space).
|
|
39
|
+
The collection or dictionary is converted to a string.
|
|
40
|
+
|
|
41
|
+
Example:
|
|
42
|
+
['line', 'graph'], ('test', 'g'), 'line',
|
|
43
|
+
' line kind '(The blanks at both ends are automatically removed), and so on.
|
|
44
|
+
|
|
45
|
+
style: Style of the arc in {'arc', 'chord', or 'pieslice'}.
|
|
46
|
+
|
|
47
|
+
return: Unique identifier solely for graphic elements.
|
|
48
|
+
'''
|
|
49
|
+
if tags is None:
|
|
50
|
+
if graph_type in ('rectangle', 'oval', 'line', 'arc'):
|
|
51
|
+
tags = f"graph {graph_type}"
|
|
52
|
+
else:
|
|
53
|
+
tags = 'graph'
|
|
54
|
+
|
|
55
|
+
com_kw = {'width': width, 'tags': tags}
|
|
56
|
+
kw = {**com_kw, 'outline': color}
|
|
57
|
+
line_kw = {**com_kw, 'fill': color}
|
|
58
|
+
if graph_type == 'line':
|
|
59
|
+
kwargs.update(line_kw)
|
|
60
|
+
else:
|
|
61
|
+
kwargs.update(kw)
|
|
62
|
+
func = eval(f"self.create_{graph_type}")
|
|
63
|
+
graph_id = func(*direction, **kwargs)
|
|
64
|
+
return graph_id
|
|
65
|
+
|
|
66
|
+
class Drawing(Canvas):
|
|
67
|
+
'''Create graphic elements (graph) including rectangular boxes (which can be square points), ovals (circular points), and segments.
|
|
68
|
+
|
|
69
|
+
Press the left mouse button to start painting, release the left mouse button for the end of the painting.
|
|
70
|
+
'''
|
|
71
|
+
|
|
72
|
+
def __init__(self, master=None, selector=None, cnf={}, **kw):
|
|
73
|
+
'''Click the left mouse button to start painting, release the left mouse button to complete the painting.
|
|
74
|
+
|
|
75
|
+
Args:
|
|
76
|
+
selector: The graphics selector, which is an instance of Selector.
|
|
77
|
+
'''
|
|
78
|
+
super().__init__(master, cnf, **kw)
|
|
79
|
+
self.selector = selector
|
|
80
|
+
|
|
81
|
+
self._init_params()
|
|
82
|
+
self._draw_bind()
|
|
83
|
+
|
|
84
|
+
def _draw_bind(self):
|
|
85
|
+
self.bind("<1>", self.update_xy)
|
|
86
|
+
self.bind("<ButtonRelease-1>", self.draw)
|
|
87
|
+
|
|
88
|
+
def _init_params(self):
|
|
89
|
+
self.x = self.y = 0
|
|
90
|
+
|
|
91
|
+
def update_xy(self, event):
|
|
92
|
+
'''Press the left mouse button to record the coordinates of the left mouse button'''
|
|
93
|
+
self.x = self.canvasx(event.x)
|
|
94
|
+
self.y = self.canvasy(event.y)
|
|
95
|
+
|
|
96
|
+
def get_bbox(self, event):
|
|
97
|
+
x0, y0 = self.x, self.y # The upper-left coordinates of the graph
|
|
98
|
+
self.update_xy(event)
|
|
99
|
+
x1, y1 = self.x, self.y # Lower-right coordinates of the graph
|
|
100
|
+
bbox = x0, y0, x1, y1
|
|
101
|
+
return bbox
|
|
102
|
+
|
|
103
|
+
def draw(self, event):
|
|
104
|
+
'''Release the left mouse button to finish painting.'''
|
|
105
|
+
self.configure(cursor="arrow")
|
|
106
|
+
bbox = self.get_bbox(event)
|
|
107
|
+
self.create_graph(bbox)
|
|
108
|
+
|
|
109
|
+
def create_graph(self, bbox):
|
|
110
|
+
'''Create a graphic.
|
|
111
|
+
|
|
112
|
+
Args:
|
|
113
|
+
bbox: (x0,y0,x1,y1)
|
|
114
|
+
'''
|
|
115
|
+
x0, y0, x1, y1 = bbox
|
|
116
|
+
cond1 = x0 == x1 and y0 == y1 and 'point' not in self.selector.graph_type
|
|
117
|
+
cond2 = 'point' in self.selector.graph_type and (x0 != x1 or y0 != y1)
|
|
118
|
+
if cond1 or cond2:
|
|
119
|
+
return
|
|
120
|
+
else:
|
|
121
|
+
self.draw_graph(self.selector.graph_type.split('_')
|
|
122
|
+
[0], bbox, self.selector.color)
|
|
123
|
+
|
|
124
|
+
class TrajectoryDrawing(Drawing):
|
|
125
|
+
'''Draw based on the mouse's trajectory.
|
|
126
|
+
|
|
127
|
+
Click the left mouse button to start painting, move the
|
|
128
|
+
left mouse button 'after_time' after the completion of painting.
|
|
129
|
+
'''
|
|
130
|
+
|
|
131
|
+
def __init__(self, master, selector, after_time=1000, cnf={}, **kw):
|
|
132
|
+
super().__init__(master, selector, cnf, **kw)
|
|
133
|
+
self.after_time = after_time
|
|
134
|
+
self.bind("<1>", self.update_xy)
|
|
135
|
+
self.bind("<ButtonRelease-1>", self.update_xy)
|
|
136
|
+
self.bind("<Button1-Motion>", self.draw)
|
|
137
|
+
|
|
138
|
+
def draw(self, event):
|
|
139
|
+
'''Release the left mouse button to finish painting.'''
|
|
140
|
+
self.configure(cursor="arrow")
|
|
141
|
+
self.after(self.after_time)
|
|
142
|
+
bbox = self.get_bbox(event)
|
|
143
|
+
self.create_graph(bbox)
|
|
144
|
+
|
|
145
|
+
class Graph(Drawing):
|
|
146
|
+
'''Graph is a drawing tool for manipulating graphic objects.'''
|
|
147
|
+
def __init__(self, master, selector, cnf={}, **kw):
|
|
148
|
+
super().__init__(master, selector, cnf, **kw)
|
|
149
|
+
self.selected_tags = None
|
|
150
|
+
self.edit_var = StringVar()
|
|
151
|
+
|
|
152
|
+
@property
|
|
153
|
+
def tags_dict(self):
|
|
154
|
+
return {
|
|
155
|
+
'Single element': 'current',
|
|
156
|
+
'All elements': 'all',
|
|
157
|
+
'All graphic elements': 'graph',
|
|
158
|
+
'⬜': 'rectangle',
|
|
159
|
+
'⚪': 'oval',
|
|
160
|
+
'⸺': 'line',
|
|
161
|
+
'⯀': 'rectangle_point',
|
|
162
|
+
'●': 'oval_point'
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
def move_strides(self, event):
|
|
166
|
+
x, y = self.x, self.y
|
|
167
|
+
self.update_xy(event)
|
|
168
|
+
x_move = self.x - x
|
|
169
|
+
y_move = self.y - y
|
|
170
|
+
return x_move, y_move
|
|
171
|
+
|
|
172
|
+
def move_graph(self, event):
|
|
173
|
+
x_move, y_move = self.move_strides(event)
|
|
174
|
+
self.move(self.selected_tags, x_move, y_move)
|
|
175
|
+
|
|
176
|
+
def delete_graph(self, event):
|
|
177
|
+
self.delete(self.selected_tags)
|
|
178
|
+
|
|
179
|
+
def select_graph(self, event, edit_option):
|
|
180
|
+
self.configure(cursor="target")
|
|
181
|
+
self.update_xy(event)
|
|
182
|
+
tags = self.tags_dict[edit_option]
|
|
183
|
+
if tags == 'current':
|
|
184
|
+
self.selected_tags = self.find_withtag(tags)
|
|
185
|
+
else:
|
|
186
|
+
self.selected_tags = tags
|
|
187
|
+
|
|
188
|
+
def bind_graph(self):
|
|
189
|
+
self.unbind('<ButtonRelease-1>')
|
|
190
|
+
self.unbind('<1>')
|
|
191
|
+
edit = self.edit_var.get()
|
|
192
|
+
if edit == 'drawing':
|
|
193
|
+
self._draw_bind() # reset bind
|
|
194
|
+
elif 'move' in edit:
|
|
195
|
+
self.bind('<1>', lambda event: self.select_graph(
|
|
196
|
+
event, edit.split('/')[1]))
|
|
197
|
+
self.bind('<ButtonRelease-1>', self.move_graph)
|
|
198
|
+
elif 'delete' in edit:
|
|
199
|
+
self.bind('<1>', lambda event: self.select_graph(
|
|
200
|
+
event, edit.split('/')[1]))
|
|
201
|
+
self.bind('<ButtonRelease-1>', self.delete_graph)
|
|
202
|
+
|
|
203
|
+
class GraphScrollable(Graph):
|
|
204
|
+
'''Pin the picture and label it on it.'''
|
|
205
|
+
|
|
206
|
+
def __init__(self, master, selector, cnf={}, **kw):
|
|
207
|
+
super().__init__(master, selector, cnf, **kw)
|
|
208
|
+
self.coord_var = StringVar()
|
|
209
|
+
self.coord_label = ttk.Label(self, textvariable=self.coord_var)
|
|
210
|
+
self._set_scroll()
|
|
211
|
+
self._scroll_command()
|
|
212
|
+
self.configure(xscrollcommand=self.scroll_x.set,
|
|
213
|
+
yscrollcommand=self.scroll_y.set)
|
|
214
|
+
self.bind("<3>", self.mouse_motion)
|
|
215
|
+
self.bind("<Configure>", self.resize)
|
|
216
|
+
self.update_idletasks()
|
|
217
|
+
#self.minsize(self.winfo_width(), self.winfo_height())
|
|
218
|
+
self.scroll_x.pack(side='top', fill='x')
|
|
219
|
+
self.pack(side='left', expand='yes', fill='both')
|
|
220
|
+
self.scroll_y.pack(side='right', fill='y')
|
|
221
|
+
self.coord_label.pack(side='bottom', fill='x')
|
|
222
|
+
|
|
223
|
+
def draw(self, event):
|
|
224
|
+
'''Release the left mouse button to finish painting.'''
|
|
225
|
+
self.configure(cursor="arrow")
|
|
226
|
+
bbox = self.get_bbox(event)
|
|
227
|
+
self.create_graph(bbox)
|
|
228
|
+
self.coord_var.set(f'coordinate: {bbox[:2]} to {bbox[2:]}')
|
|
229
|
+
|
|
230
|
+
def select_graph(self, event, edit_option):
|
|
231
|
+
self.configure(cursor="target")
|
|
232
|
+
self.update_xy(event)
|
|
233
|
+
tags = self.tags_dict[edit_option]
|
|
234
|
+
image_tags = self.find_withtag('image')
|
|
235
|
+
selected_tags = self.find_withtag(tags)
|
|
236
|
+
self.selected_tags = set(selected_tags) - set(image_tags)
|
|
237
|
+
|
|
238
|
+
def move_graph(self, event):
|
|
239
|
+
x_move, y_move = self.move_strides(event)
|
|
240
|
+
for tag in self.selected_tags:
|
|
241
|
+
self.move(tag, x_move, y_move)
|
|
242
|
+
|
|
243
|
+
def delete_graph(self, event):
|
|
244
|
+
for tag in self.selected_tags:
|
|
245
|
+
self.delete(tag)
|
|
246
|
+
|
|
247
|
+
def _set_scroll(self):
|
|
248
|
+
self.scroll_x = ttk.Scrollbar(self, orient='horizontal')
|
|
249
|
+
self.scroll_y = ttk.Scrollbar(self, orient='vertical')
|
|
250
|
+
|
|
251
|
+
def mouse_motion(self, event):
|
|
252
|
+
x = event.x
|
|
253
|
+
y = event.y
|
|
254
|
+
text = f"coordinate: ({x}, {y})|| {self.canvasx(x), self.canvasy(y)}"
|
|
255
|
+
self.coord_var.set(text)
|
|
256
|
+
|
|
257
|
+
def _scroll_command(self):
|
|
258
|
+
self.scroll_x['command'] = self.xview
|
|
259
|
+
self.scroll_y['command'] = self.yview
|
|
260
|
+
|
|
261
|
+
def resize(self, event):
|
|
262
|
+
region = self.bbox('all')
|
|
263
|
+
self.configure(scrollregion=region)
|
|
264
|
+
|