| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
import os |
| 9 |
import re |
| 10 |
import array |
| 11 |
|
| 12 |
from PyQt6 import QtWidgets, QtGui |
| 13 |
from PyQt6.QtWidgets import * |
| 14 |
from PyQt6.QtGui import * |
| 15 |
from PyQt6.QtCore import * |
| 16 |
|
| 17 |
from .EdView import EditorView |
| 18 |
|
| 19 |
|
| 20 |
class ContextEditorView(EditorView): |
| 21 |
sig_dblclick = pyqtSignal() |
| 22 |
|
| 23 |
def __init__(self, parent = None): |
| 24 |
EditorView.__init__(self, parent) |
| 25 |
|
| 26 |
self.setCaretLineBackgroundColor(QtGui.QColor("#ffaa7f")) |
| 27 |
|
| 28 |
def mouseReleaseEvent(self, ev): |
| 29 |
super(EditorView, self).mouseReleaseEvent(ev) |
| 30 |
|
| 31 |
def mousePressEvent(self, ev): |
| 32 |
pass |
| 33 |
|
| 34 |
def keyPressEvent(self, ev): |
| 35 |
pass |
| 36 |
|
| 37 |
def contextMenuEvent(self, ev): |
| 38 |
pass |
| 39 |
|
| 40 |
def mouseDoubleClickEvent(self, ev): |
| 41 |
self.sig_dblclick.emit() |
| 42 |
|
| 43 |
def show_file_line(self, fname, line): |
| 44 |
self.open_file(fname) |
| 45 |
self.goto_line(int(line)) |
| 46 |
self.show() |
| 47 |
|
| 48 |
class codeResultView(QTextBrowser): |
| 49 |
sig_result_view_openfile = pyqtSignal(str, int) |
| 50 |
def __init__(self, parent = None): |
| 51 |
QTextBrowser.__init__(self) |
| 52 |
self.setReadOnly(True) |
| 53 |
|
| 54 |
def showResultList(self, sym, res): |
| 55 |
self.res = res |
| 56 |
richstr = '<div align="center">' |
| 57 |
richstr += '<span style="font-size:10px;color:#990000;">Query of < <strong><span style="color:#000099;">' |
| 58 |
richstr += str(sym) |
| 59 |
richstr += '</span></strong></span> <span style="font-size:10px;color:#990000;"> > list</span>' |
| 60 |
richstr += '<div align="left" style="line-height:2px">' |
| 61 |
richstr += '<hr />' |
| 62 |
for itm in res: |
| 63 |
filename = itm[1] |
| 64 |
linenum = itm[2] |
| 65 |
context = itm[3] |
| 66 |
richstr += 'Line <span style="font-size:10px;color:#6600CC;">' |
| 67 |
richstr += str(linenum) |
| 68 |
richstr += '</span> of <em><a href="' |
| 69 |
richstr += str(filename) |
| 70 |
richstr += '#' |
| 71 |
richstr += str(linenum) |
| 72 |
richstr += '">' |
| 73 |
richstr += str(filename) |
| 74 |
richstr += '</a>' |
| 75 |
richstr += '</span></span></em>' |
| 76 |
richstr += '<pre style="line-height:0px"><span style="font-size:10px;color:#006600;line-height:2px;">' |
| 77 |
richstr += str(context) |
| 78 |
richstr += '</span></pre>' |
| 79 |
richstr += '</div></div>' |
| 80 |
|
| 81 |
self.setHtml(richstr) |
| 82 |
self.setReadOnly(True) |
| 83 |
self.anchorClicked.connect(self.anchorClicked_ev) |
| 84 |
|
| 85 |
def anchorClicked_ev(self, qurl): |
| 86 |
urlstr = qurl.toString() |
| 87 |
urlinfo = urlstr.split('#') |
| 88 |
fname = str(urlinfo[0]) |
| 89 |
linen = int(urlinfo[1]) |
| 90 |
self.sig_result_view_openfile.emit(fname, int(linen)) |
| 91 |
|
| 92 |
def setSource(self, qurl): |
| 93 |
self.setText('') |
| 94 |
self.anchorClicked.disconnect(self.anchorClicked_ev) |
| 95 |
|
| 96 |
|
| 97 |
class CodeContextViewPage(QFrame): |
| 98 |
def __init__(self, parent = None): |
| 99 |
QFrame.__init__(self) |
| 100 |
self.vlay = QVBoxLayout() |
| 101 |
|
| 102 |
self.cv_filetitle = QLabel() |
| 103 |
self.cv = ContextEditorView() |
| 104 |
self.resv = codeResultView() |
| 105 |
self.vlay.addWidget(self.cv_filetitle) |
| 106 |
self.vlay.addWidget(self.cv) |
| 107 |
self.vlay.addWidget(self.resv) |
| 108 |
|
| 109 |
self.resv.show() |
| 110 |
self.cv.hide() |
| 111 |
self.cv_filetitle.hide() |
| 112 |
|
| 113 |
self.setLayout(self.vlay) |
| 114 |
|
| 115 |
def showFileView(self, fname, line, sym, font): |
| 116 |
self.filename = fname |
| 117 |
self.linenum = line |
| 118 |
t = '%s:%s %s' % (fname, line, sym) |
| 119 |
self.cv_filetitle.setText(t) |
| 120 |
self.cv_filetitle.show() |
| 121 |
|
| 122 |
cv = self.cv |
| 123 |
cv.set_lexer(fname) |
| 124 |
cv.set_font(font) |
| 125 |
cv.show_line_number_cb(True) |
| 126 |
cv.show_file_line(fname, line) |
| 127 |
cv.show() |
| 128 |
|
| 129 |
|
| 130 |
class CodeContextViewManager(QTabWidget): |
| 131 |
sig_codecontext_showfile = pyqtSignal(str, int) |
| 132 |
def __init__(self, *args): |
| 133 |
QTabWidget.__init__(*(self, ) + args) |
| 134 |
self.setMovable(True) |
| 135 |
self.setTabsClosable(True) |
| 136 |
self.newContextViewPage() |
| 137 |
self.cur_query = '' |
| 138 |
self.cvp_cv_openedFile = False |
| 139 |
|
| 140 |
self.cv_font = "Monospace,10,-1,5,50,0,0,0,0,0" |
| 141 |
|
| 142 |
self.cvp.cv.sig_dblclick.connect(self.contextViewPage_openfile) |
| 143 |
self.cvp.resv.sig_result_view_openfile.connect(self.resultViewPage_openfile) |
| 144 |
|
| 145 |
def change_ev_font(self, font): |
| 146 |
if font == self.cv_font: |
| 147 |
return |
| 148 |
self.cv_font = font |
| 149 |
for inx in range(self.count()): |
| 150 |
cvp = self.widget(inx) |
| 151 |
cvp.cv.set_font(self.cv_font) |
| 152 |
|
| 153 |
def newContextViewPage(self): |
| 154 |
self.cvp = CodeContextViewPage() |
| 155 |
self.addTab(self.cvp, 'Context View') |
| 156 |
|
| 157 |
def set_cur_query(self, text): |
| 158 |
if self.cur_query == text: |
| 159 |
return False |
| 160 |
self.cur_query = text |
| 161 |
return True |
| 162 |
|
| 163 |
def showResult(self, sym, res): |
| 164 |
if sym != self.cur_query: |
| 165 |
return |
| 166 |
self.cvp.cv.clear() |
| 167 |
|
| 168 |
|
| 169 |
if (len(res) == 1): |
| 170 |
self.cvp.resv.hide() |
| 171 |
itm = res[0] |
| 172 |
self.cvp.showFileView(itm[1], itm[2], sym, self.cv_font) |
| 173 |
self.cvp_cv_openedFile = True |
| 174 |
else: |
| 175 |
self.cvp.cv.hide() |
| 176 |
self.cvp.cv_filetitle.hide() |
| 177 |
self.cvp.resv.showResultList(sym, res) |
| 178 |
self.cvp.resv.show() |
| 179 |
|
| 180 |
def contextViewPage_openfile(self): |
| 181 |
self.cvp.cv.clear() |
| 182 |
self.sig_codecontext_showfile.emit(self.cvp.filename, int(self.cvp.linenum)) |
| 183 |
|
| 184 |
def resultViewPage_openfile(self, filename, line): |
| 185 |
self.sig_codecontext_showfile.emit(filename, int(line)) |
| 186 |
|
| 187 |
def clear(self): |
| 188 |
self.cvp.cv_filetitle.clear() |
| 189 |
self.cvp.resv.clear() |
| 190 |
self.cvp.cv.clear() |
| 191 |
self.cvp.cv.hide() |