From f5cc40023f05a42f2ddf9450d650fb1524db848d Mon Sep 17 00:00:00 2001
From: Gabriel Studer <gabriel.studer@unibas.ch>
Date: Tue, 20 Mar 2018 20:11:24 +0100
Subject: [PATCH] make example code work with qt5

IMPORTANT: the harmony example has been removed. If anybody is willing to
invest the required time: go for it...
---
 examples/code_fragments/harmony/glwin.py      | 91 -------------------
 examples/code_fragments/harmony/harmony       |  2 -
 examples/code_fragments/harmony/harmony.py    | 65 -------------
 examples/code_fragments/mm/ethanol_example.py |  5 +-
 .../mm/ethanol_example_using_topology.py      |  4 +-
 examples/code_fragments/mm/gb_example.py      |  4 +-
 examples/code_fragments/trj/test_trj.py       |  5 +-
 examples/code_fragments/ui/board.py           | 34 +++----
 examples/code_fragments/ui/mdi_example.py     |  6 +-
 examples/code_fragments/ui/menubar_example.py | 11 +--
 examples/code_fragments/ui/widget_example.py  |  2 +-
 examples/demos/charmm_trj_blur.py             |  4 +-
 examples/demos/the_hammer.py                  |  5 +-
 13 files changed, 36 insertions(+), 202 deletions(-)
 delete mode 100644 examples/code_fragments/harmony/glwin.py
 delete mode 100755 examples/code_fragments/harmony/harmony
 delete mode 100644 examples/code_fragments/harmony/harmony.py

