codis 1.1.1__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.
- codis-1.1.1.dist-info/METADATA +194 -0
- codis-1.1.1.dist-info/RECORD +329 -0
- codis-1.1.1.dist-info/WHEEL +5 -0
- codis-1.1.1.dist-info/entry_points.txt +5 -0
- codis-1.1.1.dist-info/licenses/LICENSE +21 -0
- codis-1.1.1.dist-info/top_level.txt +2 -0
- run.py +190 -0
- src/__init__.py +1 -0
- src/__main__.py +81 -0
- src/_app_build.py +1507 -0
- src/_app_editor.py +1274 -0
- src/_app_kui.py +825 -0
- src/_app_lsp.py +625 -0
- src/_app_recovery.py +189 -0
- src/_app_settings.py +1385 -0
- src/_app_tools.py +2471 -0
- src/_app_webpreview.py +126 -0
- src/_font_safety.py +127 -0
- src/_shells.py +172 -0
- src/app.py +1488 -0
- src/chat/__init__.py +1 -0
- src/chat/adapters/__init__.py +33 -0
- src/chat/adapters/base.py +209 -0
- src/chat/adapters/discord.py +190 -0
- src/chat/adapters/http.py +78 -0
- src/chat/adapters/irc.py +360 -0
- src/chat/adapters/matrix.py +219 -0
- src/chat/adapters/mattermost.py +178 -0
- src/chat/adapters/mqtt.py +165 -0
- src/chat/adapters/slack.py +204 -0
- src/chat/adapters/telegram.py +117 -0
- src/chat/adapters/ws.py +154 -0
- src/chat/adapters/xmpp.py +337 -0
- src/chat/history.py +76 -0
- src/chat/models.py +207 -0
- src/chat/probe.py +79 -0
- src/chat/session.py +446 -0
- src/chat/util.py +125 -0
- src/collab/__init__.py +1 -0
- src/collab/net.py +411 -0
- src/collab/ot.py +311 -0
- src/collab/session.py +141 -0
- src/collab/tk_adapter.py +134 -0
- src/config.py +335 -0
- src/constants.py +2552 -0
- src/dep_catalog.py +446 -0
- src/diff/__init__.py +1 -0
- src/diff/engine.py +247 -0
- src/dpi.py +30 -0
- src/editor/__init__.py +1 -0
- src/editor/_pane_lsp.py +2937 -0
- src/editor/_pane_utils.py +171 -0
- src/editor/autocomplete.py +607 -0
- src/editor/code_folding.py +836 -0
- src/editor/color_preview.py +382 -0
- src/editor/complexity_lens.py +281 -0
- src/editor/diff_pane.py +623 -0
- src/editor/file_watcher.py +144 -0
- src/editor/focus_block.py +236 -0
- src/editor/heatmap.py +141 -0
- src/editor/inlay_hints.py +366 -0
- src/editor/inline_blame.py +429 -0
- src/editor/kdocs.py +3376 -0
- src/editor/large_file.py +488 -0
- src/editor/ligatures.py +180 -0
- src/editor/line_numbers.py +598 -0
- src/editor/minimap.py +691 -0
- src/editor/multi_cursor.py +269 -0
- src/editor/pane.py +2082 -0
- src/editor/rhythm.py +206 -0
- src/editor/save_hooks.py +43 -0
- src/editor/smart_selection.py +425 -0
- src/editor/smart_tips.py +719 -0
- src/editor/snippets.py +411 -0
- src/editor/sticky_note_gutter.py +410 -0
- src/editor/sticky_scroll.py +385 -0
- src/editor/suggest.py +464 -0
- src/editor/text_transforms.py +310 -0
- src/editor/vim_mode.py +1074 -0
- src/events/__init__.py +84 -0
- src/events/bus.py +427 -0
- src/events/correlator.py +244 -0
- src/events/kinds.py +125 -0
- src/events/pipeline.py +264 -0
- src/events/stream.py +416 -0
- src/events/window.py +204 -0
- src/extcompat/__init__.py +19 -0
- src/extcompat/code/__init__.py +13 -0
- src/extcompat/code/consent.py +162 -0
- src/extcompat/code/host.py +353 -0
- src/extcompat/code/hosts/py_host.py +763 -0
- src/extcompat/code/locate.py +105 -0
- src/extcompat/code/manager.py +325 -0
- src/extcompat/code/permissions.py +154 -0
- src/extcompat/code/sandbox.py +60 -0
- src/extcompat/colors.py +131 -0
- src/extcompat/common.py +184 -0
- src/extcompat/cson.py +269 -0
- src/extcompat/formats/__init__.py +22 -0
- src/extcompat/formats/_tm.py +189 -0
- src/extcompat/formats/_util.py +31 -0
- src/extcompat/formats/base16.py +66 -0
- src/extcompat/formats/brackets.py +172 -0
- src/extcompat/formats/eclipse.py +193 -0
- src/extcompat/formats/emacs.py +361 -0
- src/extcompat/formats/geany.py +147 -0
- src/extcompat/formats/generic.py +184 -0
- src/extcompat/formats/gtksource.py +117 -0
- src/extcompat/formats/helix.py +91 -0
- src/extcompat/formats/jetbrains.py +343 -0
- src/extcompat/formats/kate.py +130 -0
- src/extcompat/formats/micro.py +131 -0
- src/extcompat/formats/nano.py +111 -0
- src/extcompat/formats/notepadpp.py +140 -0
- src/extcompat/formats/pulsar.py +165 -0
- src/extcompat/formats/pyides.py +163 -0
- src/extcompat/formats/sublime.py +340 -0
- src/extcompat/jsonc.py +65 -0
- src/extcompat/lessish.py +128 -0
- src/extcompat/manager.py +122 -0
- src/extcompat/market/__init__.py +63 -0
- src/extcompat/market/sources.py +211 -0
- src/extcompat/model.py +88 -0
- src/extcompat/net.py +228 -0
- src/extcompat/openvsx.py +63 -0
- src/extcompat/readers.py +362 -0
- src/extcompat/registry.py +73 -0
- src/extcompat/safexml.py +63 -0
- src/extcompat/sexp.py +187 -0
- src/extcompat/sources.py +105 -0
- src/extcompat/store.py +276 -0
- src/extcompat/textmate.py +214 -0
- src/extcompat/themes.py +487 -0
- src/extcompat/yamlite.py +294 -0
- src/fileio.py +94 -0
- src/fs_ops.py +106 -0
- src/i18n.py +233 -0
- src/ia/__init__.py +1 -0
- src/ia/actions.py +295 -0
- src/ia/approval_gate.py +672 -0
- src/ia/checkpoints.py +308 -0
- src/ia/context_builder.py +921 -0
- src/ia/dialdiff.py +408 -0
- src/ia/iaspace.py +2078 -0
- src/ia/pair.py +296 -0
- src/ia/providers.py +954 -0
- src/ia/session.py +335 -0
- src/ia/toolbox.py +2524 -0
- src/launcher_actions.py +204 -0
- src/lint/__init__.py +1 -0
- src/lint/manager.py +1058 -0
- src/locales/.translate_manifest.json +64170 -0
- src/locales/de.json +4936 -0
- src/locales/en.json +4936 -0
- src/locales/es.json +4936 -0
- src/locales/fr.json +4936 -0
- src/locales/it.json +4936 -0
- src/locales/ja.json +4936 -0
- src/locales/ko.json +4936 -0
- src/locales/nl.json +4936 -0
- src/locales/pl.json +4936 -0
- src/locales/pt-BR.json +4936 -0
- src/locales/ru.json +4936 -0
- src/locales/tr.json +4936 -0
- src/locales/uk.json +4936 -0
- src/locales/zh-CN.json +4936 -0
- src/lsp/__init__.py +26 -0
- src/lsp/client.py +2300 -0
- src/lsp/dap_client.py +480 -0
- src/lsp/edits.py +84 -0
- src/lsp/framing.py +73 -0
- src/lsp/protocol.py +931 -0
- src/main.py +260 -0
- src/memtrim.py +91 -0
- src/plugins/__init__.py +1 -0
- src/plugins/api.py +1761 -0
- src/plugins/loader.py +571 -0
- src/plugins/process_runner.py +519 -0
- src/plugins/registry.py +342 -0
- src/plugins/safety.py +367 -0
- src/plugins/startup.py +195 -0
- src/plugins/store.py +1178 -0
- src/proc_registry.py +278 -0
- src/pyexec.py +394 -0
- src/recovery.py +259 -0
- src/stall_trace.py +92 -0
- src/standalone.py +1070 -0
- src/startup_profile.py +35 -0
- src/syntax/__init__.py +1 -0
- src/syntax/highlighter.py +776 -0
- src/syntax/patterns.py +584 -0
- src/system_profile.py +430 -0
- src/terminal/backends.py +658 -0
- src/terminal/internal.py +346 -0
- src/terminal/keys.py +138 -0
- src/terminal/palette.py +67 -0
- src/terminal/session.py +283 -0
- src/terminal/shells.py +79 -0
- src/terminal/view.py +489 -0
- src/terminal/vt.py +870 -0
- src/textfmt.py +22 -0
- src/theme/__init__.py +1 -0
- src/theme/base.py +1281 -0
- src/theme/colors.py +49 -0
- src/theme/manager.py +602 -0
- src/theme/utils.py +312 -0
- src/toolbox.py +329 -0
- src/trash_core.py +531 -0
- src/ui/__init__.py +1 -0
- src/ui/_perf_utils.py +285 -0
- src/ui/_persistence_utils.py +93 -0
- src/ui/accessibility.py +843 -0
- src/ui/activity_bar.py +860 -0
- src/ui/app_embed.py +434 -0
- src/ui/archive_panel.py +608 -0
- src/ui/bookmarks.py +314 -0
- src/ui/breadcrumb.py +383 -0
- src/ui/calculator_panel.py +982 -0
- src/ui/calendar_panel.py +1218 -0
- src/ui/chat_panel.py +1271 -0
- src/ui/chat_profile_dialog.py +278 -0
- src/ui/chat_render.py +128 -0
- src/ui/clock_panel.py +810 -0
- src/ui/code_outline.py +364 -0
- src/ui/code_runner.py +729 -0
- src/ui/code_stats.py +546 -0
- src/ui/collab_panel.py +1009 -0
- src/ui/color_picker.py +547 -0
- src/ui/command_palette.py +452 -0
- src/ui/cursor_manager.py +750 -0
- src/ui/custom_keywords_panel.py +413 -0
- src/ui/debug_panel.py +1037 -0
- src/ui/dependency_graph.py +692 -0
- src/ui/diff_panel.py +381 -0
- src/ui/docs_panel.py +514 -0
- src/ui/doom_embed.py +349 -0
- src/ui/dropdown.py +321 -0
- src/ui/easter_eggs.py +979 -0
- src/ui/env_manager.py +666 -0
- src/ui/extcode_bridge.py +215 -0
- src/ui/extcode_dialogs.py +412 -0
- src/ui/extcompat_dialog.py +583 -0
- src/ui/file_timeline.py +319 -0
- src/ui/file_tree.py +1208 -0
- src/ui/find_in_files.py +1355 -0
- src/ui/flowchart_panel.py +727 -0
- src/ui/formatter_panel.py +490 -0
- src/ui/git_auth.py +763 -0
- src/ui/git_clone_dialog.py +487 -0
- src/ui/git_panel.py +1483 -0
- src/ui/grab_safe.py +84 -0
- src/ui/internal_python_dialog.py +98 -0
- src/ui/k_ui/__init__.py +1305 -0
- src/ui/k_ui/_bgrenderer.py +317 -0
- src/ui/k_ui/_colors.py +213 -0
- src/ui/k_ui/_dock.py +545 -0
- src/ui/k_ui/_easter.py +290 -0
- src/ui/k_ui/_engine.py +5448 -0
- src/ui/k_ui/_perf.py +333 -0
- src/ui/k_ui/_tkutil.py +7 -0
- src/ui/k_ui/_tooltip.py +365 -0
- src/ui/k_ui/_widgets.py +1204 -0
- src/ui/k_ui/_world.py +837 -0
- src/ui/key_remapper.py +856 -0
- src/ui/launcher.py +1252 -0
- src/ui/lazy_panel.py +73 -0
- src/ui/macro_recorder.py +610 -0
- src/ui/menu_bar.py +898 -0
- src/ui/output_panel.py +321 -0
- src/ui/paint_canvas.py +1000 -0
- src/ui/paint_engine.py +535 -0
- src/ui/paint_panel.py +888 -0
- src/ui/panel_detach.py +112 -0
- src/ui/performance_panel.py +861 -0
- src/ui/pip_manager.py +1758 -0
- src/ui/plugin_panel.py +1586 -0
- src/ui/pomodoro_panel.py +447 -0
- src/ui/problems.py +566 -0
- src/ui/process_manager.py +1657 -0
- src/ui/project_templates.py +775 -0
- src/ui/recovery_dialog.py +122 -0
- src/ui/regex_playground.py +304 -0
- src/ui/remote_adapters.py +1398 -0
- src/ui/remote_browser.py +1224 -0
- src/ui/remote_profile_dialog.py +342 -0
- src/ui/render_panel.py +1293 -0
- src/ui/rest_client.py +549 -0
- src/ui/scratchpad.py +548 -0
- src/ui/search_bar.py +1274 -0
- src/ui/settings_editor.py +3349 -0
- src/ui/settings_window.py +1554 -0
- src/ui/snippets_panel.py +597 -0
- src/ui/sql_studio.py +1815 -0
- src/ui/status_bar.py +438 -0
- src/ui/tab_bar.py +654 -0
- src/ui/task_manager.py +1773 -0
- src/ui/terminal.py +525 -0
- src/ui/theme_preview_panel.py +851 -0
- src/ui/thread_safe_tk.py +152 -0
- src/ui/time_tracker.py +574 -0
- src/ui/tkbind.py +43 -0
- src/ui/todo_panel.py +358 -0
- src/ui/toolbox_panels.py +658 -0
- src/ui/trash_manager.py +657 -0
- src/ui/ui_scale.py +294 -0
- src/ui/ux_enhancements.py +41 -0
- src/ui/web_preview.py +397 -0
- src/ui/webdev.py +630 -0
- src/ui/welcome.py +567 -0
- src/ui/window_embed.py +1500 -0
- src/ui/wizard.py +1550 -0
- src/ui/workspace_switcher.py +418 -0
- src/ui/workspace_trust.py +370 -0
- src/ui/zen_audio.py +354 -0
- src/ui/zen_media.py +2271 -0
- src/ui/zen_media_client.py +303 -0
- src/ui/zen_media_host.py +329 -0
- src/ui/zen_player.py +907 -0
- src/ui/zen_player_widgets.py +700 -0
- src/updater/__init__.py +11 -0
- src/updater/assets.py +145 -0
- src/updater/backup.py +101 -0
- src/updater/channel.py +221 -0
- src/updater/dialog.py +492 -0
- src/updater/integrity.py +111 -0
- src/updater/manager.py +727 -0
- src/updater/result.py +114 -0
- src/venv_runtime.py +531 -0
- src/workspace_state.py +25 -0
src/ui/paint_canvas.py
ADDED
|
@@ -0,0 +1,1000 @@
|
|
|
1
|
+
"""Canvas de desenho do Paint (Tk) sobre ``paint_engine``.
|
|
2
|
+
|
|
3
|
+
Como o lag foi eliminado:
|
|
4
|
+
|
|
5
|
+
* A tela é UM ``PhotoImage`` do tamanho do viewport. Zoom/pan renderizam só o viewport
|
|
6
|
+
(``O(viewport)``, não ``O(imagem x zoom)``); desenhar atualiza só o retângulo sujo,
|
|
7
|
+
copiado para dentro da foto pelo próprio Tk (``image copy -to``).
|
|
8
|
+
* Eventos de mouse só atualizam o modelo (máscara/imagem) e agendam UM ``flush`` em
|
|
9
|
+
``after_idle``: se o mouse gera 5 eventos antes do Tk desenhar, desenha-se uma vez.
|
|
10
|
+
* A grade de pixels, o xadrez de transparência e as pré-visualizações não criam
|
|
11
|
+
itens de canvas por pixel.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import contextlib
|
|
17
|
+
import logging
|
|
18
|
+
import math
|
|
19
|
+
import os
|
|
20
|
+
import tempfile
|
|
21
|
+
import tkinter as tk
|
|
22
|
+
from collections.abc import Callable
|
|
23
|
+
from pathlib import Path
|
|
24
|
+
from tkinter import messagebox, simpledialog
|
|
25
|
+
from typing import Any
|
|
26
|
+
|
|
27
|
+
from src.i18n import _t
|
|
28
|
+
from src.theme.colors import hex_to_rgb, rgb_to_hex
|
|
29
|
+
from src.ui import paint_engine as pe
|
|
30
|
+
|
|
31
|
+
logger = logging.getLogger("Codis.Paint")
|
|
32
|
+
|
|
33
|
+
try:
|
|
34
|
+
from PIL import Image, ImageDraw, ImageFont, ImageOps, ImageTk # type: ignore
|
|
35
|
+
|
|
36
|
+
HAS_PIL = True
|
|
37
|
+
except ImportError: # pragma: no cover
|
|
38
|
+
HAS_PIL = False
|
|
39
|
+
|
|
40
|
+
CANVAS_W, CANVAS_H = 800, 600
|
|
41
|
+
MIN_ZOOM, MAX_ZOOM = 0.0625, 32.0
|
|
42
|
+
ZOOM_STEPS = [0.0625, 0.125, 0.1667, 0.25, 0.333, 0.5, 0.667, 1.0, 1.5, 2.0, 3.0, 4.0, 6.0, 8.0, 12.0, 16.0, 24.0, 32.0]
|
|
43
|
+
STROKE_TOOLS = ("brush", "pixel", "eraser")
|
|
44
|
+
RING_TOOLS = ("brush", "pixel", "eraser", "spray")
|
|
45
|
+
VIEW_BG = "#404040"
|
|
46
|
+
KEEP_VISIBLE = 64
|
|
47
|
+
|
|
48
|
+
_FONT_CANDIDATES = ("DejaVuSans.ttf", "Arial.ttf", "arial.ttf", "LiberationSans-Regular.ttf", "NotoSans-Regular.ttf",
|
|
49
|
+
"Helvetica.ttc", "Segoe UI.ttf", "segoeui.ttf")
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def load_font(size: int) -> Any:
|
|
53
|
+
for name in _FONT_CANDIDATES:
|
|
54
|
+
with contextlib.suppress(OSError):
|
|
55
|
+
return ImageFont.truetype(name, size)
|
|
56
|
+
with contextlib.suppress(Exception):
|
|
57
|
+
return ImageFont.load_default(size=size)
|
|
58
|
+
return ImageFont.load_default()
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def hex_bg(h: str) -> tuple[int, int, int]:
|
|
62
|
+
return hex_to_rgb(h)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class PaintCanvas(tk.Canvas):
|
|
66
|
+
"""Canvas de desenho principal."""
|
|
67
|
+
|
|
68
|
+
def __init__(self, parent: tk.Widget, tmgr: Any, status_cb: Callable[[str], None] | None = None,
|
|
69
|
+
zoom_cb: Callable[[float], None] | None = None, **kw: Any) -> None:
|
|
70
|
+
self._xsc = kw.pop("xscrollcommand", None)
|
|
71
|
+
self._ysc = kw.pop("yscrollcommand", None)
|
|
72
|
+
super().__init__(parent, bg=VIEW_BG, cursor="crosshair", highlightthickness=0, **kw)
|
|
73
|
+
self._tmgr = tmgr
|
|
74
|
+
self._status_cb = status_cb
|
|
75
|
+
self._zoom_cb = zoom_cb
|
|
76
|
+
self.on_change: Callable[[], None] | None = None
|
|
77
|
+
self.on_color_pick: Callable[[str, bool], None] | None = None
|
|
78
|
+
self.on_tool_request: Callable[[str], None] | None = None
|
|
79
|
+
|
|
80
|
+
self._tool = "brush"
|
|
81
|
+
self._color, self._color2 = "#000000", "#ffffff"
|
|
82
|
+
self._size, self._opacity = 4, 255
|
|
83
|
+
self._fill_shapes = False
|
|
84
|
+
self._tolerance = 30
|
|
85
|
+
self._spray_density = 20
|
|
86
|
+
|
|
87
|
+
self.doc = pe.Document(Image.new("RGBA", (CANVAS_W, CANVAS_H), (255, 255, 255, 255))) if HAS_PIL else None
|
|
88
|
+
self._zoom = 1.0
|
|
89
|
+
self._pan = [20, 20]
|
|
90
|
+
self._pan_drag: tuple[int, int, int, int] | None = None
|
|
91
|
+
self._space = False
|
|
92
|
+
|
|
93
|
+
self._photo: Any = None
|
|
94
|
+
self._photo_size = (0, 0)
|
|
95
|
+
self._img_item: int | None = None
|
|
96
|
+
self._flush_id: str | None = None
|
|
97
|
+
self._centered = False
|
|
98
|
+
|
|
99
|
+
self._mask: Any = None
|
|
100
|
+
self._stroke: dict[str, Any] | None = None
|
|
101
|
+
self._start = (0.0, 0.0)
|
|
102
|
+
self._last: tuple[float, float] | None = None
|
|
103
|
+
self._poly: list[tuple[int, int]] = []
|
|
104
|
+
self._preview: int | None = None
|
|
105
|
+
self._ring: int | None = None
|
|
106
|
+
self._last_status: tuple[int, int] | None = None
|
|
107
|
+
|
|
108
|
+
self._selection: pe.Box | None = None
|
|
109
|
+
self._float: dict[str, Any] | None = None
|
|
110
|
+
self._float_photo: Any = None
|
|
111
|
+
self._float_item: int | None = None
|
|
112
|
+
self._sel_drag: tuple[int, int] | None = None
|
|
113
|
+
self._clip: Any = None
|
|
114
|
+
|
|
115
|
+
self._bind_events()
|
|
116
|
+
self.bind("<Configure>", self._on_configure)
|
|
117
|
+
|
|
118
|
+
@property
|
|
119
|
+
def image(self) -> Any:
|
|
120
|
+
return self.doc.image if self.doc else None
|
|
121
|
+
|
|
122
|
+
@property
|
|
123
|
+
def modified(self) -> bool:
|
|
124
|
+
return bool(self.doc and self.doc.modified)
|
|
125
|
+
|
|
126
|
+
@property
|
|
127
|
+
def tool(self) -> str:
|
|
128
|
+
return self._tool
|
|
129
|
+
|
|
130
|
+
@tool.setter
|
|
131
|
+
def tool(self, v: str) -> None:
|
|
132
|
+
if v != self._tool:
|
|
133
|
+
self.commit_float()
|
|
134
|
+
if self._tool == "polygon" and v != "polygon":
|
|
135
|
+
self._clear_poly()
|
|
136
|
+
self._tool = v
|
|
137
|
+
self._hide_ring()
|
|
138
|
+
|
|
139
|
+
@property
|
|
140
|
+
def color(self) -> str:
|
|
141
|
+
return self._color
|
|
142
|
+
|
|
143
|
+
@color.setter
|
|
144
|
+
def color(self, v: str) -> None:
|
|
145
|
+
self._color = v
|
|
146
|
+
|
|
147
|
+
@property
|
|
148
|
+
def color2(self) -> str:
|
|
149
|
+
return self._color2
|
|
150
|
+
|
|
151
|
+
@color2.setter
|
|
152
|
+
def color2(self, v: str) -> None:
|
|
153
|
+
self._color2 = v
|
|
154
|
+
|
|
155
|
+
@property
|
|
156
|
+
def size(self) -> int:
|
|
157
|
+
return self._size
|
|
158
|
+
|
|
159
|
+
@size.setter
|
|
160
|
+
def size(self, v: int) -> None:
|
|
161
|
+
self._size = max(1, min(200, int(v)))
|
|
162
|
+
|
|
163
|
+
@property
|
|
164
|
+
def opacity(self) -> int:
|
|
165
|
+
return self._opacity
|
|
166
|
+
|
|
167
|
+
@opacity.setter
|
|
168
|
+
def opacity(self, v: int) -> None:
|
|
169
|
+
self._opacity = max(1, min(255, int(v)))
|
|
170
|
+
|
|
171
|
+
@property
|
|
172
|
+
def tolerance(self) -> int:
|
|
173
|
+
return self._tolerance
|
|
174
|
+
|
|
175
|
+
@tolerance.setter
|
|
176
|
+
def tolerance(self, v: int) -> None:
|
|
177
|
+
self._tolerance = max(0, min(255, int(v)))
|
|
178
|
+
|
|
179
|
+
@property
|
|
180
|
+
def fill_shapes(self) -> bool:
|
|
181
|
+
return self._fill_shapes
|
|
182
|
+
|
|
183
|
+
@fill_shapes.setter
|
|
184
|
+
def fill_shapes(self, v: bool) -> None:
|
|
185
|
+
self._fill_shapes = bool(v)
|
|
186
|
+
|
|
187
|
+
@property
|
|
188
|
+
def zoom(self) -> float:
|
|
189
|
+
return self._zoom
|
|
190
|
+
|
|
191
|
+
def _view_size(self) -> tuple[int, int]:
|
|
192
|
+
return max(1, self.winfo_width()), max(1, self.winfo_height())
|
|
193
|
+
|
|
194
|
+
def _clamp_pan(self) -> None:
|
|
195
|
+
if not self.doc:
|
|
196
|
+
return
|
|
197
|
+
vw, vh = self._view_size()
|
|
198
|
+
iw, ih = self.doc.size
|
|
199
|
+
dw, dh = iw * self._zoom, ih * self._zoom
|
|
200
|
+
self._pan[0] = int(max(KEEP_VISIBLE - dw, min(vw - KEEP_VISIBLE, self._pan[0])))
|
|
201
|
+
self._pan[1] = int(max(KEEP_VISIBLE - dh, min(vh - KEEP_VISIBLE, self._pan[1])))
|
|
202
|
+
|
|
203
|
+
def to_image(self, ex: float, ey: float) -> tuple[float, float]:
|
|
204
|
+
return (ex - self._pan[0]) / self._zoom, (ey - self._pan[1]) / self._zoom
|
|
205
|
+
|
|
206
|
+
def to_screen(self, ix: float, iy: float) -> tuple[float, float]:
|
|
207
|
+
return self._pan[0] + ix * self._zoom, self._pan[1] + iy * self._zoom
|
|
208
|
+
|
|
209
|
+
def _clamped(self, ex: int, ey: int) -> tuple[int, int]:
|
|
210
|
+
fx, fy = self.to_image(ex, ey)
|
|
211
|
+
w, h = self.doc.size
|
|
212
|
+
return max(0, min(int(math.floor(fx)), w - 1)), max(0, min(int(math.floor(fy)), h - 1))
|
|
213
|
+
|
|
214
|
+
def _on_configure(self, _e: tk.Event) -> None:
|
|
215
|
+
if not self.doc:
|
|
216
|
+
return
|
|
217
|
+
if not self._centered and self.winfo_width() > 8:
|
|
218
|
+
self._centered = True
|
|
219
|
+
self._center_image(fit_if_large=True)
|
|
220
|
+
return
|
|
221
|
+
self._clamp_pan()
|
|
222
|
+
self._render_full()
|
|
223
|
+
|
|
224
|
+
def _ensure_photo(self) -> None:
|
|
225
|
+
vw, vh = self._view_size()
|
|
226
|
+
if self._photo is None or self._photo_size != (vw, vh):
|
|
227
|
+
self._photo = ImageTk.PhotoImage(Image.new("RGB", (vw, vh), hex_bg(VIEW_BG)))
|
|
228
|
+
self._photo_size = (vw, vh)
|
|
229
|
+
if self._img_item is None:
|
|
230
|
+
self._img_item = self.create_image(0, 0, anchor="nw", image=self._photo, tags="pil_bg")
|
|
231
|
+
self.tag_lower("pil_bg")
|
|
232
|
+
else:
|
|
233
|
+
self.itemconfigure(self._img_item, image=self._photo)
|
|
234
|
+
|
|
235
|
+
def _render_full(self) -> None:
|
|
236
|
+
if not self.doc:
|
|
237
|
+
return
|
|
238
|
+
self._ensure_photo()
|
|
239
|
+
vw, vh = self._photo_size
|
|
240
|
+
img = pe.render_rect(self.doc.image, self._zoom, tuple(self._pan), (0, 0, vw, vh), hex_bg(VIEW_BG),
|
|
241
|
+
self.doc.maybe_transparent)
|
|
242
|
+
self._photo.paste(img)
|
|
243
|
+
self._update_scrollbars()
|
|
244
|
+
self._redraw_overlays()
|
|
245
|
+
|
|
246
|
+
def _blit(self, img: Any, x: int, y: int) -> None:
|
|
247
|
+
small = ImageTk.PhotoImage(img)
|
|
248
|
+
self.tk.call(str(self._photo), "copy", str(small), "-to", x, y)
|
|
249
|
+
|
|
250
|
+
def schedule_flush(self) -> None:
|
|
251
|
+
if self._flush_id is None:
|
|
252
|
+
self._flush_id = self.after_idle(self.flush)
|
|
253
|
+
|
|
254
|
+
def flush(self) -> None:
|
|
255
|
+
"""Desenha o que mudou (chamado em idle; pode ser chamado direto)."""
|
|
256
|
+
if self._flush_id is not None:
|
|
257
|
+
with contextlib.suppress(tk.TclError):
|
|
258
|
+
self.after_cancel(self._flush_id)
|
|
259
|
+
self._flush_id = None
|
|
260
|
+
if not self.doc:
|
|
261
|
+
return
|
|
262
|
+
dirty, everything = self.doc.take_dirty()
|
|
263
|
+
if everything:
|
|
264
|
+
self._render_full()
|
|
265
|
+
self._notify_change()
|
|
266
|
+
return
|
|
267
|
+
if dirty is None:
|
|
268
|
+
return
|
|
269
|
+
self._ensure_photo()
|
|
270
|
+
vw, vh = self._photo_size
|
|
271
|
+
sx0, sy0, sx1, sy1 = pe.image_box_to_screen(dirty, self._zoom, tuple(self._pan))
|
|
272
|
+
sx0, sy0, sx1, sy1 = max(0, sx0), max(0, sy0), min(vw, sx1), min(vh, sy1)
|
|
273
|
+
if sx1 > sx0 and sy1 > sy0:
|
|
274
|
+
img = pe.render_rect(self.doc.image, self._zoom, tuple(self._pan), (sx0, sy0, sx1, sy1),
|
|
275
|
+
hex_bg(VIEW_BG), self.doc.maybe_transparent)
|
|
276
|
+
self._blit(img, sx0, sy0)
|
|
277
|
+
self._notify_change()
|
|
278
|
+
|
|
279
|
+
def _notify_change(self) -> None:
|
|
280
|
+
if self.on_change:
|
|
281
|
+
with contextlib.suppress(Exception):
|
|
282
|
+
self.on_change()
|
|
283
|
+
|
|
284
|
+
def _scroll_extent(self) -> tuple[float, float, float, float]:
|
|
285
|
+
vw, vh = self._view_size()
|
|
286
|
+
iw, ih = self.doc.size
|
|
287
|
+
mx, my = vw * 0.5, vh * 0.5
|
|
288
|
+
return iw * self._zoom + 2 * mx, ih * self._zoom + 2 * my, mx, my
|
|
289
|
+
|
|
290
|
+
def _update_scrollbars(self) -> None:
|
|
291
|
+
if not self.doc:
|
|
292
|
+
return
|
|
293
|
+
vw, vh = self._view_size()
|
|
294
|
+
tw, th, mx, my = self._scroll_extent()
|
|
295
|
+
ox, oy = mx - self._pan[0], my - self._pan[1]
|
|
296
|
+
if self._xsc:
|
|
297
|
+
self._xsc(max(0.0, ox / tw), min(1.0, (ox + vw) / tw))
|
|
298
|
+
if self._ysc:
|
|
299
|
+
self._ysc(max(0.0, oy / th), min(1.0, (oy + vh) / th))
|
|
300
|
+
|
|
301
|
+
def xview(self, *args: Any) -> Any:
|
|
302
|
+
self._scroll_cmd(0, args)
|
|
303
|
+
|
|
304
|
+
def yview(self, *args: Any) -> Any:
|
|
305
|
+
self._scroll_cmd(1, args)
|
|
306
|
+
|
|
307
|
+
def _scroll_cmd(self, axis: int, args: tuple) -> None:
|
|
308
|
+
if not self.doc or not args:
|
|
309
|
+
return
|
|
310
|
+
vw, vh = self._view_size()
|
|
311
|
+
tw, th, mx, my = self._scroll_extent()
|
|
312
|
+
total, margin, view = (tw, mx, vw) if axis == 0 else (th, my, vh)
|
|
313
|
+
off = margin - self._pan[axis]
|
|
314
|
+
if args[0] == "moveto":
|
|
315
|
+
off = float(args[1]) * total
|
|
316
|
+
elif args[0] == "scroll":
|
|
317
|
+
n = int(args[1])
|
|
318
|
+
off += n * (view * 0.9 if args[2] == "pages" else 40)
|
|
319
|
+
self._pan[axis] = int(margin - off)
|
|
320
|
+
self._clamp_pan()
|
|
321
|
+
self._render_full()
|
|
322
|
+
|
|
323
|
+
def pan_by(self, dx: int, dy: int) -> None:
|
|
324
|
+
self._pan[0] += dx
|
|
325
|
+
self._pan[1] += dy
|
|
326
|
+
self._clamp_pan()
|
|
327
|
+
self._render_full()
|
|
328
|
+
|
|
329
|
+
def _set_zoom_at(self, nz: float, ex: float, ey: float) -> None:
|
|
330
|
+
nz = max(MIN_ZOOM, min(MAX_ZOOM, nz))
|
|
331
|
+
if nz == self._zoom:
|
|
332
|
+
return
|
|
333
|
+
k = nz / self._zoom
|
|
334
|
+
self._pan[0] = int(ex - (ex - self._pan[0]) * k)
|
|
335
|
+
self._pan[1] = int(ey - (ey - self._pan[1]) * k)
|
|
336
|
+
self._zoom = nz
|
|
337
|
+
self._clamp_pan()
|
|
338
|
+
self._render_full()
|
|
339
|
+
if self._zoom_cb:
|
|
340
|
+
self._zoom_cb(nz)
|
|
341
|
+
|
|
342
|
+
def _zoom_at(self, ex: int, ey: int, direction: int) -> None:
|
|
343
|
+
old = self._zoom
|
|
344
|
+
if direction > 0:
|
|
345
|
+
nz = next((z for z in ZOOM_STEPS if z > old * 1.001), MAX_ZOOM)
|
|
346
|
+
else:
|
|
347
|
+
nz = next((z for z in reversed(ZOOM_STEPS) if z < old * 0.999), MIN_ZOOM)
|
|
348
|
+
self._set_zoom_at(nz, ex, ey)
|
|
349
|
+
|
|
350
|
+
def set_zoom(self, z: float) -> None:
|
|
351
|
+
vw, vh = self._view_size()
|
|
352
|
+
self._set_zoom_at(z, vw / 2, vh / 2)
|
|
353
|
+
|
|
354
|
+
def zoom_fit(self, margin: int = 24) -> float:
|
|
355
|
+
vw, vh = self._view_size()
|
|
356
|
+
iw, ih = self.doc.size
|
|
357
|
+
z = max(MIN_ZOOM, min(MAX_ZOOM, (vw - 2 * margin) / iw, (vh - 2 * margin) / ih))
|
|
358
|
+
self._zoom = z
|
|
359
|
+
self._center_image()
|
|
360
|
+
if self._zoom_cb:
|
|
361
|
+
self._zoom_cb(z)
|
|
362
|
+
return z
|
|
363
|
+
|
|
364
|
+
def _center_image(self, fit_if_large: bool = False) -> None:
|
|
365
|
+
if not self.doc:
|
|
366
|
+
return
|
|
367
|
+
vw, vh = self._view_size()
|
|
368
|
+
iw, ih = self.doc.size
|
|
369
|
+
if fit_if_large and (iw * self._zoom > vw - 24 or ih * self._zoom > vh - 24):
|
|
370
|
+
self._zoom = max(MIN_ZOOM, min(1.0, (vw - 48) / iw, (vh - 48) / ih))
|
|
371
|
+
if self._zoom_cb:
|
|
372
|
+
self._zoom_cb(self._zoom)
|
|
373
|
+
self._pan = [int((vw - iw * self._zoom) // 2), int((vh - ih * self._zoom) // 2)]
|
|
374
|
+
self._clamp_pan()
|
|
375
|
+
self._render_full()
|
|
376
|
+
|
|
377
|
+
def _bind_events(self) -> None:
|
|
378
|
+
b = self.bind
|
|
379
|
+
b("<ButtonPress-1>", lambda e: self._on_press(e, True))
|
|
380
|
+
b("<B1-Motion>", lambda e: self._on_drag(e, True))
|
|
381
|
+
b("<ButtonRelease-1>", lambda e: self._on_release(e, True))
|
|
382
|
+
b("<ButtonPress-3>", lambda e: self._on_press(e, False))
|
|
383
|
+
b("<B3-Motion>", lambda e: self._on_drag(e, False))
|
|
384
|
+
b("<ButtonRelease-3>", lambda e: self._on_release(e, False))
|
|
385
|
+
b("<ButtonPress-2>", self._on_mid_press)
|
|
386
|
+
b("<B2-Motion>", self._on_pan_drag)
|
|
387
|
+
b("<ButtonRelease-2>", lambda _e: setattr(self, "_pan_drag", None))
|
|
388
|
+
b("<Control-z>", lambda _e: self.undo() or "break")
|
|
389
|
+
b("<Control-Z>", lambda _e: self.redo() or "break")
|
|
390
|
+
b("<Control-y>", lambda _e: self.redo() or "break")
|
|
391
|
+
b("<Control-Y>", lambda _e: self.redo() or "break")
|
|
392
|
+
b("<Control-c>", lambda _e: self.copy_selection() or "break")
|
|
393
|
+
b("<Control-x>", lambda _e: self.cut_selection() or "break")
|
|
394
|
+
b("<Control-v>", lambda _e: self.paste() or "break")
|
|
395
|
+
b("<Control-a>", lambda _e: self.select_all() or "break")
|
|
396
|
+
b("<Delete>", lambda _e: self.delete_selection())
|
|
397
|
+
b("<Escape>", lambda _e: self.cancel_operation())
|
|
398
|
+
b("<Return>", lambda _e: self._on_return())
|
|
399
|
+
b("<MouseWheel>", self._on_wheel)
|
|
400
|
+
b("<Button-4>", self._on_wheel)
|
|
401
|
+
b("<Button-5>", self._on_wheel)
|
|
402
|
+
b("<Double-Button-1>", self._on_double_click)
|
|
403
|
+
b("<Motion>", self._on_motion)
|
|
404
|
+
b("<Enter>", lambda _e: self.focus_set())
|
|
405
|
+
b("<Leave>", lambda _e: self._hide_ring())
|
|
406
|
+
b("<KeyPress-space>", lambda _e: self._set_space(True))
|
|
407
|
+
b("<KeyRelease-space>", lambda _e: self._set_space(False))
|
|
408
|
+
b("<plus>", lambda _e: self._zoom_at(*[v // 2 for v in self._view_size()], 1))
|
|
409
|
+
b("<minus>", lambda _e: self._zoom_at(*[v // 2 for v in self._view_size()], -1))
|
|
410
|
+
|
|
411
|
+
def _set_space(self, v: bool) -> None:
|
|
412
|
+
self._space = v
|
|
413
|
+
self.config(cursor="fleur" if v else "crosshair")
|
|
414
|
+
|
|
415
|
+
def _on_return(self) -> None:
|
|
416
|
+
if self._float:
|
|
417
|
+
self.commit_float()
|
|
418
|
+
elif self._tool == "polygon":
|
|
419
|
+
self._finalize_polygon()
|
|
420
|
+
|
|
421
|
+
def _on_wheel(self, ev: tk.Event) -> None:
|
|
422
|
+
up = ev.num == 4 or (getattr(ev, "delta", 0) or 0) > 0
|
|
423
|
+
if ev.state & 0x4:
|
|
424
|
+
self._zoom_at(ev.x, ev.y, 1 if up else -1)
|
|
425
|
+
elif ev.state & 0x1:
|
|
426
|
+
self.pan_by(60 if up else -60, 0)
|
|
427
|
+
else:
|
|
428
|
+
self.pan_by(0, 60 if up else -60)
|
|
429
|
+
|
|
430
|
+
def _on_mid_press(self, ev: tk.Event) -> None:
|
|
431
|
+
self._pan_drag = (ev.x, ev.y, self._pan[0], self._pan[1])
|
|
432
|
+
|
|
433
|
+
def _on_pan_drag(self, ev: tk.Event) -> None:
|
|
434
|
+
if self._pan_drag:
|
|
435
|
+
x0, y0, px, py = self._pan_drag
|
|
436
|
+
self._pan = [px + ev.x - x0, py + ev.y - y0]
|
|
437
|
+
self._clamp_pan()
|
|
438
|
+
self._schedule_full()
|
|
439
|
+
|
|
440
|
+
def _schedule_full(self) -> None:
|
|
441
|
+
if self.doc:
|
|
442
|
+
self.doc.dirty_all = True
|
|
443
|
+
self.schedule_flush()
|
|
444
|
+
|
|
445
|
+
def _on_press(self, ev: tk.Event, primary: bool) -> None:
|
|
446
|
+
self.focus_set()
|
|
447
|
+
if not self.doc:
|
|
448
|
+
return
|
|
449
|
+
if primary and (self._space or self._tool == "pan"):
|
|
450
|
+
self._pan_drag = (ev.x, ev.y, self._pan[0], self._pan[1])
|
|
451
|
+
self.config(cursor="fleur")
|
|
452
|
+
return
|
|
453
|
+
t = self._tool
|
|
454
|
+
color = self._color if primary else self._color2
|
|
455
|
+
if t == "eraser":
|
|
456
|
+
color = self._color2 if primary else self._color
|
|
457
|
+
fx, fy = self.to_image(ev.x, ev.y)
|
|
458
|
+
cx, cy = self._clamped(ev.x, ev.y)
|
|
459
|
+
self._start = (cx, cy)
|
|
460
|
+
self._last = (fx, fy)
|
|
461
|
+
if t == "eyedrop":
|
|
462
|
+
self._eyedrop(cx, cy, primary)
|
|
463
|
+
elif t == "fill":
|
|
464
|
+
self._fill(cx, cy, color, ev)
|
|
465
|
+
elif t == "text":
|
|
466
|
+
self._insert_text(cx, cy, color)
|
|
467
|
+
elif t == "polygon":
|
|
468
|
+
self._poly.append((cx, cy))
|
|
469
|
+
self._update_poly_preview(cx, cy)
|
|
470
|
+
elif t == "select":
|
|
471
|
+
self._select_press(cx, cy)
|
|
472
|
+
elif t in STROKE_TOOLS or t == "spray":
|
|
473
|
+
self._begin_stroke(t, color, fx, fy)
|
|
474
|
+
|
|
475
|
+
def _on_drag(self, ev: tk.Event, primary: bool) -> None:
|
|
476
|
+
if not self.doc:
|
|
477
|
+
return
|
|
478
|
+
if self._pan_drag:
|
|
479
|
+
self._on_pan_drag(ev)
|
|
480
|
+
return
|
|
481
|
+
t = self._tool
|
|
482
|
+
fx, fy = self.to_image(ev.x, ev.y)
|
|
483
|
+
cx, cy = self._clamped(ev.x, ev.y)
|
|
484
|
+
if self._stroke is not None:
|
|
485
|
+
self._stroke_to(fx, fy)
|
|
486
|
+
elif t in ("line", "rect", "ellipse"):
|
|
487
|
+
self._update_shape_preview(cx, cy, primary, shift=bool(ev.state & 0x1))
|
|
488
|
+
elif t == "polygon":
|
|
489
|
+
self._update_poly_preview(cx, cy)
|
|
490
|
+
elif t == "select":
|
|
491
|
+
self._select_drag(cx, cy)
|
|
492
|
+
self._move_ring(ev.x, ev.y)
|
|
493
|
+
|
|
494
|
+
def _on_release(self, ev: tk.Event, primary: bool) -> None:
|
|
495
|
+
if not self.doc:
|
|
496
|
+
return
|
|
497
|
+
if self._pan_drag:
|
|
498
|
+
self._pan_drag = None
|
|
499
|
+
self.config(cursor="fleur" if self._space else "crosshair")
|
|
500
|
+
return
|
|
501
|
+
t = self._tool
|
|
502
|
+
cx, cy = self._clamped(ev.x, ev.y)
|
|
503
|
+
if self._stroke is not None:
|
|
504
|
+
self._end_stroke()
|
|
505
|
+
elif t in ("line", "rect", "ellipse"):
|
|
506
|
+
self._finalize_shape(cx, cy, self._color if primary else self._color2, bool(ev.state & 0x1))
|
|
507
|
+
elif t == "select":
|
|
508
|
+
self._select_release(cx, cy)
|
|
509
|
+
self._last = None
|
|
510
|
+
|
|
511
|
+
def _on_double_click(self, _e: tk.Event) -> None:
|
|
512
|
+
if self._tool == "polygon":
|
|
513
|
+
self._finalize_polygon()
|
|
514
|
+
|
|
515
|
+
def _on_motion(self, ev: tk.Event) -> None:
|
|
516
|
+
if not self.doc:
|
|
517
|
+
return
|
|
518
|
+
self._move_ring(ev.x, ev.y)
|
|
519
|
+
ix, iy = (int(math.floor(v)) for v in self.to_image(ev.x, ev.y))
|
|
520
|
+
if (ix, iy) == self._last_status or not self._status_cb:
|
|
521
|
+
return
|
|
522
|
+
self._last_status = (ix, iy)
|
|
523
|
+
w, h = self.doc.size
|
|
524
|
+
if 0 <= ix < w and 0 <= iy < h:
|
|
525
|
+
r, g, b, a = self.doc.image.getpixel((ix, iy))
|
|
526
|
+
self._status_cb(f"({ix}, {iy}) {rgb_to_hex(r, g, b)} α={a}")
|
|
527
|
+
else:
|
|
528
|
+
self._status_cb(f"({ix}, {iy}) - fora da imagem")
|
|
529
|
+
|
|
530
|
+
def _move_ring(self, ex: int, ey: int) -> None:
|
|
531
|
+
if self._tool not in RING_TOOLS or self._pan_drag or self._space:
|
|
532
|
+
self._hide_ring()
|
|
533
|
+
return
|
|
534
|
+
r = max(1.0, self._size * self._zoom / 2 * (2 if self._tool == "spray" else 1))
|
|
535
|
+
if self._ring is None:
|
|
536
|
+
self._ring = self.create_oval(0, 0, 1, 1, outline="#ffffff", width=1, tags="ring")
|
|
537
|
+
self._ring2 = self.create_oval(0, 0, 1, 1, outline="#000000", width=1, tags="ring")
|
|
538
|
+
for rid, d in ((self._ring, 0), (self._ring2, 1)):
|
|
539
|
+
self.coords(rid, ex - r - d, ey - r - d, ex + r + d, ey + r + d)
|
|
540
|
+
self.tag_raise("ring")
|
|
541
|
+
|
|
542
|
+
def _hide_ring(self) -> None:
|
|
543
|
+
if self._ring is not None:
|
|
544
|
+
self.delete("ring")
|
|
545
|
+
self._ring = None
|
|
546
|
+
|
|
547
|
+
def _begin_stroke(self, tool: str, color: str, fx: float, fy: float) -> None:
|
|
548
|
+
self.commit_float()
|
|
549
|
+
self.doc.begin()
|
|
550
|
+
aa = tool != "pixel"
|
|
551
|
+
self._stroke = {"tool": tool, "rgb": hex_to_rgb(color), "aa": aa}
|
|
552
|
+
if tool in STROKE_TOOLS:
|
|
553
|
+
self._mask = pe.new_stroke_mask(self.doc)
|
|
554
|
+
self._stroke_to(fx, fy, first=True)
|
|
555
|
+
|
|
556
|
+
def _stroke_to(self, fx: float, fy: float, first: bool = False) -> None:
|
|
557
|
+
s = self._stroke
|
|
558
|
+
lx, ly = self._last if (self._last and not first) else (fx, fy)
|
|
559
|
+
if s["tool"] == "pixel":
|
|
560
|
+
fx, fy, lx, ly = math.floor(fx), math.floor(fy), math.floor(lx), math.floor(ly)
|
|
561
|
+
if s["tool"] == "spray":
|
|
562
|
+
pe.spray(self.doc, int(lx), int(ly), int(fx), int(fy), s["rgb"], self._opacity, self._size,
|
|
563
|
+
self._spray_density)
|
|
564
|
+
else:
|
|
565
|
+
box = pe.stamp_segment(self._mask, lx, ly, fx, fy, self._size, s["aa"])
|
|
566
|
+
if box:
|
|
567
|
+
pe.apply_stroke_region(self.doc, self._mask, box, s["rgb"], self._opacity)
|
|
568
|
+
self._last = (fx, fy)
|
|
569
|
+
self.schedule_flush()
|
|
570
|
+
|
|
571
|
+
def _end_stroke(self) -> None:
|
|
572
|
+
self._stroke = None
|
|
573
|
+
self._mask = None
|
|
574
|
+
self.doc.commit()
|
|
575
|
+
self.flush()
|
|
576
|
+
|
|
577
|
+
def _fill(self, x: int, y: int, color: str, ev: tk.Event | None = None) -> None:
|
|
578
|
+
self.commit_float()
|
|
579
|
+
self.config(cursor="watch")
|
|
580
|
+
self.update_idletasks()
|
|
581
|
+
try:
|
|
582
|
+
self.doc.begin()
|
|
583
|
+
global_mode = bool(ev is not None and ev.state & 0x4)
|
|
584
|
+
pe.flood_fill(self.doc, x, y, hex_to_rgb(color), self._opacity, self._tolerance, not global_mode)
|
|
585
|
+
self.doc.commit()
|
|
586
|
+
finally:
|
|
587
|
+
self.config(cursor="fleur" if self._space else "crosshair")
|
|
588
|
+
self.flush()
|
|
589
|
+
|
|
590
|
+
def _eyedrop(self, x: int, y: int, primary: bool) -> None:
|
|
591
|
+
r, g, b, _a = self.doc.image.getpixel((x, y))
|
|
592
|
+
color = rgb_to_hex(r, g, b)
|
|
593
|
+
if primary:
|
|
594
|
+
self._color = color
|
|
595
|
+
else:
|
|
596
|
+
self._color2 = color
|
|
597
|
+
if self.on_color_pick:
|
|
598
|
+
self.on_color_pick(color, primary)
|
|
599
|
+
|
|
600
|
+
def _constrain(self, x0: int, y0: int, x1: int, y1: int, shift: bool) -> tuple[int, int]:
|
|
601
|
+
if not shift:
|
|
602
|
+
return x1, y1
|
|
603
|
+
dx, dy = x1 - x0, y1 - y0
|
|
604
|
+
if self._tool == "line":
|
|
605
|
+
ang = round(math.atan2(dy, dx) / (math.pi / 4)) * (math.pi / 4)
|
|
606
|
+
d = math.hypot(dx, dy)
|
|
607
|
+
return x0 + int(round(d * math.cos(ang))), y0 + int(round(d * math.sin(ang)))
|
|
608
|
+
n = max(abs(dx), abs(dy))
|
|
609
|
+
return x0 + (n if dx >= 0 else -n), y0 + (n if dy >= 0 else -n)
|
|
610
|
+
|
|
611
|
+
def _update_shape_preview(self, cx: int, cy: int, primary: bool, shift: bool = False) -> None:
|
|
612
|
+
self.delete("preview")
|
|
613
|
+
x0, y0 = int(self._start[0]), int(self._start[1])
|
|
614
|
+
cx, cy = self._constrain(x0, y0, cx, cy, shift)
|
|
615
|
+
color = self._color if primary else self._color2
|
|
616
|
+
sx0, sy0 = self.to_screen(x0 + 0.5, y0 + 0.5)
|
|
617
|
+
sx1, sy1 = self.to_screen(cx + 0.5, cy + 0.5)
|
|
618
|
+
kw = {"width": max(1, int(self._size * self._zoom)), "dash": (4, 3), "tags": "preview"}
|
|
619
|
+
if self._tool == "line":
|
|
620
|
+
self.create_line(sx0, sy0, sx1, sy1, fill=color, **kw)
|
|
621
|
+
elif self._tool == "rect":
|
|
622
|
+
self.create_rectangle(sx0, sy0, sx1, sy1, outline=color, **kw)
|
|
623
|
+
else:
|
|
624
|
+
self.create_oval(sx0, sy0, sx1, sy1, outline=color, **kw)
|
|
625
|
+
|
|
626
|
+
def _finalize_shape(self, cx: int, cy: int, color: str, shift: bool = False) -> None:
|
|
627
|
+
self.delete("preview")
|
|
628
|
+
x0, y0 = int(self._start[0]), int(self._start[1])
|
|
629
|
+
cx, cy = self._constrain(x0, y0, cx, cy, shift)
|
|
630
|
+
if (cx, cy) == (x0, y0) and self._tool != "line":
|
|
631
|
+
return
|
|
632
|
+
self.commit_float()
|
|
633
|
+
self.doc.begin()
|
|
634
|
+
pe.draw_shape(self.doc, self._tool, [(x0, y0), (cx, cy)], hex_to_rgb(color), self._opacity, self._size,
|
|
635
|
+
self._fill_shapes)
|
|
636
|
+
self.doc.commit()
|
|
637
|
+
self.flush()
|
|
638
|
+
|
|
639
|
+
def _update_poly_preview(self, cx: int, cy: int) -> None:
|
|
640
|
+
self.delete("polyprev")
|
|
641
|
+
if not self._poly:
|
|
642
|
+
return
|
|
643
|
+
pts = [self.to_screen(x + 0.5, y + 0.5) for x, y in [*self._poly, (cx, cy)]]
|
|
644
|
+
flat = [v for p in pts for v in p]
|
|
645
|
+
if len(flat) >= 4:
|
|
646
|
+
self.create_line(*flat, fill=self._color, width=max(1, int(self._size * self._zoom)), dash=(4, 3),
|
|
647
|
+
tags="polyprev")
|
|
648
|
+
|
|
649
|
+
def _clear_poly(self) -> None:
|
|
650
|
+
self._poly.clear()
|
|
651
|
+
self.delete("polyprev")
|
|
652
|
+
|
|
653
|
+
def _finalize_polygon(self) -> None:
|
|
654
|
+
pts = list(self._poly)
|
|
655
|
+
self._clear_poly()
|
|
656
|
+
if len(pts) < 3:
|
|
657
|
+
return
|
|
658
|
+
self.doc.begin()
|
|
659
|
+
pe.draw_shape(self.doc, "polygon", pts, hex_to_rgb(self._color), self._opacity, self._size,
|
|
660
|
+
self._fill_shapes)
|
|
661
|
+
self.doc.commit()
|
|
662
|
+
self.flush()
|
|
663
|
+
|
|
664
|
+
def _insert_text(self, x: int, y: int, color: str) -> None:
|
|
665
|
+
text = simpledialog.askstring(_t("paint.text_title", "Inserir Texto"),
|
|
666
|
+
_t("paint.text_prompt", "Digite o texto:"), parent=self)
|
|
667
|
+
if not text:
|
|
668
|
+
return
|
|
669
|
+
self.commit_float()
|
|
670
|
+
font = load_font(max(8, self._size * 4))
|
|
671
|
+
probe = ImageDraw.Draw(Image.new("L", (1, 1)))
|
|
672
|
+
_, _, r, b = probe.multiline_textbbox((0, 0), text, font=font)
|
|
673
|
+
w, h = self.doc.size
|
|
674
|
+
box = pe.clip_box((x, y, x + r + 2, y + b + 2), w, h)
|
|
675
|
+
if box is None:
|
|
676
|
+
return
|
|
677
|
+
self.doc.begin()
|
|
678
|
+
self.doc.touch(box)
|
|
679
|
+
patch = Image.new("L", (box[2] - box[0], box[3] - box[1]), 0)
|
|
680
|
+
ImageDraw.Draw(patch).multiline_text((0, 0), text, fill=255, font=font)
|
|
681
|
+
pe.composite_patch(self.doc, patch, box, hex_to_rgb(color), self._opacity)
|
|
682
|
+
self.doc.commit()
|
|
683
|
+
self.flush()
|
|
684
|
+
|
|
685
|
+
def _norm_box(self, x0: int, y0: int, x1: int, y1: int) -> pe.Box:
|
|
686
|
+
return (min(x0, x1), min(y0, y1), max(x0, x1) + 1, max(y0, y1) + 1)
|
|
687
|
+
|
|
688
|
+
def _select_press(self, cx: int, cy: int) -> None:
|
|
689
|
+
sel = self._selection
|
|
690
|
+
if sel and sel[0] <= cx < sel[2] and sel[1] <= cy < sel[3]:
|
|
691
|
+
self._lift_selection()
|
|
692
|
+
self._sel_drag = (cx - self._selection[0], cy - self._selection[1])
|
|
693
|
+
return
|
|
694
|
+
self.commit_float()
|
|
695
|
+
self._selection = None
|
|
696
|
+
self._sel_drag = None
|
|
697
|
+
self.delete("sel_rect")
|
|
698
|
+
|
|
699
|
+
def _select_drag(self, cx: int, cy: int) -> None:
|
|
700
|
+
if self._sel_drag and self._float:
|
|
701
|
+
ox, oy = self._sel_drag
|
|
702
|
+
self._move_float(cx - ox, cy - oy)
|
|
703
|
+
else:
|
|
704
|
+
self._selection = self._norm_box(int(self._start[0]), int(self._start[1]), cx, cy)
|
|
705
|
+
self._draw_sel_rect()
|
|
706
|
+
|
|
707
|
+
def _select_release(self, cx: int, cy: int) -> None:
|
|
708
|
+
if self._sel_drag:
|
|
709
|
+
self._sel_drag = None
|
|
710
|
+
return
|
|
711
|
+
sel = self._selection
|
|
712
|
+
if sel and (sel[2] - sel[0] < 2 or sel[3] - sel[1] < 2):
|
|
713
|
+
self._selection = None
|
|
714
|
+
self.delete("sel_rect")
|
|
715
|
+
|
|
716
|
+
def select_all(self) -> None:
|
|
717
|
+
if not self.doc:
|
|
718
|
+
return
|
|
719
|
+
self.commit_float()
|
|
720
|
+
w, h = self.doc.size
|
|
721
|
+
self._selection = (0, 0, w, h)
|
|
722
|
+
self._draw_sel_rect()
|
|
723
|
+
if self.on_tool_request:
|
|
724
|
+
self.on_tool_request("select")
|
|
725
|
+
|
|
726
|
+
def _lift_selection(self) -> None:
|
|
727
|
+
"""Levanta os pixels da seleção (deixa a cor secundária no lugar) como camada flutuante."""
|
|
728
|
+
if self._float or not self._selection:
|
|
729
|
+
return
|
|
730
|
+
box = pe.clip_box(self._selection, *self.doc.size)
|
|
731
|
+
if box is None:
|
|
732
|
+
return
|
|
733
|
+
self.doc.begin()
|
|
734
|
+
self.doc.touch(box)
|
|
735
|
+
img = self.doc.image.crop(box)
|
|
736
|
+
self.doc.image.paste(Image.new("RGBA", img.size, (*hex_to_rgb(self._color2), 255)), (box[0], box[1]))
|
|
737
|
+
self.doc.mark(box)
|
|
738
|
+
self._float = {"img": img, "x": box[0], "y": box[1]}
|
|
739
|
+
self._selection = (box[0], box[1], box[2], box[3])
|
|
740
|
+
self.schedule_flush()
|
|
741
|
+
self._draw_float(rebuild=True)
|
|
742
|
+
|
|
743
|
+
def _move_float(self, x: int, y: int) -> None:
|
|
744
|
+
f = self._float
|
|
745
|
+
f["x"], f["y"] = x, y
|
|
746
|
+
w, h = f["img"].size
|
|
747
|
+
self._selection = (x, y, x + w, y + h)
|
|
748
|
+
self._draw_float()
|
|
749
|
+
self._draw_sel_rect()
|
|
750
|
+
|
|
751
|
+
def _draw_float(self, rebuild: bool = False) -> None:
|
|
752
|
+
f = self._float
|
|
753
|
+
if not f:
|
|
754
|
+
self.delete("float")
|
|
755
|
+
self._float_item = None
|
|
756
|
+
return
|
|
757
|
+
if rebuild or self._float_photo is None:
|
|
758
|
+
w, h = f["img"].size
|
|
759
|
+
sw, sh = max(1, int(round(w * self._zoom))), max(1, int(round(h * self._zoom)))
|
|
760
|
+
scaled = f["img"].resize((sw, sh), Image.NEAREST if self._zoom >= 1 else Image.BILINEAR)
|
|
761
|
+
self._float_photo = ImageTk.PhotoImage(scaled)
|
|
762
|
+
self.delete("float")
|
|
763
|
+
self._float_item = None
|
|
764
|
+
sx, sy = self.to_screen(f["x"], f["y"])
|
|
765
|
+
if self._float_item is None:
|
|
766
|
+
self._float_item = self.create_image(sx, sy, anchor="nw", image=self._float_photo, tags="float")
|
|
767
|
+
else:
|
|
768
|
+
self.coords(self._float_item, sx, sy)
|
|
769
|
+
self.tag_raise("sel_rect")
|
|
770
|
+
|
|
771
|
+
def commit_float(self) -> None:
|
|
772
|
+
"""Cola a camada flutuante na posição atual (uma única entrada de undo)."""
|
|
773
|
+
f = self._float
|
|
774
|
+
if not f:
|
|
775
|
+
return
|
|
776
|
+
self._float = None
|
|
777
|
+
self._float_photo = None
|
|
778
|
+
self.delete("float")
|
|
779
|
+
self._float_item = None
|
|
780
|
+
img, x, y = f["img"], f["x"], f["y"]
|
|
781
|
+
w, h = self.doc.size
|
|
782
|
+
box = pe.clip_box((x, y, x + img.width, y + img.height), w, h)
|
|
783
|
+
if box is not None:
|
|
784
|
+
self.doc.touch(box)
|
|
785
|
+
piece = img.crop((box[0] - x, box[1] - y, box[2] - x, box[3] - y))
|
|
786
|
+
self.doc.image.paste(Image.alpha_composite(self.doc.image.crop(box), piece), (box[0], box[1]))
|
|
787
|
+
self.doc.mark(box)
|
|
788
|
+
self.doc.commit()
|
|
789
|
+
self._selection = (x, y, x + img.width, y + img.height)
|
|
790
|
+
self.flush()
|
|
791
|
+
self._draw_sel_rect()
|
|
792
|
+
|
|
793
|
+
def _draw_sel_rect(self) -> None:
|
|
794
|
+
self.delete("sel_rect")
|
|
795
|
+
if not self._selection:
|
|
796
|
+
return
|
|
797
|
+
x0, y0, x1, y1 = self._selection
|
|
798
|
+
sx0, sy0 = self.to_screen(x0, y0)
|
|
799
|
+
sx1, sy1 = self.to_screen(x1, y1)
|
|
800
|
+
self.create_rectangle(sx0, sy0, sx1, sy1, outline="#0099FF", dash=(5, 3), width=1, tags="sel_rect")
|
|
801
|
+
self.create_rectangle(sx0 + 1, sy0 + 1, sx1 - 1, sy1 - 1, outline="#FFFFFF", dash=(5, 3), width=1,
|
|
802
|
+
tags="sel_rect")
|
|
803
|
+
|
|
804
|
+
def _redraw_overlays(self) -> None:
|
|
805
|
+
self._draw_sel_rect()
|
|
806
|
+
if self._float:
|
|
807
|
+
self._draw_float(rebuild=True)
|
|
808
|
+
if self._poly:
|
|
809
|
+
self._update_poly_preview(*self._poly[-1])
|
|
810
|
+
|
|
811
|
+
def _selection_image(self) -> Any:
|
|
812
|
+
if self._float:
|
|
813
|
+
return self._float["img"].copy()
|
|
814
|
+
if self._selection:
|
|
815
|
+
box = pe.clip_box(self._selection, *self.doc.size)
|
|
816
|
+
if box:
|
|
817
|
+
return self.doc.image.crop(box)
|
|
818
|
+
return None
|
|
819
|
+
|
|
820
|
+
def copy_selection(self) -> None:
|
|
821
|
+
img = self._selection_image()
|
|
822
|
+
if img is not None:
|
|
823
|
+
self._clip = img
|
|
824
|
+
|
|
825
|
+
def cut_selection(self) -> None:
|
|
826
|
+
img = self._selection_image()
|
|
827
|
+
if img is None:
|
|
828
|
+
return
|
|
829
|
+
self._clip = img
|
|
830
|
+
self.delete_selection()
|
|
831
|
+
|
|
832
|
+
def delete_selection(self) -> None:
|
|
833
|
+
if not self.doc:
|
|
834
|
+
return
|
|
835
|
+
if self._float:
|
|
836
|
+
self._float = None
|
|
837
|
+
self._float_photo = None
|
|
838
|
+
self.delete("float")
|
|
839
|
+
self._float_item = None
|
|
840
|
+
self.doc.commit()
|
|
841
|
+
self._selection = None
|
|
842
|
+
self.delete("sel_rect")
|
|
843
|
+
self.flush()
|
|
844
|
+
return
|
|
845
|
+
if self._selection:
|
|
846
|
+
box = pe.clip_box(self._selection, *self.doc.size)
|
|
847
|
+
if box:
|
|
848
|
+
self.doc.begin()
|
|
849
|
+
self.doc.touch(box)
|
|
850
|
+
self.doc.image.paste(Image.new("RGBA", (box[2] - box[0], box[3] - box[1]),
|
|
851
|
+
(*hex_to_rgb(self._color2), 255)), (box[0], box[1]))
|
|
852
|
+
self.doc.mark(box)
|
|
853
|
+
self.doc.commit()
|
|
854
|
+
self._selection = None
|
|
855
|
+
self.delete("sel_rect")
|
|
856
|
+
self.flush()
|
|
857
|
+
|
|
858
|
+
def paste(self) -> None:
|
|
859
|
+
if not self.doc:
|
|
860
|
+
return
|
|
861
|
+
img = self._system_clipboard_image() or self._clip
|
|
862
|
+
if img is None:
|
|
863
|
+
return
|
|
864
|
+
self.commit_float()
|
|
865
|
+
vw, vh = self._view_size()
|
|
866
|
+
x, y = (max(0, int(v)) for v in self.to_image(16, 16))
|
|
867
|
+
w, h = self.doc.size
|
|
868
|
+
x, y = min(x, max(0, w - 1)), min(y, max(0, h - 1))
|
|
869
|
+
self.doc.begin()
|
|
870
|
+
self._float = {"img": img.convert("RGBA"), "x": x, "y": y}
|
|
871
|
+
self._selection = (x, y, x + img.width, y + img.height)
|
|
872
|
+
self._draw_float(rebuild=True)
|
|
873
|
+
self._draw_sel_rect()
|
|
874
|
+
if self.on_tool_request:
|
|
875
|
+
self.on_tool_request("select")
|
|
876
|
+
|
|
877
|
+
def _system_clipboard_image(self) -> Any:
|
|
878
|
+
try:
|
|
879
|
+
from PIL import ImageGrab # type: ignore
|
|
880
|
+
got = ImageGrab.grabclipboard()
|
|
881
|
+
return got if isinstance(got, Image.Image) else None
|
|
882
|
+
except Exception:
|
|
883
|
+
return None
|
|
884
|
+
|
|
885
|
+
def cancel_operation(self) -> None:
|
|
886
|
+
self._clear_poly()
|
|
887
|
+
self.delete("preview")
|
|
888
|
+
if self._stroke is not None:
|
|
889
|
+
self._stroke = None
|
|
890
|
+
self._mask = None
|
|
891
|
+
self.doc.rollback()
|
|
892
|
+
self.flush()
|
|
893
|
+
if self._float:
|
|
894
|
+
self._float = None
|
|
895
|
+
self._float_photo = None
|
|
896
|
+
self.delete("float")
|
|
897
|
+
self._float_item = None
|
|
898
|
+
self.doc.rollback()
|
|
899
|
+
self.flush()
|
|
900
|
+
self._selection = None
|
|
901
|
+
self.delete("sel_rect")
|
|
902
|
+
|
|
903
|
+
def undo(self) -> bool:
|
|
904
|
+
if not self.doc:
|
|
905
|
+
return False
|
|
906
|
+
if self._float:
|
|
907
|
+
self.cancel_operation()
|
|
908
|
+
return True
|
|
909
|
+
ok = self.doc.undo()
|
|
910
|
+
self._after_history(ok)
|
|
911
|
+
return ok
|
|
912
|
+
|
|
913
|
+
def redo(self) -> bool:
|
|
914
|
+
if not self.doc:
|
|
915
|
+
return False
|
|
916
|
+
self.commit_float()
|
|
917
|
+
ok = self.doc.redo()
|
|
918
|
+
self._after_history(ok)
|
|
919
|
+
return ok
|
|
920
|
+
|
|
921
|
+
def _after_history(self, ok: bool) -> None:
|
|
922
|
+
if ok:
|
|
923
|
+
self._selection = None
|
|
924
|
+
self.delete("sel_rect")
|
|
925
|
+
self.flush()
|
|
926
|
+
|
|
927
|
+
def _reset_state(self) -> None:
|
|
928
|
+
self._float = None
|
|
929
|
+
self._float_photo = None
|
|
930
|
+
self._selection = None
|
|
931
|
+
self._stroke = None
|
|
932
|
+
self._mask = None
|
|
933
|
+
self._clear_poly()
|
|
934
|
+
self.delete("sel_rect")
|
|
935
|
+
self.delete("float")
|
|
936
|
+
self._float_item = None
|
|
937
|
+
self._last_status = None
|
|
938
|
+
|
|
939
|
+
def new_canvas(self, w: int = CANVAS_W, h: int = CANVAS_H, fill: tuple = (255, 255, 255, 255)) -> None:
|
|
940
|
+
self._reset_state()
|
|
941
|
+
self.doc.replace_image(Image.new("RGBA", (w, h), fill), record=False)
|
|
942
|
+
self._zoom = 1.0
|
|
943
|
+
self._center_image(fit_if_large=True)
|
|
944
|
+
self._notify_change()
|
|
945
|
+
|
|
946
|
+
def open_image(self, path: Path, quiet: bool = False) -> bool:
|
|
947
|
+
if not HAS_PIL:
|
|
948
|
+
messagebox.showwarning("Pillow", "Instale Pillow:\n pip install Pillow")
|
|
949
|
+
return False
|
|
950
|
+
try:
|
|
951
|
+
with Image.open(path) as src:
|
|
952
|
+
src.load()
|
|
953
|
+
img = ImageOps.exif_transpose(src).convert("RGBA")
|
|
954
|
+
except Exception as exc:
|
|
955
|
+
if not quiet:
|
|
956
|
+
messagebox.showerror(_t("paint.open_error", "Erro ao abrir"), str(exc))
|
|
957
|
+
return False
|
|
958
|
+
self._reset_state()
|
|
959
|
+
self.doc.replace_image(img, record=False)
|
|
960
|
+
self._zoom = 1.0
|
|
961
|
+
self._center_image(fit_if_large=True)
|
|
962
|
+
self._notify_change()
|
|
963
|
+
return True
|
|
964
|
+
|
|
965
|
+
def save_image(self, path: Path, quiet: bool = False) -> bool:
|
|
966
|
+
self.commit_float()
|
|
967
|
+
try:
|
|
968
|
+
fmt = (path.suffix.lstrip(".") or "png").upper()
|
|
969
|
+
fmt = {"JPG": "JPEG", "TIF": "TIFF"}.get(fmt, fmt)
|
|
970
|
+
out = pe.flatten_for_save(self.doc.image, fmt)
|
|
971
|
+
fd, tmp = tempfile.mkstemp(dir=str(path.parent), suffix=path.suffix or ".png")
|
|
972
|
+
os.close(fd)
|
|
973
|
+
try:
|
|
974
|
+
out.save(tmp, format=fmt)
|
|
975
|
+
os.replace(tmp, path)
|
|
976
|
+
finally:
|
|
977
|
+
with contextlib.suppress(OSError):
|
|
978
|
+
os.unlink(tmp)
|
|
979
|
+
self.doc.mark_saved()
|
|
980
|
+
self._notify_change()
|
|
981
|
+
return True
|
|
982
|
+
except Exception as exc:
|
|
983
|
+
if not quiet:
|
|
984
|
+
messagebox.showerror(_t("paint.save_error", "Erro ao salvar"), str(exc))
|
|
985
|
+
logger.warning("falha ao salvar %s: %s", path, exc)
|
|
986
|
+
return False
|
|
987
|
+
|
|
988
|
+
def resize_canvas(self, nw: int, nh: int, scale: bool) -> None:
|
|
989
|
+
self.commit_float()
|
|
990
|
+
old = self.doc.image
|
|
991
|
+
if scale:
|
|
992
|
+
new = old.resize((nw, nh), Image.LANCZOS)
|
|
993
|
+
else:
|
|
994
|
+
new = Image.new("RGBA", (nw, nh), (255, 255, 255, 255))
|
|
995
|
+
new.paste(old, (0, 0))
|
|
996
|
+
self._selection = None
|
|
997
|
+
self.delete("sel_rect")
|
|
998
|
+
self.doc.replace_image(new, record=True)
|
|
999
|
+
self._center_image()
|
|
1000
|
+
self._notify_change()
|