0 Star 2 Fork 1

Cadaqs/XStudio

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
XWndOutput.xcsm 23.57 KB
一键复制 编辑 原始数据 按行查看 历史
Cadaqs 提交于 2021-06-06 12:06 . 1.1.21.10657
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
class XWndOutput: QDockWidget
{
QScintilla _scicomm, _sciext;
static const long APPENTEVENTID = 1;
List<String> outputList = new List<String>();
List<String> outputListext = new List<String>();
Object outputlock = new Object();
static bool outputInFocus = false;
public static XWndOutput outputWnd;
Thread mergeThread = nilptr, mergeThreadext = nilptr;
QPushButton btnCls;
QWidget edtarea;
//Timer urlmon = new Timer();
bool [] output_flags = new bool[16];
int output_count = 2;
bool sciisHand = false;
QPushButton [] btnOutput = new QPushButton[16];
Pattern url_match = new Pattern("((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]" +
"+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|" +
"(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)");
public static void Output(String text, int id)
{
outputWnd.output(text, id);
}
public static bool inFocus(){
return outputWnd.hasFocus();
}
public static int registryOutput(String icon){
if (icon == nilptr || icon.length() == 0){
return -1;
}
return outputWnd.registry_Output(icon);
}
public int registry_Output(@NotNilptr String icon){
btnOutput[output_count].show();
icon = icon.replace("\\","/");
btnOutput[output_count].setStyleSheetString("qproperty-icon: url(" + icon +") off, url(" + icon +") on ;");
return output_count++;
}
public void output(String text, int id)
{
if (id <0 || id > 15){
return;
}
if (output_flags[id] == false){
return;
}
synchronized(outputlock) {
if (id < 2){
outputList.add(text);
if (mergeThread == nilptr) {
//this.Notify(APPENTEVENTID);
processOutputComm();
}
}else{
outputListext.add(text);
if (mergeThreadext == nilptr) {
//this.Notify(APPENTEVENTID);
processOutputExt();
}
}
}
}
public void Copy(){
if (_scicomm.isVisible()){
_scicomm.Copy();
}
}
public void clearComm(bool focus)
{
_scicomm.clear();
if (focus){
raise();
}
}
public void clearAll(bool focus)
{
_scicomm.clear();
_sciext.clear();
if (focus){
raise();
}
}
public void reconfig()
{
syntaxForOutput(_scicomm);
syntaxForOutput(_sciext);
}
/**
这里用到一个延迟加载的算法, LOG可能会输出上百万行, 高频率连续不断的刷新造成SCI无法及时处理,导致界面停止响应。
因此用一个单独的线程来对琐碎的log文本进行合并
但是合并时间不能太久,否则会造成输出卡顿
所以合并的时候需要计算合并的行数 然后得出合并花费的时间, 既刷新间隔,达到阈值后再显示,
阈值算法
阈值 = 显示所用时间 + 50毫秒 + 行数 / 100000 * 5 毫秒
经测试, 能有效处理百万行级别的log输出
滚动算法, 显示的最顶端行 + 可显示的行 如果小于最后行 说明被人为向上拉了, 则停止自动滚动, 反之自动滚动
*/
public void processOutputComm()
{
mergeThread = new Thread() {
void run() {
List<String> tmplist = nilptr;
synchronized(outputlock) {
tmplist = outputList;
outputList = new List<String>();
}
int interval = 50;
while(tmplist != nilptr) {
String finalstr = "";
List.Iterator<String> iter = tmplist.iterator();
while (iter.hasNext()) {
long start = _system_.currentTimeMillis();
int mgcount = 0;
while (iter.hasNext()) {
String str = iter.next();
finalstr = finalstr + str;
mgcount++;
if (mgcount > interval) {
mgcount = 0;
long curtime = _system_.currentTimeMillis();
if (curtime - start > interval) {
break;
}
}
}
runOnUi(new Runnable() {
String appstr = finalstr;
void run()override {
String _str = getContent();
if (_str != nilptr){
interval = appendTextAndScroll(_scicomm, _str);
}
}
String getContent(){
return appstr;
}
});
finalstr = "";
}
_system_.sleep(100);
synchronized(outputlock) {
if (outputList.size() == 0) {
mergeThread = nilptr;
break;
} else {
tmplist = outputList;
outputList = new List<String>();
}
}
}
}
};
mergeThread.start();
}
public int appendTextAndScroll(@NotNilptr QScintilla _sci,@NotNilptr String appstr){
long displaytime = _system_.currentTimeMillis();
bool bWrap = _sci.isWrap();
int cutcountline = 0;
if (bWrap == false){
int topline = _sci.getFirstVisibleLine();
int scrlines = _sci.getLinesOfDisplay();
topline += scrlines;
bool tobottom = true;
if (topline < _sci.countOfLine()) {
tobottom = false;
}
_sci.appendText(appstr);
cutcountline = _sci.countOfLine();
if (cutcountline > 50000){
long del_end_pos = _sci.getEndOfLine(10000);
_sci.selectAndReplace(0,del_end_pos,"");
}
if (tobottom) {
_sci.setFirstVisibleLine(cutcountline - scrlines);
}
}else{
cutcountline = _sci.countOfLine();
bool tobottom = _sci.getCurrentPosition() == _sci.getLength();
_sci.appendText(appstr);
cutcountline = _sci.countOfLine();
if (cutcountline > 50000){
long del_end_pos = _sci.getEndOfLine(10000);
_sci.selectAndReplace(0,del_end_pos,"");
cutcountline = _sci.countOfLine();
}
if (tobottom){
_sci.gotoPos(_sci.getLength());
}
}
displaytime = _system_.currentTimeMillis() - displaytime;
return displaytime + 50 + (cutcountline / 100000) * 5;
}
public void processOutputExt()
{
mergeThreadext = new Thread() {
void run() {
List<String> tmplist = nilptr;
synchronized(outputlock) {
tmplist = outputListext;
outputListext = new List<String>();
}
int interval = 50;
while(tmplist != nilptr) {
String finalstr = "";
List.Iterator<String> iter = tmplist.iterator();
while (iter.hasNext()) {
long start = _system_.currentTimeMillis();
int mgcount = 0;
while (iter.hasNext()) {
String str = iter.next();
finalstr = finalstr + str;
mgcount++;
if (mgcount > interval) {
mgcount = 0;
long curtime = _system_.currentTimeMillis();
if (curtime - start > interval) {
break;
}
}
}
runOnUi(new Runnable() {
String appstr = finalstr;
void run()override {
String _str = getContent();
if (_str != nilptr){
interval = appendTextAndScroll(_sciext, _str);
}
}
String getContent(){
return appstr;
}
});
finalstr = "";
}
_system_.sleep(100);
synchronized(outputlock) {
if (outputListext.size() == 0) {
mergeThreadext = nilptr;
break;
} else {
tmplist = outputListext;
outputListext = new List<String>();
}
}
}
}
};
mergeThreadext.start();
}
public void syntaxForOutput(@NotNilptr QScintilla _sci)
{
if (Setting.isDarkStyle()) {
syntaxForOutputDark(_sci);
return ;
}
_sci.sendEditor(QScintilla.SCI_SETCODEPAGE, QScintilla.SC_CP_UTF8);
//_sci.setWrap(true);
_sci.sendEditor(QScintilla.SCI_STYLESETBACK, QScintilla.STYLE_DEFAULT, 0xffffffff);
_sci.sendEditor(QScintilla.SCI_STYLESETFORE, QScintilla.STYLE_DEFAULT, 0xff222827);
_sci.sendEditor(QScintilla.SCI_STYLESETFORE, 75, 0xff222827);
_sci.sendEditor(QScintilla.SCI_STYLECLEARALL, 0, 0);
_sci.sendEditor(QScintilla.SCI_CLEARDOCUMENTSTYLE, 0, 0);
//_sci.sendEditor(QScintilla.STYLE_LINENUMBER, 1, 0);
bool bmac = (_system_.getPlatformId() == 2);
if (bmac == false) {
_sci.sendEditor(QScintilla.SCI_STYLESETFONT, QScintilla.STYLE_DEFAULT,"Consolas");
_sci.sendEditor(QScintilla.SCI_STYLESETSIZE, QScintilla.STYLE_DEFAULT,9);
} else {
_sci.sendEditor(QScintilla.SCI_STYLESETFONT, QScintilla.STYLE_DEFAULT,"Monaco");
_sci.sendEditor(QScintilla.SCI_STYLESETSIZE, QScintilla.STYLE_DEFAULT,11);
}
_sci.sendEditor(QScintilla.SCI_STYLECLEARALL, 0, 0);
_sci.sendEditor(QScintilla.SCI_SETEOLMODE, 1, 0);
_sci.sendEditor(QScintilla.SCI_SETSELBACK,1,0xfff1ebe5);
_sci.sendEditor(QScintilla.SCI_SETSELFORE,0,0);
_sci.sendEditor(QScintilla.SCI_SETMARGINTYPEN, 0, QScintilla.SC_MARGIN_NUMBER);
_sci.sendEditor(QScintilla.SCI_SETMARGINWIDTHN, 0, 65);
_sci.sendEditor(QScintilla.SCI_SETMARGINWIDTHN, 1, 5);
_sci.sendEditor(QScintilla.SCI_SETMARGINWIDTHN, 2, 0);
_sci.sendEditor(QScintilla.SCI_SETMARGINWIDTHN, 3, 0);
_sci.sendEditor(QScintilla.SCI_SETMARGINWIDTHN, 4, 0);
_sci.sendEditor(QScintilla.SCI_STYLESETBACK, QScintilla.STYLE_LINENUMBER, 0xffefefef);
_sci.sendEditor(QScintilla.SCI_STYLESETFORE, QScintilla.STYLE_LINENUMBER, 0xffaf912b);
_sci.sendEditor(QScintilla.SCI_SETMARGINLEFT, 0, 0);
_sci.sendEditor(QScintilla.SCI_SETCARETFORE,0xff000000,0);
_sci.sendEditor(QScintilla.SCI_SETCARETLINEVISIBLE, 1);
_sci.sendEditor(QScintilla.SCI_SETCARETLINEBACK, 0xffefefef);
_sci.sendEditor(QScintilla.SCI_SETTABWIDTH, 4);
_sci.setWrap(Setting.isOutputWrap());
}
public void syntaxForOutputDark(@NotNilptr QScintilla _sci)
{
_sci.sendEditor(QScintilla.SCI_SETCODEPAGE, QScintilla.SC_CP_UTF8);
//_sci.setWrap(true);
_sci.sendEditor(QScintilla.SCI_STYLESETBACK, QScintilla.STYLE_DEFAULT, 0xff262525);
_sci.sendEditor(QScintilla.SCI_STYLESETFORE, QScintilla.STYLE_DEFAULT, 0xffefefef);
_sci.sendEditor(QScintilla.SCI_STYLESETFORE, 75, 0xffefefef);
_sci.sendEditor(QScintilla.SCI_STYLECLEARALL, 0, 0);
_sci.sendEditor(QScintilla.SCI_CLEARDOCUMENTSTYLE, 0, 0);
//_sci.sendEditor(QScintilla.STYLE_LINENUMBER, 1, 0);
bool bmac = (_system_.getPlatformId() == 2);
if (bmac == false) {
_sci.sendEditor(QScintilla.SCI_STYLESETFONT, QScintilla.STYLE_DEFAULT,"Consolas");
_sci.sendEditor(QScintilla.SCI_STYLESETSIZE, QScintilla.STYLE_DEFAULT,9);
} else {
_sci.sendEditor(QScintilla.SCI_STYLESETFONT, QScintilla.STYLE_DEFAULT,"Monaco");
_sci.sendEditor(QScintilla.SCI_STYLESETSIZE, QScintilla.STYLE_DEFAULT,11);
}
_sci.sendEditor(QScintilla.SCI_STYLECLEARALL, 0, 0);
_sci.sendEditor(QScintilla.SCI_SETEOLMODE, 1, 0);
_sci.sendEditor(QScintilla.SCI_SETSELBACK,1,0xff3e4849);
_sci.sendEditor(QScintilla.SCI_SETSELFORE,0,0);
_sci.sendEditor(QScintilla.SCI_SETMARGINTYPEN, 0, QScintilla.SC_MARGIN_NUMBER);
_sci.sendEditor(QScintilla.SCI_SETMARGINWIDTHN, 0, 65);
_sci.sendEditor(QScintilla.SCI_SETMARGINWIDTHN, 1, 5);
_sci.sendEditor(QScintilla.SCI_SETMARGINWIDTHN, 2, 0);
_sci.sendEditor(QScintilla.SCI_SETMARGINWIDTHN, 3, 0);
_sci.sendEditor(QScintilla.SCI_SETMARGINWIDTHN, 4, 0);
_sci.sendEditor(QScintilla.SCI_STYLESETBACK, QScintilla.STYLE_LINENUMBER, 0xff262525);
_sci.sendEditor(QScintilla.SCI_STYLESETFORE, QScintilla.STYLE_LINENUMBER, 0xff666666);
_sci.sendEditor(QScintilla.SCI_SETMARGINLEFT, 0, 0);
_sci.sendEditor(QScintilla.SCI_SETCARETFORE,0xffffffff,0);
_sci.sendEditor(QScintilla.SCI_SETCARETLINEVISIBLE, 1);
_sci.sendEditor(QScintilla.SCI_SETCARETLINEBACK, 0xff202020);
_sci.setWrap(Setting.isOutputWrap());
}
public bool onClose()override
{
/*if (urlmon != nilptr){
urlmon.cancel();
urlmon = nilptr;
}*/
return true;
}
onClickListener extclick = new onClickListener(){
void onClick(@NotNilptr QObject obj, bool checked) {
int n = (int)obj.getTag();
output_flags[n] = checked;
switch_sci(false);
}
};
public void switch_sci(bool commMain){
bool comm_show, ext_show;
for (int i =0; i < 16; i ++){
if (output_flags[i]){
if (i < 2){
comm_show = true;
}else{
ext_show = true;
}
}
}
if (ext_show && !commMain){
_scicomm.hide();
_sciext.show();
btnOutput[0].setCheck(false);
btnOutput[1].setCheck(false);
}else{
_sciext.hide();
_scicomm.show();
if (output_flags[0] == false && output_flags[1] == false){
output_flags[0] = true;
btnOutput[0].setCheck(true);
}
for (int i =2; i < 16; i ++){
btnOutput[i].setCheck(false);
}
}
}
public void onAttach()
{
edtarea = (QWidget)attachByName(new QWidget(),"edtarea");
btnOutput[0] = (QPushButton)attachByName(new QPushButton(),"btnBuild");
btnOutput[1] = (QPushButton)attachByName(new QPushButton(),"btnDebug");
for (int i =2; i < 16; i ++){
btnOutput[i] = (QPushButton)attachByName(new QPushButton(),"btnDebug_" + i);
btnOutput[i].hide();
btnOutput[i].setTag(i);
btnOutput[i].setOnClickListener(extclick);
}
btnCls = (QPushButton)attachByName(new QPushButton(),"btnCls");
btnOutput[0].setOnClickListener(new onClickListener(){
void onClick(QObject obj, bool checked) {
output_flags[0] = checked;
switch_sci(true);
}
});
btnOutput[1].setOnClickListener(new onClickListener(){
void onClick(QObject obj, bool checked) {
output_flags[1] = checked;
switch_sci(true);
}
});
btnCls.setOnClickListener(new onClickListener(){
void onClick(QObject obj, bool checked) {
_scicomm.setText("");
_sciext.setText("");
}
});
_scicomm = new QScintilla();
_sciext = new QScintilla();
initSci(_scicomm);
initSci(_sciext);
outputWnd = this;
edtarea.setOnLayoutEventListener(new onLayoutEventListener() {
void onResize(QObject obj, int w, int h, int oldw, int oldh)override {
if (_scicomm != nilptr) {
_scicomm.resize(w, h );
}
if (_sciext != nilptr) {
_sciext.resize(w, h );
}
}
});
/*urlmon.schedule(new TimerTask(){
void run(){
runOnUi(new Runnable(){
void run()override{
testUrl();
}
});
}
}, 1000, -1);*/
switch_sci(true);
output_flags[1] = true;
btnOutput[1].setCheck(true);
}
public void initSci(@NotNilptr QScintilla _sci){
__nilptr_safe(edtarea);
if (_sci.create(edtarea)) {
syntaxForOutput(_sci);
_sci.setOnSciEventListener(new SciEventListener() {
void ON_DOUBLECLICK(@NotNilptr QScintilla sci,int position, int line, int modifiers) {
String lineText = sci.getText(line);
if (lineText.length() > 0) {
Project project = XWorkspace.workspace.getCurrentProject();
try{
if (project != nilptr){
ProjectPropInterface intel = project.getPropInterface();
if (intel != nilptr){
ICompileInfo icf = intel.parseOutputLine(sci, position, line, lineText);
if (icf != nilptr){
String tarfile = icf.getFile();
int tarline = icf.getLine();
int tarrow = icf.getRow();
String tarinfo = icf.getTips();
if (tarfile != nilptr && tarfile.length() > 0){
XSourceEditor.openForFileInfo(XWorkspace.workspace, tarfile, tarline, tarrow, icf.getTitle(), tarinfo);
}
}
}
}
}catch(Exception e){
}
}
}
});
_sci.setIndicStyle(1, QScintilla.INDIC_PLAIN);
_sci.sendEditor(QScintilla.SCI_INDICSETFORE,1,0xfff0d964);
_sci.setIndicCurrent(1);
QWidget viewport = _sci.viewPort();
if (viewport != nilptr){
viewport.setOnMouseEventListener(new onMouseEventListener() {
void onMouseMove(QObject obj, int Button, int x, int y, int flags, int source)override {
if (sciisHand) {
int pos = _sci.positionFromPoint(x, y);
int start = _sci.getIndicStart(1, pos);
int end = _sci.getIndicEnd(1, start);
int dic = _sci.IndicForPosition(start);
if (dic != 0 && start != 0 && end != 0) {
_sci.setCursor(QScintilla.cursorHand);
showTips(_sci, pos, "按下鼠标左键在浏览器中访问链接.");
}
}
}
void onMouseButtonPress(QObject obj, int Button, int x, int y, int flags, int source)override {
if (sciisHand) {
int pos = _sci.positionFromPoint(x, y);
int start = _sci.getIndicStart(1, pos);
int end = _sci.getIndicEnd(1, start);
int dic = _sci.IndicForPosition(start);
if (dic != 0 && start != 0 && end != 0) {
_sci.setSelect(start, end);
String current_Url = _sci.getSelectedText();
current_Url = current_Url.trim(true);
if (current_Url.length() > 0) {
openUrl(current_Url);
}
}
}
}
});
}
_sci.setOnKeyEventListener(new onKeyEventListener() {
bool onKeyPress(QObject obj, int key, bool repeat, int count, String text, int scanCode, int virtualKey, int modifier)override {
if (key == Constant.Key_Control && sciisHand == false) {
sciisHand = true;
testUrl(_sci);
}
return true;
}
bool onKeyRelease(QObject obj, int key, bool repeat, int count, String text, int scanCode, int virtualKey, int modifier)override {
if (key == Constant.Key_Control ) {
_sci.setCursor(QScintilla.cursorText);
sciisHand = false;
}
return true;
}
});
_sci.setOnFocusEventListener(new onFocusEventListener() {
void onFocusIn(QObject obj, bool focus, int reson) {
sciisHand = false;
XFindDlg.findinOutput = true;
_sci.setCursor(QScintilla.cursorText);
}
void onFocusOut(QObject obj, bool focus, int reson) {
sciisHand = false;
_sci.setCursor(QScintilla.cursorText);
}
});
}
}
public void showTips(@NotNilptr QScintilla _sci, int position, String content)
{
_sci.showTips(position, content);
}
public QScintilla getCurrentSci(){
if (_scicomm.isVisible()){
return _scicomm;
}else{
return _sciext;
}
}
public void testUrl(@NotNilptr QScintilla _sci)
{
if (_sci.isModified()) {
String text = _sci.getText();
_sci.clearIndic(0, _sci.getLength());
_sci.setIndicCurrent(1);
if (text.length() > 0) {
Pattern.Result rt = url_match.matchAll(text, 0, -1, 0);
for (int i = 0, c = rt.length(); i < c; i++) {
int start = rt.get(i).start();
int end = rt.get(i).end();
_sci.applyIndic(start, end - start + 1);
}
}
_sci.setSavePoint();
}
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/ixlang/XStudio.git
[email protected]:ixlang/XStudio.git
ixlang
XStudio
XStudio
master

搜索帮助