Skip to content

Commit

Permalink
Merge pull request #259 from davidpower/master
Browse files Browse the repository at this point in the history
Faking QML GUI as host window's child
  • Loading branch information
mottosso authored Aug 23, 2017
2 parents 9d13e8a + 18eff8b commit 97f749c
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 2 deletions.
27 changes: 26 additions & 1 deletion pyblish_qml/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class Application(QtGui.QGuiApplication):
shown = QtCore.pyqtSignal(QtCore.QVariant)
hidden = QtCore.pyqtSignal()
quitted = QtCore.pyqtSignal()
risen = QtCore.pyqtSignal()
inFocused = QtCore.pyqtSignal()
outFocused = QtCore.pyqtSignal()

def __init__(self, source):
super(Application, self).__init__(sys.argv)
Expand Down Expand Up @@ -96,6 +99,9 @@ def __init__(self, source):
self.shown.connect(self.show)
self.hidden.connect(self.hide)
self.quitted.connect(self.quit)
self.risen.connect(self.rise)
self.inFocused.connect(self.inFocus)
self.outFocused.connect(self.outFocus)

window.setSource(QtCore.QUrl.fromLocalFile(source))

Expand Down Expand Up @@ -185,6 +191,22 @@ def hide(self):

self.window.hide()

def rise(self):
"""Rise GUI from hidden"""
self.window.show()

def inFocus(self):
"""Set GUI on-top flag"""
if os.name == "nt":
previous_flags = self.window.flags()
self.window.setFlags(previous_flags | QtCore.Qt.WindowStaysOnTopHint)

def outFocus(self):
"""Remove GUI on-top flag"""
if os.name == "nt":
previous_flags = self.window.flags()
self.window.setFlags(previous_flags ^ QtCore.Qt.WindowStaysOnTopHint)

def listen(self):
"""Listen on incoming messages from host
Expand All @@ -206,7 +228,10 @@ def _listen():
signal = {
"show": "shown",
"hide": "hidden",
"quit": "quitted"
"quit": "quitted",
"rise": "risen",
"inFocus": "inFocused",
"outFocus": "outFocused",
}.get(payload["name"])

if not signal:
Expand Down
83 changes: 83 additions & 0 deletions pyblish_qml/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,84 @@ def _on_application_quit():
pass


def _connect_host_event(app):
"""Connect some event from host to QML
Host will connect following event to QML:
QEvent.Show -> rise QML
QEvent.Hide -> hide QML
QEvent.WindowActivate -> set QML on top
QEvent.WindowDeactivate -> remove QML on top
"""

class HostEventFilter(QtWidgets.QWidget):

eventList = [QtCore.QEvent.Show, QtCore.QEvent.Hide,
QtCore.QEvent.WindowActivate, QtCore.QEvent.WindowDeactivate]

def getServer(self):
server = None
try:
server = _state["currentServer"]
except KeyError:
# No server started
pass
return server

def eventFilter(self, widget, event):
if event.type() in self.eventList:
server = self.getServer()
if not server:
return False
else:
proxy = ipc.server.Proxy(server)

if event.type() == QtCore.QEvent.Show:
try:
proxy.rise()
return True
except IOError:
# The running instance has already been closed.
_state.pop("currentServer")
if event.type() == QtCore.QEvent.Hide:
try:
proxy.hide()
return True
except IOError:
# The running instance has already been closed.
_state.pop("currentServer")
if event.type() == QtCore.QEvent.WindowActivate:
try:
proxy.inFocus()
return True
except IOError:
# The running instance has already been closed.
_state.pop("currentServer")
if event.type() == QtCore.QEvent.WindowDeactivate:
try:
proxy.outFocus()
return True
except IOError:
# The running instance has already been closed.
_state.pop("currentServer")
return False

# Get top window in host
app_top_window = app.activeWindow()
while True:
parent_window = app_top_window.parent()
if parent_window:
app_top_window = parent_window
else:
break
# install event filter
try:
host_event_filter = HostEventFilter(app_top_window)
app_top_window.installEventFilter(host_event_filter)
except Exception:
pass


def _install_maya():
"""Helper function to Autodesk Maya support"""
from maya import utils
Expand All @@ -226,6 +304,7 @@ def threaded_wrapper(func, *args, **kwargs):

app = QtWidgets.QApplication.instance()
app.aboutToQuit.connect(_on_application_quit)
_connect_host_event(app)

if settings.ContextLabel == settings.ContextLabelDefault:
settings.ContextLabel = "Maya"
Expand All @@ -246,6 +325,7 @@ def threaded_wrapper(func, *args, **kwargs):

app = QtWidgets.QApplication.instance()
app.aboutToQuit.connect(_on_application_quit)
_connect_host_event(app)

if settings.ContextLabel == settings.ContextLabelDefault:
settings.ContextLabel = "Houdini"
Expand All @@ -269,6 +349,7 @@ def threaded_wrapper(func, *args, **kwargs):

app = QtWidgets.QApplication.instance()
app.aboutToQuit.connect(_on_application_quit)
_connect_host_event(app)

if settings.ContextLabel == settings.ContextLabelDefault:
settings.ContextLabel = "Nuke"
Expand All @@ -293,6 +374,7 @@ def threaded_wrapper(func, *args, **kwargs):

app = QtWidgets.QApplication.instance()
app.aboutToQuit.connect(_on_application_quit)
_connect_host_event(app)

if settings.ContextLabel == settings.ContextLabelDefault:
settings.ContextLabel = "Hiero"
Expand All @@ -316,6 +398,7 @@ def threaded_wrapper(func, *args, **kwargs):

app = QtWidgets.QApplication.instance()
app.aboutToQuit.connect(_on_application_quit)
_connect_host_event(app)

if settings.ContextLabel == settings.ContextLabelDefault:
settings.ContextLabel = "NukeStudio"
Expand Down
12 changes: 12 additions & 0 deletions pyblish_qml/ipc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ def quit(self):
"""Ask the GUI to quit"""
self._dispatch("quit")

def rise(self):
"""Rise GUI from hidden"""
self._dispatch("rise")

def inFocus(self):
"""Set GUI on-top flag"""
self._dispatch("inFocus")

def outFocus(self):
"""Remove GUI on-top flag"""
self._dispatch("outFocus")

def kill(self):
"""Forcefully destroy the process"""
self.popen.kill()
Expand Down
2 changes: 1 addition & 1 deletion pyblish_qml/version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

VERSION_MAJOR = 1
VERSION_MINOR = 2
VERSION_MINOR = 3
VERSION_PATCH = 0

version_info = (VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH)
Expand Down

0 comments on commit 97f749c

Please sign in to comment.