diff --git a/examples/code_fragments/harmony/glwin.py b/examples/code_fragments/harmony/glwin.py
deleted file mode 100644
index 8e0549cec..000000000
--- a/examples/code_fragments/harmony/glwin.py
+++ /dev/null
@@ -1,91 +0,0 @@
-import math
-from ost import gfx, geom, mol
-from PyQt4.QtGui import *
-from PyQt4.QtCore import *
-from PyQt4.QtOpenGL import *
-
-TRANS_VAL = 20
-
-class DokkGLCanvas(QGLWidget):
-
-  def __init__(self, format, parent=None):
-    QGLWidget.__init__(self, format, parent)
-    self.last_pos_=QPoint()
-    self.setAutoFillBackground(False)
-    #self.setAttribute(Qt.WA_KeyCompression,True)
-    self.resize(800, 800)
-    self.atom=mol.AtomHandle()
-  def initializeGL(self):
-    gfx.Scene().InitGL()
-    self.startTimer(10)
-  def paintGL(self):
-    gfx.Scene().RenderGL()
-  def paintEvent(self, event):
-    self.makeCurrent()
-    self.paintGL()
-    #painter=QPainter(self)
-    #self.RenderHUD(painter)
-    self.swapBuffers()
-
-  def RenderHUD(self, painter):
-    painter.setPen(QColor(100, 100, 100, 50))
-    painter.setBrush(QColor(200, 200, 200, 50))
-    painter.drawRect(QRect(QPoint(0, 0), QSize(self.width(), 25)))
-    painter.setPen(QPen(QColor(255,255,255), Qt.SolidLine))
-    painter.setFont(QFont("Verdana"))
-  def resizeGL(self, w, h):
-    gfx.Scene().Resize(w, h)
-
-  def mousePressEvent(self, event):
-    self.last_point_=QPoint(event.x(), event.y())
-    v1=gfx.Scene().UnProject(geom.Vec3(event.x(), self.height()-event.y(), 0.0));
-    v2=gfx.Scene().UnProject(geom.Vec3(event.x(), self.height()-event.y(), 1.0));
-    self.atom=self.world.go.PickAtom(geom.Line3(v1, v2), 0.1)
-    self.world.go.SetColor(gfx.WHITE, '')
-    if self.atom.IsValid():
-      self.world.go.SetColorForAtom(gfx.RED, self.atom)
-  def mouseReleaseEvent(self, event):
-    self.world.go.SetColor(gfx.WHITE, '')
-    self.atom=mol.AtomHandle()
-  def mouseMoveEvent(self, event):
-    delta=QPoint(event.x(), event.y())-self.last_point_
-    self.last_point_=QPoint(event.x(), event.y())
-    if event.buttons() & Qt.LeftButton:
-      if self.atom.IsValid():
-        rot=gfx.Scene().GetTransform().GetRot()
-        force=-rot.GetRow(1)*delta.y()+rot.GetRow(0)*delta.x()
-        self.world.AddForce(self.atom, force*2)
-  def wheelEvent(self, event):
-    self.OnTransform(gfx.INPUT_COMMAND_TRANSZ,0,gfx.TRANSFORM_VIEW,
-              0.1*(-event.delta()))
-
-  def timerEvent(self, event):
-    self.world.Update()
-    self.update()
-  def OnTransform(self,com, indx, trg, val):
-    gfx.Scene().Apply(gfx.InputEvent(gfx.INPUT_DEVICE_MOUSE,
-                                     com, indx,trg,val*0.5),False)
-
-  def keyReleaseEvent(self, event):
-    if event.key() == Qt.Key_Escape:
-      QApplication.exit()
-
-class DokkGLWin(gfx.GLWinBase):
-    def _CreateFormat(self):
-      fmt=QGLFormat()
-      fmt.setAlpha(True)
-      return fmt
-    def __init__(self):
-        gfx.GLWinBase.__init__(self)
-        self.canvas_=DokkGLCanvas(self._CreateFormat())
-    def DoRefresh(self):
-      self.refresh_=True
-    def SetLevel(self, level):
-      self.canvas_.SetLevel(level)
-    def Show(self, fullscreen):
-      if fullscreen:
-        self.canvas_.showFullScreen()
-      else:
-        self.canvas_.show()
-    def SetStereo():
-      pass
diff --git a/examples/code_fragments/harmony/harmony b/examples/code_fragments/harmony/harmony
deleted file mode 100755
index c315b3f08..000000000
--- a/examples/code_fragments/harmony/harmony
+++ /dev/null
@@ -1,2 +0,0 @@
-#!/bin/sh
-dng -s harmony.py
diff --git a/examples/code_fragments/harmony/harmony.py b/examples/code_fragments/harmony/harmony.py
deleted file mode 100644
index d9f98316d..000000000
--- a/examples/code_fragments/harmony/harmony.py
+++ /dev/null
@@ -1,65 +0,0 @@
-import glwin
-from ost import io, geom, gfx, mol
-
-class World:
-  def __init__(self):
-    self.atom_string=mol.CreateEntity()
-    edi=self.atom_string.EditXCS(mol.EditMode.BUFFERED_EDIT)
-    chain=edi.InsertChain("A")
-    r=edi.AppendResidue(chain, "STRING")
-    prev_atom=mol.AtomHandle()
-    for i in range(-5, 5):
-      atom=edi.InsertAtom(r, "X%02d" % (i+5), geom.Vec3(i, 0, 0))
-      if prev_atom.IsValid():
-        edi.Connect(prev_atom, atom)
-      prev_atom=atom
-    self.go=gfx.Entity("GO", gfx.CUSTOM, self.atom_string)
-    self.go.custom_options.SetBondRad(0.1)
-    self.go.custom_options.SetSphereRad(0.4)
-    scene.Add(self.go)
-    scene.CenterOn(self.go)
-    self.last_positions=[]
-    for atom in self.atom_string.atoms:
-      self.last_positions.append(atom.pos)
-    self.forces=[]
-    for i in range(len(self.last_positions)):
-      self.forces.append(geom.Vec3())
-  def AddForce(self, atom, force):
-    for index, aa in enumerate(self.atom_string.atoms):
-      if aa==atom:
-        self.forces[index]+=force
-        break
-  def Update(self):
-    last_atom=mol.AtomHandle()
-    atoms=self.atom_string.atoms
-    for index, atom in enumerate(atoms):
-      if last_atom.IsValid():
-        diff=last_atom.pos-atom.pos
-        length=geom.Length(diff)
-        diff/=length
-        force=(length-1)**2*diff*100
-        if length<1.0:
-          force*=-1
-        self.forces[index-1]-=force
-        self.forces[index]+=force
-      last_atom=atom
-    for i in range(len(atoms)):
-      for j in range(i+2, len(atoms)):
-        diff=atoms[i].pos-atoms[j].pos
-        length=geom.Length(diff)
-        diff/=length
-        force=((1.0/length)**12-(1.0/length)**6)*diff
-        self.forces[i]+=force
-        self.forces[j]-=force
-    edi=self.atom_string.EditXCS(mol.EditMode.BUFFERED_EDIT)
-    for force, atom in zip(self.forces, self.atom_string.atoms):
-      edi.SetAtomPos(atom, atom.pos+force*0.004)
-    self.go.Rebuild()
-    for i in range(len(self.last_positions)):
-      self.forces[i]=geom.Vec3()
-
-dokk_win=glwin.DokkGLWin()
-
-world=World()
-dokk_win.canvas_.world=world
-dokk_win.Show(fullscreen=('--fullscreen' in sys.argv))
diff --git a/examples/code_fragments/mm/ethanol_example.py b/examples/code_fragments/mm/ethanol_example.py
index 1f8988d38..398be2be8 100644
--- a/examples/code_fragments/mm/ethanol_example.py
+++ b/examples/code_fragments/mm/ethanol_example.py
@@ -1,5 +1,5 @@
 from ost.mol.mm import *
