class MassAttribute_UI(QDialog): """ The main UI """ class Applikator(QObject): """ This is the core applier which toggle the display of the corresponding widget and handling events' connections """ def __init__(self, parent=None): super(MassAttribute_UI.Applikator, self).__init__() self.root = parent def widget_event(self, t): """ Return the correct widget's event depending on attribute's type :param t: the attribute's type :type t: str :return: the event :rtype : Signal """ return { 'float': self.root.W_EDI_float.valueChanged, 'enum': self.root.W_EDI_enum.currentIndexChanged, 'int': self.root.W_EDI_int.valueChanged, 'bool': self.root.W_EDI_bool.stateChanged, 'str': self.root.W_EDI_str.textChanged, 'd3': self.root.W_EDI_d3.valuesChanged, 'd4': self.root.W_EDI_d4.valuesChanged, 'color': self.root.W_EDI_color.colorChanged }[t] def unset_editors(self): """ Toggle off all editors and disconnect the current one """ for widget in (self.root.W_EDI_float, self.root.W_EDI_int, self.root.W_EDI_enum, self.root.W_EDI_bool, self.root.W_EDI_str, self.root.W_EDI_d3, self.root.W_EDI_d4, self.root.W_EDI_color): widget.setVisible(False) # trying to force disconnection try: self.widget_event(self.root.ctx).disconnect( self.root.apply_value) except (KeyError, RuntimeError): pass def prepare(applier_name): """ A decorator to prepare the attribute depending on type for the corresponding widget and getting the attribute's value :param applier_name: attribute's type :type applier_name: str """ def sub_wrapper(func): def wrapper(self, attr_path): self.unset_editors() self.root.ctx = applier_name self.root.__getattribute__('W_EDI_%s' % applier_name).setVisible(True) ret = func(self, cmds.getAttr(attr_path), attr_path) return ret return wrapper return sub_wrapper @staticmethod def get_bounds(obj, attr, min_default, max_default): """ Try to retrieve the range for the given attribute, if min or max fail it'll set default values :param obj: the object's name :type obj: str :param attr: attribute's name :type attr: str :param min_default: minimum default value :param max_default: max default value :type min_default: float | int :type max_default: float | int :return: minimum, maximum :rtype : tuple """ try: assert cmds.attributeQuery(attr, n=obj, mxe=True) maxi = cmds.attributeQuery(attr, n=obj, max=True)[0] except (RuntimeError, AssertionError): maxi = max_default try: assert cmds.attributeQuery(attr, n=obj, mne=True) mini = cmds.attributeQuery(attr, n=obj, min=True)[0] except (RuntimeError, AssertionError): mini = min_default return mini, maxi @prepare('float') def apply_float(self, value, path): """ Float attribute case :param value: attribute's value :param path: attribute's path = obj.attr """ obj, attr = path.split('.', 1) self.root.W_EDI_float.setRange( *self.get_bounds(obj, attr, -100.0, 100.0)) self.root.W_EDI_float.setValue(value) @prepare('enum') def apply_enum(self, value, path): """Enum case""" self.root.W_EDI_enum.clear() obj, attr = path.split('.', 1) try: enums = [ enum.split('=')[0] for enum in cmds.attributeQuery( attr, n=obj, listEnum=True)[0].split(':') ] except RuntimeError: self.apply_int(path) else: self.root.W_EDI_enum.addItems(enums) self.root.W_EDI_enum.setCurrentIndex( enums.index(cmds.getAttr(path, asString=True))) @prepare('int') def apply_int(self, value, path): """Integer case""" obj, attr = path.split('.', 1) self.root.W_EDI_int.setRange( *self.get_bounds(obj, attr, -1000, 1000)) self.root.W_EDI_int.setValue(value) @prepare('bool') def apply_bool(self, value, path): """Boolean case""" self.root.W_EDI_bool.setChecked(value) self.root.W_EDI_bool.setText(path.split('.', 1)[1]) @prepare('str') def apply_str(self, value, path): """String case""" self.root.W_EDI_str.setText(value) @prepare('d3') def apply_d3(self, value, path): """3D array case""" self.root.W_EDI_d3.setValues(value[0]) @prepare('d4') def apply_d4(self, value, path): """4D array case""" self.root.W_EDI_d4.setValues(value[0]) @prepare('color') def apply_color(self, value, path): """Color case""" try: colors = value[0] self.root.W_EDI_color.setColor([int(c * 255) for c in colors]) except TypeError: self.apply_int(value, path) class Attribute(str): """ A custom string attribute class to ship more information into the string variable """ def __new__(cls, path='', super_type=Object): obj, attr = path.split('.', 1) str_obj = str.__new__(cls, attr) str_obj.obj, str_obj.attr = obj, attr str_obj.path = path str_obj.super_type = super_type str_obj.type = None return str_obj # static variables to pre-load icons and attributes short names ctx_icons = { 'float': QIcon(':render_decomposeMatrix.png'), 'enum': QIcon(':showLineNumbers.png'), 'bool': QIcon(':out_decomposeMatrix.png'), 'time': QIcon(':time.svg'), 'byte': QIcon(':out_defaultTextureList.png'), 'angle': QIcon(':angleDim.png'), 'string': QIcon(':text.png'), 'float3': QIcon(':animCurveTA.svg'), 'float4': QIcon(':animCurveTA.svg'), 'color': QIcon(':clampColors.svg') } for ctx in ('doubleLinear', 'double', 'long', 'short'): ctx_icons[ctx] = ctx_icons['float'] ctx_icons['double3'] = ctx_icons['float3'] ctx_icons['double4'] = ctx_icons['float4'] ctx_wide = { 'float': ('float', 'doubleLinear', 'double', 'long', 'short'), 'enum': ('enum', ), 'bool': ('bool', ), 'time': ('time', ), 'byte': ('byte', ), 'angle': ('doubleAngle', ), 'string': ('string', ), 'float3': ('double3', 'float3'), 'float4': ('double4', 'float4'), 'color': ('color', ) } def __init__(self, parent=None): super(MassAttribute_UI, self).__init__(parent) # Abstract self.applier = self.Applikator(self) self.selection = [] self.callback = None self.ctx = None # storing found attributes' types to avoid double check self.solved = {} self.setLocale(QLocale.C) self.setAttribute(Qt.WA_DeleteOnClose) self.setAttribute(Qt.WA_QuitOnClose) self.setFixedWidth(300) self.setWindowTitle('Massive Attribute Modifier') # UI L_main = QVBoxLayout() self.WV_title = QLabel('') self.WV_title.setVisible(False) self.WV_title.setFont(QFont('Verdana', 10)) self.WV_title.setContentsMargins(0, 0, 0, 7) self.WB_select = QPushButton('Select') self.WB_select.setVisible(False) self.WB_select.setFixedWidth(50) self.WB_select.clicked.connect(lambda: cmds.select(self.selection)) self.WB_update = QPushButton('Update') self.WB_update.setFixedWidth(50) self.WB_update.clicked.connect( lambda: self.update_attributes(cmds.ls(sl=True))) self.WV_search = Filter() self.WV_search.textChanged.connect(self.filter) self.WC_cases = QCheckBox('Case sensitive') self.WC_cases.stateChanged.connect(self.filter) self.WC_types = QCheckBox('Type filtering') self.WL_attrtype = QComboBox() self.WL_attrtype.setEnabled(False) for i, ctx in enumerate(sorted(self.ctx_wide)): self.WL_attrtype.addItem(ctx.title()) self.WL_attrtype.setItemIcon(i, self.ctx_icons[ctx]) L_attrtype = line(self.WC_types, self.WL_attrtype) self.WC_types.stateChanged.connect( partial(self.update_attributes, self.selection)) self.WC_types.stateChanged.connect(self.WL_attrtype.setEnabled) self.WL_attrtype.currentIndexChanged.connect(self.filter) self.WC_liveu = QCheckBox('Live') self.WC_liveu.stateChanged.connect(self.WB_update.setDisabled) self.WC_liveu.stateChanged.connect(self.set_callback) self.WC_histo = QCheckBox('Load history') self.WC_histo.setChecked(True) self.WC_histo.stateChanged.connect( partial(self.update_attributes, self.selection)) self.WC_child = QCheckBox('Children') self.WC_child.stateChanged.connect( partial(self.update_attributes, self.selection)) options = group( 'Options', line(self.WC_cases, L_attrtype), line(self.WC_child, self.WC_histo, self.WC_liveu, self.WB_update)) options.layout().setSpacing(2) self.WL_attributes = QTreeWidget() self.WL_attributes.setStyleSheet( 'QTreeView {alternate-background-color: #1b1b1b;}') self.WL_attributes.setAlternatingRowColors(True) self.WL_attributes.setHeaderHidden(True) self.WL_attributes.setRootIsDecorated(False) self.objs_attr = set() self.shps_attr = set() self.W_EDI_float = FloatBox() self.W_EDI_int = IntBox() self.W_EDI_enum = QComboBox() self.W_EDI_bool = QCheckBox() self.W_EDI_str = QLineEdit() self.W_EDI_d3 = Double3() self.W_EDI_d4 = Double4() self.W_EDI_color = ColorPicker() # Final layout L_title = line(self.WV_title, self.WB_select) L_title.setStretch(0, 1) L_main.addLayout(L_title) L_main.setAlignment(Qt.AlignLeft) L_main.addWidget(self.WV_search) L_main.addWidget(options) L_main.addWidget(self.WL_attributes) L_edits = col(self.W_EDI_bool, self.W_EDI_int, self.W_EDI_float, self.W_EDI_enum, self.W_EDI_str, self.W_EDI_d3, self.W_EDI_d4, self.W_EDI_color) L_edits.setContentsMargins(0, 8, 0, 0) L_main.addLayout(L_edits) L_main.setStretch(3, 1) L_main.setSpacing(2) self.appliers = { 'float': self.applier.apply_float, 'enum': self.applier.apply_enum, 'bool': self.applier.apply_bool, 'time': self.applier.apply_float, 'byte': self.applier.apply_int, 'angle': self.applier.apply_float, 'string': self.applier.apply_str, 'float3': self.applier.apply_d3, 'float4': self.applier.apply_d4, 'color': self.applier.apply_color } self.setLayout(L_main) # final settings self.WL_attributes.itemSelectionChanged.connect(self.update_setter) self.applier.unset_editors() def closeEvent(self, *args, **kwargs): self.set_callback(False) def set_callback(self, state): """ Toggle selection event callback :param state: checkbox's state :type state: bool | int """ if state and not self.callback: self.callback = MEventMessage.addEventCallback( 'SelectionChanged', self.update_attributes) self.update_attributes(cmds.ls(sl=True)) elif not state and self.callback: MMessage.removeCallback(self.callback) self.callback = None @staticmethod def format_title(nodes): """ Extract the matching characters from a given nodes selection, if begin matches it will return "joint*" with a wildcard when names don't match :param nodes: objects' list :type nodes: list | tuple :return: the formatted name with the corresponding characters :rtype : str """ res = None if nodes: # we get the first node as a reference node = nodes[0] # and compare with the other nodes subs = [w for w in nodes if w != node] l = 1 valid = True # will continue until l (length) match the full name's length or until names don't match while l < len(node) and valid: for sub in subs: if not sub.startswith(node[:l]): valid = False break else: l += 1 # if matching characters isn't long enough we only display the number of nodes selected if l <= 3: res = '%i objects' % len(nodes) # otherwise showing matching pattern elif l < len(node) or len(nodes) > 1: res = node[:l - 1] + '* (%i objects)' % len(nodes) else: res = node return res @staticmethod def get_history(node): """ Extract history for the given node :rtype: list """ return cmds.listHistory(node, il=2, pdo=True) or [] @staticmethod def get_shapes(node): """ Extract shape(s) for the given node :rtype: list """ return cmds.listRelatives(node, s=True, ni=True, f=True) def get_attributes_type(self, attrs): """ For a given list of attributes of type Attribute, will loop through and fill the type parameter of the attribute with the corresponding type, if type is invalid or not handled, it'll remove it :param attrs: attributes' list :type attrs: [MassAttribute_UI.Attribute] :return: cleaned and filled attributes' list :rtype: [MassAttribute_UI.Attribute] """ attrs = list(attrs) # first we sort the attributes' list attrs.sort() # then we try to extract the attribute's type for i, attr in enumerate(attrs): try: if attr.attr in self.solved: attr.type = self.solved[attr.attr] raise RuntimeError tpe = cmds.getAttr(attr.path, typ=True) assert tpe attr.type = tpe self.solved[attr.attr] = tpe except (AssertionError, ValueError, RuntimeError): pass # defining a to-remove list rm_list = set() layers = {'3': 'XYZ', '4': 'XYZW'} for i, attr in enumerate(attrs): if i in rm_list: continue # we handle some special cases here, if ever the attribute list contains RGB and separate R, G and B we # assume it's a color, if it's a double3 or float3 and we find the corresponding XYZ, we remove then to # avoid duplicates if attr.endswith('RGB'): if '%sR' % attr[:-3] in attrs: attr.type = 'color' for chan in 'RGB': rm_list.add(attrs.index('%s%s' % (attr[:-3], chan))) # if the attribute's type isn't in the list, we remove elif attr.type not in MassAttribute_UI.ctx_icons: rm_list.add(i) elif attr.endswith('R'): if '%sG' % attr[:-1] in attrs and attr[:-1] in attrs: attr.type = 'color' for chan in 'RGB': rm_list.add(attrs.index('%s%s' % (attr[:-1], chan))) elif attr.type in ('double3', 'double4', 'float3', 'float4'): if '%sX' % attr in attrs: for chan in layers[attr.type[-1]]: rm_list.add(attrs.index('%s%s' % (attr, chan))) # finally cleaning the list for i in sorted(rm_list, reverse=True): attrs.pop(i) return attrs def apply_value(self, value): """ When the value is modified in the UI, we forward the given value and applies to the object's :param value: attribute's value, mixed type :type value: mixed """ # We get the only selected object in list and get it's super type (Shape, History or Object) and # type (float, int, string) item = self.WL_attributes.selectedItems()[0] attr = item.attribute shape = attr.super_type == Shape histo = attr.super_type == History tpe = item.attribute.type # eq dict for each context value = { 'bool': bool, 'int': int, 'float': float, 'enum': int, 'str': str, 'd3': list, 'd4': list, 'color': list }[self.ctx](value) # converting the selection into a set cmds.undoInfo(openChunk=True) targets = set(self.selection) # we propagate to children if 'Children' checkbox is on if self.WC_child.isChecked(): for obj in list(targets): targets |= set(cmds.listRelatives(obj, ad=True)) # if the target attribute is on the history, we add all selection's history to the list if histo: for obj in list(targets): targets.remove(obj) targets |= set(self.get_history(obj)) # then we loop through target objects for obj in targets: # if the target is on the shape we get object's shape if shape and not histo: shapes = self.get_shapes(obj) if obj in shapes: continue else: obj = shapes[0] # then we try to apply depending on attribute's type try: correct_path = attr.path.replace(attr.obj, obj) if tpe == 'string': cmds.setAttr(correct_path, value, type='string') elif tpe in ('double3', 'double4', 'float3', 'float4', 'color'): cmds.setAttr(correct_path, *value, type='double%d' % len(value)) else: cmds.setAttr(correct_path, value) except RuntimeError: pass cmds.undoInfo(closeChunk=True) def update_setter(self): """ When the list's selection changes we update the applier widget """ item = self.WL_attributes.selectedItems() # abort if no item is selected if not len(item): return # getting attribute's parameter attr = item[0].attribute if len(self.selection): try: # looping until we find a context having the current attribute's type for applier in self.ctx_wide: if attr.type in self.ctx_wide[applier]: break # then we apply for the given path (obj.attribute) self.appliers[applier](attr.path) # and connecting event to the self.apply_value function self.applier.widget_event(self.ctx).connect(self.apply_value) # otherwise selection or type is invalid except IndexError: self.ctx = None def update_attributes(self, selection=None, *args): """ Update the attributes for the given selection, looping through objects' attributes, finding attr in common between all objects then cleaning the lists, doing the same for shapes and / or histories :param selection: object's selection """ # redefining lists as set to intersect union etc self.objs_attr = set() self.shps_attr = set() # pre init self.WL_attributes.clear() self.applier.unset_editors() self.selection = selection or (cmds.ls( sl=True) if self.WC_liveu.isChecked() else self.selection) self.WV_title.setText(self.format_title(self.selection)) self.WV_title.setVisible(bool(len(self.selection))) self.WB_select.setVisible(bool(len(self.selection))) if not len(self.selection): return def get_usable_attrs(obj, super_type): """ Small internal function to get a compatible attributes' list for the given object and assign the given super_type to it (Object, Shape or History) :param obj: object's name :type obj: str :param super_type: attribute's main type :type super_type: Object | Shape | History :return: """ return set([ MassAttribute_UI.Attribute('%s.%s' % (obj, attr), super_type) for attr in cmds.listAttr( obj, se=True, ro=False, m=True, w=True) ]) if len(self.selection): self.objs_attr = get_usable_attrs(self.selection[0], Object) # if we also want the object's history we add it to the initial set if self.WC_histo.isChecked(): for histo in self.get_history(self.selection[0]): self.objs_attr |= get_usable_attrs(histo, History) # filling the shape's set for shape in (self.get_shapes(self.selection[0]) or []): self.shps_attr |= get_usable_attrs(shape, Shape) # if selection's length bigger than one we compare by intersection with the other sets if len(self.selection) > 1: for obj in self.selection: sub_attr = get_usable_attrs(obj, Object) if self.WC_histo.isChecked(): for histo in self.get_history(obj): sub_attr |= get_usable_attrs(histo, History) self.objs_attr.intersection_update(sub_attr) for shape in (self.get_shapes(self.selection[0]) or []): self.shps_attr.intersection_update( get_usable_attrs(shape, Shape)) # finally getting all intersecting attributes' types self.objs_attr = self.get_attributes_type(self.objs_attr) self.shps_attr = self.get_attributes_type(self.shps_attr) # and filtering the list self.filter() def add_set(self, iterable, title=None): """ Adding the given iterable to the list with a first Separator object with given title :param iterable: list of item's attributes :param title: Separator's name """ if len(iterable): # if title is given we first add a Separator item to indicate coming list title if title: self.WL_attributes.addTopLevelItem( QTreeWidget_Separator(title)) items = [] for attr in sorted(iterable): item = QTreeWidgetItem([attr]) # assigning the attribute itself inside a custom parameter item.attribute = attr items.append(item) # finally adding all the items to the list self.WL_attributes.addTopLevelItems(items) def filter(self): """ Filter the list with UI's parameters, such as name or type filtering, etc """ # pre cleaning self.WL_attributes.clear() # using regex compile to avoid re execution over many attributes mask = self.WV_search.text() case = 0 if self.WC_cases.isChecked() else re.IGNORECASE re_start = re.compile(r'^%s.*?' % mask, case) re_cont = re.compile(r'.*?%s.*?' % mask, case) # getting the four different lists obj_start = set([at for at in self.objs_attr if re_start.search(at)]) shp_start = set([at for at in self.shps_attr if re_start.search(at)]) # if type filtering is one we only extract the wanted attribute's type if self.WC_types.isChecked(): obj_start = set([ at for at in obj_start if at.type in self.ctx_wide[ self.WL_attrtype.currentText().lower()] ]) shp_start = set([ at for at in shp_start if at.type in self.ctx_wide[ self.WL_attrtype.currentText().lower()] ]) # finally adding the current sets if there is a mask we add the also the containing matches if mask: # getting contains filtering and type containers filtering obj_contains = obj_start.symmetric_difference( set([at for at in self.objs_attr if re_cont.search(at)])) shp_contains = shp_start.symmetric_difference( set([at for at in self.shps_attr if re_cont.search(at)])) if self.WC_types.isChecked(): obj_contains = set([ at for at in obj_contains if at.type in self.ctx_wide[ self.WL_attrtype.currentText().lower()] ]) shp_contains = set([ at for at in shp_contains if at.type in self.ctx_wide[ self.WL_attrtype.currentText().lower()] ]) # adding the sets self.add_set(obj_start, 'Obj attributes starting with') self.add_set(obj_contains, 'Obj attributes containing') self.add_set(shp_start, 'Shape attributes starting with') self.add_set(shp_contains, 'Shape attributes containing') else: self.add_set(obj_start, 'Object\'s attributes') self.add_set(shp_start, 'Shape\'s attributes') # and we select the first one if ever there is something in the list if self.WL_attributes.topLevelItemCount(): self.WL_attributes.setItemSelected( self.WL_attributes.topLevelItem(1), True)
class MainWindow(QDialog): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.stopSign = 0 self.ws = 0 self.comment = '' self.clipboard = QApplication.clipboard() self.setWindowTitle(u'勤務表') self.setWindowIcon(QIcon('./img/tray.png')) self.systemTrayIcon = QSystemTrayIcon(self) self.systemTrayIcon.setIcon(QIcon('./img/tray.png')) self.systemTrayIcon.setVisible(True) self.systemTrayIcon.show() self.systemTrayIcon.activated.connect(self.on_systemTrayIcon_activated) self.tableLabel = QLabel('select *.xls file') self.pathText = QLineEdit() self.triggerBtn = QPushButton(u'檢查 / 開始') self.browseBtn = QPushButton(u' 瀏覽 ') self.stopBtn = QPushButton(u'停止') self.table = QTableWidget(26,0,self) self.setUpTable() self.hbox1 = QHBoxLayout() self.hbox2 = QHBoxLayout() self.hbox3 = QHBoxLayout() self.hbox4 = QHBoxLayout() self.hbox1.addWidget(self.pathText) self.hbox1.addWidget(self.browseBtn) self.hbox1.addWidget(self.triggerBtn) self.browseBtn.clicked.connect(self.OpenFile) self.triggerBtn.clicked.connect(self.setupTypeThread) self.stopBtn.clicked.connect(self.threadStop) self.status = QTreeWidget(self) self.status.setHeaderHidden(True) self.hbox2.addWidget(self.status) self.hbox3.addWidget(self.table) self.hbox4.addWidget(self.stopBtn) self.setGeometry(200, 200, 700, 400) self.status.setFixedHeight (80) self.layout = QVBoxLayout() self.layout.addWidget(self.tableLabel) self.layout.addLayout(self.hbox1) self.layout.addLayout(self.hbox2) self.layout.addLayout(self.hbox3) self.layout.addLayout(self.hbox4) self.setLayout(self.layout) self.stopBtn.setEnabled(False) def setUpTable(self): self.table.horizontalHeader().setVisible(False) for i in xrange(0, 26, 1): self.vhfont = QFont('Times', pointSize = 10, weight=QFont.Bold) timePos = i+6 if i == 0: item = QTableWidgetItem(u'[標題]') elif i == 1: item = QTableWidgetItem(u'[設定]') elif 2 < timePos < 24: item = QTableWidgetItem(('{0}~{1}').format(timePos, timePos+1)) else: item = QTableWidgetItem(('{0}~{1}').format(timePos-24, timePos-23)) item.setFont(self.vhfont) item.setTextAlignment(Qt.AlignCenter) self.table.setVerticalHeaderItem(i, item) def on_systemTrayIcon_activated(self, reason): if reason == QSystemTrayIcon.DoubleClick: if self.isHidden(): try: self.typeThread self.threadStop() except: pass self.show() else: self.hide() def setupTypeThread(self): self.clipboard.setText('') det = self.loadDetec() self.ws, vaild = self.checkTableValidation(det, self.table) ws = [] for col in self.ws: ws.append(col[0]) self.addStatus( u' 狀態: 檢查勤務表狀態....', 0) errStat = self.check(ws) if vaild != True and self.table.columnCount()!=0: self.addStatus( vaild, 1) elif self.table.columnCount() ==0: self.addStatus( u' 錯誤: 請載入勤務表', 1) self.typeThread = startType(self.ws) self.typeThread.threadDone.connect(self.showDoneMsg, Qt.QueuedConnection) self.typeThread.toTray.connect(self.toTray, Qt.QueuedConnection) self.typeThread.toCilpboard.connect(self.toCilpboard, Qt.QueuedConnection) self.typeThread.enableButtons.connect(self.enableButtons, Qt.QueuedConnection) self.typeThread.showErrMsg.connect(self.showErrMsg, Qt.QueuedConnection) self.typeThread.addStatus.connect(self.addStatus, Qt.QueuedConnection) if not self.typeThread.isRunning() and vaild == True and errStat == True: self.addStatus( u' 狀態: 檢查通過,開始進行登打作業....', -1) self.browseBtn.setEnabled(False) self.triggerBtn.setEnabled(False) self.stopBtn.setEnabled(True) self.typeThread.start() def toTray(self, state): if state: pass self.hide() self.systemTrayIcon.showMessage(u'輸入中',u'勤務表背景輸入中....\n\n雙擊圖示可暫停程序', msecs=1000000) else: self.show() def toCilpboard(self, text): self.clipboard.setText(text) def threadStop(self): self.typeThread.stopSign = 1 if self.typeThread.isRunning(): self.browseBtn.setEnabled(True) self.triggerBtn.setEnabled(True) self.stopBtn.setEnabled(False) def addStatus(self, text, err): subTreeItem = QTreeWidgetItem(self.status) subTreeItem.setText(0, text) self.activateWindow() if err == 1: font = QFont('Serif', 10, QFont.Bold) subTreeItem.setFont(0, font) subTreeItem.setForeground(0, QBrush(Qt.white)) subTreeItem.setBackground(0, QBrush(QColor(150,0,0))) elif err == 0: font = QFont('Serif', 10, QFont.Bold) subTreeItem.setFont(0, font) subTreeItem.setForeground(0, QBrush(Qt.black)) subTreeItem.setBackground(0, QBrush(Qt.white)) else: font = QFont('Serif', 10, QFont.Bold) subTreeItem.setFont(0, font) subTreeItem.setForeground(0, QBrush(Qt.white)) subTreeItem.setBackground(0, QBrush(QColor(0,150,0))) self.status.scrollToItem(subTreeItem, QAbstractItemView.PositionAtCenter) def showErrMsg(self, title, msg): self.addStatus( u'錯誤: ' + msg, 1) QMessageBox.warning(self, title, msg, QMessageBox.Ok) def showDoneMsg(self): self.addStatus( u'完成: 完成!', 1) QMessageBox.warning(self, u'完成!', u'完成!', QMessageBox.Ok) def enableButtons(self): self.clipboard.setText(self.comment) self.browseBtn.setEnabled(True) self.triggerBtn.setEnabled(True) self.stopBtn.setEnabled(False) self.show() def OpenFile(self): fileName = QFileDialog.getOpenFileName(self, "Open File.", "/home") self.comment = '' ext = fileName[0].split('.')[-1] if ext == 'xls' or ext == 'xlsx': self.pathText.setText(fileName[0]) sheet = open_workbook(fileName[0]).sheets()[0] self.setFixedHeight(550) self.addStatus( u' 狀態: 載入勤務表: [' + fileName[0] + ']', -1) for col in xrange(self.table.columnCount()-1,-1,-1): self.table.removeColumn(col) ws, header, headerNum = self.loadWS(sheet) self.appendTable(header, ws) self.check(ws) self.comment += self.yieldcomment(sheet) self.ws = ws else: self.showErrMsg(u'錯誤',u'選取檔案不是EXCEL檔') self.ws = 0 def checkTableValidation(self, detCol, table): # .[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] ws = [[], [], [], [], [], [], [], [], [], []] errStat = True if len(detCol) == 0 or (0 in detCol): errStat = u' 狀態: 勤務表標頭錯誤,點選 [ --請選擇-- ] 選取有效標頭' for i in xrange(len(detCol)-1): if sorted(detCol)[i] == sorted(detCol)[i+1]: errStat = u' 狀態: 勤務表標頭重複' print detCol for c in xrange(table.columnCount()): col = [] colNum = detCol[c] for r in xrange(2, 26, 1): col.append(table.item(r,c).text()) ws[colNum-1].append(col) for i in xrange(len(ws)): if len(ws[i]) == 0: ws[i].append(['','','','','','','','','','','','','','','','','','','','','','','','']) return (ws), errStat def loadWS(self, sheet): header, headerNum, ws= [],[],[] for c in xrange(3, 26 ,1): title = (sheet.cell_value(3, c)) if len(title) != 0 and len(header) <6: header.append(title) col = [] for m in xrange(7, 31, 1): try: col.append(str(sheet.cell_value(m, c)).strip('()').replace('.0', '').replace('.', ',')) except: col.append(u'error') ws.append(col) return ws, header, headerNum def appendTable(self, header, ws): try: font = QFont('TypeWriter', pointSize = 10, weight=QFont.Bold) for text in header: self.table.insertColumn(0) for c in xrange(len(header)): det = self.determine(header[c]) item = QTableWidgetItem(header[c]) if det == 0: item.setBackground(QBrush(QColor('#FF8D00'))) else: item.setBackground(QBrush(QColor('#005588'))) item.setFont(font) item.setTextAlignment(Qt.AlignCenter) self.table.setItem(0, c, item) nComboBox = self.newCombo() nComboBox.setCurrentIndex(det) self.table.setCellWidget(1, c, (nComboBox)) for r in xrange(2,26,1): item = QTableWidgetItem(ws[c][r-2]) item.setFont(font) item.setTextAlignment(Qt.AlignCenter) self.table.setItem(r, c, item) self.addStatus( u' 狀態: 勤務表預覽成功', -1) return 0 except: self.addStatus( u' 狀態: 勤務表預覽失敗', 1) return 'error' def loadDetec(self): det = [] for col in xrange(self.table.columnCount()): det.append( self.table.cellWidget(1, col).currentIndex()) return det def newCombo(self): taskList = [u'--請選擇--', u'值班', u'救護勤務', u'備勤', u'待命服勤', u'水源查察', u'消防查察', u'宣導勤務', u'訓(演)練', u'專案勤務', u'南山救護站'] nComboBox = QComboBox() nComboBox.addItems(taskList) for i in xrange(len(taskList)): if i == 0: nComboBox.setItemData(i, QColor('#550000'), Qt.BackgroundColorRole) nComboBox.setItemData(i, Qt.AlignCenter, Qt.TextAlignmentRole) nComboBox.setStyleSheet("text-align: right; font: bold 13px;") return nComboBox def setTableErr(self, row, state): if state == 'illegal' : color = '#CC0033' elif state == 'repeated' : color = '#FF8D00' elif state == 'legal' : color = '#201F1F' for col in xrange(self.table.columnCount()): self.table.item(row,col).setBackground(QBrush(QColor(color))) def check(self, ws): errStat = True for m in xrange(2,26): self.setTableErr(m, 'legal') for i in xrange(24): ary = [] for j in xrange(len(ws)): for each in ws[j][i].replace(' ', '').split(','): try: if each == "A": ary.append(-1) elif each == '' : pass else: each == ary.append(int(each)) except: errStat = False timePos = i+8 rptErrMSG = u" 錯誤: 於 {0} ~ {1} 時 數字: {2} --不合法, 請修正" self.setTableErr(i+2, 'illegal') print timePos if timePos < 23: print 'a' self.addStatus(rptErrMSG.format(timePos, timePos+1, each), 1) else: self.addStatus(rptErrMSG.format(timePos-24, timePos-23, each), 1) ary = sorted(ary) for idx in xrange(len(ary)-1): if ary[idx] == ary[idx+1]: errStat = False timePos = i+8 rptErrMSG = u" 錯誤: 於 {0} ~ {1} 時 數字: {2} --番號重複, 請修正" self.setTableErr(i+2, 'repeated') if timePos < 23: self.addStatus(rptErrMSG.format(timePos, timePos+1, str(ary[idx]).replace('-1', 'A')), 1) else: self.addStatus(rptErrMSG.format(timePos-24, timePos-23, str(ary[idx]).replace('-1', 'A')), 1) return errStat def determine(self, title): cmpfactor = 0 smp = [u'值班', u'救護分隊務', u'備', u'待命服', u'水源', u'消防', u'宣導', u'訓演練', u'專案其它', u'南山站'] for index, each in zip(xrange(len(smp)),smp): for text in each: for elem in title: cmpfactor += ( elem == text ) if cmpfactor > 0: return index+1 return 0 def yieldcomment(self, sheet): comment0, comment1 = '', '' # for i,j in [[24,27], [27,27], [29,27], [29,35]]: # try: # comment0 += (smart_str(sheet.cell_value(i, j)) + '\n') # except: # pass for i,j in [[31,3], [32,3], [33,3], [34,3]]: try: comment1 += (sheet.cell_value(i, j) + '\n') except: pass return comment1
class MassAttribute_UI(QDialog): """ The main UI """ class Applikator(QObject): """ This is the core applier which toggle the display of the corresponding widget and handling events' connections """ def __init__(self, parent=None): super(MassAttribute_UI.Applikator, self).__init__() self.root = parent def widget_event(self, t): """ Return the correct widget's event depending on attribute's type :param t: the attribute's type :type t: str :return: the event :rtype : Signal """ return {'float': self.root.W_EDI_float.valueChanged, 'enum': self.root.W_EDI_enum.currentIndexChanged, 'int': self.root.W_EDI_int.valueChanged, 'bool': self.root.W_EDI_bool.stateChanged, 'str': self.root.W_EDI_str.textChanged, 'd3': self.root.W_EDI_d3.valuesChanged, 'd4': self.root.W_EDI_d4.valuesChanged, 'color': self.root.W_EDI_color.colorChanged}[t] def unset_editors(self): """ Toggle off all editors and disconnect the current one """ for widget in (self.root.W_EDI_float, self.root.W_EDI_int, self.root.W_EDI_enum, self.root.W_EDI_bool, self.root.W_EDI_str, self.root.W_EDI_d3, self.root.W_EDI_d4, self.root.W_EDI_color): widget.setVisible(False) # trying to force disconnection try: self.widget_event(self.root.ctx).disconnect(self.root.apply_value) except (KeyError, RuntimeError): pass def prepare(applier_name): """ A decorator to prepare the attribute depending on type for the corresponding widget and getting the attribute's value :param applier_name: attribute's type :type applier_name: str """ def sub_wrapper(func): def wrapper(self, attr_path): self.unset_editors() self.root.ctx = applier_name self.root.__getattribute__('W_EDI_%s' % applier_name).setVisible(True) ret = func(self, cmds.getAttr(attr_path), attr_path) return ret return wrapper return sub_wrapper @staticmethod def get_bounds(obj, attr, min_default, max_default): """ Try to retrieve the range for the given attribute, if min or max fail it'll set default values :param obj: the object's name :type obj: str :param attr: attribute's name :type attr: str :param min_default: minimum default value :param max_default: max default value :type min_default: float | int :type max_default: float | int :return: minimum, maximum :rtype : tuple """ try: assert cmds.attributeQuery(attr, n=obj, mxe=True) maxi = cmds.attributeQuery(attr, n=obj, max=True)[0] except (RuntimeError, AssertionError): maxi = max_default try: assert cmds.attributeQuery(attr, n=obj, mne=True) mini = cmds.attributeQuery(attr, n=obj, min=True)[0] except (RuntimeError, AssertionError): mini = min_default return mini, maxi @prepare('float') def apply_float(self, value, path): """ Float attribute case :param value: attribute's value :param path: attribute's path = obj.attr """ obj, attr = path.split('.', 1) self.root.W_EDI_float.setRange(*self.get_bounds(obj, attr, -100.0, 100.0)) self.root.W_EDI_float.setValue(value) @prepare('enum') def apply_enum(self, value, path): """Enum case""" self.root.W_EDI_enum.clear() obj, attr = path.split('.', 1) try: enums = [enum.split('=')[0] for enum in cmds.attributeQuery(attr, n=obj, listEnum=True)[0].split(':')] except RuntimeError: self.apply_int(path) else: self.root.W_EDI_enum.addItems(enums) self.root.W_EDI_enum.setCurrentIndex(enums.index(cmds.getAttr(path, asString=True))) @prepare('int') def apply_int(self, value, path): """Integer case""" obj, attr = path.split('.', 1) self.root.W_EDI_int.setRange(*self.get_bounds(obj, attr, -1000, 1000)) self.root.W_EDI_int.setValue(value) @prepare('bool') def apply_bool(self, value, path): """Boolean case""" self.root.W_EDI_bool.setChecked(value) self.root.W_EDI_bool.setText(path.split('.', 1)[1]) @prepare('str') def apply_str(self, value, path): """String case""" self.root.W_EDI_str.setText(value) @prepare('d3') def apply_d3(self, value, path): """3D array case""" self.root.W_EDI_d3.setValues(value[0]) @prepare('d4') def apply_d4(self, value, path): """4D array case""" self.root.W_EDI_d4.setValues(value[0]) @prepare('color') def apply_color(self, value, path): """Color case""" try: colors = value[0] self.root.W_EDI_color.setColor([int(c * 255) for c in colors]) except TypeError: self.apply_int(value, path) class Attribute(str): """ A custom string attribute class to ship more information into the string variable """ def __new__(cls, path='', super_type=Object): obj, attr = path.split('.', 1) str_obj = str.__new__(cls, attr) str_obj.obj, str_obj.attr = obj, attr str_obj.path = path str_obj.super_type = super_type str_obj.type = None return str_obj # static variables to pre-load icons and attributes short names ctx_icons = {'float': QIcon(':render_decomposeMatrix.png'), 'enum': QIcon(':showLineNumbers.png'), 'bool': QIcon(':out_decomposeMatrix.png'), 'time': QIcon(':time.svg'), 'byte': QIcon(':out_defaultTextureList.png'), 'angle': QIcon(':angleDim.png'), 'string': QIcon(':text.png'), 'float3': QIcon(':animCurveTA.svg'), 'float4': QIcon(':animCurveTA.svg'), 'color': QIcon(':clampColors.svg')} for ctx in ('doubleLinear', 'double', 'long', 'short'): ctx_icons[ctx] = ctx_icons['float'] ctx_icons['double3'] = ctx_icons['float3'] ctx_icons['double4'] = ctx_icons['float4'] ctx_wide = {'float': ('float', 'doubleLinear', 'double', 'long', 'short'), 'enum': ('enum',), 'bool': ('bool',), 'time': ('time',), 'byte': ('byte',), 'angle': ('doubleAngle',), 'string': ('string',), 'float3': ('double3', 'float3'), 'float4': ('double4', 'float4'), 'color': ('color',)} def __init__(self, parent=None): super(MassAttribute_UI, self).__init__(parent) # Abstract self.applier = self.Applikator(self) self.selection = [] self.callback = None self.ctx = None # storing found attributes' types to avoid double check self.solved = {} self.setLocale(QLocale.C) self.setAttribute(Qt.WA_DeleteOnClose) self.setAttribute(Qt.WA_QuitOnClose) self.setFixedWidth(300) self.setWindowTitle('Massive Attribute Modifier') # UI L_main = QVBoxLayout() self.WV_title = QLabel('') self.WV_title.setVisible(False) self.WV_title.setFont(QFont('Verdana', 10)) self.WV_title.setContentsMargins(0, 0, 0, 7) self.WB_select = QPushButton('Select') self.WB_select.setVisible(False) self.WB_select.setFixedWidth(50) self.WB_select.clicked.connect(lambda: cmds.select(self.selection)) self.WB_update = QPushButton('Update') self.WB_update.setFixedWidth(50) self.WB_update.clicked.connect(lambda:self.update_attributes(cmds.ls(sl=True))) self.WV_search = Filter() self.WV_search.textChanged.connect(self.filter) self.WC_cases = QCheckBox('Case sensitive') self.WC_cases.stateChanged.connect(self.filter) self.WC_types = QCheckBox('Type filtering') self.WL_attrtype = QComboBox() self.WL_attrtype.setEnabled(False) for i, ctx in enumerate(sorted(self.ctx_wide)): self.WL_attrtype.addItem(ctx.title()) self.WL_attrtype.setItemIcon(i, self.ctx_icons[ctx]) L_attrtype = line(self.WC_types, self.WL_attrtype) self.WC_types.stateChanged.connect(partial(self.update_attributes, self.selection)) self.WC_types.stateChanged.connect(self.WL_attrtype.setEnabled) self.WL_attrtype.currentIndexChanged.connect(self.filter) self.WC_liveu = QCheckBox('Live') self.WC_liveu.stateChanged.connect(self.WB_update.setDisabled) self.WC_liveu.stateChanged.connect(self.set_callback) self.WC_histo = QCheckBox('Load history') self.WC_histo.setChecked(True) self.WC_histo.stateChanged.connect(partial(self.update_attributes, self.selection)) self.WC_child = QCheckBox('Children') self.WC_child.stateChanged.connect(partial(self.update_attributes, self.selection)) options = group('Options', line(self.WC_cases, L_attrtype), line(self.WC_child, self.WC_histo, self.WC_liveu, self.WB_update)) options.layout().setSpacing(2) self.WL_attributes = QTreeWidget() self.WL_attributes.setStyleSheet('QTreeView {alternate-background-color: #1b1b1b;}') self.WL_attributes.setAlternatingRowColors(True) self.WL_attributes.setHeaderHidden(True) self.WL_attributes.setRootIsDecorated(False) self.objs_attr = set() self.shps_attr = set() self.W_EDI_float = FloatBox() self.W_EDI_int = IntBox() self.W_EDI_enum = QComboBox() self.W_EDI_bool = QCheckBox() self.W_EDI_str = QLineEdit() self.W_EDI_d3 = Double3() self.W_EDI_d4 = Double4() self.W_EDI_color = ColorPicker() # Final layout L_title = line(self.WV_title, self.WB_select) L_title.setStretch(0, 1) L_main.addLayout(L_title) L_main.setAlignment(Qt.AlignLeft) L_main.addWidget(self.WV_search) L_main.addWidget(options) L_main.addWidget(self.WL_attributes) L_edits = col(self.W_EDI_bool, self.W_EDI_int, self.W_EDI_float, self.W_EDI_enum, self.W_EDI_str, self.W_EDI_d3, self.W_EDI_d4, self.W_EDI_color) L_edits.setContentsMargins(0, 8, 0, 0) L_main.addLayout(L_edits) L_main.setStretch(3, 1) L_main.setSpacing(2) self.appliers = {'float': self.applier.apply_float, 'enum': self.applier.apply_enum, 'bool': self.applier.apply_bool, 'time': self.applier.apply_float, 'byte': self.applier.apply_int, 'angle': self.applier.apply_float, 'string': self.applier.apply_str, 'float3': self.applier.apply_d3, 'float4': self.applier.apply_d4, 'color': self.applier.apply_color} self.setLayout(L_main) # final settings self.WL_attributes.itemSelectionChanged.connect(self.update_setter) self.applier.unset_editors() def closeEvent(self, *args, **kwargs): self.set_callback(False) def set_callback(self, state): """ Toggle selection event callback :param state: checkbox's state :type state: bool | int """ if state and not self.callback: self.callback = MEventMessage.addEventCallback('SelectionChanged', self.update_attributes) self.update_attributes(cmds.ls(sl=True)) elif not state and self.callback: MMessage.removeCallback(self.callback) self.callback = None @staticmethod def format_title(nodes): """ Extract the matching characters from a given nodes selection, if begin matches it will return "joint*" with a wildcard when names don't match :param nodes: objects' list :type nodes: list | tuple :return: the formatted name with the corresponding characters :rtype : str """ res = None if nodes: # we get the first node as a reference node = nodes[0] # and compare with the other nodes subs = [w for w in nodes if w != node] l = 1 valid = True # will continue until l (length) match the full name's length or until names don't match while l < len(node) and valid: for sub in subs: if not sub.startswith(node[:l]): valid = False break else: l += 1 # if matching characters isn't long enough we only display the number of nodes selected if l <= 3: res = '%i objects' % len(nodes) # otherwise showing matching pattern elif l < len(node) or len(nodes) > 1: res = node[:l - 1] + '* (%i objects)' % len(nodes) else: res = node return res @staticmethod def get_history(node): """ Extract history for the given node :rtype: list """ return cmds.listHistory(node, il=2, pdo=True) or [] @staticmethod def get_shapes(node): """ Extract shape(s) for the given node :rtype: list """ return cmds.listRelatives(node, s=True, ni=True, f=True) def get_attributes_type(self, attrs): """ For a given list of attributes of type Attribute, will loop through and fill the type parameter of the attribute with the corresponding type, if type is invalid or not handled, it'll remove it :param attrs: attributes' list :type attrs: [MassAttribute_UI.Attribute] :return: cleaned and filled attributes' list :rtype: [MassAttribute_UI.Attribute] """ attrs = list(attrs) # first we sort the attributes' list attrs.sort() # then we try to extract the attribute's type for i, attr in enumerate(attrs): try: if attr.attr in self.solved: attr.type = self.solved[attr.attr] raise RuntimeError tpe = cmds.getAttr(attr.path, typ=True) assert tpe attr.type = tpe self.solved[attr.attr] = tpe except (AssertionError, ValueError, RuntimeError): pass # defining a to-remove list rm_list = set() layers = {'3': 'XYZ', '4': 'XYZW'} for i, attr in enumerate(attrs): if i in rm_list: continue # we handle some special cases here, if ever the attribute list contains RGB and separate R, G and B we # assume it's a color, if it's a double3 or float3 and we find the corresponding XYZ, we remove then to # avoid duplicates if attr.endswith('RGB'): if '%sR' % attr[:-3] in attrs: attr.type = 'color' for chan in 'RGB': rm_list.add(attrs.index('%s%s' % (attr[:-3], chan))) # if the attribute's type isn't in the list, we remove elif attr.type not in MassAttribute_UI.ctx_icons: rm_list.add(i) elif attr.endswith('R'): if '%sG' % attr[:-1] in attrs and attr[:-1] in attrs: attr.type = 'color' for chan in 'RGB': rm_list.add(attrs.index('%s%s' % (attr[:-1], chan))) elif attr.type in ('double3', 'double4', 'float3', 'float4'): if '%sX' % attr in attrs: for chan in layers[attr.type[-1]]: rm_list.add(attrs.index('%s%s' % (attr, chan))) # finally cleaning the list for i in sorted(rm_list, reverse=True): attrs.pop(i) return attrs def apply_value(self, value): """ When the value is modified in the UI, we forward the given value and applies to the object's :param value: attribute's value, mixed type :type value: mixed """ # We get the only selected object in list and get it's super type (Shape, History or Object) and # type (float, int, string) item = self.WL_attributes.selectedItems()[0] attr = item.attribute shape = attr.super_type == Shape histo = attr.super_type == History tpe = item.attribute.type # eq dict for each context value = {'bool': bool, 'int': int, 'float': float, 'enum': int, 'str': str, 'd3': list, 'd4': list, 'color': list}[self.ctx](value) # converting the selection into a set cmds.undoInfo(openChunk=True) targets = set(self.selection) # we propagate to children if 'Children' checkbox is on if self.WC_child.isChecked(): for obj in list(targets): targets |= set(cmds.listRelatives(obj, ad=True)) # if the target attribute is on the history, we add all selection's history to the list if histo: for obj in list(targets): targets.remove(obj) targets |= set(self.get_history(obj)) # then we loop through target objects for obj in targets: # if the target is on the shape we get object's shape if shape and not histo: shapes = self.get_shapes(obj) if obj in shapes: continue else: obj = shapes[0] # then we try to apply depending on attribute's type try: correct_path = attr.path.replace(attr.obj, obj) if tpe == 'string': cmds.setAttr(correct_path, value, type='string') elif tpe in ('double3', 'double4', 'float3', 'float4', 'color'): cmds.setAttr(correct_path, *value, type='double%d' % len(value)) else: cmds.setAttr(correct_path, value) except RuntimeError: pass cmds.undoInfo(closeChunk=True) def update_setter(self): """ When the list's selection changes we update the applier widget """ item = self.WL_attributes.selectedItems() # abort if no item is selected if not len(item): return # getting attribute's parameter attr = item[0].attribute if len(self.selection): try: # looping until we find a context having the current attribute's type for applier in self.ctx_wide: if attr.type in self.ctx_wide[applier]: break # then we apply for the given path (obj.attribute) self.appliers[applier](attr.path) # and connecting event to the self.apply_value function self.applier.widget_event(self.ctx).connect(self.apply_value) # otherwise selection or type is invalid except IndexError: self.ctx = None def update_attributes(self, selection=None, *args): """ Update the attributes for the given selection, looping through objects' attributes, finding attr in common between all objects then cleaning the lists, doing the same for shapes and / or histories :param selection: object's selection """ # redefining lists as set to intersect union etc self.objs_attr = set() self.shps_attr = set() # pre init self.WL_attributes.clear() self.applier.unset_editors() self.selection = selection or (cmds.ls(sl=True) if self.WC_liveu.isChecked() else self.selection) self.WV_title.setText(self.format_title(self.selection)) self.WV_title.setVisible(bool(len(self.selection))) self.WB_select.setVisible(bool(len(self.selection))) if not len(self.selection): return def get_usable_attrs(obj, super_type): """ Small internal function to get a compatible attributes' list for the given object and assign the given super_type to it (Object, Shape or History) :param obj: object's name :type obj: str :param super_type: attribute's main type :type super_type: Object | Shape | History :return: """ return set([MassAttribute_UI.Attribute('%s.%s' % (obj, attr), super_type) for attr in cmds.listAttr(obj, se=True, ro=False, m=True, w=True)]) if len(self.selection): self.objs_attr = get_usable_attrs(self.selection[0], Object) # if we also want the object's history we add it to the initial set if self.WC_histo.isChecked(): for histo in self.get_history(self.selection[0]): self.objs_attr |= get_usable_attrs(histo, History) # filling the shape's set for shape in (self.get_shapes(self.selection[0]) or []): self.shps_attr |= get_usable_attrs(shape, Shape) # if selection's length bigger than one we compare by intersection with the other sets if len(self.selection) > 1: for obj in self.selection: sub_attr = get_usable_attrs(obj, Object) if self.WC_histo.isChecked(): for histo in self.get_history(obj): sub_attr |= get_usable_attrs(histo, History) self.objs_attr.intersection_update(sub_attr) for shape in (self.get_shapes(self.selection[0]) or []): self.shps_attr.intersection_update(get_usable_attrs(shape, Shape)) # finally getting all intersecting attributes' types self.objs_attr = self.get_attributes_type(self.objs_attr) self.shps_attr = self.get_attributes_type(self.shps_attr) # and filtering the list self.filter() def add_set(self, iterable, title=None): """ Adding the given iterable to the list with a first Separator object with given title :param iterable: list of item's attributes :param title: Separator's name """ if len(iterable): # if title is given we first add a Separator item to indicate coming list title if title: self.WL_attributes.addTopLevelItem(QTreeWidget_Separator(title)) items = [] for attr in sorted(iterable): item = QTreeWidgetItem([attr]) # assigning the attribute itself inside a custom parameter item.attribute = attr items.append(item) # finally adding all the items to the list self.WL_attributes.addTopLevelItems(items) def filter(self): """ Filter the list with UI's parameters, such as name or type filtering, etc """ # pre cleaning self.WL_attributes.clear() # using regex compile to avoid re execution over many attributes mask = self.WV_search.text() case = 0 if self.WC_cases.isChecked() else re.IGNORECASE re_start = re.compile(r'^%s.*?' % mask, case) re_cont = re.compile(r'.*?%s.*?' % mask, case) # getting the four different lists obj_start = set([at for at in self.objs_attr if re_start.search(at)]) shp_start = set([at for at in self.shps_attr if re_start.search(at)]) # if type filtering is one we only extract the wanted attribute's type if self.WC_types.isChecked(): obj_start = set([at for at in obj_start if at.type in self.ctx_wide[self.WL_attrtype.currentText().lower()]]) shp_start = set([at for at in shp_start if at.type in self.ctx_wide[self.WL_attrtype.currentText().lower()]]) # finally adding the current sets if there is a mask we add the also the containing matches if mask: # getting contains filtering and type containers filtering obj_contains = obj_start.symmetric_difference(set([at for at in self.objs_attr if re_cont.search(at)])) shp_contains = shp_start.symmetric_difference(set([at for at in self.shps_attr if re_cont.search(at)])) if self.WC_types.isChecked(): obj_contains = set([at for at in obj_contains if at.type in self.ctx_wide[self.WL_attrtype.currentText().lower()]]) shp_contains = set([at for at in shp_contains if at.type in self.ctx_wide[self.WL_attrtype.currentText().lower()]]) # adding the sets self.add_set(obj_start, 'Obj attributes starting with') self.add_set(obj_contains, 'Obj attributes containing') self.add_set(shp_start, 'Shape attributes starting with') self.add_set(shp_contains, 'Shape attributes containing') else: self.add_set(obj_start, 'Object\'s attributes') self.add_set(shp_start, 'Shape\'s attributes') # and we select the first one if ever there is something in the list if self.WL_attributes.topLevelItemCount(): self.WL_attributes.setItemSelected(self.WL_attributes.topLevelItem(1), True)