| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from PyQt6.QtWidgets import * |
| 9 |
from PyQt6.QtGui import * |
| 10 |
from PyQt6.QtCore import * |
| 11 |
|
| 12 |
class CallTreeWidgetItem(QTreeWidgetItem): |
| 13 |
def __init__(self, li): |
| 14 |
QTreeWidgetItem.__init__(self, li) |
| 15 |
func = li[0] |
| 16 |
if (func == "<global>"): |
| 17 |
self.setChildIndicatorPolicy(QTreeWidgetItem.ChildIndicatorPolicy.DontShowIndicatorWhenChildless) |
| 18 |
else: |
| 19 |
self.setChildIndicatorPolicy(QTreeWidgetItem.ChildIndicatorPolicy.ShowIndicator) |
| 20 |
self.is_done = False |
| 21 |
|
| 22 |
def set_no_children(self): |
| 23 |
self.setChildIndicatorPolicy(QTreeWidgetItem.ChildIndicatorPolicy.DontShowIndicatorWhenChildless) |
| 24 |
|
| 25 |
def column_val(self, col): |
| 26 |
return str(self.data(col, Qt.ItemDataRole.DisplayRole)) |
| 27 |
|
| 28 |
def add_result(self, res): |
| 29 |
self.is_done = True |
| 30 |
res_list = [] |
| 31 |
ret_val = True |
| 32 |
for line in res: |
| 33 |
if (line[0] == "<global>"): |
| 34 |
continue |
| 35 |
if (line[0] == self.column_val(0)): |
| 36 |
if (line [1] == self.column_val(1) and line[2] == self.column_val(2)): |
| 37 |
continue |
| 38 |
self.addChild(CallTreeWidgetItem(line)) |
| 39 |
if (self.childCount() == 0): |
| 40 |
self.set_no_children() |
| 41 |
|
| 42 |
self.treeWidget().resizeColumnToContents(0) |
| 43 |
self.treeWidget().resizeColumnToContents(1) |
| 44 |
self.treeWidget().resizeColumnToContents(2) |
| 45 |
self.treeWidget().resizeColumnToContents(3) |
| 46 |
|
| 47 |
|
| 48 |
return ret_val |
| 49 |
|
| 50 |
class CallTreeWidget(QTreeWidget): |
| 51 |
sig_show_file_line = pyqtSignal(str, int) |
| 52 |
|
| 53 |
def __init__(self, parent, cmd_func, cmd_id, cmd_opt, hint_file): |
| 54 |
QTreeWidget.__init__(self, parent) |
| 55 |
self.is_busy = False |
| 56 |
self.cmd_func = cmd_func |
| 57 |
self.cmd_id = cmd_id |
| 58 |
self.cmd_opt = cmd_opt |
| 59 |
self.hint_file = hint_file |
| 60 |
|
| 61 |
self.itemExpanded.connect(self.ctree_itemExpanded) |
| 62 |
self.itemActivated.connect(self.ctree_itemActivated) |
| 63 |
|
| 64 |
self.setExpandsOnDoubleClick(False) |
| 65 |
self.setColumnCount(4) |
| 66 |
self.setHeaderLabels(['Tag', 'File', 'Line', 'Text']) |
| 67 |
|
| 68 |
self.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection) |
| 69 |
|
| 70 |
self.setMinimumWidth(800) |
| 71 |
self.setMinimumHeight(500) |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
self.setFont(QFont("San Serif", 8)) |
| 77 |
|
| 78 |
|
| 79 |
self.setAllColumnsShowFocus(True) |
| 80 |
|
| 81 |
def add_root(self, name): |
| 82 |
parent = CallTreeWidgetItem([name, '', '', '']) |
| 83 |
self.addTopLevelItem(parent) |
| 84 |
parent.setExpanded(True) |
| 85 |
|
| 86 |
def mousePressEvent(self, m_ev): |
| 87 |
QTreeWidget.mousePressEvent(self, m_ev) |
| 88 |
if (m_ev.button() == Qt.MouseButton.RightButton): |
| 89 |
self.last_minx = self.indexAt(m_ev.pos()) |
| 90 |
|
| 91 |
def ctree_itemActivated(self, item, col): |
| 92 |
filename = item.column_val(1) |
| 93 |
try: |
| 94 |
line = int(item.column_val(2)) |
| 95 |
except: |
| 96 |
return |
| 97 |
self.sig_show_file_line.emit(filename, line) |
| 98 |
|
| 99 |
def ctree_itemExpanded(self, item): |
| 100 |
if (item.is_done): |
| 101 |
return |
| 102 |
tag = str(item.data(0, Qt.ItemDataRole.DisplayRole)) |
| 103 |
if str(item.data(1, Qt.ItemDataRole.DisplayRole)) == '': |
| 104 |
opt = self.cmd_opt |
| 105 |
else: |
| 106 |
opt = None |
| 107 |
if (self.is_busy): |
| 108 |
return |
| 109 |
self.is_busy = True |
| 110 |
self.pbar = QProgressBar(self) |
| 111 |
self.pbar.setMinimum(0) |
| 112 |
self.pbar.setMaximum(0) |
| 113 |
self.pbar.show() |
| 114 |
|
| 115 |
|
| 116 |
rquery = {} |
| 117 |
rquery['cmd'] = self.cmd_id |
| 118 |
rquery['req'] = tag |
| 119 |
rquery['opt'] = opt |
| 120 |
hfile = str(item.data(1, Qt.ItemDataRole.DisplayRole)) |
| 121 |
if hfile == '': |
| 122 |
hfile = self.hint_file |
| 123 |
rquery['hint_file'] = hfile |
| 124 |
sig_res = self.cmd_func(rquery) |
| 125 |
self.query_item = item |
| 126 |
sig_res[0].connect(self.ctree_add_result) |
| 127 |
|
| 128 |
def ctree_add_result(self, req, res): |
| 129 |
self.query_item.add_result(res) |
| 130 |
self.is_busy = False |
| 131 |
if self.pbar: |
| 132 |
self.pbar.setParent(None) |
| 133 |
self.pbar = None |
| 134 |
|
| 135 |
|
| 136 |
class CallTreeWindow(QMainWindow): |
| 137 |
sig_show_file_line = pyqtSignal(str, int) |
| 138 |
parent = None |
| 139 |
|
| 140 |
def __init__(self, req, cmd_func, cmd_args, cmd_opt, hint_file): |
| 141 |
QMainWindow.__init__(self, CallTreeWindow.parent) |
| 142 |
self.req = req |
| 143 |
|
| 144 |
self.setWindowTitle(req) |
| 145 |
|
| 146 |
self.setFont(QFont("San Serif", 8)) |
| 147 |
|
| 148 |
w = QWidget() |
| 149 |
self.setCentralWidget(w) |
| 150 |
self.vlay = QVBoxLayout() |
| 151 |
w.setLayout(self.vlay) |
| 152 |
|
| 153 |
self.sw = QStackedWidget() |
| 154 |
|
| 155 |
self.hlay = QHBoxLayout() |
| 156 |
self.vlay.addLayout(self.hlay) |
| 157 |
self.vlay.addWidget(self.sw) |
| 158 |
|
| 159 |
self.bgrp = QButtonGroup() |
| 160 |
self.bgrp.buttonClicked.connect(self.set_current) |
| 161 |
self.bgrp.setExclusive(True) |
| 162 |
|
| 163 |
self.btn = [] |
| 164 |
self.ctree = [] |
| 165 |
for inx in range(len(cmd_args)): |
| 166 |
|
| 167 |
cmd = cmd_args[inx] |
| 168 |
|
| 169 |
btn = QToolButton() |
| 170 |
btn.setText(cmd[1]) |
| 171 |
btn.setToolTip(cmd[2]) |
| 172 |
|
| 173 |
btn.setCheckable(True) |
| 174 |
self.bgrp.addButton(btn, inx) |
| 175 |
self.hlay.addWidget(btn) |
| 176 |
|
| 177 |
ct = CallTreeWidget(self, cmd_func, cmd[0], cmd_opt, hint_file) |
| 178 |
ct.sig_show_file_line.connect(self.sig_show_file_line) |
| 179 |
self.sw.addWidget(ct) |
| 180 |
|
| 181 |
self.btn.append(btn) |
| 182 |
self.ctree.append(ct) |
| 183 |
self.hlay.addStretch(0) |
| 184 |
self.set_current(self.btn[0]) |
| 185 |
|
| 186 |
def set_current(self, btn): |
| 187 |
inx = self.bgrp.id(btn) |
| 188 |
self.btn[inx].setChecked(True) |
| 189 |
self.sw.setCurrentIndex(inx) |
| 190 |
ct = self.ctree[inx] |
| 191 |
if ct.topLevelItemCount() == 0: |
| 192 |
ct.add_root(self.req) |
| 193 |
ct.setFocus() |
| 194 |
|
| 195 |
def ctree_show_file_line(filename, line): |
| 196 |
parent = CallTreeWindow.parent |
| 197 |
parent.raise_() |
| 198 |
parent.activateWindow() |
| 199 |
parent.show_file_line(filename, line) |
| 200 |
|
| 201 |
def create_page(req, cmd_func, cmd_args, cmd_opt, hint_file): |
| 202 |
w = CallTreeWindow(req, cmd_func, cmd_args, cmd_opt, hint_file) |
| 203 |
w.sig_show_file_line.connect(ctree_show_file_line) |
| 204 |
w.show() |