_widget

戈华茂
2023-12-01

import os,sys,re

from bqt import QtCore, QtGui
import bglobal

from _widget import *

if bglobal.PLATFORM == ‘linux’:
if ‘/usr/local/pfx/qube/api/python’ not in sys.path:
sys.path.append(‘/usr/local/pfx/qube/api/python’)
import qb

elif bglobal.PLATFORM == ‘win’:
if r’C:\Program Files\pfx\qube\api\python’ not in sys.path:
sys.path.append(r’C:\Program Files\pfx\qube\api\python’)
import qb

elif bglobal.PLATFORM == ‘mac’:
if ‘/Applications/pfx/qube/api/python’ not in sys.path:
sys.path.append(‘/Applications/pfx/qube/api/python’)
import qb

def getGroups():
return list(set([singleHost.get(‘groups’) for singleHost in qb.hostinfo(state=’active’)]))

_ATTRIBUTE = [
{‘label’:’filename’, ‘name’:’filename’, ‘display’: True, ‘type’:TextWidget, ‘args’: None},
{‘label’:’renderDir’, ‘name’:’render_dir’, ‘display’: True, ‘type’:GetDirWidget, ‘args’: None},
{‘label’:’renderer’, ‘name’:’renderer’, ‘display’: True, ‘type’:ComboBoxWidget, ‘args’: [‘vray’,’mayaSoftware’]},
{‘label’:’frameRange’, ‘name’:’frame_range’, ‘display’: True, ‘type’:TextWidget, ‘args’: None},
{‘label’:’camera’, ‘name’:’camera’, ‘display’: True, ‘type’:ComboBoxWidget, ‘args’: [‘persp’]},
{‘label’:’renderLayer’, ‘name’:’renderLayer’, ‘display’: True, ‘type’:ComboBoxWidget, ‘args’: [‘defaultRenderLayer’]},
{‘label’:’width’, ‘name’:’width’, ‘display’: False, ‘type’:SpinBoxWidget, ‘range’: [1,9999999]},
{‘label’:’height’, ‘name’:’height’, ‘display’: False, ‘type’:SpinBoxWidget, ‘range’: [1,9999999]},
{‘label’:’pixelAspectRatio’, ‘name’:’pixelAspectRatio’, ‘display’: False, ‘type’:DoubleSpinBoxWidget, ‘range’: [0,9999999]},
{‘label’:’mayaVersion’, ‘name’:’maya_render_tool_version’, ‘display’: False, ‘type’:ComboBoxWidget, ‘args’: [‘2014’,’2015’,’2016’]},
{‘label’:’groups’, ‘name’:’groups’, ‘display’: False, ‘type’:CheckBoxGroupWidget, ‘args’: getGroups()},
{‘label’:’chunkSize’, ‘name’:’range_chunk_size’, ‘display’: False, ‘type’:SpinBoxWidget, ‘range’: [1,9999999]},
]

class ActionWidget(QtGui.QAction):

changed = QtCore.Signal(dict)

def __init__(self,*argvs):
    super(ActionWidget,self).__init__(*argvs)
    self.setCheckable(True)

    self.toggled.connect(self.signalChanged)


def signalChanged(self,isChecked):
    self.changed.emit({'title':self.text(),'isChecked':isChecked})

class MenuWidget(QtGui.QMenu):

def __init__(self,title,parent = None):
    super(MenuWidget,self).__init__(title = title,parent = parent)
    self.children = list()
    self.setTearOffEnabled(True)
    self._addAction()


def _addAction(self):
    for i in _ATTRIBUTE:
        action = ActionWidget(i.get('label'),self)
        action.setCheckable(True)
        self.addAction(action)
        self.children.append(action)


def getChildren(self):
    return self.children

class WidgetDelegate(QtGui.QItemDelegate):

def __init__(self,parent = None):
    super(WidgetDelegate,self).__init__(parent = parent)


def createEditor(self, parent, option, index):
    editor = _ATTRIBUTE[index.column()].get('type')
    if _ATTRIBUTE[index.column()].get('args'):
        #if _ATTRIBUTE[index.column()].get('label') == 'groups':
        #    editor = editor(_ATTRIBUTE[index.column()].get('args'),parent = None)
        #else:
        editor = editor(_ATTRIBUTE[index.column()].get('args'),parent = parent)
    else:
        editor = editor(parent = parent)

    if _ATTRIBUTE[index.column()].get('range'):
        editor.setRange(_ATTRIBUTE[index.column()].get('range')[0],
                        _ATTRIBUTE[index.column()].get('range')[1])

    return editor


def setEditorData(self, editor, index):
    value = index.model().data(index, QtCore.Qt.EditRole)
    if value != None:
        editor.setValue(value)


def setModelData(self, editor, model, index):
    value = editor.getValue()

    model.setData(index, value, QtCore.Qt.EditRole)


