molforge-studio 7.4.3__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.
Files changed (219) hide show
  1. main.py +71 -0
  2. molforge/__init__.py +19 -0
  3. molforge/ai/__init__.py +15 -0
  4. molforge/ai/deepseek_harness.py +185 -0
  5. molforge/ai/mcp/desktop_client.py +38 -0
  6. molforge/ai/mcp/desktop_service.py +121 -0
  7. molforge/ai/mcp/mcp_server.py +780 -0
  8. molforge/ai/mcp_bridge.py +945 -0
  9. molforge/ai/observation.py +152 -0
  10. molforge/ai/tools.py +455 -0
  11. molforge/application/__init__.py +22 -0
  12. molforge/application/ai/__init__.py +1 -0
  13. molforge/application/ai/deepseek_client.py +293 -0
  14. molforge/application/ai/observation.py +161 -0
  15. molforge/application/ai/tools.py +200 -0
  16. molforge/application/ai_proposals.py +166 -0
  17. molforge/application/bio_selection.py +143 -0
  18. molforge/application/builtin_commands.py +1445 -0
  19. molforge/application/calculators/__init__.py +10 -0
  20. molforge/application/calculators/base.py +143 -0
  21. molforge/application/calculators/calchem_engine.py +67 -0
  22. molforge/application/calculators/calchem_subprocess_worker.py +109 -0
  23. molforge/application/calculators/gaussian_engine.py +59 -0
  24. molforge/application/calculators/gaussian_job_spec.py +240 -0
  25. molforge/application/calculators/orca_engine.py +92 -0
  26. molforge/application/calculators/qc_input_generator.py +300 -0
  27. molforge/application/calculators/registry.py +29 -0
  28. molforge/application/calculators/rest_engine.py +262 -0
  29. molforge/application/command_bus.py +139 -0
  30. molforge/application/commands.py +55 -0
  31. molforge/application/conformers.py +143 -0
  32. molforge/application/document_session.py +156 -0
  33. molforge/application/events.py +74 -0
  34. molforge/application/fragment_catalog.py +422 -0
  35. molforge/application/fragment_commands.py +268 -0
  36. molforge/application/geometry_commands.py +84 -0
  37. molforge/application/history.py +105 -0
  38. molforge/application/internal_coordinates.py +293 -0
  39. molforge/application/invalidation.py +1174 -0
  40. molforge/application/jobs.py +185 -0
  41. molforge/application/measurement.py +167 -0
  42. molforge/application/measurement_targets.py +264 -0
  43. molforge/application/modeling_commands.py +61 -0
  44. molforge/application/plugins.py +197 -0
  45. molforge/application/ports/editor2d.py +85 -0
  46. molforge/application/reaction.py +75 -0
  47. molforge/application/selection.py +164 -0
  48. molforge/application/selection_commands.py +31 -0
  49. molforge/application/trajectory.py +120 -0
  50. molforge/application/trajectory_analysis.py +231 -0
  51. molforge/application/workspace_registry.py +226 -0
  52. molforge/core/__init__.py +12 -0
  53. molforge/core/conformer.py +45 -0
  54. molforge/core/molecule.py +150 -0
  55. molforge/core/orbital.py +33 -0
  56. molforge/core/properties.py +43 -0
  57. molforge/domain/__init__.py +12 -0
  58. molforge/domain/biomolecule.py +99 -0
  59. molforge/domain/chemical_summary.py +129 -0
  60. molforge/domain/chemistry.py +13 -0
  61. molforge/domain/coordinate_store.py +164 -0
  62. molforge/domain/coordination.py +133 -0
  63. molforge/domain/custom_fragments.py +255 -0
  64. molforge/domain/document.py +469 -0
  65. molforge/domain/element_reference.json +598 -0
  66. molforge/domain/elements.py +188 -0
  67. molforge/domain/ids.py +58 -0
  68. molforge/domain/massive_system.py +246 -0
  69. molforge/domain/periodic.py +116 -0
  70. molforge/domain/provenance.py +115 -0
  71. molforge/domain/topology.py +364 -0
  72. molforge/infrastructure/__init__.py +23 -0
  73. molforge/infrastructure/hpc/cluster_config.py +122 -0
  74. molforge/infrastructure/hpc/remote_executor.py +212 -0
  75. molforge/infrastructure/hpc/slurm_generator.py +93 -0
  76. molforge/io/__init__.py +35 -0
  77. molforge/io/calchem_bridge.py +407 -0
  78. molforge/io/calculators.py +893 -0
  79. molforge/io/cclib_adapter.py +422 -0
  80. molforge/io/conformers.py +183 -0
  81. molforge/io/cube_reader.py +215 -0
  82. molforge/io/custom_fragments.py +79 -0
  83. molforge/io/format_matrix.py +157 -0
  84. molforge/io/gemmi_adapter.py +150 -0
  85. molforge/io/history_skill.py +46 -0
  86. molforge/io/mdanalysis_adapter.py +155 -0
  87. molforge/io/mforge_storage.py +261 -0
  88. molforge/io/multiwfn_driver.py +614 -0
  89. molforge/io/openbabel_adapter.py +265 -0
  90. molforge/io/openbabel_handler.py +33 -0
  91. molforge/io/rdkit_adapter.py +1865 -0
  92. molforge/io/readers/__init__.py +10 -0
  93. molforge/io/readers/cube_reader.py +71 -0
  94. molforge/io/readers/gjf_reader.py +110 -0
  95. molforge/io/readers/glog_reader.py +45 -0
  96. molforge/io/readers/mol2_reader.py +36 -0
  97. molforge/io/readers/mol_reader.py +56 -0
  98. molforge/io/readers/pdb_reader.py +39 -0
  99. molforge/io/readers/smi_reader.py +40 -0
  100. molforge/io/readers/xyz_reader.py +47 -0
  101. molforge/io/registry.py +128 -0
  102. molforge/io/writers/__init__.py +7 -0
  103. molforge/io/writers/gjf_writer.py +68 -0
  104. molforge/io/writers/pdb_writer.py +37 -0
  105. molforge/io/writers/png_writer.py +31 -0
  106. molforge/io/writers/svg_writer.py +111 -0
  107. molforge/io/writers/xyz_writer.py +39 -0
  108. molforge/rendering/__init__.py +21 -0
  109. molforge/rendering/benchmark.py +121 -0
  110. molforge/rendering/bridge.py +60 -0
  111. molforge/rendering/contracts.py +119 -0
  112. molforge/rendering/glyph_scene.py +279 -0
  113. molforge/rendering/ketcher_bridge.py +131 -0
  114. molforge/rendering/ketcher_id_mapping.py +110 -0
  115. molforge/rendering/materials.py +127 -0
  116. molforge/rendering/molstar_backend.py +245 -0
  117. molforge/rendering/native_labels.py +134 -0
  118. molforge/rendering/orbitals.py +139 -0
  119. molforge/rendering/vtk_backend.py +2294 -0
  120. molforge/runtime.py +29 -0
  121. molforge/server/__init__.py +1 -0
  122. molforge/server/api_bridge.py +947 -0
  123. molforge/ui/__init__.py +1 -0
  124. molforge/ui/assets/caffeine_2d.svg +150 -0
  125. molforge/ui/assets/caffeine_3d_preview.svg +209 -0
  126. molforge/ui/assets/caffeine_preview.json +302 -0
  127. molforge/ui/assets/icons/atom.svg +1 -0
  128. molforge/ui/assets/icons/bond.svg +1 -0
  129. molforge/ui/assets/icons/cleanup.svg +1 -0
  130. molforge/ui/assets/icons/export.svg +1 -0
  131. molforge/ui/assets/icons/fragments.svg +1 -0
  132. molforge/ui/assets/icons/history.svg +1 -0
  133. molforge/ui/assets/icons/jobs.svg +1 -0
  134. molforge/ui/assets/icons/measure.svg +1 -0
  135. molforge/ui/assets/icons/molecules.svg +1 -0
  136. molforge/ui/assets/icons/overview.svg +1 -0
  137. molforge/ui/assets/icons/ring.svg +1 -0
  138. molforge/ui/assets/icons/search.svg +1 -0
  139. molforge/ui/assets/icons/select.svg +1 -0
  140. molforge/ui/assets/icons/settings.svg +1 -0
  141. molforge/ui/assets/icons/templates.svg +1 -0
  142. molforge/ui/assets/molforge_logo.ico +0 -0
  143. molforge/ui/assets/molforge_logo.png +0 -0
  144. molforge/ui/atom_labels.py +92 -0
  145. molforge/ui/bio_structure_dialog.py +57 -0
  146. molforge/ui/conformer_dialog.py +151 -0
  147. molforge/ui/dialogs/__init__.py +2 -0
  148. molforge/ui/dialogs/aim_qtaim_dialog.py +533 -0
  149. molforge/ui/dialogs/calchem_orbitals_dialog.py +287 -0
  150. molforge/ui/dialogs/element_fragments.py +130 -0
  151. molforge/ui/dialogs/gaussian_calculation_setup.py +308 -0
  152. molforge/ui/dialogs/hpc_job_dialog.py +427 -0
  153. molforge/ui/dialogs/vibrational_spectrum_dialog.py +381 -0
  154. molforge/ui/editor2d_widget.py +1803 -0
  155. molforge/ui/element_picker.py +218 -0
  156. molforge/ui/gmv_window_manager.py +65 -0
  157. molforge/ui/gromacs_job_dialog.py +47 -0
  158. molforge/ui/hotkeys.py +107 -0
  159. molforge/ui/i18n.py +237 -0
  160. molforge/ui/live_llm_dialog.py +135 -0
  161. molforge/ui/llm_config_dialog.py +103 -0
  162. molforge/ui/modeling_actions.py +176 -0
  163. molforge/ui/molstar_widget.py +98 -0
  164. molforge/ui/molstar_window.py +85 -0
  165. molforge/ui/panels.py +2966 -0
  166. molforge/ui/qc_result_dialog.py +1924 -0
  167. molforge/ui/render3d_widget.py +1579 -0
  168. molforge/ui/scrolling.py +52 -0
  169. molforge/ui/settings_panel.py +330 -0
  170. molforge/ui/shells/__init__.py +4 -0
  171. molforge/ui/shells/base_shell.py +72 -0
  172. molforge/ui/shells/document_tabs.py +715 -0
  173. molforge/ui/shells/studio_2d.py +339 -0
  174. molforge/ui/shells/studio_3d.py +401 -0
  175. molforge/ui/shells/studio_split.py +370 -0
  176. molforge/ui/shells/workflow_inputs.py +698 -0
  177. molforge/ui/theme.py +719 -0
  178. molforge/ui/trajectory_window.py +333 -0
  179. molforge/ui/views.py +892 -0
  180. molforge/ui/vtk_multiview_widget.py +141 -0
  181. molforge/ui/welcome_window.py +223 -0
  182. molforge/ui/window.py +3320 -0
  183. molforge/workflows/__init__.py +25 -0
  184. molforge/workflows/assets/OSC_MD_workflows/templates/01-MD/delete_solvent.py +216 -0
  185. molforge/workflows/assets/OSC_MD_workflows/templates/02-RDF/extract_atoms.py +60 -0
  186. molforge/workflows/assets/OSC_MD_workflows/templates/02-RDF/extract_atoms_example.py +22 -0
  187. molforge/workflows/assets/OSC_MD_workflows/templates/03-/342/225/244/320/225/342/225/250/342/226/221/342/225/250/320/246/342/225/244/320/227/342/225/250/342/225/226/342/225/250/342/224/220/342/225/244/320/225/342/225/250/320/247/342/225/250/342/226/221/342/225/244/320/224/321/202/320/245/320/254/342/225/250/320/265/analysis_mol.py +244 -0
  188. molforge/workflows/assets/OSC_MD_workflows/templates/04-/342/225/244/320/224/321/202/320/245/320/241/342/225/250/320/254/342/225/244/320/227/321/202/320/245/320/247/321/202/320/244/320/264/342/225/244/320/225/342/225/250/342/224/220/342/225/250/320/246/342/225/244/320/225/321/202/320/245/320/241/342/225/250/342/225/242/rms_roughness.py +33 -0
  189. molforge/workflows/assets/OSC_MD_workflows/templates/04-/342/225/244/320/224/321/202/320/245/320/241/342/225/250/320/254/342/225/244/320/227/321/202/320/245/320/247/321/202/320/244/320/264/342/225/244/320/225/342/225/250/342/224/220/342/225/250/320/246/342/225/244/320/225/321/202/320/245/320/241/342/225/250/342/225/242/xpm_2.py +117 -0
  190. molforge/workflows/assets/OSC_MD_workflows/templates/09-/342/225/244/320/231/342/225/250/320/240/342/225/250/320/277/342/225/244/320/227/342/225/250/320/256/342/225/250/320/247/marcus_rate.py +60 -0
  191. molforge/workflows/assets/esp-surface-analysis/scripts/compute_descriptors.py +134 -0
  192. molforge/workflows/assets/esp-surface-analysis/scripts/parse_surfanalysis.py +58 -0
  193. molforge/workflows/assets/esp-surface-analysis/scripts/plot_esp_hist.py +115 -0
  194. molforge/workflows/assets/homo-lumo-plot/extract_orb_energy.py +57 -0
  195. molforge/workflows/assets/homo-lumo-plot/make_level_plot.py +76 -0
  196. molforge/workflows/assets/homo-lumo-plot/run_pipeline.py +84 -0
  197. molforge/workflows/assets/osc-energy-level-diagram/scripts/extract_levels.py +60 -0
  198. molforge/workflows/assets/osc-energy-level-diagram/scripts/make_editable_pptx.py +122 -0
  199. molforge/workflows/assets/osc-energy-level-diagram/scripts/make_energy_table.py +66 -0
  200. molforge/workflows/assets/osc-energy-level-diagram/scripts/make_level_lines_diagram.py +148 -0
  201. molforge/workflows/assets/osc-energy-level-diagram/scripts/make_rectangle_diagram.py +148 -0
  202. molforge/workflows/assets/osc-energy-level-diagram/scripts/verify_from_log.py +45 -0
  203. molforge/workflows/dipole_analysis.py +32 -0
  204. molforge/workflows/esp_surface.py +36 -0
  205. molforge/workflows/gromacs_submit.py +167 -0
  206. molforge/workflows/homo_lumo_plot.py +59 -0
  207. molforge/workflows/igmh_interaction.py +111 -0
  208. molforge/workflows/md_rdf_analysis.py +134 -0
  209. molforge/workflows/md_studio.py +255 -0
  210. molforge/workflows/osc_energy_diagram.py +24 -0
  211. molforge/workflows/planarity_analysis.py +66 -0
  212. molforge/workflows/reorganization_energy.py +166 -0
  213. molforge/workflows/scientific_workflows.py +650 -0
  214. molforge_studio-7.4.3.dist-info/METADATA +215 -0
  215. molforge_studio-7.4.3.dist-info/RECORD +219 -0
  216. molforge_studio-7.4.3.dist-info/WHEEL +5 -0
  217. molforge_studio-7.4.3.dist-info/entry_points.txt +3 -0
  218. molforge_studio-7.4.3.dist-info/licenses/LICENSE +93 -0
  219. molforge_studio-7.4.3.dist-info/top_level.txt +2 -0
