-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
117 lines (100 loc) · 4.21 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from functions import *
from generator import *
from mainGUI import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys
import os
import re
class MyWin(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
shortcut = QShortcut(QKeySequence("Ctrl+Return"), self)
shortcut.activated.connect(self.ui.ProcessPushButton.click)
self.Pattern = ''
self.TestString = ''
self.ui.ProcessPushButton.clicked.connect(self.PatternChanged)
self.ui.RegexTextEdit.installEventFilter(self)
self.ui.SheetDockWidget.hide()
self.ui.RegexSheetPushButton.clicked.connect(self.Sheet)
self.SheetFlag = False
self.ui.CodeGeneratorPushButton.clicked.connect(self.CodeGenerator)
self.ui.AboutPushButton.hide()
self.ResultWidget = None
self.horizontalLayout_4 = None
self.ResultTag = None
self.ResultRange = None
self.ResultText = None
self.Info = []
self.Match = []
self.ui.NoMatchesLabel.hide()
self.resize(800, 600)
self.WindowWidthFlag = 0
self.WindowHeightFlag = 0
self.ColorList = ['rgb(198, 227, 255)','rgb(198, 233, 157)','rgb(245, 171, 165)','rgb(255, 191, 109)','rgb(193, 203, 235)','rgb(215, 253, 227)','rgb(227, 255, 172)','rgb(232, 141, 238)','rgb(207, 221, 103)','rgb(255, 172, 192)','rgb(132, 214, 238)']
self.GeneratedCode = Ui_MainWindow()
def UpdateInfo(self):
'''Visual display of search results'''
for i, v in enumerate(self.Info):
if 'Full match' in v[0]:
FullMatchTagUI(self.ui.scrollAreaWidgetContents, self.ui.verticalLayout_5, v[0])
color = self.ColorList[0]
else:
color = GenerateGroupColor(self.ColorList, v[0])
InfoUI(self.ResultWidget, self.horizontalLayout_4, self.ResultTag, self.ResultRange, self.ResultText, i, color, self.ui.scrollAreaWidgetContents, self.ui.verticalLayout_5, v)
SpacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Expanding)
self.ui.verticalLayout_5.addItem(SpacerItem)
def eventFilter(self, obj, event):
if obj is self.ui.RegexTextEdit and event.type() == QEvent.KeyPress:
if event.key() in (Qt.Key_Return, Qt.Key_Enter):
self.PatternChanged()
return True
return super().eventFilter(obj, event)
def PatternChanged(self):
'''Search for test text based on a pattern and display the results'''
clearLayout(self.ui.verticalLayout_5)
self.Info = []
self.Pattern = self.ui.RegexTextEdit.toPlainText()
self.TestString = self.ui.TestStringTextEdit.toPlainText()
self.Info = Process(self.TestString, self.Pattern, self.Info, self.ui.NoMatchesLabel)
self.ResultWidget, self.horizontalLayout_4, self.ResultTag, self.ResultRange, self.ResultText = PreprocessInfo(self.Info)
self.UpdateInfo()
ClearHighlight(self.ui.TestStringTextEdit, self.TestString)
for k, v in enumerate(self.Info):
hrange = HighlightRange(v[1])
if 'Full match' in v[0]:
Highlight(self.ui.TestStringTextEdit, QColor(198, 227, 255), hrange[0], hrange[1])
else:
color = GenerateGroupColor(self.ColorList, v[0])
r, g, b = strColorTolist(color)
Highlight(self.ui.TestStringTextEdit, QColor(r,g,b), hrange[0], hrange[1])
def Sheet(self):
'''Display regular expression cheat sheet'''
if not self.SheetFlag:
self.WindowWidthFlag = self.width()
self.WindowHeightFlag = self.height()
self.ui.SheetDockWidget.show()
self.SheetFlag = True
elif self.SheetFlag:
self.ui.SheetDockWidget.close()
self.adjustSize()
self.resize(self.WindowWidthFlag,self.WindowHeightFlag)
self.SheetFlag = False
def CodeGenerator(self):
'''The code for Python regular expression is generated in a new window'''
if not self.Pattern or not self.TestString:
self.ui.NoMatchesLabel.show()
else:
self.CodeGeneratorWindow = QtWidgets.QMainWindow()
self.gen_ui = Ui_CodeGenerator()
self.gen_ui.setupUi(self.CodeGeneratorWindow)
GetCode(self.gen_ui.textEdit, self.Pattern, self.TestString)
self.CodeGeneratorWindow.show()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
myapp = MyWin()
myapp.show()
sys.exit(app.exec_())