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,35 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
|
|
5
|
+
from statstudio.core.project import Project
|
|
6
|
+
from statstudio.core.runtime import build_namespace, execute_code, sync_tables
|
|
7
|
+
from statstudio.core.table import DataTable
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass
|
|
11
|
+
class ScriptOutcome:
|
|
12
|
+
output: str
|
|
13
|
+
ok: bool
|
|
14
|
+
tables: list[DataTable]
|
|
15
|
+
selected: DataTable | None = None
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def run_script_isolated(project: Project, code: str, table: DataTable | None = None) -> ScriptOutcome:
|
|
19
|
+
"""Execute a script against copied tables so the GUI stays responsive."""
|
|
20
|
+
copied = Project(
|
|
21
|
+
name=project.name,
|
|
22
|
+
path=project.path,
|
|
23
|
+
tables=[item.copy() for item in project.tables],
|
|
24
|
+
reports=list(project.reports),
|
|
25
|
+
scripts=dict(project.scripts),
|
|
26
|
+
journal=list(project.journal),
|
|
27
|
+
connections=list(project.connections),
|
|
28
|
+
)
|
|
29
|
+
selected = copied.find_table(table.name) if table is not None else None
|
|
30
|
+
namespace = build_namespace(copied, table=selected)
|
|
31
|
+
output, ok = execute_code(code, namespace, filename="<script>", capture_errors=True)
|
|
32
|
+
if ok:
|
|
33
|
+
sync_tables(copied, namespace, selected)
|
|
34
|
+
live_selected = selected if selected is not None and any(item is selected for item in copied.tables) else None
|
|
35
|
+
return ScriptOutcome(output=output, ok=ok, tables=list(copied.tables), selected=live_selected)
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from PySide6.QtCore import Signal
|
|
6
|
+
from PySide6.QtGui import QFont
|
|
7
|
+
from PySide6.QtWidgets import (
|
|
8
|
+
QComboBox,
|
|
9
|
+
QFileDialog,
|
|
10
|
+
QHBoxLayout,
|
|
11
|
+
QInputDialog,
|
|
12
|
+
QLabel,
|
|
13
|
+
QMessageBox,
|
|
14
|
+
QPlainTextEdit,
|
|
15
|
+
QToolButton,
|
|
16
|
+
QVBoxLayout,
|
|
17
|
+
QWidget,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
from statstudio.core.project import Project
|
|
21
|
+
from statstudio.ui import icons
|
|
22
|
+
from statstudio.ui.console import PythonConsole
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class ScriptsPanel(QWidget):
|
|
26
|
+
"""A small script manager persisted inside the project file."""
|
|
27
|
+
|
|
28
|
+
project_changed = Signal()
|
|
29
|
+
|
|
30
|
+
def __init__(self, project: Project, console: PythonConsole, parent: QWidget | None = None) -> None:
|
|
31
|
+
super().__init__(parent)
|
|
32
|
+
self.project = project
|
|
33
|
+
self.console = console
|
|
34
|
+
self._job = None
|
|
35
|
+
self._dirty = False
|
|
36
|
+
self._loading = False
|
|
37
|
+
self._current_name = ""
|
|
38
|
+
layout = QVBoxLayout(self)
|
|
39
|
+
layout.setContentsMargins(4, 4, 4, 4)
|
|
40
|
+
top = QHBoxLayout()
|
|
41
|
+
top.addWidget(QLabel("Script"))
|
|
42
|
+
self.combo = QComboBox()
|
|
43
|
+
self.combo.currentIndexChanged.connect(self._switch_script)
|
|
44
|
+
top.addWidget(self.combo, 1)
|
|
45
|
+
self.dirty_label = QLabel("Modified")
|
|
46
|
+
self.dirty_label.setObjectName("statusBadge")
|
|
47
|
+
self.dirty_label.setToolTip("This script has unsaved changes.")
|
|
48
|
+
self.dirty_label.setVisible(False)
|
|
49
|
+
top.addWidget(self.dirty_label)
|
|
50
|
+
new_button = QToolButton()
|
|
51
|
+
icons.set_button_icon(new_button, "plus")
|
|
52
|
+
new_button.setToolTip("New script")
|
|
53
|
+
new_button.setAutoRaise(True)
|
|
54
|
+
new_button.clicked.connect(self._new)
|
|
55
|
+
top.addWidget(new_button)
|
|
56
|
+
import_button = QToolButton()
|
|
57
|
+
icons.set_button_icon(import_button, "import")
|
|
58
|
+
import_button.setToolTip("Import a Python script from a file into this project.")
|
|
59
|
+
import_button.setAutoRaise(True)
|
|
60
|
+
import_button.clicked.connect(self._import)
|
|
61
|
+
top.addWidget(import_button)
|
|
62
|
+
save_button = QToolButton()
|
|
63
|
+
icons.set_button_icon(save_button, "save")
|
|
64
|
+
save_button.setToolTip("Save the current script")
|
|
65
|
+
save_button.setAutoRaise(True)
|
|
66
|
+
save_button.clicked.connect(self._save)
|
|
67
|
+
top.addWidget(save_button)
|
|
68
|
+
run_button = QToolButton()
|
|
69
|
+
icons.set_button_icon(run_button, "check")
|
|
70
|
+
run_button.setToolTip("Run the current script")
|
|
71
|
+
run_button.setAutoRaise(True)
|
|
72
|
+
run_button.clicked.connect(self._run)
|
|
73
|
+
top.addWidget(run_button)
|
|
74
|
+
delete_button = QToolButton()
|
|
75
|
+
icons.set_button_icon(delete_button, "trash")
|
|
76
|
+
delete_button.setToolTip("Delete the current script")
|
|
77
|
+
delete_button.setAutoRaise(True)
|
|
78
|
+
delete_button.clicked.connect(self._delete)
|
|
79
|
+
top.addWidget(delete_button)
|
|
80
|
+
layout.addLayout(top)
|
|
81
|
+
self.editor = QPlainTextEdit()
|
|
82
|
+
self.editor.setFont(QFont("monospace", 9))
|
|
83
|
+
self.editor.setPlaceholderText("Write Python here. Use sdk.datasets(), stats, plots, project, tables().")
|
|
84
|
+
self.editor.textChanged.connect(self._mark_dirty)
|
|
85
|
+
layout.addWidget(self.editor, 1)
|
|
86
|
+
self.refresh()
|
|
87
|
+
from statstudio.ui.dialogs.help_topics import install_context_help
|
|
88
|
+
|
|
89
|
+
install_context_help(self, "scripts-panel")
|
|
90
|
+
|
|
91
|
+
def set_project(self, project: Project) -> None:
|
|
92
|
+
if self._dirty and self._current_name:
|
|
93
|
+
self.project.scripts[self._current_name] = self.editor.toPlainText()
|
|
94
|
+
self._set_clean()
|
|
95
|
+
self.project = project
|
|
96
|
+
self.refresh()
|
|
97
|
+
|
|
98
|
+
def refresh(self) -> None:
|
|
99
|
+
if self._dirty and self._current_name:
|
|
100
|
+
self.project.scripts[self._current_name] = self.editor.toPlainText()
|
|
101
|
+
self._set_clean()
|
|
102
|
+
current = self.combo.currentText()
|
|
103
|
+
self.combo.blockSignals(True)
|
|
104
|
+
self.combo.clear()
|
|
105
|
+
self.combo.addItems(sorted(self.project.scripts.keys()))
|
|
106
|
+
self.combo.blockSignals(False)
|
|
107
|
+
if current and current in self.project.scripts:
|
|
108
|
+
self.combo.setCurrentText(current)
|
|
109
|
+
elif self.combo.count():
|
|
110
|
+
self.combo.setCurrentIndex(0)
|
|
111
|
+
self._load_current(self.combo.currentText())
|
|
112
|
+
|
|
113
|
+
def _mark_dirty(self) -> None:
|
|
114
|
+
if not self._loading:
|
|
115
|
+
self._dirty = True
|
|
116
|
+
self.dirty_label.setVisible(True)
|
|
117
|
+
self.project_changed.emit()
|
|
118
|
+
|
|
119
|
+
def _set_clean(self) -> None:
|
|
120
|
+
self._dirty = False
|
|
121
|
+
self.dirty_label.setVisible(False)
|
|
122
|
+
|
|
123
|
+
def _switch_script(self, index: int) -> None:
|
|
124
|
+
if self._dirty and self._current_name:
|
|
125
|
+
self.project.scripts[self._current_name] = self.editor.toPlainText()
|
|
126
|
+
self._set_clean()
|
|
127
|
+
self._load_current(self.combo.currentText() if index >= 0 else "")
|
|
128
|
+
|
|
129
|
+
def _load_current(self, name: str) -> None:
|
|
130
|
+
self._current_name = name
|
|
131
|
+
self._loading = True
|
|
132
|
+
if name and name in self.project.scripts:
|
|
133
|
+
self.editor.setPlainText(self.project.scripts[name])
|
|
134
|
+
elif not name:
|
|
135
|
+
self.editor.clear()
|
|
136
|
+
self._loading = False
|
|
137
|
+
self._set_clean()
|
|
138
|
+
|
|
139
|
+
def _new(self) -> None:
|
|
140
|
+
name, ok = QInputDialog.getText(self, "New Script", "Script name")
|
|
141
|
+
if not ok or not name.strip():
|
|
142
|
+
return
|
|
143
|
+
name = name.strip()
|
|
144
|
+
if name in self.project.scripts:
|
|
145
|
+
QMessageBox.warning(self, "New Script", "A script with that name already exists.")
|
|
146
|
+
return
|
|
147
|
+
self.project.scripts[name] = ""
|
|
148
|
+
self.refresh()
|
|
149
|
+
self.project_changed.emit()
|
|
150
|
+
index = self.combo.findText(name)
|
|
151
|
+
if index >= 0:
|
|
152
|
+
self.combo.setCurrentIndex(index)
|
|
153
|
+
self.editor.clear()
|
|
154
|
+
|
|
155
|
+
def _import(self) -> None:
|
|
156
|
+
path, _ = QFileDialog.getOpenFileName(
|
|
157
|
+
self,
|
|
158
|
+
"Import Script",
|
|
159
|
+
"",
|
|
160
|
+
"Python scripts (*.py);;All files (*)",
|
|
161
|
+
)
|
|
162
|
+
if not path:
|
|
163
|
+
return
|
|
164
|
+
try:
|
|
165
|
+
text = Path(path).read_text(encoding="utf-8")
|
|
166
|
+
except UnicodeDecodeError:
|
|
167
|
+
try:
|
|
168
|
+
text = Path(path).read_text(encoding="latin-1")
|
|
169
|
+
except Exception as exc:
|
|
170
|
+
QMessageBox.critical(self, "Import Script", f"Could not read the file:\n{exc}")
|
|
171
|
+
return
|
|
172
|
+
except Exception as exc:
|
|
173
|
+
QMessageBox.critical(self, "Import Script", f"Could not read the file:\n{exc}")
|
|
174
|
+
return
|
|
175
|
+
name = Path(path).stem.strip() or "Imported Script"
|
|
176
|
+
if name in self.project.scripts:
|
|
177
|
+
replace = QMessageBox.question(
|
|
178
|
+
self,
|
|
179
|
+
"Import Script",
|
|
180
|
+
f"A script named {name!r} already exists. Replace it?",
|
|
181
|
+
)
|
|
182
|
+
if replace != QMessageBox.StandardButton.Yes:
|
|
183
|
+
return
|
|
184
|
+
self.project.scripts[name] = text
|
|
185
|
+
self.refresh()
|
|
186
|
+
self.project_changed.emit()
|
|
187
|
+
index = self.combo.findText(name)
|
|
188
|
+
if index >= 0:
|
|
189
|
+
self.combo.setCurrentIndex(index)
|
|
190
|
+
self._current_name = name
|
|
191
|
+
self._set_clean()
|
|
192
|
+
self.console.output.appendPlainText(f"Imported script {name!r} from {path}")
|
|
193
|
+
|
|
194
|
+
def _save(self) -> None:
|
|
195
|
+
text = self.editor.toPlainText()
|
|
196
|
+
name = self.combo.currentText().strip()
|
|
197
|
+
if not name:
|
|
198
|
+
self._new()
|
|
199
|
+
name = self.combo.currentText().strip()
|
|
200
|
+
if not name:
|
|
201
|
+
return
|
|
202
|
+
self.editor.setPlainText(text)
|
|
203
|
+
self.project.scripts[name] = self.editor.toPlainText()
|
|
204
|
+
self._current_name = name
|
|
205
|
+
self._set_clean()
|
|
206
|
+
self.project_changed.emit()
|
|
207
|
+
self.console.output.appendPlainText(f"Saved script {name!r}")
|
|
208
|
+
|
|
209
|
+
def _run(self) -> None:
|
|
210
|
+
code = self.editor.toPlainText()
|
|
211
|
+
if not code.strip() or self._job is not None:
|
|
212
|
+
return
|
|
213
|
+
window = self.console.window()
|
|
214
|
+
console_dock = getattr(window, "console_dock", None)
|
|
215
|
+
if console_dock is not None:
|
|
216
|
+
console_dock.show()
|
|
217
|
+
console_dock.raise_()
|
|
218
|
+
table = None
|
|
219
|
+
active = getattr(window, "_active_table_view", None)
|
|
220
|
+
if callable(active):
|
|
221
|
+
view = active()
|
|
222
|
+
table = getattr(view, "table", None)
|
|
223
|
+
from statstudio.ui.jobs import start_job
|
|
224
|
+
from statstudio.ui.script_jobs import run_script_isolated
|
|
225
|
+
|
|
226
|
+
project = self.project
|
|
227
|
+
self.console.history.append(code)
|
|
228
|
+
self.console.output.appendPlainText(">>> [script]")
|
|
229
|
+
|
|
230
|
+
def completed(outcome) -> None:
|
|
231
|
+
self._job = None
|
|
232
|
+
if outcome.output:
|
|
233
|
+
self.console.output.appendPlainText(outcome.output)
|
|
234
|
+
if not outcome.ok:
|
|
235
|
+
self.console.output.appendPlainText("Script failed; see the traceback above.")
|
|
236
|
+
apply = getattr(window, "apply_script_outcome", None)
|
|
237
|
+
if outcome.ok and callable(apply):
|
|
238
|
+
apply(outcome, table)
|
|
239
|
+
elif hasattr(window, "_mark_project_dirty"):
|
|
240
|
+
window._mark_project_dirty()
|
|
241
|
+
|
|
242
|
+
def failed(message: str, details: str) -> None:
|
|
243
|
+
self._job = None
|
|
244
|
+
self.console.output.appendPlainText(f"{message}\n{details}")
|
|
245
|
+
if hasattr(window, "_log"):
|
|
246
|
+
window._log("Script failed; see the Python console for details.")
|
|
247
|
+
|
|
248
|
+
def cancelled() -> None:
|
|
249
|
+
self._job = None
|
|
250
|
+
self.console.output.appendPlainText("Script cancelled.")
|
|
251
|
+
|
|
252
|
+
self._job = start_job(
|
|
253
|
+
self,
|
|
254
|
+
"Running Script",
|
|
255
|
+
lambda token, progress: run_script_isolated(project, code, table),
|
|
256
|
+
on_result=completed,
|
|
257
|
+
on_error=failed,
|
|
258
|
+
on_cancelled=cancelled,
|
|
259
|
+
)
|
|
260
|
+
|
|
261
|
+
def _delete(self) -> None:
|
|
262
|
+
name = self.combo.currentText().strip()
|
|
263
|
+
if not name:
|
|
264
|
+
return
|
|
265
|
+
if QMessageBox.question(self, "Delete Script", f"Delete script {name!r}?") != QMessageBox.StandardButton.Yes:
|
|
266
|
+
return
|
|
267
|
+
self.project.scripts.pop(name, None)
|
|
268
|
+
self._set_clean()
|
|
269
|
+
self.editor.clear()
|
|
270
|
+
self.refresh()
|
|
271
|
+
self.project_changed.emit()
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from PySide6.QtCore import Qt, Signal
|
|
4
|
+
from PySide6.QtGui import QKeyEvent
|
|
5
|
+
from PySide6.QtWidgets import (
|
|
6
|
+
QCompleter,
|
|
7
|
+
QFrame,
|
|
8
|
+
QHBoxLayout,
|
|
9
|
+
QLabel,
|
|
10
|
+
QLineEdit,
|
|
11
|
+
QPushButton,
|
|
12
|
+
QVBoxLayout,
|
|
13
|
+
QWidget,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
from statstudio.core.table import DataTable, format_value
|
|
17
|
+
from statstudio.ui import icons
|
|
18
|
+
from statstudio.ui.formula_editor import FormulaHighlighter, FormulaTextEdit, completion_model
|
|
19
|
+
from statstudio.ui.table_view import DataTableView
|
|
20
|
+
from statstudio.ui.window_controls import WindowControls
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class DoubleClickLabel(QLabel):
|
|
24
|
+
double_clicked = Signal()
|
|
25
|
+
|
|
26
|
+
def mouseDoubleClickEvent(self, event) -> None:
|
|
27
|
+
self.double_clicked.emit()
|
|
28
|
+
event.accept()
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class TableNameEdit(QLineEdit):
|
|
32
|
+
cancelled = Signal()
|
|
33
|
+
|
|
34
|
+
def keyPressEvent(self, event: QKeyEvent) -> None:
|
|
35
|
+
if event.key() == Qt.Key.Key_Escape:
|
|
36
|
+
self.cancelled.emit()
|
|
37
|
+
event.accept()
|
|
38
|
+
return
|
|
39
|
+
super().keyPressEvent(event)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class TableDocument(QWidget):
|
|
43
|
+
rename_requested = Signal(str)
|
|
44
|
+
minimize_requested = Signal()
|
|
45
|
+
maximize_requested = Signal()
|
|
46
|
+
delete_requested = Signal()
|
|
47
|
+
filter_requested = Signal(str)
|
|
48
|
+
filter_cleared = Signal()
|
|
49
|
+
formula_requested = Signal(str, str)
|
|
50
|
+
|
|
51
|
+
def __init__(self, table: DataTable, view: DataTableView, parent: QWidget | None = None) -> None:
|
|
52
|
+
super().__init__(parent)
|
|
53
|
+
self.table = table
|
|
54
|
+
self.view = view
|
|
55
|
+
self.setObjectName("tableDocument")
|
|
56
|
+
layout = QVBoxLayout(self)
|
|
57
|
+
layout.setContentsMargins(0, 0, 0, 0)
|
|
58
|
+
layout.setSpacing(0)
|
|
59
|
+
|
|
60
|
+
self.header = QFrame()
|
|
61
|
+
self.header.setObjectName("documentHeader")
|
|
62
|
+
row = QHBoxLayout(self.header)
|
|
63
|
+
row.setContentsMargins(9, 4, 6, 4)
|
|
64
|
+
row.setSpacing(8)
|
|
65
|
+
self.title_label = DoubleClickLabel(table.name)
|
|
66
|
+
self.title_label.setObjectName("documentTitle")
|
|
67
|
+
self.title_label.setToolTip("Double-click to rename this table")
|
|
68
|
+
self.title_label.double_clicked.connect(self.begin_rename)
|
|
69
|
+
self.title_edit = TableNameEdit(table.name)
|
|
70
|
+
self.title_edit.setVisible(False)
|
|
71
|
+
self.title_edit.setMinimumWidth(180)
|
|
72
|
+
self.title_edit.returnPressed.connect(self.commit_rename)
|
|
73
|
+
self.title_edit.editingFinished.connect(self.commit_rename)
|
|
74
|
+
self.title_edit.cancelled.connect(self.cancel_rename)
|
|
75
|
+
self.meta_label = QLabel("")
|
|
76
|
+
self.meta_label.setObjectName("documentMeta")
|
|
77
|
+
row.addWidget(self.title_label)
|
|
78
|
+
row.addWidget(self.meta_label)
|
|
79
|
+
row.addWidget(self.title_edit, 1)
|
|
80
|
+
row.addStretch(1)
|
|
81
|
+
self.window_controls = WindowControls()
|
|
82
|
+
self.window_controls.minimize_requested.connect(self.minimize_requested.emit)
|
|
83
|
+
self.window_controls.maximize_requested.connect(self.maximize_requested.emit)
|
|
84
|
+
self.window_controls.delete_requested.connect(self.delete_requested.emit)
|
|
85
|
+
row.addWidget(self.window_controls)
|
|
86
|
+
layout.addWidget(self.header)
|
|
87
|
+
|
|
88
|
+
filter_bar = QFrame()
|
|
89
|
+
filter_bar.setObjectName("subToolbar")
|
|
90
|
+
filter_row = QHBoxLayout(filter_bar)
|
|
91
|
+
filter_row.setContentsMargins(9, 3, 9, 3)
|
|
92
|
+
filter_row.setSpacing(6)
|
|
93
|
+
filter_label = QLabel("Filter")
|
|
94
|
+
filter_label.setProperty("role", "muted")
|
|
95
|
+
filter_row.addWidget(filter_label)
|
|
96
|
+
self.filter_edit = QLineEdit(table.row_filter or "")
|
|
97
|
+
self.filter_edit.setPlaceholderText('Filter expression, e.g. col("x") > 5')
|
|
98
|
+
self.filter_edit.returnPressed.connect(self._apply_filter)
|
|
99
|
+
filter_row.addWidget(self.filter_edit, 1)
|
|
100
|
+
apply_filter = QPushButton("Apply")
|
|
101
|
+
icons.set_button_icon(apply_filter, "check")
|
|
102
|
+
apply_filter.setToolTip("Apply the row filter")
|
|
103
|
+
apply_filter.clicked.connect(self._apply_filter)
|
|
104
|
+
clear_filter = QPushButton("Clear")
|
|
105
|
+
clear_filter.setProperty("quiet", "true")
|
|
106
|
+
clear_filter.clicked.connect(self._clear_filter)
|
|
107
|
+
filter_row.addWidget(apply_filter)
|
|
108
|
+
filter_row.addWidget(clear_filter)
|
|
109
|
+
layout.addWidget(filter_bar)
|
|
110
|
+
|
|
111
|
+
formula_bar = QFrame()
|
|
112
|
+
formula_bar.setObjectName("subToolbar")
|
|
113
|
+
formula_row = QHBoxLayout(formula_bar)
|
|
114
|
+
formula_row.setContentsMargins(9, 3, 9, 3)
|
|
115
|
+
formula_row.setSpacing(6)
|
|
116
|
+
formula_label = QLabel("Formula")
|
|
117
|
+
formula_label.setProperty("role", "muted")
|
|
118
|
+
formula_row.addWidget(formula_label)
|
|
119
|
+
self.formula_edit = FormulaTextEdit()
|
|
120
|
+
self.formula_edit.setMaximumHeight(42)
|
|
121
|
+
self.formula_edit.setPlaceholderText("Selected cell or column formula")
|
|
122
|
+
model, insert_map = completion_model(table.columns)
|
|
123
|
+
self._formula_completer = QCompleter(model, self)
|
|
124
|
+
self.formula_edit.set_completer(self._formula_completer, insert_map)
|
|
125
|
+
self._formula_highlighter = FormulaHighlighter(self.formula_edit.document())
|
|
126
|
+
formula_row.addWidget(self.formula_edit, 1)
|
|
127
|
+
apply_formula = QPushButton("Apply")
|
|
128
|
+
icons.set_button_icon(apply_formula, "check")
|
|
129
|
+
apply_formula.setToolTip("Apply the formula to the selected column or cell")
|
|
130
|
+
apply_formula.clicked.connect(self._apply_formula)
|
|
131
|
+
formula_row.addWidget(apply_formula)
|
|
132
|
+
layout.addWidget(formula_bar)
|
|
133
|
+
layout.addWidget(view, 1)
|
|
134
|
+
view.selectionModel().currentChanged.connect(lambda *_: self._sync_formula_bar())
|
|
135
|
+
self._install_help()
|
|
136
|
+
self._sync_formula_bar()
|
|
137
|
+
self.refresh_meta()
|
|
138
|
+
|
|
139
|
+
def _install_help(self) -> None:
|
|
140
|
+
from statstudio.ui.dialogs.help_topics import install_context_help
|
|
141
|
+
|
|
142
|
+
install_context_help(self, "table-editing")
|
|
143
|
+
|
|
144
|
+
def _apply_filter(self) -> None:
|
|
145
|
+
self.filter_requested.emit(self.filter_edit.text().strip())
|
|
146
|
+
|
|
147
|
+
def _clear_filter(self) -> None:
|
|
148
|
+
self.filter_edit.clear()
|
|
149
|
+
self.filter_cleared.emit()
|
|
150
|
+
|
|
151
|
+
def _selected_column(self) -> str | None:
|
|
152
|
+
index = self.view.currentIndex()
|
|
153
|
+
if not index.isValid():
|
|
154
|
+
return None
|
|
155
|
+
return self.view.model().column_name(index.column())
|
|
156
|
+
|
|
157
|
+
def _sync_formula_bar(self) -> None:
|
|
158
|
+
index = self.view.currentIndex()
|
|
159
|
+
if not index.isValid():
|
|
160
|
+
self.formula_edit.clear()
|
|
161
|
+
return
|
|
162
|
+
column = self.view.model().column_name(index.column())
|
|
163
|
+
if column is None:
|
|
164
|
+
self.formula_edit.clear()
|
|
165
|
+
return
|
|
166
|
+
spec = self.table.specs.get(column)
|
|
167
|
+
if spec is not None and spec.formula:
|
|
168
|
+
self.formula_edit.setPlainText(spec.formula)
|
|
169
|
+
return
|
|
170
|
+
row_ids = self.view.model().row_ids([index.row()])
|
|
171
|
+
if not row_ids:
|
|
172
|
+
self.formula_edit.clear()
|
|
173
|
+
return
|
|
174
|
+
row_id = row_ids[0]
|
|
175
|
+
if row_id in self.table.data.index and column in self.table.data.columns:
|
|
176
|
+
self.formula_edit.setPlainText(format_value(spec, self.table.data.at[row_id, column]))
|
|
177
|
+
else:
|
|
178
|
+
self.formula_edit.clear()
|
|
179
|
+
|
|
180
|
+
def _apply_formula(self) -> None:
|
|
181
|
+
index = self.view.currentIndex()
|
|
182
|
+
if not index.isValid():
|
|
183
|
+
return
|
|
184
|
+
column = self.view.model().column_name(index.column())
|
|
185
|
+
if column is None:
|
|
186
|
+
return
|
|
187
|
+
text = self.formula_edit.toPlainText().strip()
|
|
188
|
+
spec = self.table.specs.get(column)
|
|
189
|
+
if spec is not None and spec.formula:
|
|
190
|
+
self.formula_requested.emit(column, text)
|
|
191
|
+
else:
|
|
192
|
+
self.view.model().setData(index, text, Qt.ItemDataRole.EditRole)
|
|
193
|
+
|
|
194
|
+
def refresh_bars(self) -> None:
|
|
195
|
+
self.filter_edit.setText(self.table.row_filter or "")
|
|
196
|
+
self._sync_formula_bar()
|
|
197
|
+
self.refresh_meta()
|
|
198
|
+
|
|
199
|
+
def refresh_meta(self) -> None:
|
|
200
|
+
parts = [f"{len(self.table)} rows \u00d7 {len(self.table.columns)} columns"]
|
|
201
|
+
if self.table.row_filter:
|
|
202
|
+
parts.append("filtered")
|
|
203
|
+
if self.table.modified:
|
|
204
|
+
parts.append("modified")
|
|
205
|
+
self.meta_label.setText(" \u00b7 ".join(parts))
|
|
206
|
+
|
|
207
|
+
def set_title(self, title: str) -> None:
|
|
208
|
+
if not self.title_edit.isVisible():
|
|
209
|
+
self.title_label.setText(title)
|
|
210
|
+
self.title_edit.setText(title)
|
|
211
|
+
self.refresh_meta()
|
|
212
|
+
|
|
213
|
+
def begin_rename(self) -> None:
|
|
214
|
+
self.title_label.setVisible(False)
|
|
215
|
+
self.title_edit.setText(self.table.name)
|
|
216
|
+
self.title_edit.setVisible(True)
|
|
217
|
+
self.title_edit.selectAll()
|
|
218
|
+
self.title_edit.setFocus(Qt.FocusReason.MouseFocusReason)
|
|
219
|
+
|
|
220
|
+
def cancel_rename(self) -> None:
|
|
221
|
+
self.title_edit.setVisible(False)
|
|
222
|
+
self.title_label.setVisible(True)
|
|
223
|
+
|
|
224
|
+
def commit_rename(self) -> None:
|
|
225
|
+
if not self.title_edit.isVisible():
|
|
226
|
+
return
|
|
227
|
+
name = self.title_edit.text().strip()
|
|
228
|
+
self.title_edit.setVisible(False)
|
|
229
|
+
self.title_label.setVisible(True)
|
|
230
|
+
if name and name != self.table.name:
|
|
231
|
+
self.rename_requested.emit(name)
|
|
232
|
+
else:
|
|
233
|
+
self.title_edit.setText(self.table.name)
|