-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex_tool.py
72 lines (58 loc) · 2.21 KB
/
regex_tool.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
#! /usr/bin/env python
#
# Ben Osment
# Sun Jul 15 08:41:39 2012
import sys
from PyQt4 import QtGui, QtCore
from regexhighlighter import RegexHighlighter
class UI(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.resize(600, 400)
self.setWindowTitle('Regex Tool')
self.center()
aboutAction = QtGui.QAction('&About', self)
aboutAction.setStatusTip('Display information about regex tool')
helpAction = QtGui.QAction('&Help', self)
helpAction.setShortcut('Ctrl+H')
helpAction.setStatusTip('Display help')
exitAction = QtGui.QAction('&Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(QtGui.qApp.quit)
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(aboutAction)
fileMenu.addAction(helpAction)
fileMenu.addAction(exitAction)
# add main elements
self.box = Box(self)
self.box.setGeometry(QtCore.QRect(0, 0, 800, 500))
self.setCentralWidget(self.box)
def center(self):
screen = QtGui.QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width() - size.width()) / 2,
(screen.height() - size.height()) / 2)
class Box(QtGui.QFrame):
def __init__(self, parent):
QtGui.QFrame.__init__(self, parent)
box = QtGui.QVBoxLayout()
box.addWidget(QtGui.QLabel('<b>Text</b>'))
self.textbox = QtGui.QTextEdit(self)
box.addWidget(self.textbox)
box.addWidget(QtGui.QLabel('<b>Regular Expression</b>'))
self.regex_box = QtGui.QTextEdit(self)
self.regex_box.textChanged.connect(self.textChanged)
box.addWidget(self.regex_box)
self.setLayout(box)
self.highlighter = RegexHighlighter(self.textbox.document())
def textChanged(self):
regex_val = "%s" % self.regex_box.document().toPlainText()
self.highlighter.set_regex(regex_val)
self.highlighter.rehighlight()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ui = UI()
ui.show()
sys.exit(app.exec_())