public
Created

Simple Qt application embedding a PyMol instance.

  • Download Gist
main.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#!/usr/bin/python
"""
PyMol Widget
 
Usage:
main.py [input_files]
 
Options:
-h, --help: print this screen
"""
from pymolwidget import PyMolWidget
from PyQt4 import QtGui, QtCore
import sys, getopt
 
 
class Usage(Exception):
"""
"""
def __init__(self, msg):
self.msg = msg
 
 
if __name__ == '__main__':
argv = sys.argv
app = QtGui.QApplication(argv)
kinect_enabled = True
try:
try:
opts, args = getopt.getopt(argv[1:], "h", ["help"])
except getopt.error, msg:
raise Usage(msg)
 
# process options
for o, a in opts:
if o in ("-h", "--help"):
print __doc__
sys.exit(0)
except Usage, err:
print >>sys.stderr, err.msg
print >>sys.stderr, "for help use --help"
sys.exit(2)
 
# process args
input_files = []
if len(args):
input_files = args[:]
window = PyMolWidget()
window.show()
for f in input_files:
window.loadMolFile(f)
 
sys.exit(app.exec_())
pymolwidget.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
from PyQt4.QtOpenGL import *
from PyQt4 import QtCore
from PyQt4.Qt import Qt
from OpenGL.GL import *
import pymol2
 
buttonMap = {
Qt.LeftButton:0,
Qt.MidButton:1,
Qt.RightButton:2,
}
 
class PyMolWidget(QGLWidget):
def __init__(self, parent=None):
self._enableUi = True
f = QGLFormat()
f.setStencil(True)
f.setRgba(True)
f.setDepth(True)
f.setDoubleBuffer(True)
super(PyMolWidget, self).__init__(f, parent)
 
def initializeGL(self):
"""
Reimplemented from QGLWidget
Instance PyMOL _only_ when we're sure there's an OGL context up and running
(i.e. in this method :-)
"""
self._pymol = pymol2.PyMOL()
self._pymol.start()
if not self._enableUi:
self._pymol.cmd.set("internal_gui", 0)
self._pymol.cmd.set("internal_feedback", 0)
self._pymol.cmd.button("double_left", "None", "None")
self._pymol.cmd.button("single_right", "None", "None")
 
self._pymol.reshape(self.width(), self.height())
self.resizeGL(self.width(), self.height())
self._pymolProcess()
 
def paintGL(self):
glViewport(0, 0, self.width(), self.height())
self._pymol.idle()
self._pymol.draw()
 
def resizeGL(self, w, h):
self._pymol.reshape(w, h, True)
self._pymolProcess()
 
def loadMolFile(self, mol_file):
self._pymol.cmd.load(str(mol_file))
 
 
def _pymolProcess(self):
self._pymol.idle()
self.update()
 
def mouseMoveEvent(self, ev):
self._pymol.drag(ev.x(), self.height() - ev.y(), 0)
self._pymolProcess()
 
def mousePressEvent(self, ev):
if not self._enableUi:
self._pymol.cmd.button("double_left", "None", "None")
self._pymol.cmd.button("single_right", "None", "None")
self._pymol.button(buttonMap[ev.button()], 0, ev.x(), self.height() - ev.y(), 0)
self._pymolProcess()
 
def mouseReleaseEvent(self, ev):
self._pymol.button(buttonMap[ev.button()], 1, ev.x(), self.height() - ev.y(), 0)
self._pymolProcess()
 
def wheelEvent(self, ev):
button = 3 if ev.delta() > 0 else 4
self._pymol.button(button, 0, ev.x(), ev.y(), 0)
self._pymolProcess()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.