| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from PyQt6.QtWidgets import * |
| 9 |
from PyQt6.QtGui import * |
| 10 |
from PyQt6.QtCore import * |
| 11 |
from PyQt6.QtSvgWidgets import * |
| 12 |
import os |
| 13 |
import sys |
| 14 |
|
| 15 |
if __name__ == '__main__': |
| 16 |
import sys |
| 17 |
import os |
| 18 |
app_dir = os.path.dirname(os.path.realpath(__file__)) |
| 19 |
os.chdir(app_dir) |
| 20 |
sys.path.insert(0, os.path.abspath('..')) |
| 21 |
os.chdir(os.path.abspath('..')) |
| 22 |
|
| 23 |
from backend.plugins.PluginBase import PluginProcess |
| 24 |
|
| 25 |
class ClassGraphProcess(PluginProcess): |
| 26 |
def __init__(self, wdir, rq): |
| 27 |
PluginProcess.__init__(self, wdir, rq) |
| 28 |
self.name = 'call graph process' |
| 29 |
|
| 30 |
def parse_result(self, text, sig): |
| 31 |
return [text] |
| 32 |
|
| 33 |
|
| 34 |
class ClassGraphWidget(QWidget): |
| 35 |
def __init__(self, parent, cmd_func, cmd_id, cmd_opt): |
| 36 |
QWidget.__init__(self, parent) |
| 37 |
self.is_busy = False |
| 38 |
self.is_done = False |
| 39 |
self.cmd_func = cmd_func |
| 40 |
self.cmd_id = cmd_id |
| 41 |
self.cmd_opt = cmd_opt |
| 42 |
self.is_debug = os.getenv('SEASCOPE_CLASS_GRAPH_VIEW_DEBUG') |
| 43 |
|
| 44 |
self.vlay1 = QVBoxLayout() |
| 45 |
self.setLayout(self.vlay1) |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
self.lbl = QLabel() |
| 50 |
self.vlay1.addWidget(self.lbl) |
| 51 |
|
| 52 |
self.vlay2 = QVBoxLayout() |
| 53 |
self.scrolla = QScrollArea() |
| 54 |
self.scrolla.setLayout(self.vlay2) |
| 55 |
self.vlay1.addWidget(self.scrolla) |
| 56 |
|
| 57 |
def startQuery(self, req, dname, proj_type, inx): |
| 58 |
if self.is_done: |
| 59 |
return |
| 60 |
|
| 61 |
name = req if req else dname |
| 62 |
self.lbl.setText(['derived', 'base'][inx] + '(' + name + ')') |
| 63 |
|
| 64 |
tool_path = os.path.join('tools', 'ClassGraph.py') |
| 65 |
pargs = [sys.executable, tool_path] |
| 66 |
if inx == 1: |
| 67 |
pargs += ['-b'] |
| 68 |
pargs += ['-d', dname] |
| 69 |
if proj_type: |
| 70 |
pargs += ['-t', proj_type] |
| 71 |
if req: |
| 72 |
pargs += [req] |
| 73 |
sig_res = ClassGraphProcess('.', None).run_query_process(pargs, req) |
| 74 |
sig_res[0].connect(self.clgraph_add_result) |
| 75 |
self.is_busy = True |
| 76 |
self.show_progress_bar() |
| 77 |
|
| 78 |
def set_current(self, btn): |
| 79 |
inx = self.bgrp.id(btn) |
| 80 |
|
| 81 |
|
| 82 |
if inx == 0: |
| 83 |
print(self.svgw.renderer().defaultSize()) |
| 84 |
self.svgw.setMinimumSize(0, 0) |
| 85 |
self.svgw.setMinimumSize(self.svgw.sizeHint()) |
| 86 |
|
| 87 |
print(self.scrolla.sizeHint()) |
| 88 |
if inx == 1: |
| 89 |
print(self.svgw.renderer().defaultSize()) |
| 90 |
self.svgw.setMinimumSize(0, 0) |
| 91 |
|
| 92 |
print(self.scrolla.sizeHint()) |
| 93 |
if inx == 2: |
| 94 |
print(self.svgw.renderer().defaultSize()) |
| 95 |
self.svgw.setMinimumSize(0, 0) |
| 96 |
self.svgw.resize(self.scrolla.size()) |
| 97 |
|
| 98 |
print(self.scrolla.sizeHint()) |
| 99 |
|
| 100 |
def add_buttons(self, hlay): |
| 101 |
self.bgrp = QButtonGroup() |
| 102 |
self.bgrp.buttonClicked.connect(self.set_current) |
| 103 |
self.bgrp.setExclusive(True) |
| 104 |
for inx in range(3): |
| 105 |
btn = QToolButton() |
| 106 |
btn.setText(str(inx)) |
| 107 |
btn.setToolTip(str(inx)) |
| 108 |
|
| 109 |
btn.setCheckable(True) |
| 110 |
self.bgrp.addButton(btn, inx) |
| 111 |
hlay.addWidget(btn) |
| 112 |
|
| 113 |
def clgraph_add_result(self, req, res): |
| 114 |
if self.is_debug: |
| 115 |
print(res) |
| 116 |
self.is_busy = False |
| 117 |
self.is_done = True |
| 118 |
self.remove_progress_bar() |
| 119 |
|
| 120 |
self.svgw = QSvgWidget() |
| 121 |
self.scrolla.setWidget(self.svgw) |
| 122 |
self.svgw.load(QByteArray(res[0].encode())) |
| 123 |
|
| 124 |
|
| 125 |
sz = self.svgw.sizeHint() |
| 126 |
scale = 1 |
| 127 |
if sz.width() > 1024: |
| 128 |
scale = 0.8 |
| 129 |
min_w = int(sz.width() * scale) |
| 130 |
min_h = int(sz.height() * scale) |
| 131 |
self.svgw.setMinimumSize(min_w, min_h) |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
def show_progress_bar(self): |
| 136 |
self.pbar = QProgressBar(self.scrolla) |
| 137 |
self.pbar.setMinimum(0) |
| 138 |
self.pbar.setMaximum(0) |
| 139 |
self.pbar.show() |
| 140 |
|
| 141 |
def remove_progress_bar(self): |
| 142 |
if self.pbar: |
| 143 |
self.pbar.hide() |
| 144 |
self.pbar.setParent(None) |
| 145 |
self.pbar = None |
| 146 |
|
| 147 |
class ClassGraphWindow(QMainWindow): |
| 148 |
parent = None |
| 149 |
|
| 150 |
def __init__(self, req, dname, proj_type, cmd_func, cmd_args, cmd_opt): |
| 151 |
QMainWindow.__init__(self, ClassGraphWindow.parent) |
| 152 |
self.req = req |
| 153 |
self.dname = dname |
| 154 |
self.proj_type = proj_type |
| 155 |
|
| 156 |
if req: |
| 157 |
self.setWindowTitle(req) |
| 158 |
else: |
| 159 |
self.setWindowTitle(dname) |
| 160 |
|
| 161 |
self.setFont(QFont("San Serif", 8)) |
| 162 |
|
| 163 |
w = QWidget() |
| 164 |
self.setCentralWidget(w) |
| 165 |
self.vlay = QVBoxLayout() |
| 166 |
w.setLayout(self.vlay) |
| 167 |
|
| 168 |
self.sw = QStackedWidget() |
| 169 |
|
| 170 |
self.hlay = QHBoxLayout() |
| 171 |
self.vlay.addLayout(self.hlay) |
| 172 |
self.vlay.addWidget(self.sw) |
| 173 |
|
| 174 |
self.bgrp = QButtonGroup() |
| 175 |
self.bgrp.buttonClicked.connect(self.set_current) |
| 176 |
self.bgrp.setExclusive(True) |
| 177 |
|
| 178 |
self.btn = [] |
| 179 |
self.ctree = [] |
| 180 |
for inx in range(len(cmd_args)): |
| 181 |
|
| 182 |
cmd = cmd_args[inx] |
| 183 |
|
| 184 |
btn = QToolButton() |
| 185 |
btn.setText(cmd[1]) |
| 186 |
btn.setToolTip(cmd[2]) |
| 187 |
|
| 188 |
btn.setCheckable(True) |
| 189 |
self.bgrp.addButton(btn, inx) |
| 190 |
self.hlay.addWidget(btn) |
| 191 |
|
| 192 |
ct = ClassGraphWidget(self, cmd_func, cmd[0], cmd_opt) |
| 193 |
self.sw.addWidget(ct) |
| 194 |
|
| 195 |
self.btn.append(btn) |
| 196 |
self.ctree.append(ct) |
| 197 |
self.hlay.addStretch(0) |
| 198 |
self.set_current(self.btn[0]) |
| 199 |
|
| 200 |
def set_current(self, btn): |
| 201 |
inx = self.bgrp.id(btn) |
| 202 |
self.btn[inx].setChecked(True) |
| 203 |
self.sw.setCurrentIndex(inx) |
| 204 |
ct = self.ctree[inx] |
| 205 |
ct.setFocus() |
| 206 |
|
| 207 |
ct.startQuery(self.req, self.dname, self.proj_type, inx) |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
def create_page(req, dname, proj_type, cmd_func, cmd_args, cmd_opt): |
| 212 |
w = ClassGraphWindow(req, dname, proj_type, cmd_func, cmd_args, cmd_opt) |
| 213 |
w.resize(900, 600) |
| 214 |
w.show() |
| 215 |
return w |
| 216 |
|
| 217 |
if __name__ == '__main__': |
| 218 |
import optparse |
| 219 |
usage = "usage: %prog (-d <code_dir/file> | -t <prj_type>) [symbol]" |
| 220 |
op = optparse.OptionParser(usage=usage) |
| 221 |
op.add_option("-d", "--codedir", dest="code_dir", help="Code dir", metavar="CODE_DIR") |
| 222 |
op.add_option("-t", "--type", dest="prj_type", help="project type: idutils|gtags|cscope", metavar="PRJ_TYPE") |
| 223 |
(options, args) = op.parse_args() |
| 224 |
|
| 225 |
|
| 226 |
if not options.code_dir: |
| 227 |
print('Specify -d', file=sys.stderr) |
| 228 |
sys.exit(-1) |
| 229 |
dname = options.code_dir |
| 230 |
if not os.path.exists(dname): |
| 231 |
print('"%s": does not exist' % dname, file=sys.stderr) |
| 232 |
sys.exit(-2) |
| 233 |
wdir = dname |
| 234 |
if not os.path.isdir(wdir): |
| 235 |
wdir = os.path.dirname(wdir) |
| 236 |
|
| 237 |
|
| 238 |
sym = None |
| 239 |
if len(args): |
| 240 |
if len(args) != 1: |
| 241 |
print('Please specify only one symbol', file=sys.stderr) |
| 242 |
sys.exit(-3) |
| 243 |
sym = args[0] |
| 244 |
|
| 245 |
|
| 246 |
ptype = options.prj_type |
| 247 |
if ptype: |
| 248 |
if not sym: |
| 249 |
print('-t option needs sepficfying symbol', file=sys.stderr) |
| 250 |
sys.exit(-4) |
| 251 |
if not os.path.isdir(dname): |
| 252 |
print('-t option needs codedir to be a directory', file=sys.stderr) |
| 253 |
sys.exit(-5) |
| 254 |
pcmd = None |
| 255 |
if ptype and os.path.isdir(dname): |
| 256 |
prj_list = [ |
| 257 |
['idutils', 'ID' ], |
| 258 |
['gtags', 'GRTAGS' ], |
| 259 |
['cscope', 'cscope.out' ], |
| 260 |
['grep', '' ], |
| 261 |
] |
| 262 |
for p in prj_list: |
| 263 |
if p[0] == ptype: |
| 264 |
if not os.path.exists(os.path.join(dname, p[1])): |
| 265 |
print('Failed to find "%s" in directory "%s"' % (p[1], dname), file=sys.stderr) |
| 266 |
sys.exit(-6) |
| 267 |
|
| 268 |
app = QApplication(sys.argv) |
| 269 |
cmd_args = [ |
| 270 |
['CLGRAPH', 'D', 'Derived classes'], |
| 271 |
['CLGRAPH', 'B', 'Base classes'] |
| 272 |
] |
| 273 |
|
| 274 |
w = create_page(sym, dname, ptype, None, cmd_args, None) |
| 275 |
|
| 276 |
sys.exit(app.exec()) |