1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
| """ Maya mixin classes to add common functionality for custom PyQt/PySide widgets in Maya.
* MayaQWidgetBaseMixin Mixin that should be applied to all custom QWidgets created for Maya to automatically handle setting the objectName and parenting * MayaQWidgetDockableMixin Mixin that adds dockable capabilities within Maya controlled with the show() function """
import uuid
from maya import cmds from maya import mel from maya import OpenMayaUI as omui
try: from PySide2.QtCore import Qt, QPoint, QSize from PySide2.QtCore import Signal from PySide2.QtGui import * from PySide2.QtWidgets import * from shiboken2 import wrapInstance, getCppPointer _qtImported = 'PySide2' except ImportError, e1: try: from PyQt4.QtCore import Qt, QPoint, QSize from PyQt4.QtCore import pyqtSignal as Signal from PyQt4.QtGui import * from sip import wrapinstance as wrapInstance _qtImported = 'PyQt4' except ImportError, e2: raise ImportError, '%s, %s'%(e1,e2)
mixinWorkspaceControls = dict()
def workspaceControlDeleted(controlName): global mixinWorkspaceControls if controlName in mixinWorkspaceControls: del mixinWorkspaceControls[controlName]
def workspaceControlClosed(controlName): global mixinWorkspaceControls if controlName in mixinWorkspaceControls: mixinWorkspaceControls[controlName].dockCloseEventTriggered()
def workspaceControlReparented(controlName, isFloating): global mixinWorkspaceControls if controlName in mixinWorkspaceControls: mixinWorkspaceControls[controlName].floatingChanged(isFloating)
class MayaQWidgetBaseMixin(object): ''' Handle common actions for Maya Qt widgets during initialization: * auto-naming a Widget so it can be looked up as a string through maya.OpenMayaUI.MQtUtil.findControl() * parenting the widget under the main maya window if no parent is explicitly specified so not to have the Window disappear when the instance variable goes out of scope Integration Notes: Inheritance ordering: This class must be placed *BEFORE* the Qt class for proper execution This is needed to workaround a bug where PyQt/PySide does not call super() in its own __init__ functions Example: class MyQWidget(MayaQWidgetBaseMixin, QPushButton): def __init__(self, parent=None): super(MyQWidget, self).__init__(parent=parent) self.setText('Push Me') myWidget = MyQWidget() myWidget.show() print myWidget.objectName() ''' def __init__(self, parent=None, *args, **kwargs): super(MayaQWidgetBaseMixin, self).__init__(parent=parent, *args, **kwargs) self._initForMaya(parent=parent)
def _initForMaya(self, parent=None, *args, **kwargs): ''' Handle the auto-parenting and auto-naming. :Parameters: parent (string) Explicitly specify the QWidget parent. If 'None', then automatically parent under the main Maya window '''
self.setAttribute(Qt.WA_DontCreateNativeAncestors)
if self.objectName() == '': self.setObjectName('%s_%s'%(self.__class__.__name__, uuid.uuid4()))
def _makeMayaStandaloneWindow(self): '''Make a standalone window, though parented under Maya's mainWindow. The parenting under Maya's mainWindow is done so that the QWidget will not auto-destroy itself when the instance variable goes out of scope. ''' origParent = self.parent()
mainWindowPtr = omui.MQtUtil.mainWindow() mainWindow = wrapInstance(long(mainWindowPtr), QMainWindow) self.setParent(mainWindow)
if isinstance(self, QDockWidget): self.setWindowFlags(Qt.Dialog|Qt.FramelessWindowHint) else: self.setWindowFlags(Qt.Window)
if origParent: parentName = origParent.objectName() if parentName and len(parentName) and cmds.workspaceControl(parentName, q=True, exists=True): cmds.deleteUI(parentName, control=True)
def show(self): '''Show the widget. Overrides standard QWidget.show() ''' if self.parent() is None: self._makeMayaStandaloneWindow()
QWidget.show(self)
def setVisible(self, makeVisible): ''' Show/hide the widget. Overrides standard QWidget.setVisible() ''' if (makeVisible == True) and self.parent() is None: self._makeMayaStandaloneWindow()
QWidget.setVisible(self, makeVisible) class MayaQDockWidget(MayaQWidgetBaseMixin,QDockWidget): '''QDockWidget tailored for use with Maya. Mimics the behavior performed by Maya's internal QMayaDockWidget class and the dockControl command
:Signals: closeEventTriggered: emitted when a closeEvent occurs :Known Issues: * Manually dragging the DockWidget to dock in the Main MayaWindow will have it resize to the 'sizeHint' size of the child widget() instead of preserving its existing size. ''' closeEventTriggered = Signal() windowStateChanged = Signal()
def __init__(self, parent=None, *args, **kwargs): super(MayaQDockWidget, self).__init__(parent=parent, *args, **kwargs)
self.setAttribute(Qt.WA_MacAlwaysShowToolWindow) mainWindowPtr = omui.MQtUtil.mainWindow() mainWindow = wrapInstance(long(mainWindowPtr), QMainWindow) try: self.visibilityChanged.connect(mainWindow.handleDockWidgetVisChange) except AttributeError, e: mel.eval('evalDeferred("updateEditorToggleCheckboxes()")')
def setArea(self, area): '''Set the docking area ''' if area == Qt.NoDockWidgetArea: return
mainWindow = self.parent() if isinstance(self.parent(), QMainWindow) else wrapInstance(long(omui.MQtUtil.mainWindow()), QMainWindow)
childrenList = mainWindow.children() foundDockWidgetToTab = False for child in childrenList: if (child != self) and (isinstance(child, QDockWidget)): if not child.isHidden() and not child.isFloating(): if mainWindow.dockWidgetArea(child) == area: mainWindow.tabifyDockWidget(child, self) self.raise_() foundDockWidgetToTab = True break if not foundDockWidgetToTab: mainWindow.addDockWidget(area, self)
def resizeEvent(self, event): super(MayaQDockWidget, self).resizeEvent(event) if event.isAccepted(): self.windowStateChanged.emit()
def moveEvent(self, event): super(MayaQDockWidget, self).moveEvent(event) if event.isAccepted(): self.windowStateChanged.emit()
def closeEvent(self, evt): '''Hide the QDockWidget and trigger the closeEventTriggered signal ''' super(MayaQDockWidget, self).closeEvent(evt)
if evt.isAccepted(): self.setVisible(False)
self.closeEventTriggered.emit()
class MayaQWidgetDockableMixin(MayaQWidgetBaseMixin): ''' Handle Maya dockable actions controlled with the show() function. Integration Notes: Inheritance ordering: This class must be placed *BEFORE* the Qt class for proper execution This is needed to workaround a bug where PyQt/PySide does not call super() in its own __init__ functions Example: class MyQWidget(MayaQWidgetDockableMixin, QPushButton): def __init__(self, parent=None): super(MyQWidget, self).__init__(parent=parent) self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred ) self.setText('Push Me') myWidget = MyQWidget() myWidget.show(dockable=True) myWidget.show(dockable=False) print myWidget.showRepr() '''
closeEventTriggered = Signal() windowStateChanged = Signal()
def __del__(self): global mixinWorkspaceControls workspaceControlName = self.objectName() + 'WorkspaceControl' if workspaceControlName in mixinWorkspaceControls : del mixinWorkspaceControls[workspaceControlName]
def setDockableParameters(self, dockable=None, floating=None, area=None, allowedArea=None, width=None, widthSizingProperty=None, initWidthAsMinimum=None, height=None, heightSizingProperty=None, x=None, y=None, retain=True, plugins=None, controls=None, uiScript=None, closeCallback=None, *args, **kwargs): ''' Set the dockable parameters. :Parameters: dockable (bool) Specify if the window is dockable (default=False) floating (bool) Should the window be floating or docked (default=True) area (string) Default area to dock into (default='left') Options: 'top', 'left', 'right', 'bottom' allowedArea (string) Allowed dock areas (default='all') Options: 'top', 'left', 'right', 'bottom', 'all' width (int) Width of the window height (int) Height of the window x (int) left edge of the window y (int) top edge of the window :See: show(), hide(), and setVisible() ''' if ((dockable == True) or (dockable is None and self.isDockable())): if floating is None and area is None: floating = True
if dockable == True and not self.isDockable(): if x is None: x = self.x() if x == 0: x = 250 if y is None: y = self.y() if y == 0: y = 200 unininitializedSize = QSize(640,480) if self.size() == unininitializedSize: widgetSizeHint = self.sizeHint() else: widgetSizeHint = self.size() if width is None: width = widgetSizeHint.width() if height is None: height = widgetSizeHint.height() if widthSizingProperty is None: widthSizingProperty = 'free' if heightSizingProperty is None: heightSizingProperty = 'free' if initWidthAsMinimum is None: initWidthAsMinimum = False
if controls is None: controls = [] if plugins is None: plugins = []
workspaceControlName = self.objectName() + 'WorkspaceControl' if floating == True or area is None: workspaceControlName = cmds.workspaceControl(workspaceControlName, label=self.windowTitle(), retain=retain, loadImmediately=True, floating=True, initialWidth=width, widthProperty=widthSizingProperty, minimumWidth=initWidthAsMinimum, initialHeight=height, heightProperty=heightSizingProperty, requiredPlugin=plugins, requiredControl=controls) else: if self.parent() is None or (long(getCppPointer(self.parent())[0]) == long(omui.MQtUtil.mainWindow())): workspaceControlName = cmds.workspaceControl(workspaceControlName, label=self.windowTitle(), retain=retain, loadImmediately=True, dockToMainWindow=(area, False), initialWidth=width, widthProperty=widthSizingProperty, minimumWidth=initWidthAsMinimum, initialHeight=height, heightProperty=heightSizingProperty, requiredPlugin=plugins, requiredControl=controls) else: foundParentWorkspaceControl = False nextParent = self.parent() while nextParent is not None: dockToWorkspaceControlName = nextParent.objectName() if cmds.workspaceControl(dockToWorkspaceControlName, q=True, exists=True): workspaceControlName = cmds.workspaceControl(workspaceControlName, label=self.windowTitle(), retain=retain, loadImmediately=True, dockToControl=(dockToWorkspaceControlName, area), initialWidth=width, widthProperty=widthSizingProperty, minimumWidth=initWidthAsMinimum, initialHeight=height, heightProperty=heightSizingProperty, requiredPlugin=plugins, requiredControl=controls) foundParentWorkspaceControl = True break else: nextParent = nextParent.parent()
if foundParentWorkspaceControl == False: workspaceControlName = cmds.workspaceControl(workspaceControlName, label=self.windowTitle(), retain=retain, loadImmediately=True, floating=True, initialWidth=width, widthProperty=widthSizingProperty, minimumWidth=initWidthAsMinimum, initialHeight=height, heightProperty=heightSizingProperty, requiredPlugin=plugins, requiredControl=controls)
currParent = omui.MQtUtil.getCurrentParent() mixinPtr = omui.MQtUtil.findControl(self.objectName()) omui.MQtUtil.addWidgetToMayaLayout(long(mixinPtr), long(currParent))
if uiScript is not None and len(uiScript): cmds.workspaceControl(workspaceControlName, e=True, uiScript=uiScript)
if closeCallback is not None: cmds.workspaceControl(workspaceControlName, e=True, closeCommand=closeCallback)
global mixinWorkspaceControls mixinWorkspaceControls[workspaceControlName] = self
else: if not dockable and self.isDockable(): dockPos = self.parent().pos() if x == None: x = dockPos.x() if y == None: y = dockPos.y() if width == None: width = self.width() if height == None: height = self.height() currentVisibility = self.isVisible() self._makeMayaStandaloneWindow() self.setVisible(currentVisibility) if (width != None) or (height != None): if width == None: width = self.width() if height == None: height = self.height() self.resize(width, height) if (x != None) or (y != None): if x == None: x = self.x() if y == None: y = self.y() self.move(x,y)
def setSizeHint(self, size): ''' Virtual method used to pass the user settable width and height down to the widget whose size policy controls the actual size most of the time. ''' pass
def show(self, *args, **kwargs): ''' Show the QWidget window. Overrides standard QWidget.show() :See: setDockableParameters() for a list of parameters ''' if len(args) or len(kwargs): self.setDockableParameters(*args, **kwargs) elif self.parent() is None: self._makeMayaStandaloneWindow() QWidget.setVisible(self, True) parent = self.parent() if parent: parentName = parent.objectName() if parentName and len(parentName) and cmds.workspaceControl(parentName, q=True, exists=True): if cmds.workspaceControl(parentName, q=True, visible=True): cmds.workspaceControl(parentName, e=True, restore=True) else: cmds.workspaceControl(parentName, e=True, visible=True)
def hide(self, *args, **kwargs): '''Hides the widget. Will hide the parent widget if it is a QDockWidget. Overrides standard QWidget.hide() ''' parent = self.parent() if parent: parentName = parent.objectName() if parentName and len(parentName) and cmds.workspaceControl(parentName, q=True, exists=True): cmds.workspaceControl(parentName, e=True, visible=False) else: QWidget.setVisible(self, False)
def close(self): '''Closes the widget. Overrides standard QWidget.close() ''' parent = self.parent() if parent: parentName = parent.objectName() if parentName and len(parentName) and cmds.workspaceControl(parentName, q=True, exists=True): cmds.workspaceControl(parentName, e=True, close=True) else: QWidget.close(self)
def isVisible(self): '''Return if the widget is currently visible. Overrides standard QWidget.isVisible() :Return: bool ''' parent = self.parent() if parent: parentName = parent.objectName() if parentName and len(parentName) and cmds.workspaceControl(parentName, q=True, exists=True): return cmds.workspaceControl(parentName, q=True, visible=True) return QWidget.isVisible(self)
def setVisible(self, makeVisible, *args, **kwargs): ''' Show/hide the QWidget window. Overrides standard QWidget.setVisible() to pass along additional arguments :See: show() and hide() ''' if (makeVisible == True): return self.show(*args, **kwargs) else: return self.hide(*args, **kwargs)
def raise_(self): '''Raises the widget to the top. Will raise the parent widget if it is a QDockWidget. Overrides standard QWidget.raise_() ''' parent = self.parent() if parent: parentName = parent.objectName() if parentName and len(parentName) and cmds.workspaceControl(parentName, q=True, exists=True): cmds.workspaceControl(parentName, e=True, restore=True) else: QWidget.raise_(self)
def isDockable(self): '''Return if the widget is currently dockable (under a QDockWidget) :Return: bool ''' parent = self.parent() if parent: parentName = parent.objectName() if parentName and len(parentName): return cmds.workspaceControl(parentName, q=True, exists=True) else: return False return False
def isFloating(self): '''Return if the widget is currently floating (under a QDockWidget) Will return True if is a standalone window OR is a floating dockable window. :Return: bool ''' parent = self.parent() if parent: parentName = parent.objectName() if parentName and len(parentName) and cmds.workspaceControl(parentName, q=True, exists=True): return cmds.workspaceControl(parentName, q=True, floating=True) else: return True return True
def floatingChanged(self, isFloating): '''Triggered when QDockWidget.topLevelChanged() signal is triggered. Stub function. Override to perform actions when this happens. ''' pass
def dockCloseEventTriggered(self): '''Triggered when QDockWidget.closeEventTriggered() signal is triggered. Stub function. Override to perform actions when this happens. ''' pass
def dockArea(self): '''Return area if the widget is currently docked to the Maya MainWindow Will return None if not dockable :Return: str ''' dockControlQt = self.parent()
if not isinstance(dockControlQt, QDockWidget): return None else: mainWindow = self.parent().parent() if isinstance(self.parent().parent(), QMainWindow) \ else wrapInstance(long(omui.MQtUtil.mainWindow()), QMainWindow)
dockAreaMap = { Qt.LeftDockWidgetArea : 'left', Qt.RightDockWidgetArea : 'right', Qt.TopDockWidgetArea : 'top', Qt.BottomDockWidgetArea : 'bottom', Qt.AllDockWidgetAreas : 'all', Qt.NoDockWidgetArea : 'none', } dockWidgetAreaBitmap = mainWindow.dockWidgetArea(dockControlQt) return dockAreaMap[dockWidgetAreaBitmap]
def setWindowTitle(self, val): '''Sets the QWidget's title and also it's parent QDockWidget's title if dockable.
:Return: None ''' QWidget.setWindowTitle(self, val) parent = self.parent() if parent: parentName = parent.objectName() if parentName and len(parentName) and cmds.workspaceControl(parentName, q=True, exists=True): cmds.workspaceControl(parentName, e=True, label=val)
def showRepr(self): '''Present a string of the parameters used to reproduce the current state of the widget used in the show() command. :Return: str ''' reprDict = {} reprDict['dockable'] = self.isDockable() reprDict['floating'] = self.isFloating() reprDict['area'] = self.dockArea() if reprDict['floating']: if reprDict['dockable']: pos = self.parent().pos() else: pos = self.pos() reprDict['x'] = pos.x() reprDict['y'] = pos.y()
sz = self.geometry().size() reprDict['width'] = sz.width() reprDict['height'] = sz.height() reprShowList = ['%s=%r'%(k,v) for k,v in reprDict.items() if v != None] reprShowStr = 'show(%s)'%(', '.join(reprShowList)) return reprShowStr
|