-from PyQt4 import QtCore
+from PyQt5 import QtCore
 
 """
 MM - Demo
@@ -17,8 +17,7 @@ class Anim(QtCore.QTimer):
         self.sim=sim
         self.go = go
         self.ed = ent.EditXCS()
-        QtCore.QObject.connect(self, QtCore.SIGNAL("timeout()"), self.OnTimer)
-
+        self.timeout.connect(self.OnTimer)
         
     def OnTimer(self):
         self.sim.Steps(1)
diff --git a/examples/code_fragments/mm/ethanol_example_using_topology.py b/examples/code_fragments/mm/ethanol_example_using_topology.py
index 5587a07f9..553ef8860 100644
--- a/examples/code_fragments/mm/ethanol_example_using_topology.py
+++ b/examples/code_fragments/mm/ethanol_example_using_topology.py
@@ -1,5 +1,5 @@
 from ost.mol.mm import  *
-from PyQt4 import QtCore
+from PyQt5 import QtCore
 from math import sqrt
 from math import sin
 
@@ -28,7 +28,7 @@ class Anim(QtCore.QTimer):
         self.force_constant = param[3]
         self.steps = 0
         self.counter = 0
-        QtCore.QObject.connect(self, QtCore.SIGNAL("timeout()"), self.OnTimer)
+        self.timeout.connect(self.OnTimer)
 
     def OnTimer(self):
         if self.steps % 20 == 0:
diff --git a/examples/code_fragments/mm/gb_example.py b/examples/code_fragments/mm/gb_example.py
index 99a55343c..6859e29c7 100644
--- a/examples/code_fragments/mm/gb_example.py
+++ b/examples/code_fragments/mm/gb_example.py
@@ -1,5 +1,5 @@
 from ost.mol import mm
-from PyQt4 import QtCore
+from PyQt5 import QtCore
 
 """
 MM - Demo
@@ -17,7 +17,7 @@ class Anim(QtCore.QTimer):
         self.sim=sim
         self.go = go
         self.ed = ent.EditXCS(ost.mol.BUFFERED_EDIT)
-        QtCore.QObject.connect(self, QtCore.SIGNAL("timeout()"), self.OnTimer)
+        self.timeout.connect(self.OnTimer)
 
         
     def OnTimer(self):
diff --git a/examples/code_fragments/trj/test_trj.py b/examples/code_fragments/trj/test_trj.py
index a1625996c..5cee12d02 100644
--- a/examples/code_fragments/trj/test_trj.py
+++ b/examples/code_fragments/trj/test_trj.py
@@ -1,5 +1,5 @@
 # trajectory generated by Raimund Dutzler
-from PyQt4 import QtCore
+from PyQt5 import QtCore
 
 class Anim(QtCore.QTimer):
     def __init__(self,cg,go):
@@ -7,8 +7,7 @@ class Anim(QtCore.QTimer):
         self.cg_=cg
         self.go_=go
         self.frame_=0
-        QtCore.QObject.connect(self, QtCore.SIGNAL("timeout()"), self.OnTimer)
-
+        self.timeout.connect(self.OnTimer)
         
     def OnTimer(self):
         self.frame_=(self.frame_+1)%self.cg_.GetFrameCount()
diff --git a/examples/code_fragments/ui/board.py b/examples/code_fragments/ui/board.py
index 4a45ce1d3..6717d7291 100644
--- a/examples/code_fragments/ui/board.py
+++ b/examples/code_fragments/ui/board.py
@@ -4,15 +4,16 @@
 
 import sys
 import random
-from PyQt4 import QtCore, QtGui
+from PyQt5 import QtCore, QtGui, QtWidgets
 
-class Board(QtGui.QFrame):
+class Board(QtWidgets.QFrame):
   BoardWidth = 10
   BoardHeight = 22
   Speed = 300
+  messageToStatusBar = QtCore.pyqtSignal(str)
 
   def __init__(self, parent=None):
