statstudio 0.1.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.
- statstudio/__init__.py +9 -0
- statstudio/__main__.py +3 -0
- statstudio/app.py +83 -0
- statstudio/cli.py +116 -0
- statstudio/core/__init__.py +3 -0
- statstudio/core/archive.py +27 -0
- statstudio/core/commands.py +67 -0
- statstudio/core/formula_catalog.py +74 -0
- statstudio/core/formulas.py +605 -0
- statstudio/core/joins.py +123 -0
- statstudio/core/project.py +358 -0
- statstudio/core/runtime.py +321 -0
- statstudio/core/scripting.py +594 -0
- statstudio/core/table.py +796 -0
- statstudio/core/transform.py +450 -0
- statstudio/feature_help.py +595 -0
- statstudio/io/__init__.py +3 -0
- statstudio/io/docx.py +201 -0
- statstudio/io/export.py +99 -0
- statstudio/io/file_import.py +199 -0
- statstudio/io/odbc.py +334 -0
- statstudio/io/sample_data.py +164 -0
- statstudio/io/table_io.py +118 -0
- statstudio/plots/__init__.py +19 -0
- statstudio/plots/advanced.py +651 -0
- statstudio/plots/basic.py +957 -0
- statstudio/plots/bayesian_plots.py +94 -0
- statstudio/plots/common.py +334 -0
- statstudio/plots/diagnostics.py +214 -0
- statstudio/plots/doe_plots.py +235 -0
- statstudio/plots/plotly_builders.py +719 -0
- statstudio/plots/predictive_plots.py +72 -0
- statstudio/plots/registry.py +251 -0
- statstudio/plugins.py +111 -0
- statstudio/resources/help_topics.json +13195 -0
- statstudio/resources/logo.ico +0 -0
- statstudio/resources/logo.png +0 -0
- statstudio/resources/statstudio.svg +8 -0
- statstudio/stats/__init__.py +42 -0
- statstudio/stats/bayesian.py +1094 -0
- statstudio/stats/bayesian_optimization.py +590 -0
- statstudio/stats/common.py +140 -0
- statstudio/stats/correlation.py +135 -0
- statstudio/stats/custom_doe.py +1714 -0
- statstudio/stats/descriptive.py +200 -0
- statstudio/stats/desirability.py +132 -0
- statstudio/stats/distributions.py +331 -0
- statstudio/stats/doe.py +1070 -0
- statstudio/stats/hypothesis.py +1305 -0
- statstudio/stats/multivariate.py +307 -0
- statstudio/stats/optimal_doe.py +264 -0
- statstudio/stats/power.py +194 -0
- statstudio/stats/predictive.py +605 -0
- statstudio/stats/profiler.py +379 -0
- statstudio/stats/quality.py +1218 -0
- statstudio/stats/regression.py +1600 -0
- statstudio/stats/results.py +235 -0
- statstudio/stats/simulator.py +95 -0
- statstudio/stats/survival.py +637 -0
- statstudio/stats/timeseries.py +643 -0
- statstudio/stats/tolerance.py +154 -0
- statstudio/ui/__init__.py +0 -0
- statstudio/ui/analysis_hub.py +271 -0
- statstudio/ui/command_catalog.py +460 -0
- statstudio/ui/command_palette.py +55 -0
- statstudio/ui/console.py +239 -0
- statstudio/ui/dialogs/__init__.py +0 -0
- statstudio/ui/dialogs/advanced_dialogs.py +392 -0
- statstudio/ui/dialogs/base.py +537 -0
- statstudio/ui/dialogs/bayesian_dialogs.py +488 -0
- statstudio/ui/dialogs/bayesian_optimization_dialog.py +299 -0
- statstudio/ui/dialogs/custom_doe_dialog.py +467 -0
- statstudio/ui/dialogs/doe_dialogs.py +220 -0
- statstudio/ui/dialogs/extended_dialogs.py +665 -0
- statstudio/ui/dialogs/graph_builder.py +139 -0
- statstudio/ui/dialogs/help_dialog.py +207 -0
- statstudio/ui/dialogs/help_topics.py +280 -0
- statstudio/ui/dialogs/lazy.py +184 -0
- statstudio/ui/dialogs/odbc_dialog.py +311 -0
- statstudio/ui/dialogs/plot_dialog.py +1385 -0
- statstudio/ui/dialogs/report_composer.py +218 -0
- statstudio/ui/dialogs/script_dialogs.py +267 -0
- statstudio/ui/dialogs/stats_dialogs.py +1688 -0
- statstudio/ui/dialogs/table_dialogs.py +997 -0
- statstudio/ui/dialogs/window.py +58 -0
- statstudio/ui/docks/__init__.py +0 -0
- statstudio/ui/formula_editor.py +121 -0
- statstudio/ui/icons.py +257 -0
- statstudio/ui/jobs.py +179 -0
- statstudio/ui/main_window.py +3934 -0
- statstudio/ui/plot_interaction.py +178 -0
- statstudio/ui/profiler_widget.py +437 -0
- statstudio/ui/recent.py +82 -0
- statstudio/ui/reports/__init__.py +0 -0
- statstudio/ui/reports/report_view.py +1632 -0
- statstudio/ui/script_jobs.py +35 -0
- statstudio/ui/scripts_panel.py +271 -0
- statstudio/ui/table_document.py +233 -0
- statstudio/ui/table_model.py +285 -0
- statstudio/ui/table_view.py +285 -0
- statstudio/ui/theme.py +645 -0
- statstudio/ui/welcome.py +453 -0
- statstudio/ui/window_controls.py +53 -0
- statstudio/ui/workers.py +53 -0
- statstudio-0.1.0.dist-info/METADATA +261 -0
- statstudio-0.1.0.dist-info/RECORD +110 -0
- statstudio-0.1.0.dist-info/WHEEL +5 -0
- statstudio-0.1.0.dist-info/entry_points.txt +5 -0
- statstudio-0.1.0.dist-info/licenses/LICENSE +674 -0
- statstudio-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
import pandas as pd
|
|
7
|
+
from scipy import stats
|
|
8
|
+
|
|
9
|
+
from statstudio.stats.results import AnalysisResult
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _normal_factor(n: int, coverage: float, confidence: float, sided: str) -> float:
|
|
13
|
+
if n < 2:
|
|
14
|
+
raise ValueError("At least two observations are required.")
|
|
15
|
+
if sided != "two-sided":
|
|
16
|
+
z_value = stats.norm.ppf(coverage)
|
|
17
|
+
delta = z_value * math.sqrt(n)
|
|
18
|
+
return float(stats.nct.ppf(1 - confidence, n - 1, delta) / math.sqrt(n))
|
|
19
|
+
z_value = stats.norm.ppf((1 + coverage) / 2)
|
|
20
|
+
chi_value = stats.chi2.ppf(1 - confidence, n - 1)
|
|
21
|
+
if chi_value <= 0:
|
|
22
|
+
raise ValueError("Invalid confidence level.")
|
|
23
|
+
return float(z_value * math.sqrt((n - 1) * (1 + 1 / n) / chi_value))
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def tolerance_interval(
|
|
27
|
+
df: pd.DataFrame,
|
|
28
|
+
column: str,
|
|
29
|
+
coverage: float = 0.95,
|
|
30
|
+
confidence: float = 0.95,
|
|
31
|
+
sided: str = "two-sided",
|
|
32
|
+
method: str = "normal",
|
|
33
|
+
) -> AnalysisResult:
|
|
34
|
+
if column not in df.columns:
|
|
35
|
+
raise KeyError(column)
|
|
36
|
+
if not (0 < coverage < 1 and 0 < confidence < 1):
|
|
37
|
+
raise ValueError("Coverage and confidence must be between 0 and 1.")
|
|
38
|
+
if sided not in {"two-sided", "lower", "upper"}:
|
|
39
|
+
raise ValueError("sided must be two-sided, lower, or upper.")
|
|
40
|
+
method = method.lower()
|
|
41
|
+
values = pd.to_numeric(df[column], errors="coerce").dropna().to_numpy(dtype=float)
|
|
42
|
+
n = len(values)
|
|
43
|
+
if n < 2:
|
|
44
|
+
raise ValueError("At least two observations are required.")
|
|
45
|
+
order_label = "Parametric"
|
|
46
|
+
achieved = confidence
|
|
47
|
+
if method in {"normal", "parametric"}:
|
|
48
|
+
mean = float(np.mean(values))
|
|
49
|
+
std = float(np.std(values, ddof=1))
|
|
50
|
+
factor = _normal_factor(n, coverage, confidence, sided)
|
|
51
|
+
if sided == "two-sided":
|
|
52
|
+
lower, upper = mean - factor * std, mean + factor * std
|
|
53
|
+
elif sided == "lower":
|
|
54
|
+
lower, upper = mean - factor * std, np.inf
|
|
55
|
+
else:
|
|
56
|
+
lower, upper = -np.inf, mean + factor * std
|
|
57
|
+
method_label = "Normal tolerance interval"
|
|
58
|
+
else:
|
|
59
|
+
ordered = np.sort(values)
|
|
60
|
+
tail = 1 if sided != "two-sided" else 2
|
|
61
|
+
chosen = 1
|
|
62
|
+
achieved = 0.0
|
|
63
|
+
for k in range(1, n // tail + 1):
|
|
64
|
+
alpha = n - tail * k + 1
|
|
65
|
+
beta = tail * k
|
|
66
|
+
probability = float(stats.beta.sf(coverage, alpha, beta)) if alpha > 0 else 0.0
|
|
67
|
+
if k == 1:
|
|
68
|
+
achieved = probability
|
|
69
|
+
if probability >= confidence:
|
|
70
|
+
chosen = k
|
|
71
|
+
achieved = probability
|
|
72
|
+
break
|
|
73
|
+
if k == 1:
|
|
74
|
+
achieved = probability
|
|
75
|
+
if sided == "two-sided":
|
|
76
|
+
lower, upper = float(ordered[chosen - 1]), float(ordered[-chosen])
|
|
77
|
+
order_label = f"X({chosen}) and X({n - chosen + 1})"
|
|
78
|
+
elif sided == "lower":
|
|
79
|
+
lower, upper = float(ordered[chosen - 1]), np.inf
|
|
80
|
+
order_label = f"X({chosen})"
|
|
81
|
+
else:
|
|
82
|
+
lower, upper = -np.inf, float(ordered[-chosen])
|
|
83
|
+
order_label = f"X({n - chosen + 1})"
|
|
84
|
+
factor = np.nan
|
|
85
|
+
mean = float(np.mean(values))
|
|
86
|
+
std = float(np.std(values, ddof=1))
|
|
87
|
+
method_label = "Nonparametric tolerance interval"
|
|
88
|
+
table = pd.DataFrame([{
|
|
89
|
+
"Method": method_label,
|
|
90
|
+
"N": n,
|
|
91
|
+
"Mean": mean,
|
|
92
|
+
"Std Dev": std,
|
|
93
|
+
"Coverage": coverage,
|
|
94
|
+
"Confidence": confidence,
|
|
95
|
+
"Sided": sided,
|
|
96
|
+
"K Factor": factor,
|
|
97
|
+
"Order Statistics": order_label,
|
|
98
|
+
"Achieved Confidence": achieved,
|
|
99
|
+
"Lower Limit": lower,
|
|
100
|
+
"Upper Limit": upper,
|
|
101
|
+
}])
|
|
102
|
+
result = AnalysisResult(
|
|
103
|
+
title=f"Tolerance Interval: {column}",
|
|
104
|
+
analysis_type="tolerance_interval",
|
|
105
|
+
params={"column": column, "coverage": coverage, "confidence": confidence, "sided": sided, "method": method},
|
|
106
|
+
)
|
|
107
|
+
result.add_table("Tolerance Interval", table)
|
|
108
|
+
result.add_plot("histogram", {"column": column}, title=f"Tolerance Interval: {column}", data=df)
|
|
109
|
+
result.summary = (
|
|
110
|
+
f"{method_label} for {coverage:.0%} coverage with {confidence:.0%} confidence: "
|
|
111
|
+
f"[{lower:.6g}, {upper:.6g}]."
|
|
112
|
+
)
|
|
113
|
+
if method not in {"normal", "parametric"} and achieved + 1e-12 < confidence:
|
|
114
|
+
result.notes = (
|
|
115
|
+
f"The largest available distribution-free interval reaches "
|
|
116
|
+
f"{achieved:.1%} confidence, below the requested {confidence:.1%}. "
|
|
117
|
+
"Collect more observations to meet the requested confidence."
|
|
118
|
+
)
|
|
119
|
+
return result
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def sample_size_tolerance(
|
|
123
|
+
standard_deviation: float,
|
|
124
|
+
margin: float,
|
|
125
|
+
coverage: float = 0.95,
|
|
126
|
+
confidence: float = 0.95,
|
|
127
|
+
sided: str = "two-sided",
|
|
128
|
+
) -> AnalysisResult:
|
|
129
|
+
if standard_deviation <= 0 or margin <= 0:
|
|
130
|
+
raise ValueError("Standard deviation and margin must be positive.")
|
|
131
|
+
for n in range(2, 1_000_000):
|
|
132
|
+
factor = _normal_factor(n, coverage, confidence, sided)
|
|
133
|
+
if factor * standard_deviation <= margin:
|
|
134
|
+
break
|
|
135
|
+
else:
|
|
136
|
+
raise ValueError("Could not find a sample size.")
|
|
137
|
+
result = AnalysisResult(
|
|
138
|
+
title="Sample Size for Tolerance Interval",
|
|
139
|
+
analysis_type="tolerance_sample_size",
|
|
140
|
+
params={"standard_deviation": standard_deviation, "margin": margin, "coverage": coverage, "confidence": confidence, "sided": sided},
|
|
141
|
+
)
|
|
142
|
+
result.add_table(
|
|
143
|
+
"Sample Size",
|
|
144
|
+
pd.DataFrame([{
|
|
145
|
+
"N": n,
|
|
146
|
+
"Std Dev": standard_deviation,
|
|
147
|
+
"Margin": margin,
|
|
148
|
+
"Coverage": coverage,
|
|
149
|
+
"Confidence": confidence,
|
|
150
|
+
"K Factor": factor,
|
|
151
|
+
}]),
|
|
152
|
+
)
|
|
153
|
+
result.summary = f"Requires {n} observations for a {coverage:.0%}/{confidence:.0%} tolerance interval within {margin:g}."
|
|
154
|
+
return result
|
|
File without changes
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from PySide6.QtCore import QSize, Qt, QTimer
|
|
4
|
+
from PySide6.QtWidgets import (
|
|
5
|
+
QFrame,
|
|
6
|
+
QHBoxLayout,
|
|
7
|
+
QLabel,
|
|
8
|
+
QLineEdit,
|
|
9
|
+
QListWidget,
|
|
10
|
+
QListWidgetItem,
|
|
11
|
+
QPushButton,
|
|
12
|
+
QSplitter,
|
|
13
|
+
QTreeWidget,
|
|
14
|
+
QTreeWidgetItem,
|
|
15
|
+
QVBoxLayout,
|
|
16
|
+
QWidget,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
from statstudio.ui import icons
|
|
20
|
+
from statstudio.ui.command_catalog import HubCommand
|
|
21
|
+
from statstudio.ui.dialogs.window import IndependentDialog
|
|
22
|
+
from statstudio.ui.recent import favorite_commands, recent_commands, toggle_favorite
|
|
23
|
+
|
|
24
|
+
_ALL = "All commands"
|
|
25
|
+
_FAVORITES = "Favorites"
|
|
26
|
+
_RECENT = "Recent"
|
|
27
|
+
_CATEGORY_ORDER = ("Data", "Compare", "Model", "Quality", "DOE", "Graphs", "Reports", "Scripting", "Workspace")
|
|
28
|
+
_CATEGORY_ICONS = {
|
|
29
|
+
"Data": "table",
|
|
30
|
+
"Compare": "analyze",
|
|
31
|
+
"Model": "graph",
|
|
32
|
+
"Quality": "quality",
|
|
33
|
+
"DOE": "doe",
|
|
34
|
+
"Graphs": "graph",
|
|
35
|
+
"Reports": "report",
|
|
36
|
+
"Scripting": "script",
|
|
37
|
+
"Workspace": "home",
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _score(command: HubCommand, tokens: list[str]) -> int:
|
|
42
|
+
if not tokens:
|
|
43
|
+
return 1
|
|
44
|
+
title = command.title.lower()
|
|
45
|
+
haystack = " ".join(
|
|
46
|
+
[
|
|
47
|
+
title,
|
|
48
|
+
command.description.lower(),
|
|
49
|
+
command.category.lower(),
|
|
50
|
+
command.menu_path.lower(),
|
|
51
|
+
" ".join(command.keywords).lower(),
|
|
52
|
+
]
|
|
53
|
+
)
|
|
54
|
+
if not all(token in haystack for token in tokens):
|
|
55
|
+
return 0
|
|
56
|
+
score = 10
|
|
57
|
+
for token in tokens:
|
|
58
|
+
if title.startswith(token):
|
|
59
|
+
score += 8
|
|
60
|
+
elif token in title:
|
|
61
|
+
score += 5
|
|
62
|
+
elif token in command.category.lower():
|
|
63
|
+
score += 3
|
|
64
|
+
elif token in command.menu_path.lower():
|
|
65
|
+
score += 1
|
|
66
|
+
return score
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class AnalysisHubDialog(IndependentDialog):
|
|
70
|
+
def __init__(
|
|
71
|
+
self,
|
|
72
|
+
commands: dict[str, HubCommand],
|
|
73
|
+
parent: QWidget | None = None,
|
|
74
|
+
*,
|
|
75
|
+
initial_query: str = "",
|
|
76
|
+
) -> None:
|
|
77
|
+
super().__init__(parent)
|
|
78
|
+
self.setWindowTitle("Analysis Hub")
|
|
79
|
+
self.resize(940, 640)
|
|
80
|
+
self.commands = commands
|
|
81
|
+
self.chosen_id: str | None = None
|
|
82
|
+
self._category = _ALL
|
|
83
|
+
self._items: dict[str, QTreeWidgetItem] = {}
|
|
84
|
+
|
|
85
|
+
layout = QVBoxLayout(self)
|
|
86
|
+
layout.setContentsMargins(16, 14, 16, 14)
|
|
87
|
+
layout.setSpacing(10)
|
|
88
|
+
|
|
89
|
+
heading = QLabel("Analysis Hub")
|
|
90
|
+
heading.setProperty("role", "h2")
|
|
91
|
+
layout.addWidget(heading)
|
|
92
|
+
hint = QLabel("Find an analysis, graph, design, report, or workspace command.")
|
|
93
|
+
hint.setProperty("role", "muted")
|
|
94
|
+
layout.addWidget(hint)
|
|
95
|
+
|
|
96
|
+
self.search = QLineEdit()
|
|
97
|
+
self.search.setObjectName("hubSearch")
|
|
98
|
+
self.search.setPlaceholderText("Search by task, method, menu name, or keyword")
|
|
99
|
+
self.search.setClearButtonEnabled(True)
|
|
100
|
+
self.search.textChanged.connect(self._refresh_commands)
|
|
101
|
+
self.search.returnPressed.connect(self._open_selected)
|
|
102
|
+
layout.addWidget(self.search)
|
|
103
|
+
|
|
104
|
+
splitter = QSplitter(Qt.Orientation.Horizontal)
|
|
105
|
+
splitter.setChildrenCollapsible(False)
|
|
106
|
+
self.categories = QListWidget()
|
|
107
|
+
self.categories.setObjectName("hubCategories")
|
|
108
|
+
self.categories.setFixedWidth(170)
|
|
109
|
+
self.categories.currentItemChanged.connect(self._category_changed)
|
|
110
|
+
splitter.addWidget(self.categories)
|
|
111
|
+
|
|
112
|
+
right = QWidget()
|
|
113
|
+
right_layout = QVBoxLayout(right)
|
|
114
|
+
right_layout.setContentsMargins(0, 0, 0, 0)
|
|
115
|
+
right_layout.setSpacing(8)
|
|
116
|
+
self.tree = QTreeWidget()
|
|
117
|
+
self.tree.setObjectName("hubCommands")
|
|
118
|
+
self.tree.setHeaderLabels(["Command", "Description"])
|
|
119
|
+
self.tree.setRootIsDecorated(False)
|
|
120
|
+
self.tree.setUniformRowHeights(True)
|
|
121
|
+
self.tree.setAlternatingRowColors(False)
|
|
122
|
+
self.tree.setSelectionMode(QTreeWidget.SelectionMode.SingleSelection)
|
|
123
|
+
self.tree.setIconSize(QSize(18, 18))
|
|
124
|
+
self.tree.setColumnWidth(0, 285)
|
|
125
|
+
self.tree.header().setStretchLastSection(True)
|
|
126
|
+
self.tree.itemActivated.connect(lambda *_: self._open_selected())
|
|
127
|
+
self.tree.itemDoubleClicked.connect(lambda *_: self._open_selected())
|
|
128
|
+
self.tree.currentItemChanged.connect(self._selection_changed)
|
|
129
|
+
right_layout.addWidget(self.tree, 1)
|
|
130
|
+
self.empty = QLabel("No commands match this search.")
|
|
131
|
+
self.empty.setObjectName("emptyStateTitle")
|
|
132
|
+
self.empty.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
|
133
|
+
self.empty.setProperty("role", "muted")
|
|
134
|
+
self.empty.setVisible(False)
|
|
135
|
+
right_layout.addWidget(self.empty, 1)
|
|
136
|
+
splitter.addWidget(right)
|
|
137
|
+
splitter.setStretchFactor(1, 1)
|
|
138
|
+
layout.addWidget(splitter, 1)
|
|
139
|
+
|
|
140
|
+
self.detail = QFrame()
|
|
141
|
+
self.detail.setProperty("card", True)
|
|
142
|
+
detail_layout = QHBoxLayout(self.detail)
|
|
143
|
+
detail_layout.setContentsMargins(12, 8, 12, 8)
|
|
144
|
+
self.detail_text = QLabel("Select a command to see its menu location.")
|
|
145
|
+
self.detail_text.setWordWrap(True)
|
|
146
|
+
self.detail_text.setProperty("role", "muted")
|
|
147
|
+
detail_layout.addWidget(self.detail_text, 1)
|
|
148
|
+
self.pin_button = QPushButton(icons.action_icon("pin"), "Pin")
|
|
149
|
+
self.pin_button.setProperty("quiet", "true")
|
|
150
|
+
self.pin_button.clicked.connect(self._toggle_pin)
|
|
151
|
+
detail_layout.addWidget(self.pin_button)
|
|
152
|
+
layout.addWidget(self.detail)
|
|
153
|
+
|
|
154
|
+
buttons = QHBoxLayout()
|
|
155
|
+
self.status = QLabel("")
|
|
156
|
+
self.status.setProperty("role", "muted")
|
|
157
|
+
buttons.addWidget(self.status, 1)
|
|
158
|
+
close_button = QPushButton("Close")
|
|
159
|
+
close_button.clicked.connect(self.reject)
|
|
160
|
+
buttons.addWidget(close_button)
|
|
161
|
+
open_button = QPushButton("Open Command")
|
|
162
|
+
open_button.setProperty("primary", "true")
|
|
163
|
+
open_button.clicked.connect(self._open_selected)
|
|
164
|
+
buttons.addWidget(open_button)
|
|
165
|
+
layout.addLayout(buttons)
|
|
166
|
+
|
|
167
|
+
self._build_categories()
|
|
168
|
+
self.search.setText(initial_query)
|
|
169
|
+
self._refresh_commands()
|
|
170
|
+
QTimer.singleShot(0, self.search.setFocus)
|
|
171
|
+
|
|
172
|
+
def _build_categories(self) -> None:
|
|
173
|
+
self.categories.clear()
|
|
174
|
+
for label in (_ALL, _FAVORITES, _RECENT, *_CATEGORY_ORDER):
|
|
175
|
+
item = QListWidgetItem(label)
|
|
176
|
+
item.setData(Qt.ItemDataRole.UserRole, label)
|
|
177
|
+
if label in _CATEGORY_ICONS:
|
|
178
|
+
item.setIcon(icons.action_icon(_CATEGORY_ICONS[label]))
|
|
179
|
+
self.categories.addItem(item)
|
|
180
|
+
self.categories.setCurrentRow(0)
|
|
181
|
+
|
|
182
|
+
def _category_changed(self, current, _previous) -> None:
|
|
183
|
+
if current is None:
|
|
184
|
+
return
|
|
185
|
+
self._category = str(current.data(Qt.ItemDataRole.UserRole) or _ALL)
|
|
186
|
+
self._refresh_commands()
|
|
187
|
+
|
|
188
|
+
def _matching(self) -> list[HubCommand]:
|
|
189
|
+
tokens = [token for token in self.search.text().lower().split() if token]
|
|
190
|
+
favorites = set(favorite_commands())
|
|
191
|
+
recent = recent_commands()
|
|
192
|
+
if self._category == _FAVORITES:
|
|
193
|
+
candidates = [command for command in self.commands.values() if command.id in favorites]
|
|
194
|
+
elif self._category == _RECENT:
|
|
195
|
+
candidates = [self.commands[cid] for cid in recent if cid in self.commands]
|
|
196
|
+
elif self._category == _ALL:
|
|
197
|
+
candidates = list(self.commands.values())
|
|
198
|
+
else:
|
|
199
|
+
candidates = [command for command in self.commands.values() if command.category == self._category]
|
|
200
|
+
scored = [(command, _score(command, tokens)) for command in candidates]
|
|
201
|
+
scored = [(command, score) for command, score in scored if score]
|
|
202
|
+
if self._category == _RECENT:
|
|
203
|
+
order = {cid: index for index, cid in enumerate(recent)}
|
|
204
|
+
scored.sort(key=lambda pair: order.get(pair[0].id, 9999))
|
|
205
|
+
else:
|
|
206
|
+
scored.sort(
|
|
207
|
+
key=lambda pair: (
|
|
208
|
+
-pair[1],
|
|
209
|
+
_CATEGORY_ORDER.index(pair[0].category)
|
|
210
|
+
if pair[0].category in _CATEGORY_ORDER
|
|
211
|
+
else 99,
|
|
212
|
+
pair[0].title.lower(),
|
|
213
|
+
)
|
|
214
|
+
)
|
|
215
|
+
return [command for command, _score_value in scored]
|
|
216
|
+
|
|
217
|
+
def _refresh_commands(self, *_args) -> None:
|
|
218
|
+
selected_id = self._selected_id()
|
|
219
|
+
self.tree.clear()
|
|
220
|
+
self._items.clear()
|
|
221
|
+
commands = self._matching()
|
|
222
|
+
for command in commands:
|
|
223
|
+
item = QTreeWidgetItem([command.title, command.description])
|
|
224
|
+
item.setData(0, Qt.ItemDataRole.UserRole, command.id)
|
|
225
|
+
item.setToolTip(0, command.menu_path)
|
|
226
|
+
item.setToolTip(1, command.description)
|
|
227
|
+
item.setIcon(0, icons.action_icon(_CATEGORY_ICONS.get(command.category, "command")))
|
|
228
|
+
self.tree.addTopLevelItem(item)
|
|
229
|
+
self._items[command.id] = item
|
|
230
|
+
self.tree.setVisible(bool(commands))
|
|
231
|
+
self.empty.setVisible(not commands)
|
|
232
|
+
if commands:
|
|
233
|
+
item = self._items.get(selected_id or "") or self.tree.topLevelItem(0)
|
|
234
|
+
self.tree.setCurrentItem(item)
|
|
235
|
+
if selected_id is None:
|
|
236
|
+
item.setSelected(True)
|
|
237
|
+
else:
|
|
238
|
+
self._selection_changed(None, None)
|
|
239
|
+
self.status.setText(f"{len(commands)} command{'s' if len(commands) != 1 else ''}")
|
|
240
|
+
|
|
241
|
+
def _selected_id(self) -> str | None:
|
|
242
|
+
item = self.tree.currentItem()
|
|
243
|
+
return str(item.data(0, Qt.ItemDataRole.UserRole)) if item is not None else None
|
|
244
|
+
|
|
245
|
+
def _selection_changed(self, current, _previous) -> None:
|
|
246
|
+
command = self.commands.get(self._selected_id() or "")
|
|
247
|
+
if command is None:
|
|
248
|
+
self.detail_text.setText("Select a command to see its menu location.")
|
|
249
|
+
self.pin_button.setEnabled(False)
|
|
250
|
+
self.pin_button.setText("Pin")
|
|
251
|
+
return
|
|
252
|
+
self.detail_text.setText(f"{command.description}\n{command.menu_path}")
|
|
253
|
+
favorites = set(favorite_commands())
|
|
254
|
+
pinned = command.id in favorites
|
|
255
|
+
self.pin_button.setEnabled(True)
|
|
256
|
+
self.pin_button.setText("Unpin" if pinned else "Pin")
|
|
257
|
+
self.pin_button.setIcon(icons.action_icon("pin"))
|
|
258
|
+
|
|
259
|
+
def _toggle_pin(self) -> None:
|
|
260
|
+
command_id = self._selected_id()
|
|
261
|
+
if not command_id:
|
|
262
|
+
return
|
|
263
|
+
toggle_favorite(command_id)
|
|
264
|
+
self._refresh_commands()
|
|
265
|
+
|
|
266
|
+
def _open_selected(self) -> None:
|
|
267
|
+
command_id = self._selected_id()
|
|
268
|
+
if not command_id:
|
|
269
|
+
return
|
|
270
|
+
self.chosen_id = command_id
|
|
271
|
+
self.accept()
|