Skip to content

Instantly share code, notes, and snippets.

@masci
Created September 4, 2013 13:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save masci/6437112 to your computer and use it in GitHub Desktop.
Save masci/6437112 to your computer and use it in GitHub Desktop.
Simple Qt application embedding a PyMol instance.
#!/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_())
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment