Pop Up Windows

# -*- coding: utf-8 -*-
"""
Author: Virgil
Date: 2020/6/21 14:08
"""

from PySide2 import QtCore
from PySide2 import QtGui
from PySide2 import QtWidgets
from shiboken2 import wrapInstance

import maya.OpenMayaUI as omui
import maya.cmds as cmds
import maya.mel as mel

def maya_main_window():
    """
    Return the Maya main window widget as a Python object
    """
    main_window_ptr = omui.MQtUtil.mainWindow()
    return wrapInstance(long(main_window_ptr), QtWidgets.QWidget)

class PopUpWindow(QtWidgets.QDialog):
    def __init__(self, name, parent=None):
        super(PopUpWindow, self).__init__(parent)

        self.setWindowTitle("{0} Options".format(name))
        self.setWindowFlags(QtCore.Qt.Popup)

        self.create_widgets()
        self.create_layout()

    def create_widgets(self):
        self.size_sb = QtWidgets.QSpinBox()
        self.size_sb.setRange(1, 100)
        self.size_sb.setValue(12)

        self.opacity_sb = QtWidgets.QSpinBox()
        self.opacity_sb.setRange(0, 100)
        self.opacity_sb.setValue(100)

    def create_layout(self):
        layout = QtWidgets.QFormLayout(self)
        layout.addRow("Size:", self.size_sb)
        layout.addRow("Opacity:", self.opacity_sb)

class ToolboxButton(QtWidgets.QPushButton):
    def __init__(self, name, parent=None):
        super(ToolboxButton, self).__init__(parent)

        self.pop_up_window = PopUpWindow(name, self)

    def mousePressEvent(self, event):
        if event.button() == QtCore.Qt.RightButton:

            # pop_up_pos = self.mapToGlobal(event.pos())
            pop_up_pos = self.mapToGlobal(QtCore.QPoint(14, self.height() + 14))
            self.pop_up_window.move(pop_up_pos)

            self.pop_up_window.show()
            return

        super(ToolboxButton, self).mousePressEvent(event)

class ToolboxDialog(QtWidgets.QDialog):
    WINDOW_TITLE = "ToolBox"
    
    def __init__(self, parent=maya_main_window()):
        super(ToolboxDialog, self).__init__(parent)

        self.setWindowTitle(self.WINDOW_TITLE)
        self.setMinimumSize(150, 40)

        self.create_widgets()
        self.create_layout()
        self.create_connections()

    def create_widgets(self):
        self.pencil_bttn = ToolboxButton("Pencil")
        self.pencil_bttn.setFixedSize(30, 30)
        self.pencil_bttn.setCheckable(True)
        self.pencil_bttn.setChecked(True)
        self.pencil_bttn.setFlat(True)
        self.pencil_bttn.setIcon(QtGui.QIcon(":pencilCursor.png"))

        self.brush_bttn = ToolboxButton("Brush")
        self.brush_bttn.setFixedSize(30, 30)
        self.brush_bttn.setCheckable(True)
        # self.brush_bttn.setChecked(True)
        self.brush_bttn.setFlat(True)
        self.brush_bttn.setIcon(QtGui.QIcon(":brush.png"))

        self.eraser_bttn = ToolboxButton("Eraser")
        self.eraser_bttn.setFixedSize(30, 30)
        self.eraser_bttn.setCheckable(True)
        # self.eraser_bttn.setChecked(True)
        self.eraser_bttn.setFlat(True)
        self.eraser_bttn.setIcon(QtGui.QIcon(":Erase.png"))

        self.text_bttn = ToolboxButton("Text")
        self.text_bttn.setFixedSize(30, 30)
        self.text_bttn.setCheckable(True)
        # self.text_bttn.setChecked(True)
        self.text_bttn.setFlat(True)
        self.text_bttn.setIcon(QtGui.QIcon(":text.png"))

    def create_layout(self):
        main_layout = QtWidgets.QHBoxLayout(self)
        main_layout.setContentsMargins(5, 5, 5, 5)
        main_layout.addWidget(self.pencil_bttn)
        main_layout.addWidget(self.brush_bttn)
        main_layout.addWidget(self.eraser_bttn)
        main_layout.addWidget(self.text_bttn)

        bttn_grp = QtWidgets.QButtonGroup(self)
        bttn_grp.addButton(self.pencil_bttn)
        bttn_grp.addButton(self.brush_bttn)
        bttn_grp.addButton(self.eraser_bttn)
        bttn_grp.addButton(self.text_bttn)

    def create_connections(self):
        pass
    
def showWindow():
    global dialog
    try:
        dialog.close()
        dialog.deleteLater()
    except:
        pass

    dialog = ToolboxDialog()
    dialog.show()

if __name__ == "__main__":
    try:
        dialog.close()
        dialog.deleteLater()
    except:
        pass

    dialog = ToolboxDialog()
    dialog.show()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章