Skip to content

Commit

Permalink
Adds support for Py3 and QT5
Browse files Browse the repository at this point in the history
fixes #11
  • Loading branch information
furti committed Aug 27, 2018
1 parent 31162ff commit 1752e36
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 50 deletions.
6 changes: 3 additions & 3 deletions create_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import FreeCAD, FreeCADGui
import Mesh, Part, MeshPart
from PySide import QtGui

import lithophane_utils
from utils.timer import Timer, computeOverallTime
from utils.resource_utils import iconPath
import utils.qtutils as qtutils

def createBottomRectangle(lines):
facets = []
Expand Down Expand Up @@ -129,7 +129,7 @@ def Activated(self):
lithophaneImage = lithophane_utils.findSelectedImage()

if lithophaneImage is None:
QtGui.QMessageBox.information(QtGui.qApp.activeWindow(), "No LithophaneImage selected", "Select exactly one LithophaneImage to continue")
qtutils.showInfo("No LithophaneImage selected", "Select exactly one LithophaneImage to continue")

return

Expand Down Expand Up @@ -187,7 +187,7 @@ def IsActive(self):
if command.IsActive():
command.Activated()
else:
QtGui.QMessageBox.information( QtGui.qApp.activeWindow(), "No open Document", "There is no open document")
qtutils.showInfo("No open Document", "There is no open document")
else:
import toolbars
toolbars.toolbarManager.registerCommand(CreateGeometryCommand())
4 changes: 3 additions & 1 deletion image_viewer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from PySide import QtGui, QtCore

import utils.qtutils as qtutils

class ImageViewer(QtGui.QDialog):
def __init__(self, image):
super(ImageViewer, self).__init__(QtGui.qApp.activeWindow())
super(ImageViewer, self).__init__(qtutils.activeWindow())

self.imageLabel = QtGui.QLabel()
self.imageLabel.setBackgroundRole(QtGui.QPalette.Base)
Expand Down
6 changes: 3 additions & 3 deletions import_image.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import FreeCAD, FreeCADGui
from PySide import QtGui

import lithophane_image
from utils.resource_utils import iconPath
import utils.qtutils as qtutils

class ImportImageCommand:
toolbarName = 'Image_Tools'
Expand All @@ -14,7 +14,7 @@ def GetResources(self):
'Pixmap': iconPath('ImportImage.svg')}

def Activated(self):
fileName = QtGui.QFileDialog.getOpenFileName(QtGui.qApp.activeWindow(), "Open Image", '', "Image Files (*.png *.jpg *.bmp)")[0]
fileName = qtutils.userSelectedFile("Open Image", qtutils.IMAGE_FILES)

if fileName is None or fileName == '':
FreeCAD.Console.PrintMessage('No File Selected')
Expand All @@ -34,7 +34,7 @@ def IsActive(self):
if command.IsActive():
command.Activated()
else:
QtGui.QMessageBox.information( QtGui.qApp.activeWindow(), "No open Document", "There is no open document")
qtutils.showInfo("No open Document", "There is no open document")
else:
import toolbars
toolbars.toolbarManager.registerCommand(ImportImageCommand())
34 changes: 10 additions & 24 deletions lithophane_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

import math
import FreeCAD, FreeCADGui
from PySide import QtGui, QtCore
from pivy import coin

from image_viewer import ImageViewer
from utils.geometry_utils import pointCloudToLines
from lithophane_utils import toChunks, tupleToVector, vectorToTuple, processEvents
from lithophane_utils import toChunks, tupleToVector, vectorToTuple
from utils.timer import Timer, computeOverallTime
import utils.qtutils as qtutils

class AverageVector:
def __init__(self):
Expand All @@ -33,46 +33,33 @@ def mmPerPixel(ppi):

return 1 / pixelsPerMm

def readImage(imagePath):
imageReader = QtGui.QImageReader(imagePath)

if imageReader.canRead():
image = imageReader.read()

if image.isNull():
QtGui.QMessageBox.information( QtGui.qApp.activeWindow(), "Image Read Error", "Can't read image: %s" % imageReader.errorString())

return image
else:
QtGui.QMessageBox.information( QtGui.qApp.activeWindow(), "Image Read Error", "Can't read image: %s" % imageReader.errorString())

def imageChanged(lithophaneImage, newPath):
if not hasattr(lithophaneImage, 'image') or not hasattr(lithophaneImage, 'lastPath'):
return True

return newPath != lithophaneImage.lastPath

def imgToBase64(image):
ba = QtCore.QByteArray()
ba = qtutils.QByteArray()

