CustomColorButton

# -*- coding: utf-8 -*-
"""
Author: Virgil
Date: 2020/5/23 10:06
"""

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 CustomColorButton(QtWidgets.QLabel):
    color_changed = QtCore.Signal()

    def __init__(self, color=QtCore.Qt.white, parent=None):
        super(CustomColorButton, self).__init__(parent)

        self._color = QtGui.QColor()

        self.set_size(50, 14)
        self.set_color(color)

    def set_size(self, width, height):
        self.setFixedSize(width, height)

    def set_color(self, color):
        color = QtGui.QColor(color)

        if self._color != color:
            self._color = color

            pixmap = QtGui.QPixmap(self.size())
            pixmap.fill(self._color)
            self.setPixmap(pixmap)

            self.color_changed.emit()

    def get_color(self):
        return self._color

    def select_color(self):
        color = QtWidgets.QColorDialog.getColor(self.get_color(), self, options=QtWidgets.QColorDialog.DontUseNativeDialog)
        if color.isValid():
            self.set_color(color)

    def mouseReleaseEvent(self, event):
        if event.button() == QtCore.Qt.LeftButton:
            self.select_color()

class CustomDialog(QtWidgets.QDialog):
    WINDOW_TITLE = "Custom Color"
    
    dlg_instance = None
    def __init__(self, parent=maya_main_window()):
        super(CustomDialog, self).__init__(parent)

        self.setWindowTitle(self.WINDOW_TITLE)
        self.setMinimumSize(600, 400)
        self.setWindowFlags(self.windowFlags() ^ QtCore.Qt.WindowContextHelpButtonHint)

        self.foreground_color = QtGui.QColor(QtCore.Qt.white)
        self.background_color = QtGui.QColor(QtCore.Qt.black)

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

    def create_widgets(self):
        self.foreground_color_bttn = CustomColorButton(QtCore.Qt.white)
        self.background_color_bttn = CustomColorButton(QtCore.Qt.black)

        self.print_bttn = QtWidgets.QPushButton("Print")
        self.close_bttn = QtWidgets.QPushButton("Close")

    def create_layout(self):
        color_layout = QtWidgets.QFormLayout()
        color_layout.addRow("Foreground:", self.foreground_color_bttn)
        color_layout.addRow("Background:", self.background_color_bttn)

        color_grp = QtWidgets.QGroupBox("Color Options")
        color_grp.setLayout(color_layout)

        bttn_layout = QtWidgets.QHBoxLayout()
        bttn_layout.setSpacing(2)
        bttn_layout.addStretch()
        bttn_layout.addWidget(self.print_bttn)
        bttn_layout.addWidget(self.close_bttn)

        main_layout = QtWidgets.QVBoxLayout(self)
        main_layout.setContentsMargins(5, 5, 5, 5)
        main_layout.addWidget(color_grp)
        main_layout.addStretch()
        main_layout.addLayout(bttn_layout)

    def create_connections(self):
        self.foreground_color_bttn.color_changed.connect(self.print_colors)
        self.background_color_bttn.color_changed.connect(self.print_colors)

        self.print_bttn.clicked.connect(self.print_colors)
        self.close_bttn.clicked.connect(self.close)


    def print_colors(self):
        fg_color = self.foreground_color_bttn.get_color()
        bg_color = self.background_color_bttn.get_color()

        print("Foreground Color: [{0}, {1}, {2}]".format(fg_color.red(), fg_color.green(), fg_color.blue()))
        print("Background Color: [{0}, {1}, {2}]".format(bg_color.red(), bg_color.green(), bg_color.blue()))

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

    dialog = CustomDialog()
    dialog.show()

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

    dialog = CustomDialog()
    dialog.show()
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 游动-白 设计师: 上身试试
应支付0元
点击重新获取
扫码支付

支付成功即可阅读