forked from DFRobot/uPyCraft_src
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainComponents.py
1218 lines (1047 loc) · 47.8 KB
/
mainComponents.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import os
import shutil
import time
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.Qsci import QsciScintilla, QsciScintillaBase, QsciLexerPython
from PyQt5.QtWidgets import *
rootDirectoryPath =os.path.expanduser("~")
rootDirectoryPath =rootDirectoryPath.replace("\\","/")
currentExamplesPath="%s/AppData/Local/uPyCraft/examples"%rootDirectoryPath
class myTerminal(QTextEdit):
customContextMenuRequestedSig = pyqtSignal(QPoint)
def __init__(self,queue,parent):
super(myTerminal,self).__init__(parent)
self.eventFilterEnable=False
self.setStyleSheet("""QTextEdit{background-color: qlineargradient(x1: 0, x2: 1, stop: 0 #262D34, stop: 1 #222529);
border-style:none;
color:white;}
QScrollBar:vertical{background-color:rgb(94,98,102);
border:0px;
width: 15px;
margin:0px 0px 0px 0px;
}
QScrollBar::add-page:vertical{background-color:rgb(61,62,64);
width: 15px;
margin:0px 0px 0px 0px;
}
QScrollBar::sub-page:vertical{background-color:rgb(61,62,64);
width: 15px;
margin:0px 0px 0px 0px;
}
""")
#self.setReadOnly(False)
self.installEventFilter(self)
self.setTextInteractionFlags(Qt.TextSelectableByMouse)
p=QPalette()
p.setColor(QPalette.Inactive,QPalette.Highlight,QColor(102,184,255))
self.setPalette(p)
self.setAcceptDrops(False)
self.terminalRightMenu=None
self.createTerminalRightMenu()
self.ui=parent
self.queue=queue
self.currentBoard="esp32"
self.ui.changeCurrentBoard.connect(self.changeBoard)
self.ui.initRecvdata.connect(self.initRecvdata)
self.ui.initMessycode.connect(self.initMessycode)
self.keyPressMsg=""
self.recvdata=""
self.messycode=b''
self.isChinese=0
self.cursor=None
self.startPosition=0
self.terminalSelect=False
def createTerminalRightMenu(self):
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequestedSig.connect(self.slotTerminalRightClickMenu)
self.terminalRightMenu=QMenu(self)
self.terminalRightMenu.setStyleSheet(
"""QMenu{background-color:rgb(67,67,67);
color:white;}
QMenu::item{padding:4px 16px;}
QMenu::item::selected{background-color:rgb(124,124,124);}"""
)
terminalCopy=QAction(QIcon(":/copy.png"),self.tr("copy"),self)
terminalCopy.triggered.connect(self.slotCopy)
terminalPaste=QAction(QIcon(":/copy.png"),self.tr("paste"),self)
terminalPaste.triggered.connect(self.slotPaste)
self.terminalRightMenu.addAction(terminalCopy)
self.terminalRightMenu.addAction(terminalPaste)
def slotTerminalRightClickMenu(self,point):
self.terminalRightMenu.exec_(self.mapToGlobal(point))
def slotCopy(self):
self.copy()
self.ui.cutCopyPasteMsg = QApplication.clipboard().setText(QApplication.clipboard().text())
self.terminalSelect=False
def slotPaste(self):
if not self.ui.myserial.ser.isOpen():
return
self.keyPressMsg=""
for i in str(QApplication.clipboard().text()):
if i=="\x0a":
i="\x0d\x0a"
self.ui.readwriteQueue.put("uitouart:::%s"%i)
def setEventFilterEnable(self,enable):
self.eventFilterEnable=enable
def eventFilter(self,watch,event):
if not self.eventFilterEnable:
return QMainWindow.eventFilter(self,watch,event)
if event.type()==QEvent.KeyPress:
#print ("event.text:%s"%(event.text()))
#print ("evevt.type:%d"%event.type())
#print (event.key())
self.keyPress(event)
return True
elif event.type()==QEvent.InputMethod:
print ("event.text0")
e=QInputMethodEvent(event)
print(e.commitString())
self.queue.put("uitouart:::%s"%e.commitString())
return True
else:
return QMainWindow.eventFilter(self,watch,event)
def changeBoard(self,board):
#print("changeBoard")
self.currentBoard=board
def initRecvdata(self):
print("initRecvdata")
self.recvdata=""
def initMessycode(self):
print("initMessycode")
self.messycode=b""
def keyPress(self,event):
print("keypress")
if self.terminalSelect:
self.terminalSelect=False
self.setTextCursor(self.ui.cursor)
self.messycode=b''
if event.key()==Qt.Key_Backspace:
self.keyPressMsg="\x08"
self.queue.put("uitouart:::%s"%self.keyPressMsg)
elif event.key()==Qt.Key_Left:
self.keyPressMsg='\x1b\x5b\x44'
self.queue.put("uitouart:::%s"%self.keyPressMsg)
elif event.key()==Qt.Key_Right:
self.keyPressMsg='\x1b\x5b\x43'
self.queue.put("uitouart:::%s"%self.keyPressMsg)
elif event.key()==Qt.Key_Delete:
self.keyPressMsg="\x1b\x5b\x33\x7e"
self.queue.put("uitouart:::%s"%self.keyPressMsg)
elif event.key()==Qt.Key_Up:
self.keyPressMsg="\x1b\x5b\x41"
self.queue.put("uitouart:::%s"%self.keyPressMsg)
elif event.key()==Qt.Key_Down:
self.keyPressMsg="\x1b\x5b\x42"
self.queue.put("uitouart:::%s"%self.keyPressMsg)
elif event.key()==Qt.Key_Tab:
self.keyPressMsg="\x09"
self.queue.put("uitouart:::%s"%self.keyPressMsg)
elif event.text()=="":
self.keyPressMsg="else"
return
else:
self.keyPressMsg="else"
if event.key()==Qt.Key_Return or event.key()==Qt.Key_Enter:
self.ui.cursor.movePosition(QTextCursor.End, QTextCursor.MoveAnchor)
self.moveCursor(QTextCursor.End)
self.queue.put("uitouart:::%s"%str(event.text()))
def uiRecvFromUart(self,data):
if data=="" or data==b'':
return
if (type(data) is bytes) and (str(data).find("b'\\xe")>=0):
self.keyPressMsg="else"
self.messycode=b''
self.isChinese=1
if self.isChinese==1:
if type(data) is bytes:
try:
self.messycode+=data
except:
self.messycode+=data.decode("utf-8")
if len(self.messycode)<3:
return
else:
try:
self.insertPlainText(self.messycode.decode("utf-8"))
except Exception as e:
print(e)
self.isChinese=0
return
else:
self.isChinese=0
self.messycode=b''
if self.keyPressMsg=="\x08" and self.ui.cursor.atEnd()==True:#Backspace
mycursor=self.textCursor()
self.recvdata+=data
if self.currentBoard=="microbit":
if self.recvdata=="\x08\x20\x08":
mycursor.deletePreviousChar()
self.recvdata=""
elif self.recvdata=="\x08\x08\x08\x08\x20\x20\x20\x20\x08\x08\x08\x08":
for i in range(0,4):
mycursor.deletePreviousChar()
self.recvdata=""
else:
if self.recvdata.find("\x08\x1b\x5b\x4b")==0:
mycursor.deletePreviousChar()
self.recvdata=""
elif self.recvdata.find("\x08\x1b\x5b\x4b")>0:
for i in range(self.recvdata.find("\x08\x1b\x5b\x4b")+1):
if self.recvdata[i]=="\x08":
mycursor.deletePreviousChar()
self.recvdata=""
elif self.keyPressMsg=="\x08" and not self.ui.cursor.atEnd():#Backspace
mycursor=self.textCursor()
self.recvdata+=data
if self.currentBoard=="microbit":
if len(self.recvdata)>5:
if self.recvdata[-2:]=="\x08\x08":
pass
elif self.recvdata[-1]=="\x08" and self.recvdata[-2]!="\x20":
for i in range(self.recvdata.find("\x20")):
mycursor.deletePreviousChar()
self.recvdata=""
self.keyPressMsg="else"
else:
pass
else:
pass
else:
if self.recvdata.find("\x1b\x5b")>0 and self.recvdata[-1]=="\x08":
for i in range(self.recvdata.find("\x1b\x5b")):
if self.recvdata[i]=="\x08":
mycursor.deletePreviousChar()
self.recvdata=""
self.keyPressMsg="else"
elif self.recvdata.count("\x1b\x5b")==2 and self.recvdata[-1]=="\x44":
for i in range(self.recvdata.find("\x1b\x5b")):
if self.recvdata[i]=="\x08":
mycursor.deletePreviousChar()
self.recvdata=""
#else:
# print("9999")
elif self.keyPressMsg=="\x09" and not self.ui.cursor.atEnd():#debug
self.recvdata+=data
if self.recvdata=="\x08":
lastLineCursorNum=self.ui.cursor.columnNumber()
allMsg=self.toPlainText()
showMsg=""
movecursorNum=0
allMsg=allMsg.split("\n")
for i in allMsg:
if i==allMsg[-1]:
showMsg+=i[0:-1]
movecursorNum=len(i[lastLineCursorNum:])
else:
showMsg+=i+"\n"
self.setPlainText(showMsg)
self.ui.cursorLeftOrRight-=movecursorNum
self.ui.cursor=self.textCursor()
self.moveCursor(QTextCursor.Left,QTextCursor.MoveAnchor)
self.recvdata=""
elif self.recvdata[0]=="\x1b":
if self.recvdata[-1]=="\x44":
movecursorNum=int(self.recvdata[self.recvdata.find("\x1b")+2:-1])
lastLineCursorNum=self.ui.cursor.columnNumber()
allMsg=self.toPlainText()
showMsg=""
allMsg=allMsg.split("\n")
for i in allMsg:
if i==allMsg[-1]:
showMsg+=i[0:lastLineCursorNum]
else:
showMsg+=i+"\n"
self.setPlainText(showMsg)
self.keyPressMsg="else"
self.recvdata=""
self.ui.cursorLeftOrRight-=movecursorNum
self.ui.cursor=self.textCursor()
self.moveCursor(QTextCursor.Left,QTextCursor.MoveAnchor)
else:
self.ui.cursor.insertText(self.recvdata)
self.recvdata=""
elif self.keyPressMsg=="\x1b\x5b\x33\x7e" and not self.ui.cursor.atEnd():#Delete
self.recvdata+=data
allMsg=self.toPlainText()
allMsg=allMsg.split("\n")
showMsg=""
lastLineCursorNum=self.ui.cursor.columnNumber()
if self.currentBoard=="microbit":
if (len(allMsg[-1])-lastLineCursorNum==1) and self.recvdata=="\x20\x08":
self.ui.cursor.deleteChar()
self.keyPressMsg="else"
self.recvdata=""
else:
if self.recvdata[-2:]=="\x08\x08":
pass
elif self.recvdata[-1]=="\x08" and self.recvdata[-2]!="\x20":
self.ui.cursor.deleteChar()
self.keyPressMsg="else"
self.recvdata=""
else:
pass
else:
if self.recvdata=="\x1b\x5b\x4b" and (len(allMsg[-1])-lastLineCursorNum==1):
self.ui.cursor.deleteChar()
self.keyPressMsg="else"
self.recvdata=""
elif len(self.recvdata)>3 and self.recvdata[-1]=="\x08":#这里,不应该让keypressmsg=else
self.ui.cursor.deleteChar()
self.keyPressMsg="else"
self.recvdata=""
elif len(self.recvdata)>3 and self.recvdata[3:].find("\x1b\x5b")>0 and self.recvdata[-1]=="\x44":
self.ui.cursor.deleteChar()
self.keyPressMsg="else"
self.recvdata=""
elif self.recvdata=="\x08":
self.recvdata=""
else:
pass
elif self.keyPressMsg=="\x1b\x5b\x44":#Key_Left
if data=="\x08":
self.ui.cursorLeftOrRight-=1
self.ui.cursor=self.textCursor()
self.moveCursor(QTextCursor.Left,QTextCursor.MoveAnchor)
elif self.keyPressMsg=="\x1b\x5b\x43":#Key_Right
self.ui.cursorLeftOrRight+=1
self.ui.cursor=self.textCursor()
self.moveCursor(QTextCursor.Right, QTextCursor.MoveAnchor)
elif self.keyPressMsg=="\x1b\x5b\x41":#Key_Up
if data == "\x08":
myBottomMsg=self.toPlainText()
msgNum=self.document().lineCount()
plainMsg=""
mySplite=myBottomMsg.split("\n")
for mysplite in mySplite:
msgNum-=1
if msgNum != 0:
plainMsg+=mysplite+"\n"
plainMsg+=">>> "
self.setPlainText(plainMsg)
elif data == "\x1b" or self.recvdata.find("\x1b")>=0:
self.recvdata+=data
if self.recvdata.find("[K")>=0:
self.recvdata=""
myBottomMsg=self.toPlainText()
msgNum=self.document().lineCount()
plainMsg=""
mySplite=myBottomMsg.split("\n")
for mysplite in mySplite:
msgNum-=1
if msgNum != 0:
plainMsg+=mysplite+"\n"
plainMsg+=">>> "
self.setPlainText(plainMsg)
elif self.recvdata.find("D")>=0:
self.recvdata=""
myBottomMsg=self.toPlainText()
msgNum=self.document().lineCount()
plainMsg=""
mySplite=myBottomMsg.split("\n")
for mysplite in mySplite:
msgNum-=1
if msgNum != 0:
plainMsg+=mysplite+"\n"
plainMsg+=">>> "
self.setPlainText(plainMsg)
else:
self.ui.cursor.insertText(data)
elif self.keyPressMsg=="\x1b\x5b\x42":#Key_Down
if data == "\x08":
myBottomMsg=self.toPlainText()
msgNum=self.document().lineCount()
plainMsg=""
mySplite=myBottomMsg.split("\n")
for mysplite in mySplite:
msgNum-=1
if msgNum != 0:
plainMsg+=mysplite+"\n"
plainMsg+=">>> "
self.setPlainText(plainMsg)
elif data == "\x1b" or self.recvdata.find("\x1b")>=0:
self.recvdata+=data
if self.recvdata.find("D")>=0:
self.recvdata=""
myBottomMsg=self.toPlainText()
msgNum=self.document().lineCount()
plainMsg=""
mySplite=myBottomMsg.split("\n")
for mysplite in mySplite:
msgNum-=1
if msgNum != 0:
plainMsg+=mysplite+"\n"
plainMsg+=">>> "
self.setPlainText(plainMsg)
elif self.recvdata.find("[K")>=0:
self.recvdata=""
myBottomMsg=self.toPlainText()
msgNum=self.document().lineCount()
plainMsg=""
mySplite=myBottomMsg.split("\n")
for mysplite in mySplite:
msgNum-=1
if msgNum != 0:
plainMsg+=mysplite+"\n"
plainMsg+=">>> "
self.setPlainText(plainMsg)
else:
self.ui.cursor.insertText(data)
else:
if not self.ui.cursor.atEnd():
self.recvdata+=data
if self.recvdata.find("\x08")>=0 and len(self.recvdata)>1 and self.recvdata[1:].find("\x08")>=0:
if self.messycode!=b'':
self.recvdata=''
self.messycode=b''
return
self.recvdata=self.recvdata[0]
self.ui.cursor.insertText(self.recvdata)
self.recvdata=""
elif self.recvdata=="\x08":
self.recvdata=""
elif self.recvdata.find("[")>0 and self.recvdata[-1]=="\x44":
if self.messycode!=b'':
self.recvdata=''
self.messycode=b''
return
self.recvdata=self.recvdata[0]
self.ui.cursor.insertText(self.recvdata)
self.recvdata=""
elif self.recvdata=="\x1b\x5b\x4b":
self.keyPressMsg="\x1b\x5b\x33\x7e"
else:
if data=="\n":
data=""
try:
self.insertPlainText(data)
except:
print('recv unexpected word.')
def mousePressEvent(self,event):
if event.button()==Qt.LeftButton:
self.startCursorPosition =event.pos()
self.cursor = self.cursorForPosition(self.startCursorPosition)
self.startPosition = self.cursor.position()
self.setCursor.emit()
def mouseMoveEvent(self,event):
if event.button()==Qt.NoButton:
self.terminalSelect=True
self.endCursorPosition = event.pos()
self.cursor = self.cursorForPosition(self.endCursorPosition)
position = self.cursor.position()
self.cursor.setPosition(self.startPosition)
self.cursor.setPosition(position, QTextCursor.KeepAnchor)
self.setTextCursor(self.cursor)
self.cursor.select(QTextCursor.WordUnderCursor)
def mouseReleaseEvent(self,event):
if event.button()==Qt.LeftButton:
pass
class myTreeView(QTreeView):
customContextMenuRequestedSig = pyqtSignal(QPoint)
def __init__(self,parent):
super(myTreeView,self).__init__(parent)
self.ui=parent
self.setHeaderHidden(True)
self.setStyleSheet("""
QTreeView{
background-color: qlineargradient(y1: 0, y2: 1,stop: 0 #0D0B0B, stop: 1 #5D5C5C);
border-width:1px;
border-color:#888888;
border-style:solid;
color:white;
}
QTreeView::branch:closed:has-children {
border-image: none;
image: url(':/treeBranchOpen.png');
}
QTreeView::branch:open:has-children {
border-image: none;
image: url(':/treeBranchClose.png');
}""")
self.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.setAcceptDrops(True)
self.setDragDropMode(QAbstractItemView.DragDrop)
self.filename=""
self.dragFrom=""
self.dropDir=""
#self.connect(self,SIGNAL("clicked(QModelIndex)"),self.chooseFile)
# self.connect(self,SIGNAL("pressed(QModelIndex)"),self.treepressed)
self.pressed.connect(self.treepressed)
def setmodel(self,model):
self.setModel(model)
#def mousePressEvent(self,event):
# if event.button()==Qt.LeftButton:
# print("left")
# elif event.button()==Qt.RightButton:
# print("right")
# else:
# print("else")
# return QMainWindow.eventFilter(self,event)
def createRightMenu(self):
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.runFile=QAction(self.tr("Run"),self)
self.runFile.triggered.connect(self.rightMenuRunFile)
self.openFile=QAction(self.tr("Open"),self)
self.openFile.triggered.connect(self.rightMenuOpenFile)
self.closeFile=QAction(self.tr("Close"),self)
self.closeFile.triggered.connect(self.rightMenuCloseFile)
self.deleteFile=QAction(self.tr("Delete"),self)
self.deleteFile.triggered.connect(self.rightMenuDeleteFile)
self.defaultProgram=QAction(self.tr("Default Run"),self)
self.defaultProgram.triggered.connect(self.rightMenuDefaultProgram)
self.rename=QAction(self.tr("Rename"),self)
self.rename.triggered.connect(self.rightMenuRename)
self.newDir=QAction(self.tr("New Dir"),self)
self.newDir.triggered.connect(self.rightMenuNewDir)
self.rightClickMenu=QMenu(self)
self.rightClickMenu.setStyleSheet(
"""QMenu{background-color:rgb(67,67,67);
color:white;}
QMenu::item{padding:4px 16px;}
QMenu::item::selected{background-color:rgb(127,127,127);}"""
)
self.rightClickMenu.addAction(self.runFile)
self.rightClickMenu.addAction(self.openFile)
self.rightClickMenu.addAction(self.closeFile)
self.rightClickMenu.addAction(self.deleteFile)
self.rightClickMenu.addAction(self.defaultProgram)
self.rightClickMenu.addAction(self.rename)
self.rightClickMenu.addAction(self.newDir)
self.customContextMenuRequestedSig.connect(self.slotRightClickMenu)
def slotRightClickMenu(self,point):
self.rightClickMenu.clear()
if (self.ui.fileName=="." or self.ui.fileName=="/flash") and self.ui.myserial.ser.isOpen():
self.rightClickMenu.addAction(self.newDir)
elif (self.ui.fileName=="." or self.ui.fileName=="/flash") and not self.ui.myserial.ser.isOpen():
pass
elif self.ui.fileName=="/sd" or \
(((sys.platform=="linux" and self.ui.fileName.find(rootDirectoryPath)>=0) or (sys.platform=="win32" and self.ui.fileName.find(":")>=0) or (sys.platform=="darwin" and self.ui.fileName.find(rootDirectoryPath)>=0)) and self.ui.fileName.split("/")[-1]=="uPy_lib"):
pass
elif ((sys.platform=="linux" and self.ui.fileName.find(rootDirectoryPath)>=0) or (sys.platform=="win32" and self.ui.fileName.find(":")>=0) or (sys.platform=="darwin" and self.ui.fileName.find(rootDirectoryPath)>=0)) and self.ui.fileName.find("uPy_lib")>0:
self.rightClickMenu.addAction(self.openFile)
self.rightClickMenu.addAction(self.closeFile)
elif ((sys.platform=="linux" and self.ui.fileName.find(rootDirectoryPath)>=0) or (sys.platform=="win32" and self.ui.fileName.find(":")>=0) or (sys.platform=="darwin" and self.ui.fileName.find(rootDirectoryPath)>=0)) and self.ui.fileName.split("/")[-1]=="workSpace":
self.rightClickMenu.addAction(self.openFile)
elif ((sys.platform=="linux" and self.ui.fileName.find(rootDirectoryPath)>=0) or (sys.platform=="win32" and self.ui.fileName.find(":")>=0) or (sys.platform=="darwin" and self.ui.fileName.find(rootDirectoryPath)>=0)) and self.ui.fileName.find("workSpace")>0:
self.rightClickMenu.addAction(self.openFile)
self.rightClickMenu.addAction(self.closeFile)
self.rightClickMenu.addAction(self.deleteFile)
else:
self.rightClickMenu.addAction(self.runFile)
self.rightClickMenu.addAction(self.openFile)
self.rightClickMenu.addAction(self.closeFile)
self.rightClickMenu.addAction(self.deleteFile)
self.rightClickMenu.addAction(self.defaultProgram)
self.rightClickMenu.addAction(self.rename)
self.rightClickMenu.addAction(self.newDir)
self.rightClickMenu.exec_(self.mapToGlobal(point))
def rightMenuRunFile(self):
self.ui.treeRightMenuRunFile()
def rightMenuOpenFile(self):
self.ui.treeRightMenuOpenFile()
def rightMenuCloseFile(self):
self.ui.treeRightMenuCloseFile()
def rightMenuDeleteFile(self):
self.ui.treeRightMenuDeleteFile()
def rightMenuDefaultProgram(self):
self.ui.treeRightMenuDefaultProgram()
def rightMenuRename(self):
self.ui.treeRightMenuRename()
def rightMenuNewDir(self):
self.ui.treeRightMenuNewDir()
def getQmodelIndexParent(self,index):
if index.data() != None:
self.ui.fileName="/"+str(index.data())+self.ui.fileName
self.getQmodelIndexParent(index.parent())
else:
return
def chooseFile(self,index):
self.ui.fileName=""
self.getQmodelIndexParent(index)
print("--4--%s"%self.ui.fileName)
dirList=self.ui.fileName.split("/")
if dirList[1]=="device":
self.ui.fileName=self.ui.fileName[7:]
self.ui.fileName=self.ui.rootDir+self.ui.fileName
elif dirList[1]=="sd":
pass
elif dirList[1]=="uPy_lib":
self.ui.fileName="%s/AppData/Local/uPyCraft/examples"%rootDirectoryPath+self.ui.fileName
elif dirList[1]=="workSpace":
if self.ui.createWorkSpacePath():
self.ui.fileName=self.ui.workspacePath[0:-10]+self.ui.fileName
self.ui.fileName=self.ui.fileName.replace("\\","/")
self.ui.fileName=self.ui.fileName.replace("//","/")
else:
self.ui.fileName=""
else:
pass
print("--5--%s"%self.ui.fileName)
def treepressed(self,index):
self.chooseFile(index)
self.dropDir=""
self.getDropDir(index)
def dragEnterEvent(self,event):
if event.mimeData().hasFormat("text/uri-list")==True:#External drag
self.dragFrom="External"
event.acceptProposedAction()
elif event.mimeData().hasFormat("application/x-qabstractitemmodeldatalist")==True:#Internal drag
self.dragFrom="Internal"
event.acceptProposedAction()
else:
self.dragFrom=""
event.ignore()
def dragMoveEvent(self,event):
pass
def dragLeaveEvent(self,event):
pass
def dropEvent(self,event):
if event.mimeData().hasUrls:
urls=event.mimeData().urls()
dropDownFileName=""
for url in urls:
dropDownFileName=url.toLocalFile()
index=self.indexAt(self.mapFrom(self,event.pos()))
if self.dragFrom=="External":
self.dropDir=""
self.getDropDir(index)
print("dropdir=%s"%self.dropDir)
if self.dropDir=="":
self.ui.terminal.append("drop to this place is not support")
return
filename=""
dorpdownfilename=""
dirList=self.dropDir.split("/")
if dirList[1]=="device" and str(dirList[-1]).find(".")<0:
self.dropDir=self.dropDir[7:]
dorpdownfilename=dropDownFileName.split("/")
filename=self.ui.rootDir+self.dropDir+"/"+dorpdownfilename[-1]
elif dirList[1]=="device" and str(dirList[-1]).find(".")>0:
self.dropDir=self.dropDir[7:str(self.dropDir).find(dirList[-1])-1]
dorpdownfilename=dropDownFileName.split("/")
filename=self.ui.rootDir+self.dropDir+"/"+dorpdownfilename[-1]
elif dirList[1]=="workSpace" and str(dirList[-1]).find(".")<0:
shutil.copyfile(dropDownFileName,self.ui.workspacePath[0:-10]+self.dropDir+"/"+dropDownFileName.split("/")[-1])
self.ui.createWorkSpaceMenu()
return
elif dirList[1]=="workSpace" and str(dirList[-1]).find(".")>0:
newPath=self.ui.workspacePath[0:-10]
for i in self.dropDir.split("/"):
if i=="":
pass
elif i==self.dropDir.split("/")[-1]:
break
else:
newPath=newPath+"/"+i
newPath=newPath+"/"+dropDownFileName.split("/")[-1]
shutil.copyfile(dropDownFileName,newPath)
self.ui.createWorkSpaceMenu()
return
else:
self.ui.terminal.append("drop to this place is not support")
return
if not self.ui.myserial.ser.isOpen():
self.ui.terminal.append("serial not open,can not download file")
return
self.ui.changeDragDropModel.emit(True)
self.ui.uitoctrlQueue.put("dropdownfile:::%s:::%s"%(filename,dropDownFileName))
print("filename=%s"%filename)
print("dropDownFileName=%s"%dropDownFileName)
elif self.dragFrom=="Internal":
if not self.ui.myserial.ser.isOpen():
self.ui.terminal.append("serial not open,can not drag file")
return
oldDragFileName=self.dropDir
print("oldDragFileName=%s"%oldDragFileName)
if oldDragFileName=="/device" or oldDragFileName=="/sd" or \
oldDragFileName=="/uPy_lib" or oldDragFileName=="workSpace":
self.ui.terminal.append("dir device,sd,uPy_lib can't be move.")
return
self.dropDir=""
self.getDropDir(index)
newDropFileName=self.dropDir
if self.dropDir=="":
self.ui.terminal.append("error place")
return
try:
purposeFileName=str(self.dropDir).split("/")[1]
except Exception:
self.ui.terminal.append("drag error")
return
if purposeFileName=="sd" or purposeFileName=="uPy_lib" or purposeFileName=="workSpace":
self.ui.terminal.append("file can't be move to sd or uPy_lib or workSpace.")
return
if str(oldDragFileName).find(".")<0:
self.ui.terminal.append("now version only suport drag file,no suport for dir.")
return
dirListDrag=oldDragFileName.split("/")
dirListDrop=newDropFileName.split("/")
if dirListDrag[1]=="device" and str(dirListDrop[-1]).find(".")<0:
if oldDragFileName[0:str(oldDragFileName).find(dirListDrag[-1])-1]==newDropFileName:
self.ui.terminal.append("the same dir,not move.")
return
oldDragFileName=oldDragFileName[7:]
oldDragFileName=self.ui.rootDir+oldDragFileName
newDropFileName=newDropFileName[7:]
newDropFileName=self.ui.rootDir+newDropFileName
self.ui.uitoctrlQueue.put("dragChangeDir:::%s:::%s"%(oldDragFileName,newDropFileName))
elif dirListDrag[1]=="device" and str(dirListDrop[-1]).find(".")>0:
if oldDragFileName[0:str(oldDragFileName).find(dirListDrag[-1])-1]\
==newDropFileName[0:str(newDropFileName).find(dirListDrop[-1])-1]:
self.ui.terminal.append("the same dir,not move.")
return
oldDragFileName=oldDragFileName[7:]
oldDragFileName=self.ui.rootDir+oldDragFileName
newDropFileName=newDropFileName[7:str(newDropFileName).find(dirListDrop[-1])-1]
newDropFileName=self.ui.rootDir+newDropFileName
self.ui.uitoctrlQueue.put("dragChangeDir:::%s:::%s"%(oldDragFileName,newDropFileName))
elif dirListDrag[1]=="sd":
pass
elif dirListDrag[1]=="uPy_lib" or dirListDrag[1]=="workSpace":#device外部拖入,需下载
if dirListDrag[1]=="uPy_lib":
oldDragFileName="%s/AppData/Local/uPyCraft/examples"%rootDirectoryPath+oldDragFileName
elif dirListDrag[1]=="workSpace":
oldDragFileName=self.ui.workspacePath[0:-10]+oldDragFileName
if str(dirListDrop[-1]).find(".")<0:
newDropFileName=newDropFileName[7:]
newDropFileName=self.ui.rootDir+newDropFileName
else:
newDropFileName=newDropFileName[7:str(newDropFileName).find(dirListDrop[-1])-1]
newDropFileName=self.ui.rootDir+newDropFileName
self.ui.changeDragDropModel.emit(True)
self.ui.uitoctrlQueue.put("dragChangeDir:::%s:::%s"%(oldDragFileName,newDropFileName))
else:
pass
else:
pass
else:
event.ignore()
def getDropDir(self,index):
if index.data() != None:
self.dropDir="/"+index.data()+self.dropDir
self.getDropDir(index.parent())
else:
return
class myTabWidget(QTabWidget):
def __init__(self,editorRightMenu,fileitem,parent):
super(myTabWidget,self).__init__(parent)
self.ui=parent
self.setAcceptDrops(True)
self.editorRightMenu=editorRightMenu
self.fileitem=fileitem
self.currentTab=-1
self.line=0
self.index=0
self.tabCloseRequested.connect(self.closeTab)
self.currentChanged.connect(self.currentTabChange)
def closeTab(self,tabId):
if tabId<0:
return
tabname = self.tabText(tabId)
tabTip = self.tabToolTip(tabId)
self.removeTab(tabId)
if tabname=="untitled":
pass
else:
if tabTip in self.fileitem.list:
self.fileitem.list.remove(tabTip)
self.fileitem.size-=1
def dragEnterEvent(self,event):
print("tabwidget dragenterevent1.")
if event.mimeData().hasFormat("text/uri-list")==True:#External drag
event.acceptProposedAction()
elif event.mimeData().hasFormat("application/x-qabstractitemmodeldatalist")==True:#Internal drag
event.ignore()
else:
event.ignore()
def dragMoveEvent(self,event):
event.acceptProposedAction()
def dragLeaveEvent(self,event):
pass
def dropEvent(self,event):
print("tabwidget dropevent1.")
if event.mimeData().hasUrls:
urls=event.mimeData().urls()
dropOpenFileName=""
for url in urls:
dropOpenFileName=url.toLocalFile()
print("dropOpenFileName=%s"%dropOpenFileName)
print(type(dropOpenFileName))
if dropOpenFileName.find(".py")<0 and\
dropOpenFileName.find(".txt")<0 and\
dropOpenFileName.find(".json")<0 and\
dropOpenFileName.find(".ini")<0:
print("current version can not open this file")
return
self.ui.pcOpenFile(dropOpenFileName)
def currentTabChange(self,tabId):
self.currentTab=tabId
print(self.currentTab)
def createNewTab(self,filename,msg,lexer):
if type(msg) is bytes:
msg=msg.decode(encoding='utf-8')
#if str(msg).find("\r\n")>=0:
# msg=msg.replace('\n','')
#elif str(msg).find("\n")>=0 and str(msg).find("\r")<0:
# msg=msg.replace("\n","\r")
#else:
# print("creatNewTab has other endswith.")
msg=msg.replace("\r\n","\r")
msg=msg.replace("\n","\r")
#editor=QsciScintilla()
editor=myQsciScintilla()
editor.setUtf8(True)
editor.setLexer(lexer)
editor.setMarginsBackgroundColor(QColor(39,43,48))#行号背景色
editor.setMarginsForegroundColor(QColor(255,255,255))#行号前景色
editor.setAutoCompletionThreshold(2)
editor.setAutoCompletionSource(QsciScintilla.AcsAll)
editor.setEolMode(QsciScintilla.EolUnix)
#显示缩进参考线
editor.SendScintilla(QsciScintilla.SCI_SETINDENTATIONGUIDES,QsciScintilla.SC_IV_LOOKFORWARD)
#设置匹配项的背景色
editor.setMatchedBraceBackgroundColor(QColor(30,120,184))
if str(filename).find("/")>=0:
tabname=filename.split("/")
tabname=tabname[-1]
elif str(filename)=="untitled":
tabname="untitled"
else:
tabname=filename
self.addTab(editor,tabname)
self.setTabToolTip(self.count()-1,filename)
if filename=="untitled":
self.tabBar().setTabTextColor(self.count()-1, QColor(Qt.red))
self.setTabIcon(self.count()-1, QIcon(':/pc.png'))
if self.ui.currentBoard=="microbit":
msg="from microbit import *\r#write your program:\r"
elif (sys.platform=="linux" and str(filename).find(rootDirectoryPath)>=0) or (sys.platform=="win32" and str(filename).find(":")>0) or (sys.platform=="darwin" and str(filename).find(rootDirectoryPath)>=0):
self.tabBar().setTabTextColor(self.count()-1, QColor(Qt.red))
self.setTabIcon(self.count()-1, QIcon(':/pc.png'))
else:
self.tabBar().setTabTextColor(self.count()-1, QColor(Qt.blue))
self.setTabIcon(self.count()-1, QIcon(':/ic.png'))
editor.setText(msg)
editor.setContextMenuPolicy(Qt.CustomContextMenu)
editor.customContextMenuRequested.connect(self.slotEditorRightClickMenu)
if self.editorRightMenu==None:
self.editorRightMenu=QMenu(self)
self.editorRightMenu.setStyleSheet("""
QMenu{background-color:rgb(67,67,67);
color:white;}
QMenu::item{padding:4px 16px;}
QMenu::item::selected{background-color:rgb(124,124,124);}
""")
undo=QAction(self.tr("Undo"),self)
undo.setShortcut("Ctrl+Z")
undo.triggered.connect(self.slotUndo)
redo=QAction(self.tr("Redo"),self)
redo.setShortcut("Ctrl+Y")
redo.triggered.connect(self.slotRedo)
cut=QAction(self.tr("Cut"),self)
cut.setShortcut("Ctrl+X")
cut.triggered.connect(self.slotCut)
copy=QAction(self.tr("Copy"),self)
copy.setShortcut("Ctrl+C")
copy.triggered.connect(self.slotCopy)
paste=QAction(self.tr("Paste"),self)
paste.setShortcut("Ctrl+V")
paste.triggered.connect(self.slotPaste)
self.editorRightMenu.addAction(undo)
self.editorRightMenu.addAction(redo)
self.editorRightMenu.addAction(cut)
self.editorRightMenu.addAction(copy)
self.editorRightMenu.addAction(paste)
#set brace match
editor.setBraceMatching(editor.StrictBraceMatch)
#set indent replace 4 space
editor.setIndentationsUseTabs(False)
editor.setTabWidth(2)
#The line number display area
editor.setMarginType(0, QsciScintilla.NumberMargin)
editor.setMarginLineNumbers(0, True)
editor.setMarginWidth(0,35)
#set auto indentation
editor.setAutoIndent(True)
#syntax check
editor.setMarginType(1, QsciScintilla.SymbolMargin)
editor.setMarginLineNumbers(1, False)
editor.setMarginWidth(1,5)
editor.setMarginSensitivity(1,False)
editor.setMarginMarkerMask(1,0x1FFFFFF)
editor.markerDefine(QsciScintilla.Background,1)
#set cursor color
editor.setCaretForegroundColor(QColor(255,255,255))
#Automatic folding area
editor.setFolding(QsciScintilla.PlainFoldStyle)
editor.setFoldMarginColors(QColor(39,43,48),QColor(39,43,48))
editor.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
editor.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
#set tab's stylesheet
editor.setStyleSheet("""QWidget{font-size:20px;border: 0px solid white;border-radius:1px;}""")