def updateEditorGeometry(self, editor, option, index):
    editor.setGeometry(option.rect)

class TableWidget(QtGui.QTableView):

def __init__(self,parent = None):
    super(TableWidget,self).__init__(parent = parent)
    self.model = QtGui.QStandardItemModel(0, len(_ATTRIBUTE))
    self.model.setHorizontalHeaderLabels([i.get('label') for i in _ATTRIBUTE])
    self.setModel(self.model)

    self.delegate = WidgetDelegate()
    self.setItemDelegate(self.delegate)
    for i in xrange(len(_ATTRIBUTE)):
        self.setColumnHidden(i,not _ATTRIBUTE[i].get('display'))

    self.setContextMenuPolicy(QtCore.Qt.ContextMenuPolicy(3))
    self.customContextMenuRequested.connect(self.rightClicked)
    self.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
    self.setDragDropMode(QtGui.QAbstractItemView.DropOnly)
    #QtGui.QAbstractItemView.DragOnly
    #QtGui.QAbstractItemView.DropOnly
    #self.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)


def appendRow(self):
    row_count = self.model.rowCount()
    self.model.insertRow(row_count)
    return row_count


def removeRow(self,index):
    if isinstance(index,(list,tuple)):
        index.sort()
        index.reverse()
        for i in index:
            self.model.removeRow(i)
    else:
        self.model.removeRow(index)
        #self.model.takeRow(index)


def selectedRowsIndexes(self):
    indexes = self.selectedIndexes()
    rowsIndexes = list()
    for index in indexes:
        _index = index.row()
        if not _index in rowsIndexes:
            rowsIndexes.append(_index)
    return rowsIndexes


def removeSelectRows(self):
    indexs = self.selectedRowsIndexes()
    if indexs:
        self.removeRow(indexs)


def setValue(self,index,value):
    '''
    value is a dict:
        filename
        render_dir
        renderer
        startFrame
        endFrame
        camera
        renderLayer
        width
        height
        pixelAspectRatio
        groups
        maya_render_tool_version
        groups
        range_chunk_size
        ...
    '''
    if index < self.model.rowCount():
        for column in range(len(_ATTRIBUTE)):
            model_index = self.model.index(index, column, QtCore.QModelIndex())
            self.model.setData(model_index, value.get(_ATTRIBUTE[column].get('name')))
            self.resizeColumnsToContents()
    else:
        raise Exception('error: wrong index')


def getValue(self,index):
    value = dict()
    if index < self.model.rowCount():
        for column in range(len(_ATTRIBUTE)):
            model_index = self.model.index(index, column, QtCore.QModelIndex())
            value[_ATTRIBUTE[column].get('name')] = self.model.data(model_index)

        return value
    else:
        raise Exception('error: wrong index')


def setColumnDisPlay(self,info = {'title':'camera','isChecked':True}):
    self.setColumnHidden([i.get('label') for i in _ATTRIBUTE].index(info.get('title')),
                                                                    not info.get('isChecked'))
    _ATTRIBUTE[[i.get('label') for i in _ATTRIBUTE].index(info.get('title'))]['display'] = info.get('isChecked')


def dropEvent(self,event):
    _widget = event.source()
    if _widget:
        indexes = _widget.selectedRowsIndexes()
        for index in indexes:
            row_index = self.appendRow()
            value = _widget.getValue(index)
            self.setValue(row_index,value)


def rightClicked(self,point):

    menu = QtGui.QMenu(self)

    args = [
                {
                    'name':'create new Task',
                    'cmd':self.appendRow,
                    'isDisabled':False,
                },

                {
                    'name':'remove selected Task',
                    'cmd':self.removeSelectRows,
                    'isDisabled':False,
                },

            ]

    args[0]['isDisabled'] = True
    if self.currentIndex():
        args[1]['isDisabled'] = True

    for i in args:
        if i.get('isDisabled'):
            ac = QtGui.QAction(i.get('name'),menu)
            ac.triggered.connect(i.get('cmd'))
            menu.addAction(ac)

    menu.addSeparator()

    child = MenuWidget('display',menu)
    num = 0
    for i in child.getChildren():
        i.changed.connect(self.setColumnDisPlay)
        i.setChecked(False)
        i.setChecked(_ATTRIBUTE[num].get('display'))
        num +=1
    menu.addMenu(child)

    menu.popup(self.mapToGlobal(point))

class TableWidget2(TableWidget):

def __init__(self,parent = None):
    super(TableWidget2,self).__init__(parent = parent)
    self.setDragDropMode(QtGui.QAbstractItemView.DragOnly)
    #QtGui.QAbstractItemView.DragOnly
    #QtGui.QAbstractItemView.DropOnly
    self.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)

app = QtGui.QApplication(sys.argv)
win = TableWidget()
win.show()
win2 = TableWidget2()
win2.show()
app.exec_()

 类似资料:

相关阅读

相关文章

相关问答