logqbit 0.6__tar.gz → 0.6.2__tar.gz
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.
- logqbit-0.6.2/.gitignore +38 -0
- {logqbit-0.6 → logqbit-0.6.2}/PKG-INFO +3 -4
- {logqbit-0.6 → logqbit-0.6.2}/README.md +1 -3
- {logqbit-0.6 → logqbit-0.6.2}/pyproject.toml +25 -21
- {logqbit-0.6 → logqbit-0.6.2}/src/logqbit/catalog.py +53 -7
- {logqbit-0.6 → logqbit-0.6.2}/src/logqbit/cli/demo.py +3 -2
- {logqbit-0.6 → logqbit-0.6.2}/src/logqbit/cli/main.py +15 -5
- {logqbit-0.6 → logqbit-0.6.2}/src/logqbit/cli/shortcuts.py +2 -11
- {logqbit-0.6 → logqbit-0.6.2}/src/logqbit/cli/svg2ico.py +23 -21
- logqbit-0.6.2/src/logqbit/dataframe.py +424 -0
- logqbit-0.6.2/src/logqbit/gui/browser/__init__.py +1 -0
- {logqbit-0.6 → logqbit-0.6.2}/src/logqbit/gui/browser/__main__.py +1 -1
- logqbit-0.6.2/src/logqbit/gui/browser/about.py +22 -0
- logqbit-0.6.2/src/logqbit/gui/browser/detail/__init__.py +1 -0
- logqbit-0.6.2/src/logqbit/gui/browser/detail/data.py +332 -0
- logqbit-0.6.2/src/logqbit/gui/browser/detail/files.py +83 -0
- logqbit-0.6/src/logqbit/gui/browser/detail.py → logqbit-0.6.2/src/logqbit/gui/browser/detail/view.py +87 -326
- logqbit-0.6.2/src/logqbit/gui/browser/detail/yaml.py +73 -0
- logqbit-0.6.2/src/logqbit/gui/browser/plot/__init__.py +1 -0
- {logqbit-0.6/src/logqbit/gui → logqbit-0.6.2/src/logqbit/gui/browser}/plot/cursor.py +6 -4
- logqbit-0.6/src/logqbit/gui/plot/fit.py → logqbit-0.6.2/src/logqbit/gui/browser/plot/fitting.py +3 -3
- logqbit-0.6/src/logqbit/gui/plot/widget.py → logqbit-0.6.2/src/logqbit/gui/browser/plot/manager.py +107 -74
- {logqbit-0.6/src/logqbit/gui → logqbit-0.6.2/src/logqbit/gui/browser}/plot/mesh.py +1 -3
- logqbit-0.6.2/src/logqbit/gui/browser/startup.py +167 -0
- logqbit-0.6.2/src/logqbit/gui/browser/window/__init__.py +1 -0
- logqbit-0.6.2/src/logqbit/gui/browser/window/model.py +137 -0
- logqbit-0.6.2/src/logqbit/gui/browser/window/preferences.py +112 -0
- logqbit-0.6/src/logqbit/gui/browser/window.py → logqbit-0.6.2/src/logqbit/gui/browser/window/view.py +241 -495
- {logqbit-0.6 → logqbit-0.6.2}/src/logqbit/gui/live_plotter.py +72 -26
- {logqbit-0.6 → logqbit-0.6.2}/src/logqbit/logfolder.py +16 -60
- {logqbit-0.6 → logqbit-0.6.2}/src/logqbit/templates/move_from_labrad.py +16 -11
- logqbit-0.6.2/tests/core/test_browser_launcher.py +136 -0
- {logqbit-0.6 → logqbit-0.6.2}/tests/core/test_cli.py +24 -1
- logqbit-0.6.2/tests/core/test_dataframe.py +261 -0
- {logqbit-0.6 → logqbit-0.6.2}/tests/core/test_log_catalog.py +67 -7
- {logqbit-0.6 → logqbit-0.6.2}/tests/core/test_logfolder.py +81 -67
- {logqbit-0.6 → logqbit-0.6.2}/tests/core/test_registry.py +1 -0
- logqbit-0.6.2/tests/gui/browser/conftest.py +37 -0
- logqbit-0.6.2/tests/gui/browser/test_catalog.py +177 -0
- logqbit-0.6.2/tests/gui/browser/test_data.py +114 -0
- logqbit-0.6.2/tests/gui/browser/test_detail.py +414 -0
- logqbit-0.6.2/tests/gui/browser/test_list.py +182 -0
- logqbit-0.6/tests/gui/test_plotter.py → logqbit-0.6.2/tests/gui/browser/test_plot.py +171 -33
- logqbit-0.6.2/tests/gui/browser/test_preferences.py +67 -0
- logqbit-0.6.2/tests/gui/browser/test_startup.py +83 -0
- logqbit-0.6.2/tests/gui/browser/test_window.py +427 -0
- logqbit-0.6.2/tests/gui/browser/test_yaml.py +42 -0
- {logqbit-0.6 → logqbit-0.6.2}/tests/gui/test_live_plotter.py +9 -3
- logqbit-0.6/.gitignore +0 -231
- logqbit-0.6/src/logqbit/dataframe.py +0 -208
- logqbit-0.6/src/logqbit/gui/browser/__init__.py +0 -5
- logqbit-0.6/src/logqbit/gui/plot/__init__.py +0 -5
- logqbit-0.6/tests/core/test_dataframe.py +0 -123
- logqbit-0.6/tests/gui/test_browser.py +0 -981
- {logqbit-0.6 → logqbit-0.6.2}/LICENSE +0 -0
- {logqbit-0.6 → logqbit-0.6.2}/src/logqbit/__init__.py +0 -0
- {logqbit-0.6 → logqbit-0.6.2}/src/logqbit/assets/browser.svg +0 -0
- {logqbit-0.6 → logqbit-0.6.2}/src/logqbit/assets/live_plotter.svg +0 -0
- {logqbit-0.6 → logqbit-0.6.2}/src/logqbit/cli/__init__.py +0 -0
- {logqbit-0.6 → logqbit-0.6.2}/src/logqbit/cli/__main__.py +0 -0
- {logqbit-0.6 → logqbit-0.6.2}/src/logqbit/file_version.py +0 -0
- {logqbit-0.6 → logqbit-0.6.2}/src/logqbit/gui/__init__.py +0 -0
- {logqbit-0.6 → logqbit-0.6.2}/src/logqbit/metadata.py +0 -0
- {logqbit-0.6 → logqbit-0.6.2}/src/logqbit/registry.py +0 -0
- {logqbit-0.6 → logqbit-0.6.2}/tests/core/test_metadata.py +0 -0
- {logqbit-0.6 → logqbit-0.6.2}/tests/core/test_public_api.py +0 -0
- {logqbit-0.6 → logqbit-0.6.2}/tests/for_all_pythons.ps1 +0 -0
- {logqbit-0.6 → logqbit-0.6.2}/tests/gui/conftest.py +0 -0
logqbit-0.6.2/.gitignore
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Local project data
|
|
2
|
+
AGENTS.md
|
|
3
|
+
/logqbit_example/
|
|
4
|
+
/src/logqbit/assets/*.ico
|
|
5
|
+
|
|
6
|
+
# Python bytecode and native extensions
|
|
7
|
+
__pycache__/
|
|
8
|
+
*.py[codz]
|
|
9
|
+
*.so
|
|
10
|
+
|
|
11
|
+
# Environments and local configuration
|
|
12
|
+
/.venv/
|
|
13
|
+
.env
|
|
14
|
+
.envrc
|
|
15
|
+
.pypirc
|
|
16
|
+
|
|
17
|
+
# Builds and installed-package metadata
|
|
18
|
+
/build/
|
|
19
|
+
/dist/
|
|
20
|
+
/wheels/
|
|
21
|
+
*.egg-info/
|
|
22
|
+
|
|
23
|
+
# Test, lint, and type-check caches
|
|
24
|
+
/.pytest_cache/
|
|
25
|
+
/.ruff_cache/
|
|
26
|
+
/.mypy_cache/
|
|
27
|
+
/.pytype/
|
|
28
|
+
/.coverage
|
|
29
|
+
/.coverage.*
|
|
30
|
+
/htmlcov/
|
|
31
|
+
|
|
32
|
+
# Documentation and notebook output
|
|
33
|
+
/site/
|
|
34
|
+
/docs/_build/
|
|
35
|
+
.ipynb_checkpoints/
|
|
36
|
+
|
|
37
|
+
# OS metadata
|
|
38
|
+
.DS_Store
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: logqbit
|
|
3
|
-
Version: 0.6
|
|
3
|
+
Version: 0.6.2
|
|
4
4
|
Summary: Simple data logger and live plotter for lab-scale experiments.
|
|
5
5
|
Project-URL: Homepage, https://github.com/Qiujv/logqbit
|
|
6
6
|
Author-email: Qiujv <qiujv@outlook.com>
|
|
@@ -14,6 +14,7 @@ Requires-Dist: tqdm>=4.67.1
|
|
|
14
14
|
Requires-Dist: typing-extensions>=4.15.0
|
|
15
15
|
Provides-Extra: gui
|
|
16
16
|
Requires-Dist: numba>=0.65.0; extra == 'gui'
|
|
17
|
+
Requires-Dist: pygments>=2.19.2; extra == 'gui'
|
|
17
18
|
Requires-Dist: pyqtgraph>=0.14.0; extra == 'gui'
|
|
18
19
|
Requires-Dist: pyside6>=6.9.3; extra == 'gui'
|
|
19
20
|
Requires-Dist: scipy>=1.16.0; extra == 'gui'
|
|
@@ -28,7 +29,6 @@ LogQbit 是一个面向实验室场景的轻量数据记录工具包,适合记
|
|
|
28
29
|
|
|
29
30
|
- 用简单的接口记录和管理实验数据。
|
|
30
31
|
- 基于目录的实验数据组织,用通用可读的格式储存数据。
|
|
31
|
-
- 在需要时再启用浏览器和实时绘图等 GUI 能力。
|
|
32
32
|
|
|
33
33
|
完整文档见:https://qiujv.github.io/logqbit/
|
|
34
34
|
|
|
@@ -81,8 +81,7 @@ logqbit browser
|
|
|
81
81
|
logqbit browser-demo
|
|
82
82
|
```
|
|
83
83
|
|
|
84
|
-
这会在当前目录创建 `logqbit_example
|
|
85
|
-
包含一百万个数据点,首次生成可能需要一点时间。
|
|
84
|
+
这会在当前目录创建 `logqbit_example/`,生成示例数据并启动图形化浏览器。
|
|
86
85
|
|
|
87
86
|
更多内容:
|
|
88
87
|
|
|
@@ -6,7 +6,6 @@ LogQbit 是一个面向实验室场景的轻量数据记录工具包,适合记
|
|
|
6
6
|
|
|
7
7
|
- 用简单的接口记录和管理实验数据。
|
|
8
8
|
- 基于目录的实验数据组织,用通用可读的格式储存数据。
|
|
9
|
-
- 在需要时再启用浏览器和实时绘图等 GUI 能力。
|
|
10
9
|
|
|
11
10
|
完整文档见:https://qiujv.github.io/logqbit/
|
|
12
11
|
|
|
@@ -59,8 +58,7 @@ logqbit browser
|
|
|
59
58
|
logqbit browser-demo
|
|
60
59
|
```
|
|
61
60
|
|
|
62
|
-
这会在当前目录创建 `logqbit_example
|
|
63
|
-
包含一百万个数据点,首次生成可能需要一点时间。
|
|
61
|
+
这会在当前目录创建 `logqbit_example/`,生成示例数据并启动图形化浏览器。
|
|
64
62
|
|
|
65
63
|
更多内容:
|
|
66
64
|
|
|
@@ -14,6 +14,28 @@ dependencies = [
|
|
|
14
14
|
"typing-extensions>=4.15.0",
|
|
15
15
|
]
|
|
16
16
|
|
|
17
|
+
[dependency-groups]
|
|
18
|
+
dev = [
|
|
19
|
+
"ipykernel>=6.30.1",
|
|
20
|
+
"ipywidgets>=8.1.7",
|
|
21
|
+
"matplotlib>=3.10.7",
|
|
22
|
+
"mkdocs>=1.6.1",
|
|
23
|
+
"mkdocstrings[python]>=0.29.1",
|
|
24
|
+
"pytest>=8.4.2",
|
|
25
|
+
"pytest-cov>=7.0.0",
|
|
26
|
+
"ruff>=0.15.12",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[project.optional-dependencies]
|
|
30
|
+
gui = [
|
|
31
|
+
"numba>=0.65.0",
|
|
32
|
+
"pygments>=2.19.2",
|
|
33
|
+
"pyqtgraph>=0.14.0",
|
|
34
|
+
"pyside6>=6.9.3",
|
|
35
|
+
"scipy>=1.16.0",
|
|
36
|
+
"send2trash>=1.8.3",
|
|
37
|
+
]
|
|
38
|
+
|
|
17
39
|
[project.urls]
|
|
18
40
|
Homepage = "https://github.com/Qiujv/logqbit"
|
|
19
41
|
|
|
@@ -22,7 +44,7 @@ logqbit = "logqbit.cli:main"
|
|
|
22
44
|
|
|
23
45
|
[project.gui-scripts]
|
|
24
46
|
logqbit-live-plotter = "logqbit.gui.live_plotter:main"
|
|
25
|
-
logqbit-browser = "logqbit.gui.browser:main"
|
|
47
|
+
logqbit-browser = "logqbit.gui.browser.startup:main"
|
|
26
48
|
|
|
27
49
|
[build-system]
|
|
28
50
|
requires = ["hatchling", "hatch-vcs"]
|
|
@@ -40,23 +62,5 @@ include = ["src/", "tests/", "README.md", "LICENSE", "pyproject.toml"]
|
|
|
40
62
|
[tool.pytest.ini_options]
|
|
41
63
|
testpaths = ["tests"]
|
|
42
64
|
|
|
43
|
-
[
|
|
44
|
-
|
|
45
|
-
"ipykernel>=6.30.1",
|
|
46
|
-
"ipywidgets>=8.1.7",
|
|
47
|
-
"matplotlib>=3.10.7",
|
|
48
|
-
"mkdocs>=1.6.1",
|
|
49
|
-
"mkdocstrings[python]>=0.29.1",
|
|
50
|
-
"pytest>=8.4.2",
|
|
51
|
-
"pytest-cov>=7.0.0",
|
|
52
|
-
"ruff>=0.15.12",
|
|
53
|
-
]
|
|
54
|
-
|
|
55
|
-
[project.optional-dependencies]
|
|
56
|
-
gui = [
|
|
57
|
-
"numba>=0.65.0",
|
|
58
|
-
"pyqtgraph>=0.14.0",
|
|
59
|
-
"pyside6>=6.9.3",
|
|
60
|
-
"scipy>=1.16.0",
|
|
61
|
-
"send2trash>=1.8.3",
|
|
62
|
-
]
|
|
65
|
+
[tool.ruff.format]
|
|
66
|
+
exclude = ["notebooks/**"]
|
|
@@ -4,7 +4,7 @@ from __future__ import annotations
|
|
|
4
4
|
|
|
5
5
|
import logging
|
|
6
6
|
import shutil
|
|
7
|
-
from collections.abc import Callable, Iterable
|
|
7
|
+
from collections.abc import Callable, Iterable, Sequence
|
|
8
8
|
from dataclasses import dataclass
|
|
9
9
|
from functools import cached_property
|
|
10
10
|
from pathlib import Path
|
|
@@ -25,6 +25,51 @@ _KNOWN_LOG_FILENAMES = {"const.yaml", "data.feather", "metadata.json"}
|
|
|
25
25
|
_RETRY_VERSION = FileVersion(mtime_ns=-1, size=-1, inode=-1)
|
|
26
26
|
|
|
27
27
|
|
|
28
|
+
@dataclass(frozen=True)
|
|
29
|
+
class PlotColumns:
|
|
30
|
+
"""Plot roles resolved against the columns available in one record."""
|
|
31
|
+
|
|
32
|
+
axes: tuple[str, ...]
|
|
33
|
+
fields: tuple[str, ...]
|
|
34
|
+
ignored: tuple[str, ...]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def resolve_plot_columns(
|
|
38
|
+
columns: Sequence[str],
|
|
39
|
+
preferred_axes: Sequence[str],
|
|
40
|
+
preferred_fields: Sequence[str],
|
|
41
|
+
) -> PlotColumns:
|
|
42
|
+
"""Resolve stored plot preferences into ordered, disjoint column roles."""
|
|
43
|
+
|
|
44
|
+
columns = _ordered_unique(columns)
|
|
45
|
+
available = set(columns)
|
|
46
|
+
axes = [column for column in _ordered_unique(preferred_axes) if column in available]
|
|
47
|
+
axes_set = set(axes)
|
|
48
|
+
fields = [
|
|
49
|
+
column
|
|
50
|
+
for column in _ordered_unique(preferred_fields)
|
|
51
|
+
if column in available and column not in axes_set
|
|
52
|
+
]
|
|
53
|
+
fields_set = set(fields)
|
|
54
|
+
ignored = [
|
|
55
|
+
column
|
|
56
|
+
for column in columns
|
|
57
|
+
if column not in axes_set and column not in fields_set
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
if not axes and ignored:
|
|
61
|
+
axes.append(ignored.pop(0))
|
|
62
|
+
if not fields and ignored:
|
|
63
|
+
fields.append(ignored.pop(0))
|
|
64
|
+
return PlotColumns(tuple(axes), tuple(fields), tuple(ignored))
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _ordered_unique(items: Sequence[str]) -> tuple[str, ...]:
|
|
68
|
+
if isinstance(items, str):
|
|
69
|
+
return (items,)
|
|
70
|
+
return tuple(dict.fromkeys(str(item) for item in items))
|
|
71
|
+
|
|
72
|
+
|
|
28
73
|
@dataclass(frozen=True)
|
|
29
74
|
class LogRecord:
|
|
30
75
|
"""One catalog record with cached summary fields and explicit disk access."""
|
|
@@ -80,12 +125,13 @@ class LogRecord:
|
|
|
80
125
|
return bool(self.meta.root.get("trash", False))
|
|
81
126
|
|
|
82
127
|
@property
|
|
83
|
-
def
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
128
|
+
def resolved_plot_columns(self) -> PlotColumns:
|
|
129
|
+
"""Return the effective plot roles for the current data columns."""
|
|
130
|
+
return resolve_plot_columns(
|
|
131
|
+
self.columns,
|
|
132
|
+
self.meta.root.get("plot_axes", ()),
|
|
133
|
+
self.meta.root.get("plot_fields", ()),
|
|
134
|
+
)
|
|
89
135
|
|
|
90
136
|
@property
|
|
91
137
|
def create_time(self) -> str:
|
|
@@ -124,9 +124,10 @@ def create_example_data() -> int:
|
|
|
124
124
|
|
|
125
125
|
print(f"\n✓ Created 5 example log folders in: {example_dir}")
|
|
126
126
|
print("\nLaunching browser...")
|
|
127
|
-
from logqbit.gui.browser import
|
|
127
|
+
from logqbit.gui.browser.startup import launch_browser
|
|
128
128
|
|
|
129
|
-
|
|
129
|
+
launch_browser(example_dir)
|
|
130
|
+
return 0
|
|
130
131
|
except ImportError as exc:
|
|
131
132
|
print(f"Error: Missing required dependency: {exc}", file=sys.stderr)
|
|
132
133
|
print("Please ensure logqbit is properly installed.", file=sys.stderr)
|
|
@@ -77,7 +77,11 @@ def _build_parser() -> argparse.ArgumentParser:
|
|
|
77
77
|
type=Path,
|
|
78
78
|
help="Directory to open (default: current directory)",
|
|
79
79
|
)
|
|
80
|
-
|
|
80
|
+
browser_parser.add_argument(
|
|
81
|
+
"--foreground",
|
|
82
|
+
action="store_true",
|
|
83
|
+
help="Run in the current process and keep the terminal attached",
|
|
84
|
+
)
|
|
81
85
|
subparsers.add_parser(
|
|
82
86
|
"browser-demo",
|
|
83
87
|
help="Create example data and launch browser",
|
|
@@ -104,10 +108,16 @@ def main(argv: Sequence[str] | None = None) -> int:
|
|
|
104
108
|
if args.command == "copy-template":
|
|
105
109
|
return copy_template(args.template, args.output)
|
|
106
110
|
if args.command == "browser":
|
|
107
|
-
from logqbit.gui.browser import
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
+
from logqbit.gui.browser.startup import (
|
|
112
|
+
launch_browser,
|
|
113
|
+
run_browser_application,
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
if args.foreground:
|
|
117
|
+
browser_args = [str(args.directory)] if args.directory else []
|
|
118
|
+
return run_browser_application(browser_args)
|
|
119
|
+
launch_browser(args.directory)
|
|
120
|
+
return 0
|
|
111
121
|
if args.command == "browser-demo":
|
|
112
122
|
from logqbit.cli.demo import create_example_data
|
|
113
123
|
|
|
@@ -10,15 +10,6 @@ from importlib.resources import files
|
|
|
10
10
|
from pathlib import Path
|
|
11
11
|
|
|
12
12
|
|
|
13
|
-
def _windows_gui_python_executable() -> Path:
|
|
14
|
-
executable = Path(sys.executable)
|
|
15
|
-
if sys.platform == "win32" and executable.name.lower() != "pythonw.exe":
|
|
16
|
-
pythonw = executable.with_name("pythonw.exe")
|
|
17
|
-
if pythonw.exists():
|
|
18
|
-
return pythonw
|
|
19
|
-
return executable
|
|
20
|
-
|
|
21
|
-
|
|
22
13
|
def _powershell_single_quoted(value: object) -> str:
|
|
23
14
|
return "'" + str(value).replace("'", "''") + "'"
|
|
24
15
|
|
|
@@ -81,11 +72,11 @@ def create_shortcuts(output_dir: Path | None = None) -> int:
|
|
|
81
72
|
|
|
82
73
|
output_dir.mkdir(parents=True, exist_ok=True)
|
|
83
74
|
shortcut_path = output_dir / "LogQbit Browser.lnk"
|
|
75
|
+
browser_entrypoint = Path(sys.executable).with_name("logqbit-browser.exe")
|
|
84
76
|
powershell_script = f"""
|
|
85
77
|
$WshShell = New-Object -ComObject WScript.Shell
|
|
86
78
|
$Shortcut = $WshShell.CreateShortcut({_powershell_single_quoted(shortcut_path)})
|
|
87
|
-
$Shortcut.TargetPath = {_powershell_single_quoted(
|
|
88
|
-
$Shortcut.Arguments = '-m logqbit.gui.browser'
|
|
79
|
+
$Shortcut.TargetPath = {_powershell_single_quoted(browser_entrypoint)}
|
|
89
80
|
$Shortcut.IconLocation = {_powershell_single_quoted(browser_ico)}
|
|
90
81
|
$Shortcut.Save()
|
|
91
82
|
"""
|
|
@@ -8,7 +8,7 @@ The ICO file will contain 7 resolutions: 16x16, 24x24, 32x32, 48x48, 64x64, 128x
|
|
|
8
8
|
Usage:
|
|
9
9
|
# Convert default browser.svg to browser.ico
|
|
10
10
|
python -m logqbit.cli.svg2ico
|
|
11
|
-
|
|
11
|
+
|
|
12
12
|
# Convert any SVG to ICO
|
|
13
13
|
python -m logqbit.cli.svg2ico input.svg output.ico
|
|
14
14
|
"""
|
|
@@ -25,12 +25,13 @@ from PySide6.QtWidgets import QApplication
|
|
|
25
25
|
# ICO format supports these standard sizes
|
|
26
26
|
SIZES = (16, 24, 32, 48, 64, 128, 256)
|
|
27
27
|
|
|
28
|
+
|
|
28
29
|
def pix_to_png_bytes(pix: QPixmap) -> bytes:
|
|
29
30
|
"""Convert QPixmap to PNG bytes.
|
|
30
|
-
|
|
31
|
+
|
|
31
32
|
Args:
|
|
32
33
|
pix: QPixmap to convert
|
|
33
|
-
|
|
34
|
+
|
|
34
35
|
Returns:
|
|
35
36
|
PNG image data as bytes
|
|
36
37
|
"""
|
|
@@ -41,52 +42,52 @@ def pix_to_png_bytes(pix: QPixmap) -> bytes:
|
|
|
41
42
|
buffer.close()
|
|
42
43
|
return bytes(byte_array)
|
|
43
44
|
|
|
45
|
+
|
|
44
46
|
def write_ico(png_map: dict[int, bytes], outfile: str):
|
|
45
47
|
"""Write multiple PNG images as a single ICO file.
|
|
46
|
-
|
|
48
|
+
|
|
47
49
|
ICO format structure:
|
|
48
50
|
- ICONDIR header (6 bytes): reserved(2) + type(2) + count(2)
|
|
49
51
|
- ICONDIRENTRY for each image (16 bytes each)
|
|
50
52
|
- PNG image data
|
|
51
|
-
|
|
53
|
+
|
|
52
54
|
Reference: https://en.wikipedia.org/wiki/ICO_(file_format)
|
|
53
|
-
|
|
55
|
+
|
|
54
56
|
Args:
|
|
55
57
|
png_map: Dictionary mapping size to PNG bytes
|
|
56
58
|
outfile: Output ICO file path
|
|
57
59
|
"""
|
|
58
60
|
# 1. 先收集目录项
|
|
59
61
|
entries = []
|
|
60
|
-
offset = 6 + len(png_map) * 16
|
|
62
|
+
offset = 6 + len(png_map) * 16 # 文件头 + 目录长度
|
|
61
63
|
for size in sorted(png_map):
|
|
62
64
|
png = png_map[size]
|
|
63
65
|
# 在 ICO 格式中,256 用 0 表示
|
|
64
66
|
w = 0 if size == 256 else size
|
|
65
67
|
h = 0 if size == 256 else size
|
|
66
68
|
# 宽/高/颜色数/保留 各 1 字节, 色彩平面/位深度 各 2 字节, 数据大小/偏移 各 4 字节
|
|
67
|
-
entries.append(struct.pack("<BBBBHHLL",
|
|
68
|
-
w, h, 0, 0, 1, 32,
|
|
69
|
-
len(png), offset))
|
|
69
|
+
entries.append(struct.pack("<BBBBHHLL", w, h, 0, 0, 1, 32, len(png), offset))
|
|
70
70
|
offset += len(png)
|
|
71
71
|
|
|
72
72
|
# 2. 写文件头
|
|
73
73
|
with open(outfile, "wb") as ico:
|
|
74
74
|
ico.write(struct.pack("<HHH", 0, 1, len(png_map))) # 保留+类型+数量
|
|
75
|
-
ico.write(b"".join(entries))
|
|
75
|
+
ico.write(b"".join(entries)) # 目录
|
|
76
76
|
# 3. 写图像数据
|
|
77
77
|
for size in sorted(png_map):
|
|
78
78
|
ico.write(png_map[size])
|
|
79
79
|
|
|
80
|
+
|
|
80
81
|
def svg_to_ico(svg_path: str, ico_path: str):
|
|
81
82
|
"""Convert SVG file to multi-resolution ICO file.
|
|
82
|
-
|
|
83
|
+
|
|
83
84
|
Creates an ICO file containing the following sizes:
|
|
84
85
|
16x16, 24x24, 32x32, 48x48, 64x64, 128x128, 256x256
|
|
85
|
-
|
|
86
|
+
|
|
86
87
|
Args:
|
|
87
88
|
svg_path: Path to input SVG file
|
|
88
89
|
ico_path: Path to output ICO file
|
|
89
|
-
|
|
90
|
+
|
|
90
91
|
Raises:
|
|
91
92
|
ValueError: If SVG file is invalid
|
|
92
93
|
"""
|
|
@@ -94,28 +95,29 @@ def svg_to_ico(svg_path: str, ico_path: str):
|
|
|
94
95
|
app = QApplication.instance()
|
|
95
96
|
if app is None:
|
|
96
97
|
app = QApplication(sys.argv)
|
|
97
|
-
|
|
98
|
+
|
|
98
99
|
# 加载 SVG
|
|
99
100
|
renderer = QSvgRenderer(svg_path)
|
|
100
101
|
if not renderer.isValid():
|
|
101
102
|
raise ValueError(f"Invalid SVG file: {svg_path}")
|
|
102
|
-
|
|
103
|
+
|
|
103
104
|
png_map = {}
|
|
104
105
|
for s in SIZES:
|
|
105
106
|
# 创建透明背景的 pixmap
|
|
106
107
|
pix = QPixmap(s, s)
|
|
107
108
|
pix.fill(Qt.transparent)
|
|
108
|
-
|
|
109
|
+
|
|
109
110
|
# 使用 QPainter 渲染 SVG
|
|
110
111
|
painter = QPainter(pix)
|
|
111
112
|
renderer.render(painter, QRectF(0, 0, s, s))
|
|
112
113
|
painter.end()
|
|
113
|
-
|
|
114
|
+
|
|
114
115
|
png_map[s] = pix_to_png_bytes(pix)
|
|
115
|
-
|
|
116
|
+
|
|
116
117
|
write_ico(png_map, ico_path)
|
|
117
118
|
print(f"✓ Created ICO file: {ico_path}")
|
|
118
119
|
|
|
120
|
+
|
|
119
121
|
if __name__ == "__main__":
|
|
120
122
|
if len(sys.argv) == 3:
|
|
121
123
|
# 命令行参数模式
|
|
@@ -124,11 +126,11 @@ if __name__ == "__main__":
|
|
|
124
126
|
# 默认模式:转换项目中的 browser.svg
|
|
125
127
|
svg_path = Path(__file__).parent.parent / "assets" / "browser.svg"
|
|
126
128
|
ico_path = Path.cwd() / "browser.ico"
|
|
127
|
-
|
|
129
|
+
|
|
128
130
|
if not svg_path.exists():
|
|
129
131
|
print(f"Error: SVG file not found: {svg_path}", file=sys.stderr)
|
|
130
132
|
sys.exit(1)
|
|
131
|
-
|
|
133
|
+
|
|
132
134
|
print(f"Converting {svg_path.name} to {ico_path.name}...")
|
|
133
135
|
svg_to_ico(str(svg_path), str(ico_path))
|
|
134
136
|
else:
|