-    QtGui.QFrame.__init__(self, parent)
+    QtWidgets.QFrame.__init__(self, parent)
     self.resize(180, 380)
     self.setMinimumHeight(380)
     self.setMinimumWidth(180)
@@ -32,6 +33,7 @@ class Board(QtGui.QFrame):
   
     self.nextPiece.setRandomShape()
 
+
   def shapeAt(self, x, y):
     return self.board[(y * Board.BoardWidth) + x]
 
@@ -52,9 +54,8 @@ class Board(QtGui.QFrame):
     self.isWaitingAfterLine = False
     self.numLinesRemoved = 0
     self.clearBoard()
-  
-    self.emit(QtCore.SIGNAL("messageToStatusbar(QString)"), 
-      str(self.numLinesRemoved))
+
+    self.messageToStatusBar.emit(str(self.numLinesRemoved))
   
     self.newPiece()
     self.timer.start(Board.Speed, self)
@@ -66,12 +67,10 @@ class Board(QtGui.QFrame):
     self.isPaused = not self.isPaused
     if self.isPaused:
       self.timer.stop()
-      self.emit(QtCore.SIGNAL("messageToStatusbar(QString)"), "paused")
+      self.messageToStatusBar.emit("paused")
     else:
       self.timer.start(Board.Speed, self)
-      self.emit(QtCore.SIGNAL("messageToStatusbar(QString)"), 
-      str(self.numLinesRemoved))
-  
+      self.messageToStatusBar.emit(str(self.numLinesRemoved))
     self.update()
 
   def paintEvent(self, event):
@@ -98,7 +97,7 @@ class Board(QtGui.QFrame):
 
   def keyPressEvent(self, event):
     if not self.isStarted or self.curPiece.shape() == Tetrominoes.NoShape:
-      QtGui.QWidget.keyPressEvent(self, event)
+      QtWidgets.QWidget.keyPressEvent(self, event)
       return
 
     key = event.key()
@@ -120,7 +119,7 @@ class Board(QtGui.QFrame):
     elif key == QtCore.Qt.Key_D:
       self.oneLineDown()
     else:
-      QtGui.QWidget.keyPressEvent(self, event)
+      QtWidgets.QWidget.keyPressEvent(self, event)
 
   def timerEvent(self, event):
     if event.timerId() == self.timer.timerId():
@@ -185,8 +184,7 @@ class Board(QtGui.QFrame):
 
     if numFullLines > 0:
       self.numLinesRemoved = self.numLinesRemoved + numFullLines
-      self.emit(QtCore.SIGNAL("messageToStatusbar(QString)"), 
-    str(self.numLinesRemoved))
+      self.messageToStatusBar.emit(str(self.LinesRemoved))
       self.isWaitingAfterLine = True
       self.curPiece.setShape(Tetrominoes.NoShape)
       self.update()
@@ -201,9 +199,7 @@ class Board(QtGui.QFrame):
       self.curPiece.setShape(Tetrominoes.NoShape)
       self.timer.stop()
       self.isStarted = False
-      self.emit(QtCore.SIGNAL("messageToStatusbar(QString)"), "Game over")
-
-
+      self.messageToStatusBar.emit("Game over")
 
   def tryMove(self, newPiece, newX, newY):
     for i in range(4):
@@ -228,11 +224,11 @@ class Board(QtGui.QFrame):
     painter.fillRect(x + 1, y + 1, self.squareWidth() - 2, 
     self.squareHeight() - 2, color)
 
-    painter.setPen(color.light())
+    painter.setPen(color.lighter())
     painter.drawLine(x, y + self.squareHeight() - 1, x, y)
     painter.drawLine(x, y, x + self.squareWidth() - 1, y)
 
