1
1
import sys
2
2
from .ec_handler import Handler
3
3
from .ec_classes import RuntimeError
4
- from .ec_keyboard import Keyboard , TextReceiver
5
- from PySide6 .QtCore import Qt , QTimer
4
+ from PySide6 .QtCore import Qt , QTimer , Signal
6
5
from PySide6 .QtGui import QPixmap
7
6
from PySide6 .QtWidgets import (
8
7
QApplication ,
@@ -80,6 +79,38 @@ def showEvent(self, event):
80
79
def afterShown (self ):
81
80
if 'action' in self .record : self .record ['action' ]()
82
81
82
+ class ClickableLineEdit (QLineEdit ):
83
+ clicked = Signal ()
84
+
85
+ def __init__ (self ):
86
+ super ().__init__ ()
87
+ self .program = None
88
+
89
+ def setCallback (self , program , pc ):
90
+ self .program = program
91
+ self .pc = pc
92
+
93
+ def mousePressEvent (self , event ):
94
+ self .clicked .emit ()
95
+ super ().mousePressEvent (event )
96
+ if self .program != None : self .program .run (self .pc )
97
+
98
+ class ClickablePlainTextEdit (QPlainTextEdit ):
99
+ clicked = Signal ()
100
+
101
+ def __init__ (self ):
102
+ super ().__init__ ()
103
+ self .program = None
104
+
105
+ def setCallback (self , program , pc ):
106
+ self .program = program
107
+ self .pc = pc
108
+
109
+ def mousePressEvent (self , event ):
110
+ self .clicked .emit ()
111
+ super ().mousePressEvent (event )
112
+ if self .program != None : self .program .run (self .pc )
113
+
83
114
#############################################################################
84
115
# Keyword handlers
85
116
@@ -297,21 +328,23 @@ def k_createGroupBox(self, command):
297
328
298
329
def k_createLabel (self , command ):
299
330
text = self .compileConstant ('' )
331
+ size = self .compileConstant (20 )
300
332
while True :
301
333
token = self .peek ()
302
334
if token == 'text' :
303
335
self .nextToken ()
304
336
text = self .nextValue ()
305
337
elif token == 'size' :
306
338
self .nextToken ()
307
- command [ ' size' ] = self .nextValue ()
339
+ size = self .nextValue ()
308
340
elif token == 'align' :
309
341
self .nextToken ()
310
342
token = self .nextToken ()
311
343
if token in ['left' , 'right' , 'center' , 'centre' , 'justify' ]:
312
344
command ['align' ] = token
313
345
else : break
314
346
command ['text' ] = text
347
+ command ['size' ] = size
315
348
self .add (command )
316
349
return True
317
350
@@ -340,11 +373,19 @@ def k_createCheckBox(self, command):
340
373
return True
341
374
342
375
def k_createLineEdit (self , command ):
343
- if self .peek () == 'size' :
344
- self .nextToken ()
345
- size = self .nextValue ()
346
- else : size = self .compileConstant (10 )
376
+ text = self .compileConstant ('' )
377
+ size = self .compileConstant (40 )
378
+ while True :
379
+ token = self .peek ()
380
+ if token == 'text' :
381
+ self .nextToken ()
382
+ text = self .nextValue ()
383
+ elif token == 'size' :
384
+ self .nextToken ()
385
+ size = self .nextValue ()
386
+ else : break ;
347
387
command ['size' ] = size
388
+ command ['text' ] = text
348
389
self .add (command )
349
390
return True
350
391
@@ -479,6 +520,10 @@ def r_createGroupBox(self, command, record):
479
520
480
521
def r_createLabel (self , command , record ):
481
522
label = QLabel (str (self .getRuntimeValue (command ['text' ])))
523
+ label .setStyleSheet ("""
524
+ background-color: transparent;
525
+ border: none;
526
+ """ )
482
527
if 'size' in command :
483
528
fm = label .fontMetrics ()
484
529
c = label .contentsMargins ()
@@ -500,7 +545,7 @@ def r_createPushbutton(self, command, record):
500
545
if 'size' in command :
501
546
fm = pushbutton .fontMetrics ()
502
547
c = pushbutton .contentsMargins ()
503
- w = fm .horizontalAdvance ('m' ) * self .getRuntimeValue (command ['size' ]) + c .left ()+ c .right ()
548
+ w = fm .horizontalAdvance ('m' ) * self .getRuntimeValue (command ['size' ]) + c .left ()+ c .right ()
504
549
pushbutton .setMaximumWidth (w )
505
550
record ['widget' ] = pushbutton
506
551
return self .nextPC ()
@@ -511,7 +556,8 @@ def r_createCheckBox(self, command, record):
511
556
return self .nextPC ()
512
557
513
558
def r_createLineEdit (self , command , record ):
514
- lineinput = QLineEdit ()
559
+ lineinput = self .ClickableLineEdit ()
560
+ lineinput .setText (self .getRuntimeValue (command ['text' ]))
515
561
fm = lineinput .fontMetrics ()
516
562
m = lineinput .textMargins ()
517
563
c = lineinput .contentsMargins ()
@@ -521,7 +567,7 @@ def r_createLineEdit(self, command, record):
521
567
return self .nextPC ()
522
568
523
569
def r_createMultiLineEdit (self , command , record ):
524
- textinput = QPlainTextEdit ()
570
+ textinput = self . ClickablePlainTextEdit ()
525
571
fontMetrics = textinput .fontMetrics ()
526
572
charWidth = fontMetrics .horizontalAdvance ('x' )
527
573
charHeight = fontMetrics .height ()
@@ -552,13 +598,13 @@ def r_createDialog(self, command, record):
552
598
mainLayout .addWidget (QLabel (prompt ))
553
599
elif dialogType == 'lineedit' :
554
600
mainLayout .addWidget (QLabel (prompt ))
555
- dialog .lineEdit = QLineEdit (dialog )
601
+ dialog .lineEdit = self . ClickableLineEdit (dialog )
556
602
dialog .value = self .getRuntimeValue (command ['value' ])
557
603
dialog .lineEdit .setText (dialog .value )
558
604
mainLayout .addWidget (dialog .lineEdit )
559
605
elif dialogType == 'multiline' :
560
606
mainLayout .addWidget (QLabel (prompt ))
561
- dialog .textEdit = QPlainTextEdit (self )
607
+ dialog .textEdit = self . ClickablePlainTextEdit (self )
562
608
dialog .textEdit .setText (dialog .value )
563
609
mainLayout .addWidget (dialog .textEdit )
564
610
buttonBox = QDialogButtonBox (QDialogButtonBox .Ok | QDialogButtonBox .Cancel )
@@ -690,7 +736,7 @@ def k_multiline(self, command):
690
736
def r_multiline (self , command ):
691
737
return self .nextPC ()
692
738
693
- # on click {pushbutton}
739
+ # on click {pushbutton}/{lineinput}/{multiline}
694
740
# on select {combobox}/{listbox}
695
741
# on tick
696
742
def k_on (self , command ):
@@ -723,7 +769,7 @@ def setupOn():
723
769
if token == 'click' :
724
770
if self .nextIsSymbol ():
725
771
record = self .getSymbolRecord ()
726
- if record ['keyword' ] == 'pushbutton' :
772
+ if record ['keyword' ] in [ 'pushbutton' , 'lineinput' , 'multiline' ] :
727
773
command ['name' ] = record ['name' ]
728
774
setupOn ()
729
775
return True
@@ -770,6 +816,8 @@ def r_on(self, command):
770
816
keyword = record ['keyword' ]
771
817
if keyword == 'pushbutton' :
772
818
widget .clicked .connect (lambda : self .run (command ['goto' ]))
819
+ if keyword in ['lineinput' , 'multiline' ]:
820
+ widget .setCallback (self .program , command ['goto' ])
773
821
elif keyword == 'combobox' :
774
822
widget .currentIndexChanged .connect (lambda : self .run (command ['goto' ]))
775
823
elif keyword == 'listbox' :
@@ -895,7 +943,7 @@ def k_set(self, command):
895
943
self .skip ('of' )
896
944
if self .nextIsSymbol ():
897
945
record = self .getSymbolRecord ()
898
- if record ['keyword' ] in ['label' , 'pushbutton' , 'lineinput' , 'multiline' ]:
946
+ if record ['keyword' ] in ['label' , 'pushbutton' , 'lineinput' , 'multiline' , 'element' ]:
899
947
command ['name' ] = record ['name' ]
900
948
self .skip ('to' )
901
949
command ['value' ] = self .nextValue ()
@@ -968,7 +1016,8 @@ def r_set(self, command):
968
1016
widget = self .getVariable (command ['name' ])['widget' ]
969
1017
text = self .getRuntimeValue (command ['value' ])
970
1018
keyword = record ['keyword' ]
971
- if keyword in ['label' , 'pushbutton' , 'lineinput' ]:
1019
+ setText = getattr (widget , "setText" , None )
1020
+ if callable (setText ):
972
1021
widget .setText (text )
973
1022
elif keyword == 'multiline' :
974
1023
widget .setPlainText (text )
0 commit comments