@@ -0,0 +1,401 @@
1
+ """
2
+ ui/shells/studio_3d.py - GaussView 风格 3D 多窗口建模系统 (Studio3DShell & 全新独立 Studio3DWindow)
3
+ =====================================================================================================
4
+ 严格遵循 PROMPT_v7_1_complete.md 规范:
5
+ - Studio3DWindow 是从零构建的全新 QMainWindow,绝不包装或继承 MolForgeWindow!
6
+ - 无左侧导航栏、无右侧属性面板、无顶部工具栏
7
+ - 顶部菜单栏:File / Edit / Build / View / Calculate / Results / Windows / Help
8
+ - 中央区域:100% 占满的 VTK 3D 画布 (Render3DWidget(chromeless=True)),支持单窗口 1 / 2 / 4 视口 (P1..P4)
9
+ - 底部状态栏:Atoms: N Electrons: M Charge: Q Spin: S
10
+ """
11
+
12
+ from __future__ import annotations
13
+ from molforge import __version__
14
+
15
+ from pathlib import Path
16
+ from .base_shell import BaseStudioShell
17
+ try:
18
+ from .document_tabs import StudioTabs
19
+ except ImportError:
20
+ class StudioTabs:
21
+ pass
22
+ from ..vtk_multiview_widget import VtkMultiviewController
23
+ from ...application.workspace_registry import PaneCount
24
+ from ...domain.document import MoleculeDocument
25
+ from ...io.rdkit_adapter import (
26
+ create_instant_caffeine_document,
27
+ load_molecule_document,
28
+ compute_formula_and_weight_fast,
29
+ )
30
+
31
+ try:
32
+ from PyQt6.QtCore import Qt, QTimer
33
+ from PyQt6.QtGui import QAction
34
+ from PyQt6.QtWidgets import (
35
+ QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel,
36
+ QFileDialog, QMessageBox, QSplitter, QStatusBar
37
+ )
38
+ _HAS_QT = True
39
+ except ImportError: # pragma: no cover
40
+ _HAS_QT = False
41
+ QMainWindow = object # type: ignore[misc,assignment]
42
+
43
+
44
+ def _compute_electron_count(doc) -> tuple[int, int, int]:
45
+ """计算分子的总原子数、总电子数、总形式电荷与默认自旋多重度 (Atoms, Electrons, Charge, Spin)。"""
46
+ if doc is None:
47
+ return 0, 0, 0, 1
48
+ n_atoms = doc.num_atoms
49
+ charge = int(doc.properties.get("charge", sum(doc.topology.get_atom(aid).formal_charge for aid in doc.topology.atom_ids)))
50
+ z_sum = sum(doc.topology.get_atom(aid).atomic_num for aid in doc.topology.atom_ids)
51
+ n_elec = max(0, z_sum - charge)
52
+ default_spin = 1 if (n_elec % 2 == 0) else 2
53
+ spin = int(doc.properties.get("multiplicity", default_spin))
54
+ return n_atoms, n_elec, charge, spin
55
+
56
+
57
+
58
+ class Studio3DShell(BaseStudioShell):
59
+ shell_kind = "studio_3d"
60
+ menu_titles = ("File", "Edit", "Tools", "Builder", "View", "Calculate", "Results", "Windows", "Help")
61
+
62
+ def __init__(self, *args, **kwargs) -> None:
63
+ super().__init__(*args, **kwargs)
64
+ self.multiview = VtkMultiviewController(self.view_session)
65
+
66
+ def set_pane_layout(self, count: PaneCount) -> dict[str, tuple[float, float, float, float]]:
67
+ return self.multiview.set_layout(count)
68
+
69
+ def spawn_clone_view(self, pane_count: PaneCount = 1) -> "Studio3DShell":
70
+ session, cloned_view = self.gmv_manager.register_clone_view(self.gmv_tag, pane_count=pane_count)
71
+ return Studio3DShell(session=session, view_session=cloned_view)
72
+
73
+ def spawn_group_molecule(self, document: MoleculeDocument | None = None, pane_count: PaneCount = 1) -> "Studio3DShell":
74
+ doc = document or MoleculeDocument(name="group_mol")
75
+ session, new_view = self.gmv_manager.register_add_molecule_to_group(
76
+ self.view_session.group_id, doc, pane_count=pane_count
77
+ )
78
+ return Studio3DShell(session=session, view_session=new_view)
79
+
80
+
81
+ class Studio3DWindow(StudioTabs, QMainWindow):
82
+ """
83
+ 对标 GaussView 6 的全新简洁 3D 建模与量化主窗口(纯顶部菜单栏 + 100% 中央 VTK 画布 + 底部状态栏,绝不包装 MolForgeWindow)。
84
+ """
85
+
86
+ _open_windows: list["Studio3DWindow"] = []
87
+
88
+ def __init__(
89
+ self,
90
+ document: MoleculeDocument | None = None,
91
+ pane_count: PaneCount = 1,
92
+ shell: Studio3DShell | None = None,
93
+ session=None,
94
+ ):
95
+ super().__init__()
96
+ doc = document if document is not None else create_instant_caffeine_document()
97
+ self.shell = shell if shell is not None else Studio3DShell(document=doc, session=session, pane_count=pane_count)
98
+ self.session = self.shell.session
99
+ self.view_session = self.shell.view_session
100
+ self.document = self.shell.document
101
+ self.command_bus = self.session.command_bus
102
+ self.selection = self.session.selection
103
+ self.group_tag = self.shell.gmv_tag
104
+
105
+ self.setWindowTitle(f"MolForge {__version__} Studio 3D · [{self.group_tag}] {self.document.name}")
106
+ self.resize(1120, 760)
107
+
108
+ from ..render3d_widget import Render3DWidget
109
+ self.canvas3d = Render3DWidget(self)
110
+ self.canvas3d.set_selection_service(self.selection)
111
+ self.canvas3d.set_command_bus(self.command_bus)
112
+
113
+ # 状态栏先于 init_tabs 创建(init_tabs 内会触发 refresh_views 写 status_label)
114
+ self.status_label = QLabel()
115
+ self.statusBar().addPermanentWidget(self.status_label, 1)
116
+
117
+ self.init_tabs(self.canvas3d)
118
+
119
+ self._build_menus()
120
+ self.install_studio_toolbar()
121
+ self.install_window_menu()
122
+ Studio3DWindow._open_windows.append(self)
123
+
124
+ QTimer.singleShot(15, lambda: self._init_3d_scene(pane_count))
125
+ self.refresh_views()
126
+
127
+ def _init_3d_scene(self, pane_count: PaneCount) -> None:
128
+ self.canvas3d.set_document(self.document)
129
+ if pane_count in (1, 2, 4):
130
+ self.set_3d_multiview_count(pane_count)
131
+
132
+ def _build_menus(self) -> None:
133
+ mb = self.menuBar()
134
+ mb.clear()
135
+
136
+ # 1. 文件
137
+ m_file = mb.addMenu("文件(&F)")
138
+ act_new = QAction("新建分子组 (G:M:V)... (Ctrl+N)", self)
139
+ act_new.setShortcut("Ctrl+N")
140
+ act_new.triggered.connect(lambda: self.add_document_tab())
141
+ m_file.addAction(act_new)
142
+
143
+ act_open = QAction("打开结构 / Gaussian 输出 (.gjf/.log/.xyz/.mol/.sdf/.pdb)... (Ctrl+O)", self)
144
+ act_open.setShortcut("Ctrl+O")
145
+ act_open.triggered.connect(self._on_open_file)
146
+ m_file.addAction(act_open)
147
+
148
+ act_save_gjf = QAction("保存完整工程 (Ctrl+S)", self)
149
+ act_save_gjf.setShortcut("Ctrl+S")
150
+ act_save_gjf.triggered.connect(lambda: self.save_project())
151
+ m_file.addAction(act_save_gjf)
152
+
153
+ m_file.addSeparator()
154
+ act_welcome = QAction("返回欢迎界面...", self)
155
+ act_welcome.triggered.connect(self._open_welcome)
156
+ m_file.addAction(act_welcome)
157
+
158
+ act_close = QAction("关闭窗口 (Ctrl+W)", self)
159
+ act_close.setShortcut("Ctrl+W")
160
+ act_close.triggered.connect(self.close)
161
+ m_file.addAction(act_close)
162
+
163
+ # 2. 编辑
164
+ m_edit = mb.addMenu("编辑(&E)")
165
+ act_undo = QAction("撤销 (Ctrl+Z)", self)
166
+ act_undo.setShortcut("Ctrl+Z")
167
+ act_undo.triggered.connect(lambda: (self.command_bus.undo(), self.refresh_views()))
168
+ m_edit.addAction(act_undo)
169
+
170
+ act_redo = QAction("重做 (Ctrl+Y)", self)
171
+ act_redo.setShortcut("Ctrl+Y")
172
+ act_redo.triggered.connect(lambda: (self.command_bus.redo(), self.refresh_views()))
173
+ m_edit.addAction(act_redo)
174
+
175
+ m_edit.addSeparator()
176
+ act_clean = QAction("3D 几何优化 (力场弛豫, Ctrl+M)", self)
177
+ act_clean.setShortcut("Ctrl+M")
178
+ act_clean.triggered.connect(self._clean_active)
179
+ m_edit.addAction(act_clean)
180
+
181
+ act_sync_3d = QAction("⚡ 重新生成 3D 空间立体构象 (ETKDG + UFF/MMFF94)... (F6)", self)
182
+ act_sync_3d.setShortcut("F6")
183
+ act_sync_3d.triggered.connect(self.generate_3d_conformation)
184
+ m_edit.addAction(act_sync_3d)
185
+
186
+ act_proj_2d = QAction("📐 将当前 3D 观察视角投影生成 2D 布局 (Ctrl+Shift+P)", self)
187
+ act_proj_2d.setShortcut("Ctrl+Shift+P")
188
+ act_proj_2d.triggered.connect(self.project_3d_to_2d)
189
+ m_edit.addAction(act_proj_2d)
190
+
191
+ # 3. 构建
192
+ m_build = mb.addMenu("构建(&B)")
193
+ for tool_lbl, tool_name, sc in (
194
+ ("选择 / 拖动原子模式 (V)", "选择", "V"),
195
+ ("放置 / 生长原子模式 (A)", "原子", "A"),
196
+ ("绘制 / 切换键模式 (B)", "键", "B"),
197
+ ("擦除原子 / 键模式 (E)", "擦除", "E"),
198
+ ):
199
+ act = QAction(tool_lbl, self)
200
+ act.setShortcut(sc)
201
+ act.triggered.connect(lambda _=False, tn=tool_name: self.canvas3d.set_current_tool(tn))
202
+ m_build.addAction(act)
203
+
204
+ m_build.addSeparator()
205
+ act_efrag = QAction("元素片段面板 (C / O / N / 常用)...", self)
206
+ act_efrag.triggered.connect(self._on_open_element_fragments)
207
+ m_build.addAction(act_efrag)
208
+
209
+ act_rfrag = QAction("环 / R-基团片段库...", self)
210
+ act_rfrag.triggered.connect(self._on_open_ring_fragments)
211
+ m_build.addAction(act_rfrag)
212
+
213
+ m_build.addSeparator()
214
+ for coord_lbl, kind in (
215
+ ("修改键长...", "bond"),
216
+ ("修改键角...", "angle"),
217
+ ("修改二面角...", "dihedral"),
218
+ ):
219
+ act = QAction(coord_lbl, self)
220
+ act.triggered.connect(lambda _=False, k=kind: self._on_modify_internal_coord(k))
221
+ m_build.addAction(act)
222
+
223
+ # 4. 视图
224
+ m_view = mb.addMenu("视图(&V)")
225
+ for cnt, lbl in (
226
+ (1, "1 视口 (单视图 - P1)"),
227
+ (2, "2 视口 (左右分屏 - P1 | P2)"),
228
+ (4, "4 视口 (前/侧/俯/透视 - P1..P4)"),
229
+ ):
230
+ act = QAction(lbl, self)
231
+ act.triggered.connect(lambda _=False, c=cnt: self.set_3d_multiview_count(c))
232
+ m_view.addAction(act)
233
+
234
+ m_view.addSeparator()
235
+ from ...rendering.contracts import RepresentationMode
236
+ for mode_enum, mode_lbl in (
237
+ (RepresentationMode.BALL_AND_STICK, "显示模式: 球棍模型"),
238
+ (RepresentationMode.CPK, "显示模式: CPK 空间填充"),
239
+ (RepresentationMode.LINE, "显示模式: 线框"),
240
+ (RepresentationMode.SURFACE, "显示模式: 范德华分子表面"),
241
+ ):
242
+ act = QAction(mode_lbl, self)
243
+ act.triggered.connect(lambda _=False, m=mode_enum: self.canvas3d.set_representation_mode(m))
244
+ m_view.addAction(act)
245
+
246
+ m_view.addSeparator()
247
+ act_reset_cam = QAction("居中 & 重置 3D 相机", self)
248
+ act_reset_cam.triggered.connect(self._center_active)
249
+ m_view.addAction(act_reset_cam)
250
+
251
+ # 5. 计算
252
+ m_calc = mb.addMenu("计算(&C)")
253
+ act_gauss = QAction("Gaussian 计算设置 (11 标签页向导)... (Ctrl+G)", self)
254
+ act_gauss.setShortcut("Ctrl+G")
255
+ act_gauss.triggered.connect(self._on_gaussian_setup)
256
+ m_calc.addAction(act_gauss)
257
+
258
+ act_rest = QAction("复旦 REST 输入设置 (ctrl.in)...", self)
259
+ act_rest.triggered.connect(self._on_rest_setup)
260
+ m_calc.addAction(act_rest)
261
+
262
+ act_xtb = QAction("GFN2-xTB 几何优化... (Ctrl+Shift+X)", self)
263
+ act_xtb.setShortcut("Ctrl+Shift+X")
264
+ act_xtb.triggered.connect(self._on_run_xtb)
265
+ m_calc.addAction(act_xtb)
266
+
267
+ # 6. 结果
268
+ m_res = mb.addMenu("结果(&R)")
269
+ from ..qc_result_dialog import populate_v7_results_menu
270
+ populate_v7_results_menu(m_res, self, lambda: self.document)
271
+
272
+ # 7. 窗口
273
+ m_win = mb.addMenu("窗口(&W)")
274
+ act_clone = QAction("添加克隆共享视图 (同 DocumentSession, G1:M1:V2)...", self)
275
+ act_clone.triggered.connect(self._on_spawn_clone_view)
276
+ m_win.addAction(act_clone)
277
+
278
+ act_add_mol = QAction("向当前组添加新分子 (G1:M2:V1)...", self)
279
+ act_add_mol.triggered.connect(self._on_spawn_group_molecule)
280
+ m_win.addAction(act_add_mol)
281
+
282
+ # 8. 帮助
283
+ m_help = mb.addMenu("帮助(&H)")
284
+ act_about = QAction("关于 MolForge Studio 3D…", self)
285
+ act_about.triggered.connect(
286
+ lambda: QMessageBox.information(
287
+ self, "MolForge Studio 3D", f"MolForge v{__import__('molforge').__version__} · GaussView 风格简洁 3D 多窗口与量化系统"
288
+ )
289
+ )
290
+ m_help.addAction(act_about)
291
+
292
+ def set_3d_multiview_count(self, pane_count: PaneCount = 1) -> list:
293
+ self.shell.set_pane_layout(pane_count)
294
+ aux = self.canvas3d.set_multiview_layout(pane_count)
295
+ self.refresh_views()
296
+ return aux
297
+
298
+ def open_molecule_file(self, path: str) -> None:
299
+ self.open_document_tab(path)
300
+
301
+ def _on_open_file(self) -> None:
302
+ path, _ = QFileDialog.getOpenFileName(
303
+ self, "打开结构或量子输出", "",
304
+ "Chemistry & QC Files (*.gjf *.com *.log *.out *.fchk *.cube *.xyz *.mol *.sdf *.pdb *.mol2);;All Files (*)"
305
+ )
306
+ if path:
307
+ self.open_molecule_file(path)
308
+
309
+ def _on_open_element_fragments(self) -> None:
310
+ from ..dialogs.element_fragments import ElementFragmentsDialog
311
+ from ...application.fragment_commands import InsertFragmentCommand
312
+ dlg = ElementFragmentsDialog(parent=self, initial_category="C")
313
+ if dlg.exec() and dlg.selected_fragment is not None:
314
+ sel_atoms = list(self.selection.selected_atoms)
315
+ anchor = sel_atoms[-1] if sel_atoms else None
316
+ self.command_bus.execute(InsertFragmentCommand(dlg.selected_fragment, anchor_atom_id=anchor))
317
+ self.refresh_views()
318
+
319
+ def _on_open_ring_fragments(self) -> None:
320
+ from ..views import BuilderFragmentDialog
321
+ BuilderFragmentDialog(self, "Ring").exec()
322
+ self.refresh_views()
323
+
324
+ def _on_modify_internal_coord(self, kind: str) -> None:
325
+ from ..views import ModifyInternalCoordDialog
326
+ ModifyInternalCoordDialog(self, kind).exec()
327
+ self.refresh_views()
328
+
329
+ def _on_gaussian_setup(self) -> None:
330
+ from ..dialogs.gaussian_calculation_setup import GaussianCalculationSetupDialog
331
+ GaussianCalculationSetupDialog(self.document, parent=self).exec()
332
+
333
+ def _on_rest_setup(self) -> None:
334
+ from ..qc_result_dialog import QcResultDialog
335
+ QcResultDialog(parent=self, document=self.document, initial_tab=1).exec()
336
+
337
+ def _on_run_xtb(self) -> None:
338
+ from ...io.calculators import run_xtb_optimization
339
+ from ...application.builtin_commands import MoveMultiAtoms3DCommand
340
+ res = run_xtb_optimization(self.document)
341
+ if res.get("success") and res.get("coords") is not None:
342
+ new_c = res["coords"]
343
+ atom_ids = list(self.document.topology.atom_ids)
344
+ old_map = {aid: self.document.get_atom_3d_pos(aid) for aid in atom_ids}
345
+ new_map = {aid: new_c[i] for i, aid in enumerate(atom_ids) if i < len(new_c)}
346
+ self.command_bus.execute(MoveMultiAtoms3DCommand(old_map, new_map, description="xtb/MMFF94 3D 几何优化"))
347
+ self.refresh_views()
348
+
349
+ def _on_new_group_window(self) -> "Studio3DWindow":
350
+ win = Studio3DWindow(document=MoleculeDocument(name="new_group_mol"), pane_count=1)
351
+ win.move(self.x() + 36, self.y() + 36)
352
+ win.show()
353
+ return win
354
+
355
+ def _on_spawn_clone_view(self) -> "Studio3DWindow":
356
+ cloned_shell = self.shell.spawn_clone_view(pane_count=self.view_session.pane_count)
357
+ win = Studio3DWindow(document=self.document, pane_count=self.view_session.pane_count, shell=cloned_shell)
358
+ win.move(self.x() + 42, self.y() + 42)
359
+ win.show()
360
+ return win
361
+
362
+ def _on_spawn_group_molecule(self) -> "Studio3DWindow":
363
+ grp_shell = self.shell.spawn_group_molecule(document=create_instant_caffeine_document(), pane_count=1)
364
+ win = Studio3DWindow(document=grp_shell.document, pane_count=1, shell=grp_shell)
365
+ win.move(self.x() + 48, self.y() + 48)
366
+ win.show()
367
+ return win
368
+
369
+ def _open_welcome(self) -> None:
370
+ from ..welcome_window import AppController
371
+ AppController.instance().show_welcome()
372
+
373
+ def refresh_views(self) -> None:
374
+ if hasattr(self, "tabs") and self.tabs.currentWidget() is not None:
375
+ self.tabs.setTabText(self.tabs.currentIndex(), self.document.name)
376
+ self.canvas3d.set_document(self.document)
377
+ n_atoms, n_elec, charge, spin = _compute_electron_count(self.document)
378
+ formula, _ = compute_formula_and_weight_fast(self.document)
379
+ self.setWindowTitle(f"MolForge {__version__} Studio 3D · [{self.group_tag}] {self.document.name}")
380
+ self.status_label.setText(
381
+ f"Atoms: {n_atoms} Electrons: {n_elec} Charge: {charge} Spin: {spin} "
382
+ f"| Formula: {formula} | Viewports: {self.view_session.pane_count} ({self.group_tag})"
383
+ )
384
+
385
+
386
+ def project_3d_to_2d(self) -> None:
387
+ if self.document.num_atoms == 0:
388
+ return
389
+ from ...application.builtin_commands import Project3DTo2DCommand
390
+ right, up = self.canvas3d.get_camera_view_basis()
391
+ res = self.command_bus.execute(Project3DTo2DCommand(camera_right=right, camera_up=up))
392
+ QMessageBox.information(self, "3D ➜ 2D 投影", res.message)
393
+
394
+ def generate_3d_conformation(self) -> None:
395
+ if self.document.num_atoms == 0:
396
+ return
397
+ from ...application.builtin_commands import GenerateConformation3DCommand
398
+ res = self.command_bus.execute(GenerateConformation3DCommand(optimize_iters=150))
399
+ self.canvas3d.set_document(self.document)
400
+ self.canvas3d.reset_camera()
401
+ QMessageBox.information(self, "3D 构象生成", res.message)