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 != 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 
202 class WindowMenu(QMenu):
203  def __init__(self, parent=None):
204  QMenu.__init__(self, parent)
205  self.setTitle('Window')
206  gosty=gui.GostyApp.Instance()
207  QObject.connect(self, SIGNAL('aboutToShow()'), self._AboutToShow)
208  inspector_visible=gosty.GetWidget('InspectorDialog').isVisible()
209  self._gl_win_action=gui.AddMenuAction(self, 'GL Window',
210  self._ToggleShowGLWindow, checkable=True,
211  checked=gosty.gl_win.qobject.isVisible(),
212  shortcut='Ctrl+G')
213  self._inspector_action=gui.AddMenuAction(self, 'Inspector',
215  checkable=True,
216  checked=inspector_visible,
217  shortcut='Ctrl+I')
218  self.addSeparator()
219  self.addMenu(gosty.perspective.panels.menu)
220  gui.AddMenuAction(self, 'Reset View', self._ResetView)
221  def _AboutToShow(self):
222  gosty=gui.GostyApp.Instance()
223  self._gl_win_action.setChecked(gosty.gl_win.qobject.isVisible())
224  inspector_visible=gosty.GetWidget('InspectorDialog').isVisible()
225  self._inspector_action.setChecked(inspector_visible)
226 
227  def _ToggleShowGLWindow(self):
228  gosty=gui.GostyApp.Instance()
229  gl_win=gosty.GetGLWin()
230  if gl_win and gl_win.qobject.isHidden():
231  gl_win.Show()
232  else:
233  gl_win.Hide()
234 
235  def _ToggleShowInspector(self):
236  gosty=gui.GostyApp.Instance()
237  inspector=gosty.GetWidget('InspectorDialog')
238  if inspector and inspector.isHidden():
239  inspector.show()
240  else:
241  inspector.hide()
242 
243  def _ResetView(self):
244  msg_box = QMessageBox()
245  msg_box.setWindowTitle("Reset the Panels and Widget");
246  msg_box.setIcon(QMessageBox.Question)
247  msg_box.setText("Do you really want to reset the Panels and Widgets?");
248  msg_box.setStandardButtons(QMessageBox.Yes |
249  QMessageBox.Cancel);
250  msg_box.setDefaultButton(QMessageBox.Cancel);
251  ret = msg_box.exec_();
252  if(ret == QMessageBox.Yes):
253  settings = QSettings()
254  settings.setValue("restore_settings",QVariant(False))
255  info_box = QMessageBox()
256  info_box.setStandardButtons(QMessageBox.Ok)
257  info_box.setIcon(QMessageBox.Information)
258  info_box.setWindowTitle("Restart OpenStructure")
259  info_box.setText("You must restart OpenStructure for the changes to take effect!");
260  info_box.exec_();
261 
262 class HelpMenu(QMenu):
263  def __init__(self, parent=None):
264  QMenu.__init__(self, parent)
265  self.setTitle('Help')
266  gui.AddMenuAction(self, 'Documentation', self._VisitDocs)
267  gui.AddMenuAction(self, 'About', self._ShowAboutDialog)
268  if sys.platform=='darwin':
269  gui.AddMenuAction(self, 'Install Command Line Tool',
270  termuse.InstallTerminalPrograms)
271  def _VisitDocs(self):
272  QDesktopServices.openUrl(QUrl("http://www.openstructure.org/docs/"))
273 
274  def _ShowAboutDialog(self):
275  _InitSplash()
276 
277 def _InitMenu():
278  _InitMenu.mbar=gui.GostyApp.Instance().perspective.GetMenuBar()
279  file_menu=FileMenu(_InitMenu.mbar)
280  scene_menu=SceneMenu(_InitMenu.mbar)
281  win_menu=WindowMenu(_InitMenu.mbar)
282  help_menu=HelpMenu(_InitMenu.mbar)
283  _InitMenu.mbar.addMenu(file_menu)
284  _InitMenu.mbar.addMenu(scene_menu)
285  _InitMenu.mbar.addMenu(win_menu)
286  _InitMenu.mbar.addMenu(help_menu)