UI/ToolNativeWidgetHolder.py

UI/ToolNativeWidgetHolder.py
1 # Copyright 2012 Autodesk, Inc. All rights reserved.
2 # Use of this software is subject to the terms of the Autodesk license agreement
3 # provided at the time of installation or download, or which otherwise accompanies
4 # this software in either electronic or hard copy form.
5 #
6 # Script description:
7 # Create a tool that demo how to embed native Qt widgets created by PySide2 into MoBu framework.
8 #
9 # Topic: FBWidgetHolder, FBTool
10 #
11 
12 from pyfbsdk import *
13 from pyfbsdk_additions import *
14 from PySide2 import QtWidgets
15 from PySide2 import shiboken2
16 
17 #
18 # Subclass FBWidgetHolder and override its WidgetCreate function
19 #
20 class NativeWidgetHolder(FBWidgetHolder):
21  #
22  # Override WidgetCreate function to create native widget via PySide2.
23  # \param parentWidget Memory address of Parent QWidget.
24  # \return Memory address of the child native widget.
25  #
26  def WidgetCreate(self, pWidgetParent):
27  #
28  # IN parameter pWidgetparent is the memory address of the parent Qt widget.
29  # here we should PySide2.shiboken2.wrapInstance() function to convert it to PySide2.QtWidget object.
30  # and use it the as the parent for native Qt widgets created via Python.
31  # Similiar approach is available in the sip python module for PyQt
32  #
33  # Only a single widget is allowed to be the *direct* child of the IN parent widget.
34  #
35  self.mNativeQtWidget = QtWidgets.QPushButton("Push Button", shiboken2.wrapInstance(pWidgetParent, QtWidgets.QWidget))
36 
37  #
38  # return the memory address of the *single direct* child QWidget.
39  #
40  return shiboken2.getCppPointer(self.mNativeQtWidget)[0]
41 
42 class NativeQtWidgetTool(FBTool):
43  def BuildLayout(self):
44  x = FBAddRegionParam(0,FBAttachType.kFBAttachLeft,"")
45  y = FBAddRegionParam(0,FBAttachType.kFBAttachTop,"")
46  w = FBAddRegionParam(0,FBAttachType.kFBAttachRight,"")
47  h = FBAddRegionParam(0,FBAttachType.kFBAttachBottom,"")
48  self.AddRegion("main","main", x, y, w, h)
49  self.SetControl("main", self.mNativeWidgetHolder)
50 
51  def __init__(self, name):
52  FBTool.__init__(self, name)
53  self.mNativeWidgetHolder = NativeWidgetHolder();
54  self.BuildLayout()
55  self.StartSizeX = 600
56  self.StartSizeY = 400
57 
58 gToolName = "NativeQtWidgetTool"
59 
60 #Development? - need to recreate each time!!
61 gDEVELOPMENT = True
62 
63 if gDEVELOPMENT:
64  FBDestroyToolByName(gToolName)
65 
66 if gToolName in FBToolList:
67  tool = FBToolList[gToolName]
68  ShowTool(tool)
69 else:
70  tool=NativeQtWidgetTool(gToolName)
71  FBAddTool(tool)
72  if gDEVELOPMENT:
73  ShowTool(tool)