def handle_dict_to_tree(self, root): """ change json dict to item """ if self.tree_top_item: root_item = self.window.treeWidget.invisibleRootItem() root_item.removeChild(self.tree_top_item) self.tree_top_item = None stack = [] item_stack = [] current_stack = 0 stack.append(root) self.total_time = root["total_time"] while stack: current = stack.pop() if current is None: current_stack = current_stack - 1 continue while len(item_stack) > current_stack: item_stack.pop() item = QTreeWidgetItem(None, [ current["function_name"], str(current["count"]), str(current["total_time"]), str(current["self_time"]), str(current["children_time"]) ]) current_total_time = current["total_time"] brush = get_brush(current_total_time, self.total_time) item.setBackground(0, brush) for column_index in [1, 2, 3, 4]: item.setTextAlignment(column_index, Qt.AlignRight) item.setBackground(column_index, brush) item.setFont(column_index, self.mono_space_font) if current_stack == 0: self.window.treeWidget.addTopLevelItem(item) self.tree_top_item = item else: item_stack[-1].addChild(item) item_stack.append(item) if "children" not in current: continue stack.append(None) current_stack = current_stack + 1 for child in current["children"][::-1]: stack.append(child)
def add_list_to_view(self): """ add list to view """ self.window.listWidget.invisibleRootItem().takeChildren() list_data = [] for value in self.list_dict.values(): list_data.append(value) for data in list_data: item_data = [ data["function_name"], (data["count"]), (data["total_time"]), (data["self_time"]), (data["children_time"]) ] item = QTreeWidgetItem(None, item_data) for column_index in [1, 2, 3, 4]: item.setTextAlignment(column_index, Qt.AlignRight) item.setBackground( column_index, get_brush(item_data[column_index], self.total_time)) item.setFont(column_index, self.mono_space_font) item.setData(column_index, Qt.DisplayRole, item_data[column_index]) self.window.listWidget.addTopLevelItem(item) self.window.listWidget.sortItems(2, Qt.DescendingOrder)
def handle_packet(self, packet, writer): """ 处理抓到的数据包 :parma packet: 需要处理分类的包 """ unkown = False try: # 如果暂停,则不对列表进行更新操作 if not self.pause_flag and packet.name == "Ethernet": protocol = None if self.packet_id == 1: self.start_timestamp = packet.time packet_time = packet.time - self.start_timestamp # 网络层 net = packet.payload network_layer = net.name version_add = "" # IPv4 if network_layer == "IP": source = packet[IP].src destination = packet[IP].dst self.counter["ipv4"] += 1 # IPv6 elif network_layer == "IPv6": source = packet[IPv6].src destination = packet[IPv6].dst version_add = "v6" self.counter["ipv6"] += 1 # ARP elif network_layer == "ARP": self.counter["arp"] += 1 protocol = network_layer source = packet[Ether].src destination = packet[Ether].dst if destination == "ff:ff:ff:ff:ff:ff": destination = "Broadcast" else: try: protocol = network_layer unkown = True except: # 其他协议不处理 return if network_layer != "ARP" and not unkown: # 传输层 trans = net.payload protocol = trans.name sport = None dport = None if protocol == "TCP": sport = packet[TCP].sport dport = packet[TCP].dport protocol += version_add self.counter["tcp"] += 1 elif protocol == "UDP": sport = packet[UDP].sport dport = packet[UDP].dport protocol += version_add self.counter["udp"] += 1 elif len(protocol) >= 4 and protocol[0:4] == "ICMP": protocol = "ICMP" + version_add self.counter["icmp"] += 1 else: return if trans.haslayer(HTTPRequest) or trans.haslayer( HTTPResponse): protocol = "HTTP" + version_add elif trans.haslayer(TLS): protocol = https_version[trans[TLS].version] if sport and dport: if sport in ports: protocol = ports[sport] + version_add elif dport in ports: protocol = ports[dport] + version_add item = QTreeWidgetItem(self.main_window.packet_tree) # 根据协议类型不同设置颜色 try: color = color_dict[protocol] except: color = "#ff80c0" for i in range(7): item.setBackground(i, QBrush(QColor(color))) # 添加行内容 item.setData(0, Qt.DisplayRole, self.packet_id) item.setText(1, f"{packet_time:.6f}") item.setText(2, source) item.setText(3, destination) item.setText(4, protocol) item.setData(5, Qt.DisplayRole, len(packet)) item.setText(6, packet.summary()) # 时间栏右对齐,为了格式化后不影响排序 # item.setTextAlignment(1, Qt.AlignRight) self.packet_id += 1 if writer: writer.write(packet) except: pass
def _updateAll(self, newGroup=None): try: self.chSelector.itemChanged.disconnect(self.itemChanged) self.sSelector.itemChanged.disconnect(self.itemChanged) except: pass if newGroup == '': return chStruct = self.chartData[newGroup] sStruct = self.chartData[newGroup].getColStructure() self.chSelector.clear() for ws in chStruct: gparent = QTreeWidgetItem(self.chSelector) gparent.setText(0, ws) gparent.setBackgroundColor(0, Qt.white) gparent.setFlags(Qt.ItemIsEnabled) for key in chStruct[ws]: parent = QTreeWidgetItem(gparent) parent.setText(0, key) if chStruct[ws][key][0] == True: dataNames = chStruct[ws][key][3] sColor = QColor(chStruct[ws][key][4]) sColor.setAlpha(100) parent.setBackgroundColor(0, sColor) else: dataNames = ','.join(chStruct[ws][key][2]) sColor = QColor(chStruct[ws][key][3]) sColor.setAlpha(100) parent.setBackgroundColor(0, sColor) parent.setText(1, dataNames) if chStruct[ws][key][1] == True: parent.setCheckState(0, Qt.Checked) else: parent.setCheckState(0, Qt.Unchecked) parent.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled) self.sSelector.clear() self.gSelector.clear() for ws in sStruct: firstChannel = sStruct[ws][0] isOneSignal = self.chartData[newGroup][firstChannel][ws][0] if isOneSignal: gparent = QTreeWidgetItem(self.sSelector) gparent.setText(0, ws) gparent.setFlags(Qt.ItemIsEnabled | Qt.ItemIsDragEnabled | Qt.ItemIsSelectable | Qt.ItemIsUserCheckable) if True: ## if chStruct['CH1'][ws][5] == True: gparent.setCheckState(0, Qt.Checked) else: gparent.setCheckState(0, Qt.Unchecked) for key in sStruct[ws]: parent = QTreeWidgetItem(gparent) parent.setText(0, key) if chStruct[key][ws][2] == True: parent.setCheckState(0, Qt.Checked) else: parent.setCheckState(0, Qt.Unchecked) parent.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled) sColor = QColor(chStruct[key][ws][4]) sColor.setAlpha(100) sGradient = QLinearGradient(0, 0, 100, 10) sGradient.setColorAt(0, sColor) sGradient.setColorAt(1, Qt.white) sBrush = QBrush(sGradient) sBrush.setStyle(Qt.LinearGradientPattern) sBrush.setColor(sColor) gparent.setBackground(0, sBrush) else: gparent = QTreeWidgetItem(self.gSelector) gparent.setText(0, ws) gparent.setFlags(Qt.ItemIsEnabled | Qt.ItemIsDropEnabled | Qt.ItemIsUserCheckable) if chStruct['CH1'][ws][5] == True: gparent.setCheckState(0, Qt.Checked) else: gparent.setCheckState(0, Qt.Unchecked) signalNames = chStruct[key][ws][2] sColor = QColor(chStruct[key][ws][3]) sColor.setAlpha(100) gparent.setBackgroundColor(0, sColor) for signal in signalNames: parent = QTreeWidgetItem(gparent) parent.setText(0, signal) parent.setFlags(Qt.ItemIsEnabled) for key in sStruct[signal]: sColor = QColor(chStruct[key][signal][4]) sColor.setAlpha(100) parent.setBackgroundColor(0, sColor) break self.chSelector.itemChanged.connect(self.itemChanged) self.sSelector.itemChanged.connect(self.itemChanged) self.curSelect = None
def update_xsd_files_information(self): ''' update 函数直接与vs类关联,得到所有的文件类,这样只用update就能更新最新 :return: ''' if self.vsp == None: QMessageBox.information(self.main_window, "提示", "没有加载项目,无法获得xsd文件") return self.xsd_files_item = [] try: filenames = list(self.vsp.relative_path_XVI_dict.keys()) filenames = sorted(filenames) tw = self.main_window_ui.xsdFileTreeWidget index = 0 tw.clear() # TODO :这里让用户自选显示的顺序和内容 tw.setHeaderLabels(self.xsd_file_headers) tw.setColumnCount(len(self.xsd_file_headers)) file_name_item_dict = {} root = QTreeWidgetItem(tw) root.setText(0, self.vsp.local_project_dir) for file in filenames: l = [root] trees = file.split("/")[1:] # 第一个元素是“”,是root # 按照文件目录树进行,如果能够获取到子集就开始增加内容,否则增加child for i in range(len(trees) + 1): # 对于剩下的child node_name = "/".join(trees[:i]) try: # 尝试找到这个node,如果没有就创建 node = file_name_item_dict[node_name] except: # 创建child node,加入到字典中 node = QTreeWidgetItem(l[-1]) index += 1 node.setText(0, node_name.split("/")[-1]) file_name_item_dict[node_name] = node try: # 这里面的每个显示都是直接从xvi信息中获取,然后设置 # 这里如果node能够获取到文件,就开始加上文件信息,否则就是空的只作为有内容的node的parent xvi_item = self.vsp.relative_path_XVI_dict[ "/" + node_name] for i in range(len(self.xsd_file_contents)): #if getattr(xvi_item,"type","") == "":xvi_item.type = Type.Origin node.setText( i + 1, getattr(xvi_item, self.xsd_file_contents[i], "None")) if self.xsd_file_contents[i] == "status": # 这些set使用index进行的,所以先判断是不是在相应的列,也可改成名称为key,value为index node.setBackground( i + 1, QBrush(STATUS_COLOR[xvi_item.status])) if self.xsd_file_contents[i] == "type": try: node.setBackground( i + 1, QBrush(Type_Color[xvi_item.type])) except: # 这个是应对之前没有这个attr的项目,之后可以删除此 node.setBackground( i + 1, QBrush(Type_Color[Type.Origin])) if self.xsd_file_contents[i] == "mark_text": node.setText( i + 1, getattr(xvi_item, self.xsd_file_contents[i], "None")) try: node.setBackground( i + 1, QBrush(QColor( xvi_item.mark_color))) except: traceback.print_exc() node.setBackground( i + 1, QBrush(QColor(255, 255, 255))) node.file_path = "/" + node_name # 这里强行给这个实例增加了属性,之后直接调用,这里相当于继承 self.xsd_files_item.append(node) except KeyError: pass except: traceback.print_exc() l.append(node) l[-2].addChild(l[-1]) def load_column_status(): # 这里设置列的长度,首先用一个变量去存储这些列,存储在save project中 try: info = self.vsp.xsd_tree_widget_param["column_status"] for i in range(tw.columnCount()): tw.setColumnWidth(i, info[i]) except: return load_column_status() # 把node的信息存储一下!用于接下来搞node的扩展 self.tree_node_widget_item_info = file_name_item_dict # 这里我们用一个与tree node widget 。。key一模一样的字典去存储node的expand信息,之后 # 用这个key去获取node以及node的expand信息然后修改! def expand(): # 存储各个列的expand情况 try: info = self.vsp.xsd_tree_widget_param["expanded_status"] tw.expandAll() for key in self.tree_node_widget_item_info.keys(): try: self.tree_node_widget_item_info[key].setExpanded( info[key]) except: continue except: pass expand() print(tw.rootIndex()) return except: QMessageBox.information(self.main_window, "提示", "No xsd files.") traceback.print_exc() return