Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os, sys
- import time
- from PySide6.QtCore import Qt, QThread, QObject, QSize, QPointF
- from PySide6.QtCore import Signal, Slot
- from PySide6.QtCore import QUrl, QEvent, QPoint, QRect
- from PySide6.QtWidgets import QTextEdit, QFrame, QApplication
- from PySide6.QtGui import QTextDocument, QTextCursor, QFont, QFontMetrics
- from PySide6.QtWidgets import QVBoxLayout, QHBoxLayout
- import math
- style_subs = '''
- QFrame {
- background-color: rgba(0, 0, 0, 128);
- color: white;
- font-family: "Arial";
- }
- '''
- class thread_subtitles(QObject):
- update_subtitles = Signal(str)
- @Slot()
- def main(self):
- subs = ""
- sub_file = "My first long sub, for example..."
- changed = False
- start_time = time.time()
- while 1:
- time.sleep(0.01)
- if time.time() - start_time > 6 and changed is False:
- print("---- Ready for new sub!")
- sub_file = "Shorter one!"
- changed = True
- if sub_file != subs:
- print("EMITTING")
- self.update_subtitles.emit(sub_file)
- subs = sub_file
- class drawing_layer(QTextEdit):
- def __init__(self, parent=None):
- super().__init__()
- self.setReadOnly(True)
- self.setCursorWidth(0)
- self.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
- self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
- self.setAlignment(Qt.AlignVCenter)
- self.setLineWrapMode(QTextEdit.LineWrapMode.NoWrap)
- self.document().setDocumentMargin(0)
- self.setContentsMargins(0, 0, 0, 0)
- self.verticalScrollBar().setEnabled(False)
- self.horizontalScrollBar().setEnabled(False)
- self.n_lines = 1
- self.longest_line = 10
- font = self.currentFont()
- font.setPointSize(50)
- font.setStyleStrategy(QFont.StyleStrategy.PreferAntialias)
- self.setFont(font)
- def update_subs(self, text):
- print("call update_subs")
- self.text_splitted = text.split('\n')
- self.n_lines = len(self.text_splitted)
- self.longest_line = max(self.text_splitted, key=len)
- self.len_text = len(text)
- for line in self.text_splitted:
- self.append(line)
- self.setAlignment(Qt.AlignCenter)
- print("end update_subs")
- def event(self, ev):
- print("QTextEdit:", ev.type())
- res = super().event(ev)
- return res
- def paintEvent(self, event):
- print("Sleeping 1 sec in `paintEvent` for QTextEdit...")
- time.sleep(1)
- super().paintEvent(event)
- def minimumSizeHint(self):
- return QSize(5, 5)
- class main_class(QFrame):
- def __init__(self, config):
- super().__init__(parent=None)
- print("IN CONSTRUCTOR")
- self.thread_subs = QThread()
- self.obj = thread_subtitles()
- self.obj.update_subtitles.connect(self.render_subtitles)
- self.obj.moveToThread(self.thread_subs)
- self.thread_subs.started.connect(self.obj.main)
- self.thread_subs.start()
- self.config = config
- #self.setWindowFlags(Qt.X11BypassWindowManagerHint)
- self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground, True);
- self.setStyleSheet(style_subs)
- self.subtext = drawing_layer(parent=self)
- self.subtitles_vbox = QVBoxLayout(self)
- self.subtitles_vbox.addStretch()
- self.subtitles_vbox.setContentsMargins(0, 0, 0, 0)
- hbox = QHBoxLayout()
- hbox.addWidget(self.subtext)
- self.subtitles_vbox.addLayout(hbox)
- self.subtitles_vbox.addStretch()
- self.stretch_pixels = 10
- def render_subtitles(self, text):
- print("-------")
- print("Input:", text)
- self.subtext.clear()
- subs2 = text
- subs2 = subs2.split('\n')
- for i in range(len(subs2)):
- subs2[i] = " " + subs2[i] + " "
- subs2 = '\n'.join(subs2)
- self.subtext.update_subs(subs2)
- self.subtext.resize(QSize(
- self.subtext.fontMetrics().boundingRect(self.subtext.longest_line).width() + 4,
- self.subtext.fontMetrics().height() * self.subtext.n_lines + 4
- ))
- self.resize(QSize(
- self.subtext.width() + self.stretch_pixels,
- self.subtext.height() + self.stretch_pixels
- ))
- # This "hide" can help and solve the weird behavior. However, unless
- # this time.sleep is set below, the next text will not be displayed...
- #self.hide()
- #time.sleep(0.01)
- x = (self.config['screen_width']/2) - (self.width()/2)
- y = self.config['screen_height'] - self.height() - 100
- self.move(int(x), int(y))
- print("CALLING SHOW")
- self.show()
- self.updateGeometry()
- def event(self, ev):
- print("QFrame:", ev.type())
- res = super().event(ev)
- return res
- def paintEvent(self, event):
- print("Sleeping 1 sec in `paintEvent` for QFrame...")
- time.sleep(1)
- super().paintEvent(event)
- if __name__ == "__main__":
- app = QApplication(sys.argv)
- config = {}
- config['screen_width'] = app.primaryScreen().size().width()
- config['screen_height'] = app.primaryScreen().size().height()
- form = main_class(config)
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement