diff --git a/codeEdit.py b/codeEdit.py index 946a9cd684348b69c3bbc8590ef8fb4f8a28ab8c..437f9f4df16d9ece2651ec69f5f18c28dda2590a 100644 --- a/codeEdit.py +++ b/codeEdit.py @@ -12,7 +12,6 @@ from PyQt5.Qsci import QsciLexerCPP, QsciScintilla, QsciAPIs, QsciScintillaBase import keyword, yaml, easygui - class CodeEditor(QsciScintilla): def __init__(self, parent=None): @@ -20,7 +19,7 @@ class CodeEditor(QsciScintilla): self.setUtf8(True) self.font = QFont() - self.font.setFamily("Courier New") + self.font.setFamily("Point Code") self.font.setPointSize(13) self.font.setFixedPitch(True) self.setFont(self.font) @@ -123,39 +122,47 @@ class CodeEditor(QsciScintilla): # set lexer self.lexer = QsciLexerCPP(None) + # load configure with open('highlights/default.yml', 'rb')as f: res = yaml.load(f, Loader=yaml.FullLoader) self.yml = res self.lexer.setFont(self.font) self.setLexer(self.lexer) - self.lexer.setColor(QColor(f"#{res['cpp-defualt']['default']}")) - self.lexer.setPaper(QColor(f"#{res['cpp-defualt']['background']}")) - self.lexer.setColor(QColor(f"#{res['cpp-defualt']['keyword']}"), QsciLexerCPP.Keyword) + # defult color + self.lexer.setColor(QColor(res['cpp-defualt']['default'])) + # bg color + self.lexer.setPaper(QColor(res['cpp-defualt']['background'])) + self.lexer.setColor(QColor(res['cpp-defualt']['keyword']), QsciLexerCPP.Keyword) # 文档注释 /**开头的颜色 - self.lexer.setColor(QColor(f"#{res['cpp-defualt']['comments']['document']}"), QsciLexerCPP.CommentDoc) - self.lexer.setColor(QColor(f"#{res['cpp-defualt']['comments']['block']}"), QsciLexerCPP.Comment) # 块注释 的颜色 - self.lexer.setColor(QColor(f"#{res['cpp-defualt']['comments']['line']}"), QsciLexerCPP.CommentLine) # 行注释的颜色 - self.lexer.setColor(QColor(f"#{res['cpp-defualt']['constants']['number']}"), QsciLexerCPP.Number) # 数字 的颜色 - self.lexer.setColor(QColor(f"#{res['cpp-defualt']['constants']['char']}"), + self.lexer.setColor(QColor(res['cpp-defualt']['comments'] + ['document']), QsciLexerCPP.CommentDoc) + self.lexer.setColor(QColor(res['cpp-defualt']['comments'] + ['block']), QsciLexerCPP.Comment) # 块注释 的颜色 + self.lexer.setColor(QColor(res['cpp-defualt']['comments'] + ['line']), QsciLexerCPP.CommentLine) # 行注释的颜色 + self.lexer.setColor(QColor(res['cpp-defualt']['constants'] + ['number']), QsciLexerCPP.Number) # 数字 的颜色 + self.lexer.setColor(QColor(res['cpp-defualt']['constants'] + ['char']), QsciLexerCPP.DoubleQuotedString) # 双引号字符串的颜色 - self.lexer.setColor(QColor(f"#{res['cpp-defualt']['constants']['string']}"), + self.lexer.setColor(QColor(res['cpp-defualt']['constants'] + ['string']), QsciLexerCPP.SingleQuotedString) # 单引号字符的颜色 - self.lexer.setColor(QColor(f"#{res['cpp-defualt']['pre']}"), QsciLexerCPP.PreProcessor) # 预编译语句的颜色 - self.lexer.setColor(QColor('#ffadbc'), QsciLexerCPP.Operator) + self.lexer.setColor(QColor(res['cpp-defualt']['pre']), QsciLexerCPP.PreProcessor) # 预编译语句的颜色 + self.lexer.setColor(QColor(res['cpp-defualt']['operator']), QsciLexerCPP.Operator) # 操作符的颜色 # self.setColor(QColor("#000000"), QsciLexerCPP.Identifier) #可识别字符的颜色,这个范围很广,包含了关键词,函数名;所以要取消这句 - self.lexer.setColor(QColor("#e7db60"), + self.lexer.setColor(QColor(res['cpp-defualt']['constants'] + ['unclosed']), QsciLexerCPP.UnclosedString) # 未完成输入的字符串的颜色 - # font = self.font - # font.setBold(True) - # self.lexer.setFont(font, 5) # 默认的字体加粗。 + font = self.font + font.setItalic(True) + self.lexer.setFont(font, QsciLexerCPP.Comment) # 注释的字体用斜体 - # font = self.font - # font.setItalic(True) - # self.lexer.setFont(font, QsciLexerCPP.Comment) # 注释的字体用斜体。 + # EOL parse for i in range(0, QsciScintillaBase.STYLE_MAX): desc = self.lexer.description(i) if desc: - self.lexer.setEolFill(True, i) # 将填充的底色延伸到屏幕的右端;否则默认只有文字的地方。 + self.lexer.setEolFill(True, i) # 将填充的底色延伸到屏幕的右端;否则默认只有文字的地方 # high light code @@ -163,5 +170,4 @@ class CodeEditor(QsciScintilla): self.linesChanged.connect(self.linesChange) def linesChange(self): - # print(self.lines()) self.setMarginWidth(0, self.fontmetrics.width(str(self.lines())) + 5) diff --git a/font/PointCode.ttf b/font/PointCode.ttf new file mode 100644 index 0000000000000000000000000000000000000000..843c715b4822258fc54bf97de1a32a099788b6ac Binary files /dev/null and b/font/PointCode.ttf differ diff --git a/highlights/default.yml b/highlights/default.yml index 1a1a87035e50417552c750e743f6bad0f3ae58c3..e934463f0e012357d84110671ee2a8333ee4d58f 100644 --- a/highlights/default.yml +++ b/highlights/default.yml @@ -1,15 +1,16 @@ -#默认使用的标色文件,建议备份后再修改(已有一件备份在samples/下) -#使用16进制rgb颜色(去掉“#”) +# Some Addition by Albert Z.~~~ cpp-defualt: - background: "232333" #背景颜色 - default: dedeff #默认字体颜色 - keyword: 5cd9ff #关键字 + background: "#232333" #背景颜色 + default: "#DEDEFF" #默认字体颜色 + keyword: "#569CD6" #关键字 comments: - document: 746e50 #/**开头的颜色 - block: 746e50 #注释块颜色 - line: 746e50 #单行注释颜色 + document: "#57A64A" #/**开头的颜色 + block: "#57A64A" #注释块颜色 + line: "#57A64A" #单行注释颜色 constants: - number: ac80ff #数字颜色 - char: 87edac #双引号字符颜色 - string: 87edac #单引号字符颜色 - pre: dedeff #预编译语句的颜色 \ No newline at end of file + number: "#CCFFCC" #数字颜色 + char: "#D69D85" #双引号字符颜色 + string: "#FFC125" #单引号字符颜色 + unclosed: "#FEDEEE" + pre: "#DDDDDD" #预编译语句的颜色 + operator: '#FFFFEE' #操作符颜色 \ No newline at end of file diff --git a/main.pyw b/main.py similarity index 69% rename from main.pyw rename to main.py index d80be6a191534dc26334ebd652ea2af3f5bce27d..e33e178d7a882507db2669b669b166a2ae73c758 100644 --- a/main.pyw +++ b/main.py @@ -1,4 +1,4 @@ -import sys,os +import sys, os import qdarkstyle from PyQt5.QtCore import * from PyQt5.QtGui import * @@ -6,11 +6,14 @@ from PyQt5 import QtWidgets as QtW from PyQt5.QtWidgets import * import ui_mainWin import codeEdit -import win32api,subprocess,yaml +import win32api, subprocess, yaml + class Gui(ui_mainWin.Ui_MainWindow, QtW.QMainWindow): def __init__(self,file=None): super().__init__() + self.windowList = [] + QFontDatabase.addApplicationFont('./font/PointCode.ttf') self.setupUi(self) self.showMaximized() self.codeEdit = codeEdit.CodeEditor() @@ -18,14 +21,12 @@ class Gui(ui_mainWin.Ui_MainWindow, QtW.QMainWindow): self.codeEdit.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5()) self.setCentralWidget(self.codeEdit) self.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5()) - self.newfile = lambda: win32api.ShellExecute( - 0, 'open', os.path.realpath(__file__), '', '', 1) self.setup() - if(file): + if file: self.openfile(file) def setup(self): - self.opendfile=''; + self.opendfile = ''; self.actionNew.triggered.connect(self.newfile) self.actionOpen.triggered.connect(self.DLGopenfile) self.actionSave.triggered.connect(self.DLGsavefile) @@ -38,7 +39,7 @@ class Gui(ui_mainWin.Ui_MainWindow, QtW.QMainWindow): res = yaml.load(f, Loader=yaml.FullLoader) cmd=res['compile_cmd'].replace(r'{filename}',self.opendfile) cmd=cmd.replace(r'{exename}',os.path.splitext(self.opendfile)[0]+'.exe') - with open('compile_temp.bat','w',encoding='utf-8')as fp: + with open('compile_temp.bat','w',encoding='utf-8') as fp: fp.write('@echo off\n') fp.write(cmd) fp.write('\necho Compile Finished:'+os.path.splitext(self.opendfile)[0]+'.exe') @@ -50,11 +51,13 @@ class Gui(ui_mainWin.Ui_MainWindow, QtW.QMainWindow): self.DLGsavefile() def runCpp(self): - if(self.opendfile): - with open('config/compilerConfig.yml', 'rb')as f: - res = yaml.load(f, Loader=yaml.FullLoader) - cmd=res['run_cmd'].replace(r'{filename}',self.opendfile) - with open('run_temp.bat','w',encoding='utf-8')as fp: + if not self.opendfile: + QtW.QMessageBox.critical(self,'运行',f"请保存文件后再运行") + self.DLGsavefile() + with open('config/compilerConfig.yml', 'rb')as f: + res = yaml.load(f, Loader=yaml.FullLoader) + cmd = res['run_cmd'].replace(r'{filename}',self.opendfile) + with open('run_temp.bat','w',encoding='utf-8') as fp: fp.write('@echo off\n') fp.write(cmd) fp.write('\necho\\') @@ -63,50 +66,54 @@ class Gui(ui_mainWin.Ui_MainWindow, QtW.QMainWindow): fp.write('\npause') win32api.ShellExecute( 0, 'open', 'run_temp.bat', '', '', 1) - else: - QtW.QMessageBox.critical(self,'运行',f"请保存文件后再运行") - self.DLGsavefile() - - windowList = [] + def DLGopenfile(self): - file, filetype = QtW.QFileDialog.getOpenFileName(self,"打开文件", + file, filetype = QtW.QFileDialog.getOpenFileName(self, "打开文件", 'C:\\', # 起始路径 - "C++ Files (*.cpp;*.cc;*.c++;*.hpp;*.hh;*.hxx;*.h++;*.h;*.ii;*.c)") # 设置文件扩展名过滤,用双分号间隔 + "C++ Files (*.cpp;*.cc;*.c++;*.hpp;*.hh;*.hxx;*.h++;*.h;*.ii;*.c)") # 设置文件扩展名过滤, 用分号间隔 if(file): - if(self.opendfile): - w=Gui(file) - self.windowList.append(w) - w.show() + if self.opendfile: + win = Gui(file) + self.windowList.append(win) + win.show() else: self.openfile(file) def DLGsavefile(self): - if(not self.opendfile): + if not self.opendfile: file, filetype = QtW.QFileDialog.getSaveFileName(self,"保存文件", 'C:\\', # 起始路径 "C++ Files (*.cpp;*.cc;*.c++;*.hpp;*.hh;*.hxx;*.h++;*.h;*.ii;*.c)") # 设置文件扩展名过滤,用双分号间隔 - if(file): - with open(file,'w',encoding='utf-8')as fp: + if file: + with open(file,'w',encoding='utf-8') as fp: fp.write(self.codeEdit.text()) - self.opendfile=file; + self.opendfile = file; else: - with open(self.opendfile,'w',encoding='utf-8')as fp: + with open(self.opendfile, 'w',encoding='utf-8') as fp: fp.write(self.codeEdit.text()) + + + def newfile(self): + self.DLGsavefile() + self.opendfile = '' + self.codeEdit.setText('') def openfile(self,filename): if(os.path.exists(filename)): try: - with open(filename,'r',encoding='utf-8')as fp: - data=(fp.read()) + with open(filename,'r', encoding='utf-8') as fp: + data = fp.read() self.codeEdit.setText(data) - self.opendfile=filename; + self.opendfile = filename; except Exception as e: QtW.QMessageBox.critical(self,'打开文件错误',f"打开文件 “{filename}” 时出现错误:\n{str(e)}") else: QtW.QMessageBox.critical(self,'打开文件错误',f"打开文件 “{filename}” 时出现错误:\n文件不存在") -app = QtW.QApplication(sys.argv) -mainwin = Gui() -mainwin.show() -sys.exit(app.exec_()) + +if __name__ == '__main__': + app = QtW.QApplication(sys.argv) + mainwin = Gui() + mainwin.show() + sys.exit(app.exec_())