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