buffer = QtCore.QBuffer(ba)
buffer.open(QtCore.QIODevice.WriteOnly)
buffer = qtutils.QBuffer(ba)
buffer.open(qtutils.QIODevice.WriteOnly)
image.save(buffer, 'PNG')

return ba.toBase64().data()

def imageFromBase64(base64):
ba = QtCore.QByteArray.fromBase64(QtCore.QByteArray(base64))
ba = qtutils.QByteArray.fromBase64(qtutils.QByteArray(base64))

return QtGui.QImage.fromData(ba, 'PNG')
return qtutils.QImage.fromData(ba, 'PNG')

def calculatePixelHeight(image, x, y, baseHeight, maximumHeight):
'''Calculate the height of the pixel based on its lightness value.
Lighter colors mean lower height because the light must come through.
Maximum lightness 255 means the base height
Minium lightness 0 means the full height of base height + additional height
'''
color = QtGui.QColor(image.pixel(x, y))
color = qtutils.QColor(image.pixel(x, y))
lightness = color.lightness()

reversedLightness = (255 - lightness) # Reverse the value. Lighter means lower height
Expand Down Expand Up @@ -185,7 +172,7 @@ def execute(self, fp):

if imageChanged(self, fp.Path):
timers.append(Timer('ReloadingImage (1/4)'))
self.image = readImage(fp.Path)
self.image = qtutils.readImage(fp.Path)
self.lastPath = fp.Path

imageSize = self.image.size()
Expand Down Expand Up @@ -296,8 +283,7 @@ def createImage(imagePath):
import os
imagePath = os.path.join(os.path.dirname(os.path.abspath(__file__)), './testimages/medium.png')

imageReader = QtGui.QImageReader(imagePath)
image = imageReader.read()
image = qtutils.readImage(imagePath)

if image.isNull():
FreeCAD.Console.PrintMessage(imageReader.errorString())
Expand Down
7 changes: 1 addition & 6 deletions lithophane_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import FreeCAD, FreeCADGui
import time, itertools, Mesh
from PySide import QtGui
import itertools, Mesh

def recomputeView():
FreeCAD.ActiveDocument.recompute()
Expand Down Expand Up @@ -40,10 +39,6 @@ def findSelectedMesh():
def vectorAtGround(vector):
return FreeCAD.Vector(vector.x, vector.y, 0)

def processEvents():
time.sleep(0.001)
QtGui.QApplication.processEvents()

