OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
menu.py
Go to the documentation of this file.
1 from PyQt4.QtCore import *
2 from PyQt4.QtGui import *
3 from ost import *
4 from ost import gui
5 from ost.gui.init_splash import _InitSplash
6 from ost.gui.dng import termuse
7 from ost.gui.dng import superpositiondialog
8 
9 import sys
10 class FileMenu(QMenu):
11  def __init__(self, parent=None):
12  QMenu.__init__(self, parent)
13  self.setTitle('File')
14  gui.AddMenuAction(self, 'Open', self._Open, shortcut='Ctrl+O')
15  gui.AddMenuAction(self, 'Save As...', self._SaveAs,
16  shortcut='Ctrl+Shift+S',
17  enabled=gui.OneOf(gfx.Entity))
18  def _Open(self):
19  filename=QFileDialog.getOpenFileName(None, 'Open file','')
20  if(QFileInfo(filename).isFile()):
21  gui.FileLoader.LoadObject(str(filename))
22 
23  def _SaveAs(self):
24  sel_node=gui.SceneSelection.Instance().GetActiveNode(0)
25  filename=QFileDialog.getSaveFileName(None, 'Save As',
26  '%s.pdb' % sel_node.name)
27  io.SavePDB(sel_node.view, str(filename))
28 
29 class ClipWidget(QWidget):
30  def __init__(self, width=500, height=500):
31  QWidget.__init__(self)
32  self.setWindowFlags(Qt.Tool)
33  l = QGridLayout(self)
34  l.addWidget(QLabel("Near"), 0, 0)
35  l.addWidget(QLabel("Far"), 1, 0)
36  bounds_near = QLineEdit(str(0))
37  bounds_near.setValidator(QIntValidator(0, 9999, bounds_near))
38  bounds_near.setMaxLength(4)
39  bounds_near.setMaximumWidth(50)
40  bounds_far = QLineEdit(str(200))
41  bounds_far.setValidator(QIntValidator(0, 9999, bounds_far))
42  bounds_far.setMaxLength(4)
43  bounds_far.setMaximumWidth(50)
44  l.addWidget(bounds_near, 0, 1, 2, 1)
45  l.addWidget(bounds_far, 0, 3, 2, 1)
46  self.near_ = QSlider(Qt.Horizontal)
47  self.near_.setMinimum(int(bounds_near.text()))
48  self.near_.setMaximum(int(bounds_far.text()))
49  self.near_.setValue(int(gfx.Scene().near))
50  self.far_ = QSlider(Qt.Horizontal)
51  self.far_.setMinimum(int(bounds_near.text()))
52  self.far_.setMaximum(int(bounds_far.text()))
53  far = int(gfx.Scene().far)
54  if far>sys.maxint:
55  far = sys.maxint
56  self.far_.setValue(far)
57  self.auto_ = QCheckBox("Continuous Automatic Clipping")
58  self.auto_.setChecked(gfx.Scene().GetAutoAutoslab())
59 
60  l.addWidget(self.near_, 0, 2)
61  l.addWidget(self.far_, 1, 2)
62  l.addWidget(self.auto_, 2, 0, 1, 4)
63  self.connect(self.near_, SIGNAL('valueChanged(int)'), self.SetNear)
64  self.connect(self.far_, SIGNAL('valueChanged(int)'), self.SetFar)
65  self.connect(self.auto_, SIGNAL('stateChanged(int)'), self.SetAuto)
66  self.connect(bounds_near, SIGNAL('textEdited(QString)'), self.SetNearBounds)
67  self.connect(bounds_far, SIGNAL('textEdited(QString)'), self.SetFarBounds)
68 
69  def SetNear(self, val):
70  gfx.Scene().near = val
71 
72  def SetFar(self, val):
73  gfx.Scene().far = val
74 
75  def SetAuto(self, val):
76  gfx.Scene().AutoAutoslab(val)
77  gfx.Scene().near = int(self.near_.value())
78  gfx.Scene().far = int(self.far_.value())
79 
80  def SetNearBounds(self, text):
81  if text!='':
82  self.near_.setMinimum(int(text))
83  self.far_.setMinimum(int(text))
84 
85  def SetFarBounds(self, text):
86  if text!='':
87  self.near_.setMaximum(int(text))
88  self.far_.setMaximum(int(text))
89 
90 class ExportSceneDialog(QDialog):
91  def __init__(self, width=500, height=500):
92  QDialog.__init__(self)
93  l=QGridLayout(self)
94  l.setColumnMinimumWidth(0, 100)
95  l.addWidget(QLabel("Width (px)"), 0, 0)
96  l.addWidget(QLabel("Height (px)"), 1, 0)
97  self.width_=QLineEdit(str(width))
98  self.height_=QLineEdit((str(height)))
99  self.width_.setValidator(QIntValidator(50, 3000, self.width_))
100  self.height_.setValidator(QIntValidator(50, 3000, self.height_))
101  self.opaque_=QCheckBox("Force Opaque Background")
102  l.addWidget(self.width_, 0, 1)
103  l.addWidget(self.height_, 1, 1)
104  l.addWidget(self.opaque_, 2, 1)
105  hbox=QHBoxLayout()
106  cancel=QPushButton("Cancel")
107  QObject.connect(cancel, SIGNAL('clicked()'), self.reject)
108  hbox.addWidget(cancel)
109  export=QPushButton("Export")
110  hbox.addWidget(export)
111  export.setDefault(True)
112  l.addLayout(hbox, 3, 1, 2, 1)
113  QObject.connect(export, SIGNAL('clicked()'), self.accept)
114  @property
115  def transparent(self):
116  return not self.opaque_.isChecked()
117 
118  @property
119  def width(self):
120  return int(self.width_.text())
121 
122  @property
123  def height(self):
124  return int(self.height_.text())
125 
126 class SceneMenu(QMenu):
127  def __init__(self, parent=None):
128  QMenu.__init__(self, parent)
129  self.setTitle('Scene')
130  QObject.connect(self, SIGNAL('aboutToShow()'), self._AboutToShow)
131  gui.AddMenuAction(self, 'Background Color', self._SetSceneBackground)
132  self.fog_action=gui.AddMenuAction(self, 'Depth Cueing', self._ToggleFog,
133  shortcut='Ctrl+Shift+F', checkable=True,
134  checked=gfx.Scene().fog)
135  gui.AddMenuAction(self, 'Center', self._Center,
136  enabled=gui.ManyOf(gfx.GfxObj))
137  gui.AddMenuAction(self, 'Fit To Screen', self._FitToScreen,
138  enabled=gui.OneOf(gfx.Entity))
139  gui.AddMenuAction(self, 'Superpose', self._SuperposeDialog,
140  enabled=gui.TwoOf(gfx.Entity))
141  gui.AddMenuAction(self, 'Save Snapshot', self._ExportScene)
142  gui.AddMenuAction(self, 'Scene Clipping', self._ClipScene, shortcut='Ctrl+Shift+C')
143 
144  def _ExportScene(self):
145  qd=ExportSceneDialog()
146  if not qd.exec_():
147  return
148  filename=QFileDialog.getSaveFileName(None, 'Save Snapshot',
149  'snapshot.png')
150  if filename:
151  gfx.Scene().Export(str(filename), qd.width, qd.height, qd.transparent)
152 
153  def _ClipScene(self):
154  self.cw = ClipWidget()
155  self.cw.show()
156 
157  def _AboutToShow(self):
158  self.fog_action.setChecked(gfx.Scene().fog)
159 
160  def _Center(self):
161  sel=gui.SceneSelection.Instance()
162  gfx.Scene().center=sel.GetActiveNode(0).center
163 
164  def _SetSceneBackground(self):
165  new_color=gui.PickColor(gfx.Scene().bg)
166  if new_color:
167  gfx.Scene().bg=new_color
168 
169  def _ToggleFog(self):
170  gfx.Scene().fog=not gfx.Scene().fog
171  self.fog_action.setChecked(gfx.Scene().fog)
172 
173  def _FitToScreen(self):
174  sel=gui.SceneSelection.Instance()
175  gfx.FitToScreen(sel.GetActiveNode(0))
176 
177  def _SuperposeDialog(self):
178  sel=gui.SceneSelection.Instance()
179  act_count=sel.GetActiveNodeCount()
180  # we now that there have to be 2 gfx.Entities, because of using TwoOf to
181  # enable menu entry!
182  i = 0;
183  gfx_ent_1 = sel.GetActiveNode(i)
184  while not isinstance(gfx_ent_1, gfx.Entity):
185  i += 1
186  gfx_ent_1 = sel.GetActiveNode(i)
187  i += 1
188  gfx_ent_2 = sel.GetActiveNode(i)
189  while not isinstance(gfx_ent_2, gfx.Entity):
190  i += 1
191  gfx_ent_2 = sel.GetActiveNode(i)
192  sd = superpositiondialog.SuperpositionDialog(gfx_ent_1, gfx_ent_2)
193  if sd.rmsd_superposed_atoms != None:
194  if sd.reference == 0:
195  gfx_ent_2.UpdatePositions()
196  gfx.Scene().CenterOn(gfx_ent_1)
197  else:
198  gfx_ent_1.UpdatePositions()
199  gfx.Scene().CenterOn(gfx_ent_2)
200  LogScript('RMSD: %.3f'%sd.rmsd)
201  LogScript('RMSD superposed atoms: %.3f'%sd.rmsd_superposed_atoms)
202  LogScript('fraction superposed: %.3f'%sd.fraction_superposed)
203  elif sd.rmsd != None:
204  if sd.reference == 0:
205  gfx_ent_2.UpdatePositions()
206  gfx.Scene().CenterOn(gfx_ent_1)
207  else:
208  gfx_ent_1.UpdatePositions()
209  gfx.Scene().CenterOn(gfx_ent_2)
210  LogScript('RMSD: %.3f'%sd.rmsd)
211 
212 class WindowMenu(QMenu):
213  def __init__(self, parent=None):
214  QMenu.__init__(self, parent)
215  self.setTitle('Window')
216  gosty=gui.GostyApp.Instance()
217  QObject.connect(self, SIGNAL('aboutToShow()'), self._AboutToShow)
218  inspector_visible=gosty.GetWidget('InspectorDialog').isVisible()
219  self._gl_win_action=gui.AddMenuAction(self, 'GL Window',
220  self._ToggleShowGLWindow, checkable=True,
221  checked=gosty.gl_win.qobject.isVisible(),
222  shortcut='Ctrl+G')
223  self._inspector_action=gui.AddMenuAction(self, 'Inspector',
225  checkable=True,
226  checked=inspector_visible,
227  shortcut='Ctrl+I')
228  self.addSeparator()
229  self.addMenu(gosty.perspective.panels.menu)
230  gui.AddMenuAction(self, 'Reset View', self._ResetView)
231  def _AboutToShow(self):
232  gosty=gui.GostyApp.Instance()
233  self._gl_win_action.setChecked(gosty.gl_win.qobject.isVisible())
234  inspector_visible=gosty.GetWidget('InspectorDialog').isVisible()
235  self._inspector_action.setChecked(inspector_visible)
236 
237  def _ToggleShowGLWindow(self):
238  gosty=gui.GostyApp.Instance()
239  gl_win=gosty.GetGLWin()
240  if gl_win and gl_win.qobject.isHidden():
241  gl_win.Show()
242  else:
243  gl_win.Hide()
244 
245  def _ToggleShowInspector(self):
246  gosty=gui.GostyApp.Instance()
247  inspector=gosty.GetWidget('InspectorDialog')
248  if inspector and inspector.isHidden():
249  inspector.show()
250  else:
251  inspector.hide()
252 
253  def _ResetView(self):
254  msg_box = QMessageBox()
255  msg_box.setWindowTitle("Reset the Panels and Widget");
256  msg_box.setIcon(QMessageBox.Question)
257  msg_box.setText("Do you really want to reset the Panels and Widgets?");
258  msg_box.setStandardButtons(QMessageBox.Yes |
259  QMessageBox.Cancel);
260  msg_box.setDefaultButton(QMessageBox.Cancel);
261  ret = msg_box.exec_();
262  if(ret == QMessageBox.Yes):
263  settings = QSettings()
264  settings.setValue("restore_settings",QVariant(False))
265  info_box = QMessageBox()
266  info_box.setStandardButtons(QMessageBox.Ok)
267  info_box.setIcon(QMessageBox.Information)
268  info_box.setWindowTitle("Restart OpenStructure")
269  info_box.setText("You must restart OpenStructure for the changes to take effect!");
270  info_box.exec_();
271 
272 class HelpMenu(QMenu):
273  def __init__(self, parent=None):
274  QMenu.__init__(self, parent)
275  self.setTitle('Help')
276  gui.AddMenuAction(self, 'Documentation', self._VisitDocs)
277  gui.AddMenuAction(self, 'About', self._ShowAboutDialog)
278  if sys.platform=='darwin':
279  gui.AddMenuAction(self, 'Install Command Line Tool',
280  termuse.InstallTerminalPrograms)
281  def _VisitDocs(self):
282  QDesktopServices.openUrl(QUrl("http://www.openstructure.org/docs/"))
283 
284  def _ShowAboutDialog(self):
285  _InitSplash()
286 
287 def _InitMenu():
288  _InitMenu.mbar=gui.GostyApp.Instance().perspective.GetMenuBar()
289  file_menu=FileMenu(_InitMenu.mbar)
290  scene_menu=SceneMenu(_InitMenu.mbar)
291  win_menu=WindowMenu(_InitMenu.mbar)
292  help_menu=HelpMenu(_InitMenu.mbar)
293  _InitMenu.mbar.addMenu(file_menu)
294  _InitMenu.mbar.addMenu(scene_menu)
295  _InitMenu.mbar.addMenu(win_menu)
296  _InitMenu.mbar.addMenu(help_menu)
main class for organization and root for the graphical display
Definition: scene.hh:81
main class for all graphic objects
Definition: gfx_object.hh:51
graphical rendering of mol::EntityHandle entites
Definition: entity.hh:63