Skip to content

Commit 41a4d23

Browse files
authored
Merge pull request #4 from user0706/feature/code-generator
Add files via upload
2 parents f4d7887 + 9048221 commit 41a4d23

File tree

4 files changed

+439
-5
lines changed

4 files changed

+439
-5
lines changed

Qt_ui/generator.ui

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>CodeGenerator</class>
4+
<widget class="QMainWindow" name="CodeGenerator">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>800</width>
10+
<height>333</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>PyRex Code Generaotr</string>
15+
</property>
16+
<property name="windowIcon">
17+
<iconset resource="src.qrc">
18+
<normaloff>:/icons/icons/windowIcon.png</normaloff>:/icons/icons/windowIcon.png</iconset>
19+
</property>
20+
<widget class="QWidget" name="centralwidget">
21+
<layout class="QVBoxLayout" name="verticalLayout">
22+
<property name="leftMargin">
23+
<number>0</number>
24+
</property>
25+
<property name="topMargin">
26+
<number>0</number>
27+
</property>
28+
<property name="rightMargin">
29+
<number>0</number>
30+
</property>
31+
<property name="bottomMargin">
32+
<number>0</number>
33+
</property>
34+
<item>
35+
<widget class="QTextEdit" name="textEdit">
36+
<property name="styleSheet">
37+
<string notr="true">border-bottom: 2px;
38+
border-style: solid;
39+
border-bottom-color: rgb(255, 85, 0);</string>
40+
</property>
41+
<property name="lineWidth">
42+
<number>1</number>
43+
</property>
44+
<property name="html">
45+
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
46+
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
47+
p, li { white-space: pre-wrap; }
48+
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;&quot;&gt;
49+
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt;import re&lt;/span&gt;&lt;/p&gt;
50+
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:8pt;&quot;&gt;&lt;br /&gt;&lt;/p&gt;
51+
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt;regex = r&amp;quot;&amp;lt;CHANGE_ME&amp;gt;pattern&amp;lt;CHANGE_ME&amp;gt;&amp;quot;&lt;/span&gt;&lt;/p&gt;
52+
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt;test_str = &amp;quot;&amp;lt;CHANGE_ME&amp;gt;string&amp;lt;CHANGE_ME&amp;gt;&amp;quot;&lt;/span&gt;&lt;/p&gt;
53+
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:8pt;&quot;&gt;&lt;br /&gt;&lt;/p&gt;
54+
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt;matches = re.finditer(regex, test_str, re.MULTILINE)&lt;/span&gt;&lt;/p&gt;
55+
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:8pt;&quot;&gt;&lt;br /&gt;&lt;/p&gt;
56+
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt;for matchNum, match in enumerate(matches, start=1):&lt;/span&gt;&lt;/p&gt;
57+
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt; print (&amp;quot;Match {matchNum} was found at {start}-{end}: {match}&amp;quot;.format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))&lt;/span&gt;&lt;/p&gt;
58+
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt; for groupNum in range(0, len(match.groups())):&lt;/span&gt;&lt;/p&gt;
59+
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt; groupNum = groupNum + 1&lt;/span&gt;&lt;/p&gt;
60+
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:8pt;&quot;&gt;&lt;br /&gt;&lt;/p&gt;
61+
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt; print (&amp;quot;Group {groupNum} found at {start}-{end}: {group}&amp;quot;.format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
62+
</property>
63+
<property name="textInteractionFlags">
64+
<set>Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
65+
</property>
66+
</widget>
67+
</item>
68+
<item>
69+
<layout class="QHBoxLayout" name="horizontalLayout">
70+
<property name="topMargin">
71+
<number>0</number>
72+
</property>
73+
<item>
74+
<spacer name="horizontalSpacer">
75+
<property name="orientation">
76+
<enum>Qt::Horizontal</enum>
77+
</property>
78+
<property name="sizeHint" stdset="0">
79+
<size>
80+
<width>40</width>
81+
<height>20</height>
82+
</size>
83+
</property>
84+
</spacer>
85+
</item>
86+
<item alignment="Qt::AlignHCenter|Qt::AlignVCenter">
87+
<widget class="QPushButton" name="CopyPushButton">
88+
<property name="styleSheet">
89+
<string notr="true">QPushButton#CopyPushButton {
90+
min-width:60px;
91+
max-width:60px;
92+
min-height:35;
93+
max-height:35;
94+
95+
color: #495057;
96+
97+
background: rgba(255,255,255,100%);
98+
border-bottom: 2px;
99+
border-style: solid;
100+
border-bottom-color: rgba(0,0,0,.3);
101+
border-top-left-radius: 0px;
102+
border-top-right-radius: 3px;
103+
border-bottom-right-radius: 0px;
104+
border-bottom-left-radius: 0px;
105+
}
106+
107+
QPushButton#CopyPushButton:hover {
108+
background: rgba(255,255,255,50%);
109+
border-bottom-color:rgb(255, 85, 0);
110+
}
111+
112+
QPushButton#CopyPushButton:pressed {
113+
background: rgba(255,255,255,100%);
114+
border-bottom-color:rgb(255, 85, 0);
115+
}
116+
117+
QPushButton#CopyPushButton:disabled {
118+
background: rgba(0,0,0,.04);
119+
border-bottom-color: rgba(0,0,0,.15);
120+
}</string>
121+
</property>
122+
<property name="text">
123+
<string>Copy</string>
124+
</property>
125+
</widget>
126+
</item>
127+
<item alignment="Qt::AlignHCenter|Qt::AlignVCenter">
128+
<widget class="QPushButton" name="ClosePushButton">
129+
<property name="styleSheet">
130+
<string notr="true">QPushButton#ClosePushButton {
131+
min-width:60px;
132+
max-width:60px;
133+
min-height:35;
134+
max-height:35;
135+
136+
color: #495057;
137+
138+
background: rgba(255,255,255,100%);
139+
border-bottom: 2px;
140+
border-style: solid;
141+
border-bottom-color: rgba(0,0,0,.3);
142+
border-top-left-radius: 0px;
143+
border-top-right-radius: 3px;
144+
border-bottom-right-radius: 0px;
145+
border-bottom-left-radius: 0px;
146+
}
147+
148+
QPushButton#ClosePushButton:hover {
149+
background: rgba(255,255,255,50%);
150+
border-bottom-color:rgb(255, 85, 0);
151+
}
152+
153+
QPushButton#ClosePushButton:pressed {
154+
background: rgba(255,255,255,100%);
155+
border-bottom-color:rgb(255, 85, 0);
156+
}
157+
158+
QPushButton#ClosePushButton:disabled {
159+
background: rgba(0,0,0,.04);
160+
border-bottom-color: rgba(0,0,0,.15);
161+
}</string>
162+
</property>
163+
<property name="text">
164+
<string>Close</string>
165+
</property>
166+
</widget>
167+
</item>
168+
<item>
169+
<spacer name="horizontalSpacer_2">
170+
<property name="orientation">
171+
<enum>Qt::Horizontal</enum>
172+
</property>
173+
<property name="sizeHint" stdset="0">
174+
<size>
175+
<width>40</width>
176+
<height>20</height>
177+
</size>
178+
</property>
179+
</spacer>
180+
</item>
181+
</layout>
182+
</item>
183+
</layout>
184+
</widget>
185+
<widget class="QMenuBar" name="menubar">
186+
<property name="geometry">
187+
<rect>
188+
<x>0</x>
189+
<y>0</y>
190+
<width>800</width>
191+
<height>21</height>
192+
</rect>
193+
</property>
194+
</widget>
195+
<widget class="QStatusBar" name="statusbar"/>
196+
</widget>
197+
<resources>
198+
<include location="src.qrc"/>
199+
</resources>
200+
<connections>
201+
<connection>
202+
<sender>ClosePushButton</sender>
203+
<signal>clicked()</signal>
204+
<receiver>CodeGenerator</receiver>
205+
<slot>close()</slot>
206+
<hints>
207+
<hint type="sourcelabel">
208+
<x>432</x>
209+
<y>560</y>
210+
</hint>
211+
<hint type="destinationlabel">
212+
<x>399</x>
213+
<y>299</y>
214+
</hint>
215+
</hints>
216+
</connection>
217+
<connection>
218+
<sender>CopyPushButton</sender>
219+
<signal>clicked()</signal>
220+
<receiver>textEdit</receiver>
221+
<slot>selectAll()</slot>
222+
<hints>
223+
<hint type="sourcelabel">
224+
<x>366</x>
225+
<y>560</y>
226+
</hint>
227+
<hint type="destinationlabel">
228+
<x>399</x>
229+
<y>280</y>
230+
</hint>
231+
</hints>
232+
</connection>
233+
<connection>
234+
<sender>CopyPushButton</sender>
235+
<signal>clicked()</signal>
236+
<receiver>textEdit</receiver>
237+
<slot>copy()</slot>
238+
<hints>
239+
<hint type="sourcelabel">
240+
<x>366</x>
241+
<y>560</y>
242+
</hint>
243+
<hint type="destinationlabel">
244+
<x>399</x>
245+
<y>280</y>
246+
</hint>
247+
</hints>
248+
</connection>
249+
</connections>
250+
</ui>

functions.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from mainGUI import *
2+
from generator import *
23
from PyQt5.QtGui import *
34
from PyQt5.QtWidgets import *
45
from PyQt5.QtCore import *
@@ -254,4 +255,17 @@ def strColorTolist(string):
254255
return colorList[2] => 50
255256
'''
256257
colorList = [int(i) for i in re.sub(r'(rgb\(|\))','',string).split(', ')]
257-
return colorList[0],colorList[1],colorList[2]
258+
return colorList[0],colorList[1],colorList[2]
259+
260+
def GetCode(textEdit, pattern, string):
261+
'''Generate code for Python regular expression based on the template
262+
263+
:param textEdit: The QTextEdit element carries a raw code template
264+
:type textEdit: PyQt5.QtWidgets.QTextEdit
265+
:param pattern: A pattern to be entered in the code template
266+
:type pattern: str
267+
:param string: A test string to be entered in the code template
268+
:type string: str
269+
'''
270+
template = textEdit.toPlainText()
271+
textEdit.setText(template.replace("<CHANGE_ME>pattern<CHANGE_ME>", pattern).replace("<CHANGE_ME>string<CHANGE_ME>", string))

0 commit comments

Comments
 (0)