def toChunks(iterable, chunksize):
"""
Splits the iterable into evenly sized chunks. The last chunk can be smaller when iterable contains not enough elements
Expand Down
6 changes: 3 additions & 3 deletions make_solid.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import math
import FreeCAD, FreeCADGui
import Mesh, Part, MeshPart
from PySide import QtGui

import lithophane_utils
from utils.timer import Timer, computeOverallTime
from utils.resource_utils import iconPath
import utils.qtutils as qtutils

class Neighbours:
def __init__(self, face):
Expand Down Expand Up @@ -81,7 +81,7 @@ def Activated(self):
mesh = lithophane_utils.findSelectedMesh()

if mesh is None:
QtGui.QMessageBox.information(QtGui.qApp.activeWindow(), "No mesh selected", "Select exactly one mesh to continue")
qtutils.showInfo("No mesh selected", "Select exactly one mesh to continue")

return

Expand Down Expand Up @@ -123,7 +123,7 @@ def IsActive(self):
if command.IsActive():
command.Activated()
else:
QtGui.QMessageBox.information( QtGui.qApp.activeWindow(), "No open Document", "There is no open document")
qtutils.showInfo("No open Document", "There is no open document")
else:
import toolbars
toolbars.toolbarManager.registerCommand(MakeSolidCommand())
8 changes: 4 additions & 4 deletions measure.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import FreeCAD, FreeCADGui
from PySide import QtGui

import lithophane_utils
from utils.resource_utils import iconPath
from utils.format_utils import formatLength
import utils.qtutils as qtutils

class MeasureCommand:
toolbarName = 'Debugging_Tools'
Expand All @@ -18,7 +18,7 @@ def Activated(self):
mesh = lithophane_utils.findSelectedMesh()

if mesh is None:
QtGui.QMessageBox.information(QtGui.qApp.activeWindow(), "No Mesh selected", "Select exactly one Mesh to continue")
qtutils.showInfo("No Mesh selected", "Select exactly one Mesh to continue")

return

Expand All @@ -30,7 +30,7 @@ def Activated(self):

message = "Length (X): %s\n\nWidth (Y): %s\n\nHeight (Z): %s" %(length, width, height)

QtGui.QMessageBox.information(QtGui.qApp.activeWindow(), "Bounding Informations", message)
qtutils.showInfo("Bounding Informations", message)

def IsActive(self):
"""There should be at least an active document."""
Expand All @@ -42,7 +42,7 @@ def IsActive(self):
if command.IsActive():
command.Activated()
else:
QtGui.QMessageBox.information( QtGui.qApp.activeWindow(), "No open Document", "There is no open document")
qtutils.showInfo("No open Document", "There is no open document")
else:
import toolbars
toolbars.toolbarManager.registerCommand(MeasureCommand())
6 changes: 3 additions & 3 deletions show_pointcloud.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import FreeCAD, FreeCADGui
import Points
from PySide import QtGui

import lithophane_utils
from utils.geometry_utils import linesToPointCloud
from utils.resource_utils import iconPath
import utils.qtutils as qtutils

def showPointCloud(pts, name):
pointCloud = Points.Points()
Expand All @@ -25,7 +25,7 @@ def Activated(self):
lithophaneImage = lithophane_utils.findSelectedImage()

if lithophaneImage is None:
QtGui.QMessageBox.information(QtGui.qApp.activeWindow(), "No LithophaneImage selected", "Select exactly one LithophaneImage to continue")
qtutils.showInfo("No LithophaneImage selected", "Select exactly one LithophaneImage to continue")

return

Expand All @@ -43,7 +43,7 @@ def IsActive(self):
if command.IsActive():
command.Activated()
else:
QtGui.QMessageBox.information( QtGui.qApp.activeWindow(), "No open Document", "There is no open document")
qtutils.showInfo("No open Document", "There is no open document")
else:
import toolbars
toolbars.toolbarManager.registerCommand(ShowPointCloudCommand())
63 changes: 63 additions & 0 deletions utils/qtutils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
IS_QT_5 = False

if IS_QT_5:
from PySide2 import QtGui, QtCore, QtWidgets
IS_QT_5 = True
else:
from PySide import QtGui, QtCore
import PySide.QtGui as QtWidgets

import time

# Common classes
QByteArray = QtCore.QByteArray
QBuffer = QtCore.QBuffer
QIODevice = QtCore.QIODevice
Qt = QtCore.Qt
QImage = QtGui.QImage
QColor = QtGui.QColor
QDialog = QtWidgets.QDialog
QLabel = QtWidgets.QLabel
QVBoxLayout = QtWidgets.QVBoxLayout
QScrollArea = QtWidgets.QScrollArea
QPalette = QtWidgets.QPalette
QSizePolicy = QtWidgets.QSizePolicy
QPixmap = QtWidgets.QPixmap

# File patterns
IMAGE_FILES = "Image Files (*.png *.jpg *.bmp)"


def activeWindow():
return QtWidgets.QApplication.activeWindow()

def userSelectedFile(title, filePattern):
fileName = QtWidgets.QFileDialog.getOpenFileName(activeWindow(), title, '', filePattern)[0]

if fileName == '':
return None

return fileName

def showInfo(title, message):
QtWidgets.QMessageBox.information(activeWindow(), title, message)

def readImage(imagePath):
imageReader = QtGui.QImageReader(imagePath)

if imageReader.canRead():
image = imageReader.read()

if image.isNull():
showInfo("Image Read Error", "Can't read image: %s" % imageReader.errorString())

return image
else:
showInfo("Image Read Error", "Can't read image: %s" % imageReader.errorString())

def processEvents():
time.sleep(0.001)
QtGui.QApplication.processEvents()

# https://github.com/PySide/pyside2/wiki/My_Practice_:_Porting_python_scripts_to_PySide2#qhboxlayout-qvboxlayout
# important info about layout in qt4 and qt5
6 changes: 3 additions & 3 deletions utils/timer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import FreeCAD
import time

import lithophane_utils
import utils.qtutils as qtutils

class Timer:
def __init__(self, message):
Expand All @@ -10,7 +10,7 @@ def __init__(self, message):

def start(self):
FreeCAD.Console.PrintMessage('Start: %s...\n' % (self.message))
lithophane_utils.processEvents()
qtutils.processEvents()

self.startSeconds = time.time()

Expand All @@ -22,7 +22,7 @@ def stop(self):
self.seconds = endSeconds - self.startSeconds

FreeCAD.Console.PrintMessage('Finished: %s (%.3f s)\n' % (self.message, self.seconds))
lithophane_utils.processEvents()
qtutils.processEvents()

def computeOverallTime(timers):
return sum(map(lambda timer: timer.seconds, timers))

0 comments on commit 1752e36

Please sign in to comment.