Advertisement
Guest User

Untitled

a guest
Apr 6th, 2021
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.88 KB | None | 0 0
  1. import os, sys
  2. import time
  3.  
  4. from PySide6.QtCore import Qt, QThread, QObject, QSize, QPointF
  5. from PySide6.QtCore import Signal, Slot
  6. from PySide6.QtCore import QUrl, QEvent, QPoint, QRect
  7. from PySide6.QtWidgets import QTextEdit, QFrame, QApplication
  8.  
  9. from PySide6.QtGui import QTextDocument, QTextCursor, QFont, QFontMetrics
  10. from PySide6.QtWidgets import QVBoxLayout, QHBoxLayout
  11.  
  12. import math
  13.  
  14. style_subs = '''   
  15.     QFrame {   
  16.        background-color: rgba(0, 0, 0, 128);
  17.         color: white;  
  18.        font-family: "Arial";
  19.     }  
  20. '''
  21.  
  22. class thread_subtitles(QObject):
  23.     update_subtitles = Signal(str)
  24.    
  25.     @Slot()
  26.     def main(self):
  27.         subs = ""
  28.         sub_file = "My first long sub, for example..."
  29.         changed = False
  30.         start_time = time.time()
  31.    
  32.         while 1:
  33.             time.sleep(0.01)
  34.            
  35.             if time.time() - start_time > 6 and changed is False:
  36.                 print("---- Ready for new sub!")
  37.                 sub_file = "Shorter one!"
  38.                 changed = True
  39.            
  40.             if sub_file != subs:
  41.                 print("EMITTING")
  42.                 self.update_subtitles.emit(sub_file)
  43.                
  44.                 subs = sub_file
  45.  
  46.  
  47. class drawing_layer(QTextEdit):
  48.     def __init__(self, parent=None):
  49.         super().__init__()
  50.        
  51.         self.setReadOnly(True)
  52.         self.setCursorWidth(0)
  53.        
  54.         self.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
  55.         self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
  56.        
  57.         self.setAlignment(Qt.AlignVCenter)
  58.        
  59.         self.setLineWrapMode(QTextEdit.LineWrapMode.NoWrap)
  60.        
  61.         self.document().setDocumentMargin(0)
  62.         self.setContentsMargins(0, 0, 0, 0)
  63.        
  64.         self.verticalScrollBar().setEnabled(False)
  65.         self.horizontalScrollBar().setEnabled(False)
  66.        
  67.         self.n_lines = 1
  68.        
  69.         self.longest_line = 10
  70.        
  71.         font = self.currentFont()
  72.         font.setPointSize(50)
  73.         font.setStyleStrategy(QFont.StyleStrategy.PreferAntialias)
  74.         self.setFont(font)
  75.        
  76.     def update_subs(self, text):
  77.         print("call update_subs")
  78.         self.text_splitted = text.split('\n')
  79.        
  80.         self.n_lines = len(self.text_splitted)
  81.         self.longest_line = max(self.text_splitted, key=len)
  82.        
  83.         self.len_text = len(text)
  84.        
  85.         for line in self.text_splitted:
  86.             self.append(line)
  87.             self.setAlignment(Qt.AlignCenter)
  88.         print("end update_subs")
  89.        
  90.     def event(self, ev):
  91.         print("QTextEdit:", ev.type())
  92.         res = super().event(ev)
  93.         return res
  94.    
  95.     def paintEvent(self, event):
  96.         print("Sleeping 1 sec in `paintEvent` for QTextEdit...")
  97.         time.sleep(1)
  98.         super().paintEvent(event)
  99.    
  100.     def minimumSizeHint(self):
  101.         return QSize(5, 5)
  102.    
  103. class main_class(QFrame):
  104.     def __init__(self, config):
  105.         super().__init__(parent=None)
  106.        
  107.         print("IN CONSTRUCTOR")
  108.         self.thread_subs = QThread()
  109.         self.obj = thread_subtitles()
  110.         self.obj.update_subtitles.connect(self.render_subtitles)
  111.         self.obj.moveToThread(self.thread_subs)
  112.         self.thread_subs.started.connect(self.obj.main)
  113.         self.thread_subs.start()
  114.        
  115.         self.config = config
  116.        
  117.         #self.setWindowFlags(Qt.X11BypassWindowManagerHint)
  118.        
  119.         self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground, True);
  120.         self.setStyleSheet(style_subs)
  121.        
  122.         self.subtext = drawing_layer(parent=self)
  123.        
  124.         self.subtitles_vbox = QVBoxLayout(self)
  125.         self.subtitles_vbox.addStretch()
  126.         self.subtitles_vbox.setContentsMargins(0, 0, 0, 0)
  127.        
  128.         hbox = QHBoxLayout()
  129.         hbox.addWidget(self.subtext)
  130.        
  131.         self.subtitles_vbox.addLayout(hbox)
  132.         self.subtitles_vbox.addStretch()
  133.        
  134.         self.stretch_pixels = 10
  135.            
  136.     def render_subtitles(self, text):
  137.         print("-------")
  138.         print("Input:", text)
  139.        
  140.         self.subtext.clear()
  141.        
  142.         subs2 = text
  143.        
  144.         subs2 = subs2.split('\n')
  145.         for i in range(len(subs2)):
  146.             subs2[i] = " " + subs2[i] + " "
  147.        
  148.         subs2 = '\n'.join(subs2)
  149.        
  150.         self.subtext.update_subs(subs2)
  151.        
  152.         self.subtext.resize(QSize(
  153.             self.subtext.fontMetrics().boundingRect(self.subtext.longest_line).width() + 4,
  154.             self.subtext.fontMetrics().height() * self.subtext.n_lines + 4
  155.             ))
  156.        
  157.         self.resize(QSize(
  158.             self.subtext.width() + self.stretch_pixels,
  159.             self.subtext.height() + self.stretch_pixels
  160.             ))
  161.        
  162.         # This "hide" can help and solve the weird behavior. However, unless
  163.         # this time.sleep is set below, the next text will not be displayed...
  164.        
  165.         #self.hide()
  166.         #time.sleep(0.01)
  167.        
  168.         x = (self.config['screen_width']/2) - (self.width()/2)
  169.         y = self.config['screen_height'] - self.height() - 100
  170.        
  171.         self.move(int(x), int(y))
  172.        
  173.         print("CALLING SHOW")
  174.         self.show()
  175.        
  176.         self.updateGeometry()
  177.  
  178.     def event(self, ev):
  179.         print("QFrame:", ev.type())
  180.         res = super().event(ev)
  181.         return res
  182.        
  183.     def paintEvent(self, event):
  184.         print("Sleeping 1 sec in `paintEvent` for QFrame...")
  185.         time.sleep(1)
  186.         super().paintEvent(event)
  187.    
  188.    
  189. if __name__ == "__main__":        
  190.     app = QApplication(sys.argv)
  191.    
  192.     config = {}
  193.     config['screen_width'] = app.primaryScreen().size().width()
  194.     config['screen_height'] = app.primaryScreen().size().height()
  195.    
  196.     form = main_class(config)
  197.    
  198.     sys.exit(app.exec_())
  199.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement