MoleditPy 1.16.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 (54) hide show
  1. moleditpy/__init__.py +4 -0
  2. moleditpy/__main__.py +29 -0
  3. moleditpy/main.py +37 -0
  4. moleditpy/modules/__init__.py +36 -0
  5. moleditpy/modules/about_dialog.py +92 -0
  6. moleditpy/modules/align_plane_dialog.py +281 -0
  7. moleditpy/modules/alignment_dialog.py +261 -0
  8. moleditpy/modules/analysis_window.py +197 -0
  9. moleditpy/modules/angle_dialog.py +428 -0
  10. moleditpy/modules/assets/icon.icns +0 -0
  11. moleditpy/modules/assets/icon.ico +0 -0
  12. moleditpy/modules/assets/icon.png +0 -0
  13. moleditpy/modules/atom_item.py +336 -0
  14. moleditpy/modules/bond_item.py +303 -0
  15. moleditpy/modules/bond_length_dialog.py +368 -0
  16. moleditpy/modules/calculation_worker.py +754 -0
  17. moleditpy/modules/color_settings_dialog.py +309 -0
  18. moleditpy/modules/constants.py +76 -0
  19. moleditpy/modules/constrained_optimization_dialog.py +667 -0
  20. moleditpy/modules/custom_interactor_style.py +737 -0
  21. moleditpy/modules/custom_qt_interactor.py +49 -0
  22. moleditpy/modules/dialog3_d_picking_mixin.py +96 -0
  23. moleditpy/modules/dihedral_dialog.py +431 -0
  24. moleditpy/modules/main_window.py +830 -0
  25. moleditpy/modules/main_window_app_state.py +747 -0
  26. moleditpy/modules/main_window_compute.py +1203 -0
  27. moleditpy/modules/main_window_dialog_manager.py +454 -0
  28. moleditpy/modules/main_window_edit_3d.py +531 -0
  29. moleditpy/modules/main_window_edit_actions.py +1449 -0
  30. moleditpy/modules/main_window_export.py +744 -0
  31. moleditpy/modules/main_window_main_init.py +1668 -0
  32. moleditpy/modules/main_window_molecular_parsers.py +1037 -0
  33. moleditpy/modules/main_window_project_io.py +429 -0
  34. moleditpy/modules/main_window_string_importers.py +270 -0
  35. moleditpy/modules/main_window_ui_manager.py +567 -0
  36. moleditpy/modules/main_window_view_3d.py +1211 -0
  37. moleditpy/modules/main_window_view_loaders.py +350 -0
  38. moleditpy/modules/mirror_dialog.py +110 -0
  39. moleditpy/modules/molecular_data.py +290 -0
  40. moleditpy/modules/molecule_scene.py +1964 -0
  41. moleditpy/modules/move_group_dialog.py +586 -0
  42. moleditpy/modules/periodic_table_dialog.py +72 -0
  43. moleditpy/modules/planarize_dialog.py +209 -0
  44. moleditpy/modules/settings_dialog.py +1071 -0
  45. moleditpy/modules/template_preview_item.py +148 -0
  46. moleditpy/modules/template_preview_view.py +62 -0
  47. moleditpy/modules/translation_dialog.py +353 -0
  48. moleditpy/modules/user_template_dialog.py +621 -0
  49. moleditpy/modules/zoomable_view.py +98 -0
  50. moleditpy-1.16.3.dist-info/METADATA +274 -0
  51. moleditpy-1.16.3.dist-info/RECORD +54 -0
  52. moleditpy-1.16.3.dist-info/WHEEL +5 -0
  53. moleditpy-1.16.3.dist-info/entry_points.txt +2 -0
  54. moleditpy-1.16.3.dist-info/top_level.txt +1 -0
@@ -0,0 +1,428 @@
1
+ from PyQt6.QtWidgets import (
2
+ QDialog, QVBoxLayout, QLabel, QHBoxLayout, QPushButton, QLineEdit, QWidget, QRadioButton
3
+ )
4
+
5
+ try:
6
+ from .dialog3_d_picking_mixin import Dialog3DPickingMixin
7
+ except Exception:
8
+ from modules.dialog3_d_picking_mixin import Dialog3DPickingMixin
9
+
10
+ from PyQt6.QtCore import Qt
11
+ from PyQt6.QtWidgets import QMessageBox
12
+ import numpy as np
13
+
14
+
15
+ class AngleDialog(Dialog3DPickingMixin, QDialog):
16
+ def __init__(self, mol, main_window, preselected_atoms=None, parent=None):
17
+ QDialog.__init__(self, parent)
18
+ Dialog3DPickingMixin.__init__(self)
19
+ self.mol = mol
20
+ self.main_window = main_window
21
+ self.atom1_idx = None
22
+ self.atom2_idx = None # vertex atom
23
+ self.atom3_idx = None
24
+
25
+ # 事前選択された原子を設定
26
+ if preselected_atoms and len(preselected_atoms) >= 3:
27
+ self.atom1_idx = preselected_atoms[0]
28
+ self.atom2_idx = preselected_atoms[1] # vertex
29
+ self.atom3_idx = preselected_atoms[2]
30
+
31
+ self.init_ui()
32
+
33
+ def init_ui(self):
34
+ self.setWindowTitle("Adjust Angle")
35
+ self.setModal(False) # モードレスにしてクリックを阻害しない
36
+ self.setWindowFlags(Qt.WindowType.Window | Qt.WindowType.WindowStaysOnTopHint) # 常に前面表示
37
+ layout = QVBoxLayout(self)
38
+
39
+ # Instructions
40
+ instruction_label = QLabel("Click three atoms in order: first-vertex-third. The angle around the vertex atom will be adjusted.")
41
+ instruction_label.setWordWrap(True)
42
+ layout.addWidget(instruction_label)
43
+
44
+ # Selected atoms display
45
+ self.selection_label = QLabel("No atoms selected")
46
+ layout.addWidget(self.selection_label)
47
+
48
+ # Current angle display
49
+ self.angle_label = QLabel("")
50
+ layout.addWidget(self.angle_label)
51
+
52
+ # New angle input
53
+ angle_layout = QHBoxLayout()
54
+ angle_layout.addWidget(QLabel("New angle (degrees):"))
55
+ self.angle_input = QLineEdit()
56
+ self.angle_input.setPlaceholderText("109.5")
57
+ angle_layout.addWidget(self.angle_input)
58
+ layout.addLayout(angle_layout)
59
+
60
+ # Movement options
61
+ group_box = QWidget()
62
+ group_layout = QVBoxLayout(group_box)
63
+ group_layout.addWidget(QLabel("Rotation Options:"))
64
+
65
+ self.rotate_group_radio = QRadioButton("Atom 1,2: Fixed, Atom 3: Rotate connected group")
66
+ self.rotate_group_radio.setChecked(True)
67
+ group_layout.addWidget(self.rotate_group_radio)
68
+
69
+ self.rotate_atom_radio = QRadioButton("Atom 1,2: Fixed, Atom 3: Rotate atom only")
70
+ group_layout.addWidget(self.rotate_atom_radio)
71
+
72
+ self.both_groups_radio = QRadioButton("Vertex fixed: Both arms rotate equally")
73
+ group_layout.addWidget(self.both_groups_radio)
74
+
75
+
76
+ layout.addWidget(group_box)
77
+
78
+ # Buttons
79
+ button_layout = QHBoxLayout()
80
+ self.clear_button = QPushButton("Clear Selection")
81
+ self.clear_button.clicked.connect(self.clear_selection)
82
+ button_layout.addWidget(self.clear_button)
83
+
84
+ button_layout.addStretch()
85
+
86
+ self.apply_button = QPushButton("Apply")
87
+ self.apply_button.clicked.connect(self.apply_changes)
88
+ self.apply_button.setEnabled(False)
89
+ button_layout.addWidget(self.apply_button)
90
+
91
+ close_button = QPushButton("Close")
92
+ close_button.clicked.connect(self.reject)
93
+ button_layout.addWidget(close_button)
94
+
95
+ layout.addLayout(button_layout)
96
+
97
+ # Connect to main window's picker for AngleDialog
98
+ self.picker_connection = None
99
+ self.enable_picking()
100
+
101
+ # 事前選択された原子がある場合は初期表示を更新
102
+ if self.atom1_idx is not None:
103
+ self.show_atom_labels()
104
+ self.update_display()
105
+
106
+ def on_atom_picked(self, atom_idx):
107
+ """原子がピックされたときの処理"""
108
+ if self.atom1_idx is None:
109
+ self.atom1_idx = atom_idx
110
+ elif self.atom2_idx is None:
111
+ self.atom2_idx = atom_idx
112
+ elif self.atom3_idx is None:
113
+ self.atom3_idx = atom_idx
114
+ else:
115
+ # Reset and start over
116
+ self.atom1_idx = atom_idx
117
+ self.atom2_idx = None
118
+ self.atom3_idx = None
119
+
120
+ # 原子ラベルを表示
121
+ self.show_atom_labels()
122
+ self.update_display()
123
+
124
+ def keyPressEvent(self, event):
125
+ """キーボードイベントを処理"""
126
+ if event.key() == Qt.Key.Key_Return or event.key() == Qt.Key.Key_Enter:
127
+ if self.apply_button.isEnabled():
128
+ self.apply_changes()
129
+ event.accept()
130
+ else:
131
+ super().keyPressEvent(event)
132
+
133
+ def closeEvent(self, event):
134
+ """ダイアログが閉じられる時の処理"""
135
+ self.clear_atom_labels()
136
+ self.disable_picking()
137
+ super().closeEvent(event)
138
+
139
+ def reject(self):
140
+ """キャンセル時の処理"""
141
+ self.clear_atom_labels()
142
+ self.disable_picking()
143
+ super().reject()
144
+
145
+ def accept(self):
146
+ """OK時の処理"""
147
+ self.clear_atom_labels()
148
+ self.disable_picking()
149
+ super().accept()
150
+
151
+ def clear_selection(self):
152
+ """選択をクリア"""
153
+ self.atom1_idx = None
154
+ self.atom2_idx = None # vertex atom
155
+ self.atom3_idx = None
156
+ self.clear_selection_labels()
157
+ self.update_display()
158
+
159
+ def show_atom_labels(self):
160
+ """選択された原子にラベルを表示"""
161
+ # 既存のラベルをクリア
162
+ self.clear_atom_labels()
163
+
164
+ # 新しいラベルを表示
165
+ if not hasattr(self, 'selection_labels'):
166
+ self.selection_labels = []
167
+
168
+ selected_atoms = [self.atom1_idx, self.atom2_idx, self.atom3_idx]
169
+ labels = ["1st", "2nd (vertex)", "3rd"]
170
+ colors = ["yellow", "yellow", "yellow"] # 全て黄色に統一
171
+
172
+ for i, atom_idx in enumerate(selected_atoms):
173
+ if atom_idx is not None:
174
+ pos = self.main_window.atom_positions_3d[atom_idx]
175
+ label_text = f"{labels[i]}"
176
+
177
+ # ラベルを追加
178
+ label_actor = self.main_window.plotter.add_point_labels(
179
+ [pos], [label_text],
180
+ point_size=20,
181
+ font_size=12,
182
+ text_color=colors[i],
183
+ always_visible=True
184
+ )
185
+ self.selection_labels.append(label_actor)
186
+
187
+ def clear_atom_labels(self):
188
+ """原子ラベルをクリア"""
189
+ if hasattr(self, 'selection_labels'):
190
+ for label_actor in self.selection_labels:
191
+ try:
192
+ self.main_window.plotter.remove_actor(label_actor)
193
+ except:
194
+ pass
195
+ self.selection_labels = []
196
+
197
+ def clear_selection_labels(self):
198
+ """選択ラベルをクリア"""
199
+ if hasattr(self, 'selection_labels'):
200
+ for label_actor in self.selection_labels:
201
+ try:
202
+ self.main_window.plotter.remove_actor(label_actor)
203
+ except:
204
+ pass
205
+ self.selection_labels = []
206
+
207
+ def add_selection_label(self, atom_idx, label_text):
208
+ """選択された原子にラベルを追加"""
209
+ if not hasattr(self, 'selection_labels'):
210
+ self.selection_labels = []
211
+
212
+ # 原子の位置を取得
213
+ pos = self.main_window.atom_positions_3d[atom_idx]
214
+
215
+ # ラベルを追加
216
+ label_actor = self.main_window.plotter.add_point_labels(
217
+ [pos], [label_text],
218
+ point_size=20,
219
+ font_size=12,
220
+ text_color='yellow',
221
+ always_visible=True
222
+ )
223
+ self.selection_labels.append(label_actor)
224
+
225
+ def update_display(self):
226
+ """表示を更新"""
227
+ # 既存のラベルをクリア
228
+ self.clear_selection_labels()
229
+
230
+ if self.atom1_idx is None:
231
+ self.selection_label.setText("No atoms selected")
232
+ self.angle_label.setText("")
233
+ self.apply_button.setEnabled(False)
234
+ # Clear angle input when no selection
235
+ try:
236
+ self.angle_input.clear()
237
+ except Exception:
238
+ pass
239
+ elif self.atom2_idx is None:
240
+ symbol1 = self.mol.GetAtomWithIdx(self.atom1_idx).GetSymbol()
241
+ self.selection_label.setText(f"First atom: {symbol1} (index {self.atom1_idx})")
242
+ self.angle_label.setText("")
243
+ self.apply_button.setEnabled(False)
244
+ # ラベル追加
245
+ self.add_selection_label(self.atom1_idx, "1")
246
+ # Clear angle input while selection is incomplete
247
+ try:
248
+ self.angle_input.clear()
249
+ except Exception:
250
+ pass
251
+ elif self.atom3_idx is None:
252
+ symbol1 = self.mol.GetAtomWithIdx(self.atom1_idx).GetSymbol()
253
+ symbol2 = self.mol.GetAtomWithIdx(self.atom2_idx).GetSymbol()
254
+ self.selection_label.setText(f"Selected: {symbol1}({self.atom1_idx}) - {symbol2}({self.atom2_idx}) - ?")
255
+ self.angle_label.setText("")
256
+ self.apply_button.setEnabled(False)
257
+ # ラベル追加
258
+ self.add_selection_label(self.atom1_idx, "1")
259
+ self.add_selection_label(self.atom2_idx, "2(vertex)")
260
+ # Clear angle input while selection is incomplete
261
+ try:
262
+ self.angle_input.clear()
263
+ except Exception:
264
+ pass
265
+ else:
266
+ symbol1 = self.mol.GetAtomWithIdx(self.atom1_idx).GetSymbol()
267
+ symbol2 = self.mol.GetAtomWithIdx(self.atom2_idx).GetSymbol()
268
+ symbol3 = self.mol.GetAtomWithIdx(self.atom3_idx).GetSymbol()
269
+ self.selection_label.setText(f"Angle: {symbol1}({self.atom1_idx}) - {symbol2}({self.atom2_idx}) - {symbol3}({self.atom3_idx})")
270
+
271
+ # Calculate current angle
272
+ current_angle = self.calculate_angle()
273
+ self.angle_label.setText(f"Current angle: {current_angle:.2f}°")
274
+ self.apply_button.setEnabled(True)
275
+ # Update angle input box with current angle
276
+ try:
277
+ self.angle_input.setText(f"{current_angle:.2f}")
278
+ except Exception:
279
+ pass
280
+ # ラベル追加
281
+ self.add_selection_label(self.atom1_idx, "1")
282
+ self.add_selection_label(self.atom2_idx, "2(vertex)")
283
+ self.add_selection_label(self.atom3_idx, "3")
284
+
285
+ def calculate_angle(self):
286
+ """現在の角度を計算"""
287
+ conf = self.mol.GetConformer()
288
+ pos1 = np.array(conf.GetAtomPosition(self.atom1_idx))
289
+ pos2 = np.array(conf.GetAtomPosition(self.atom2_idx)) # vertex
290
+ pos3 = np.array(conf.GetAtomPosition(self.atom3_idx))
291
+
292
+ vec1 = pos1 - pos2
293
+ vec2 = pos3 - pos2
294
+
295
+ cos_angle = np.dot(vec1, vec2) / (np.linalg.norm(vec1) * np.linalg.norm(vec2))
296
+ cos_angle = np.clip(cos_angle, -1.0, 1.0)
297
+ angle_rad = np.arccos(cos_angle)
298
+ return np.degrees(angle_rad)
299
+
300
+ def apply_changes(self):
301
+ """変更を適用"""
302
+ if self.atom1_idx is None or self.atom2_idx is None or self.atom3_idx is None:
303
+ return
304
+
305
+ try:
306
+ new_angle = float(self.angle_input.text())
307
+ if new_angle < 0 or new_angle >= 360:
308
+ QMessageBox.warning(self, "Invalid Input", "Angle must be between 0 and 360 degrees.")
309
+ return
310
+ except ValueError:
311
+ QMessageBox.warning(self, "Invalid Input", "Please enter a valid number.")
312
+ return
313
+
314
+ # Undo状態を保存
315
+ self.main_window.push_undo_state()
316
+
317
+ # Apply the angle change
318
+ self.adjust_angle(new_angle)
319
+
320
+ # キラルラベルを更新
321
+ self.main_window.update_chiral_labels()
322
+
323
+ def adjust_angle(self, new_angle_deg):
324
+ """角度を調整(均等回転オプション付き)"""
325
+ conf = self.mol.GetConformer()
326
+ pos1 = np.array(conf.GetAtomPosition(self.atom1_idx))
327
+ pos2 = np.array(conf.GetAtomPosition(self.atom2_idx)) # vertex
328
+ pos3 = np.array(conf.GetAtomPosition(self.atom3_idx))
329
+
330
+ vec1 = pos1 - pos2
331
+ vec2 = pos3 - pos2
332
+
333
+ # Current angle
334
+ current_angle_rad = np.arccos(np.clip(
335
+ np.dot(vec1, vec2) / (np.linalg.norm(vec1) * np.linalg.norm(vec2)), -1.0, 1.0))
336
+
337
+ # Target angle
338
+ target_angle_rad = np.radians(new_angle_deg)
339
+
340
+ # Rotation axis (perpendicular to the plane containing vec1 and vec2)
341
+ rotation_axis = np.cross(vec1, vec2)
342
+ rotation_axis_norm = np.linalg.norm(rotation_axis)
343
+
344
+ if rotation_axis_norm == 0:
345
+ # Vectors are parallel, cannot rotate
346
+ return
347
+
348
+ rotation_axis = rotation_axis / rotation_axis_norm
349
+
350
+ # Total rotation angle needed
351
+ total_rotation_angle = target_angle_rad - current_angle_rad
352
+
353
+ # Rodrigues' rotation formula
354
+ def rotate_vector(v, axis, angle):
355
+ cos_a = np.cos(angle)
356
+ sin_a = np.sin(angle)
357
+ return v * cos_a + np.cross(axis, v) * sin_a + axis * np.dot(axis, v) * (1 - cos_a)
358
+
359
+ if self.both_groups_radio.isChecked():
360
+ # Both arms rotate equally (half angle each in opposite directions)
361
+ half_rotation = total_rotation_angle / 2
362
+
363
+ # Get both connected groups
364
+ group1_atoms = self.get_connected_group(self.atom1_idx, exclude=self.atom2_idx)
365
+ group3_atoms = self.get_connected_group(self.atom3_idx, exclude=self.atom2_idx)
366
+
367
+ # Rotate group 1 by -half_rotation
368
+ for atom_idx in group1_atoms:
369
+ current_pos = np.array(conf.GetAtomPosition(atom_idx))
370
+ relative_pos = current_pos - pos2
371
+ rotated_pos = rotate_vector(relative_pos, rotation_axis, -half_rotation)
372
+ new_pos = pos2 + rotated_pos
373
+ conf.SetAtomPosition(atom_idx, new_pos.tolist())
374
+ self.main_window.atom_positions_3d[atom_idx] = new_pos
375
+
376
+ # Rotate group 3 by +half_rotation
377
+ for atom_idx in group3_atoms:
378
+ current_pos = np.array(conf.GetAtomPosition(atom_idx))
379
+ relative_pos = current_pos - pos2
380
+ rotated_pos = rotate_vector(relative_pos, rotation_axis, half_rotation)
381
+ new_pos = pos2 + rotated_pos
382
+ conf.SetAtomPosition(atom_idx, new_pos.tolist())
383
+ self.main_window.atom_positions_3d[atom_idx] = new_pos
384
+
385
+ elif self.rotate_atom_radio.isChecked():
386
+ # Move only the third atom
387
+ new_vec2 = rotate_vector(vec2, rotation_axis, total_rotation_angle)
388
+ new_pos3 = pos2 + new_vec2
389
+ conf.SetAtomPosition(self.atom3_idx, new_pos3.tolist())
390
+ self.main_window.atom_positions_3d[self.atom3_idx] = new_pos3
391
+ else:
392
+ # Rotate the connected group around atom2 (vertex) - default behavior
393
+ atoms_to_move = self.get_connected_group(self.atom3_idx, exclude=self.atom2_idx)
394
+
395
+ for atom_idx in atoms_to_move:
396
+ current_pos = np.array(conf.GetAtomPosition(atom_idx))
397
+ # Transform to coordinate system centered at atom2
398
+ relative_pos = current_pos - pos2
399
+ # Rotate around the rotation axis
400
+ rotated_pos = rotate_vector(relative_pos, rotation_axis, total_rotation_angle)
401
+ # Transform back to world coordinates
402
+ new_pos = pos2 + rotated_pos
403
+ conf.SetAtomPosition(atom_idx, new_pos.tolist())
404
+ self.main_window.atom_positions_3d[atom_idx] = new_pos
405
+
406
+ # Update the 3D view
407
+ self.main_window.draw_molecule_3d(self.mol)
408
+
409
+ def get_connected_group(self, start_atom, exclude=None):
410
+ """指定された原子から連結されているグループを取得"""
411
+ visited = set()
412
+ to_visit = [start_atom]
413
+
414
+ while to_visit:
415
+ current = to_visit.pop()
416
+ if current in visited or current == exclude:
417
+ continue
418
+
419
+ visited.add(current)
420
+
421
+ # Get neighboring atoms
422
+ atom = self.mol.GetAtomWithIdx(current)
423
+ for bond in atom.GetBonds():
424
+ other_idx = bond.GetOtherAtomIdx(current)
425
+ if other_idx not in visited and other_idx != exclude:
426
+ to_visit.append(other_idx)
427
+
428
+ return visited
Binary file
Binary file
Binary file