-    painter.setPen(color.dark())
+    painter.setPen(color.darker())
     painter.drawLine(x + 1, y + self.squareHeight() - 1,
       x + self.squareWidth() - 1, y + self.squareHeight() - 1)
     painter.drawLine(x + self.squareWidth() - 1, 
diff --git a/examples/code_fragments/ui/mdi_example.py b/examples/code_fragments/ui/mdi_example.py
index ba8b14d8c..d6cd2abe2 100644
--- a/examples/code_fragments/ui/mdi_example.py
+++ b/examples/code_fragments/ui/mdi_example.py
@@ -16,14 +16,14 @@
 # along with this library; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 #------------------------------------------------------------------------------
-from PyQt4 import QtCore, QtGui
+from PyQt5 import QtGui, QtCore, QtWidgets
 
 from ost import gui
 import sys, random
 
-class Points(QtGui.QWidget):
+class Points(QtWidgets.QWidget):
     def __init__(self, parent=None):
-        QtGui.QWidget.__init__(self, parent)
+        QtWidgets.QWidget.__init__(self, parent)
         self.setWindowTitle('Some Points')
 
     def paintEvent(self, event):
diff --git a/examples/code_fragments/ui/menubar_example.py b/examples/code_fragments/ui/menubar_example.py
index fc953b7ad..52000903c 100644
--- a/examples/code_fragments/ui/menubar_example.py
+++ b/examples/code_fragments/ui/menubar_example.py
@@ -19,24 +19,23 @@
 from ost import gui
 from ost.gui import FileLoader
 
-from PyQt4 import QtCore, QtGui
+from PyQt5 import QtCore, QtGui, QtWidgets
 
 class InitMenuBar(QtCore.QObject):
   def __init__(self, menu_bar=None):
     QtCore.QObject.__init__(self, menu_bar)
     self.scene_selection_ = gui.SceneSelection.Instance()
     
-    test_action = QtGui.QAction('Test Menu Point', self)
+    test_action = QtWidgets.QAction('Test Menu Point', self)
     test_action.setStatusTip('Print Hello World')
     test_action.setShortcut('Ctrl+T')
-
-    self.connect(test_action, QtCore.SIGNAL('triggered()'), self.TestMethod)
+    test_action.triggered.connect(self.TestMethod)
 
     test = menu_bar.addMenu('&Test')
     test.addAction(test_action)
 
   def TestMethod(self):
-    reply = QtGui.QMessageBox()
+    reply = QtWidgets.QMessageBox()
     
     node_count = self.scene_selection_.GetActiveNodeCount()
     if(node_count > 0):
@@ -47,7 +46,7 @@ class InitMenuBar(QtCore.QObject):
       reply.setText("Oh, there are selected entities: %s" % string)
     else:
       reply.setText("This is a test!")
-    reply.addButton(QtGui.QMessageBox.Yes)
+    reply.addButton(QtWidgets.QMessageBox.Yes)
     reply.exec_()
     
 menu_bar=gui.GostyApp.Instance().perspective.GetMenuBar()
diff --git a/examples/code_fragments/ui/widget_example.py b/examples/code_fragments/ui/widget_example.py
index 80aee7d5e..1c996c253 100644
--- a/examples/code_fragments/ui/widget_example.py
+++ b/examples/code_fragments/ui/widget_example.py
@@ -19,7 +19,7 @@
 
 from ost import gui
 
-from PyQt4 import QtCore, QtGui
+from PyQt5 import QtCore, QtGui
 from board import Board
 
 #Get Panels (Class which manages widgets)
diff --git a/examples/demos/charmm_trj_blur.py b/examples/demos/charmm_trj_blur.py
index 8edfdc0c2..f65d0dbcc 100644
--- a/examples/demos/charmm_trj_blur.py
+++ b/examples/demos/charmm_trj_blur.py
@@ -1,5 +1,5 @@
 # trajectory generated by Raimund Dutzler
-from PyQt4 import QtCore
+from PyQt5 import QtCore
 scene.RemoveAll()
 
 
@@ -14,7 +14,7 @@ class Anim(QtCore.QTimer):
       self.cg_=cg
       self.go_=go
       self.frame_=0
-      QtCore.QObject.connect(self, QtCore.SIGNAL("timeout()"), self.OnTimer)
+      self.timeout.connect(self.OnTimer)
       
   def OnTimer(self):
     self.frame_=(self.frame_+1)%self.cg_.GetFrameCount()
diff --git a/examples/demos/the_hammer.py b/examples/demos/the_hammer.py
index 4f6266d28..1c308b2d7 100644
--- a/examples/demos/the_hammer.py
+++ b/examples/demos/the_hammer.py
@@ -1,4 +1,4 @@
-from PyQt4 import QtCore
+from PyQt5 import QtCore
 import math
 # remove all objects from scene, just in case
 scene.RemoveAll()
@@ -11,8 +11,7 @@ class Anim(QtCore.QTimer):
         self.angle=0.0
         self.dir=0.01
         self.edi=self.a.view.handle.EditXCS(mol.UNBUFFERED_EDIT)
-        QtCore.QObject.connect(self, QtCore.SIGNAL("timeout()"), self.OnTimer)
-
+        self.timeout.connect(self.OnTimer)
         
     def OnTimer(self):
         self.angle+=self.dir
-- 
GitLab