Skip to content

Commit

Permalink
fix(thread): fix thread management in main scene
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanCarollo committed Mar 18, 2024
1 parent 506aa10 commit 107a816
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
4 changes: 2 additions & 2 deletions window/scene/LoadingScene.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import math

# This is the loading scene called after the MainScene, this will need some improvements
class LoadingScene(QWidget, TranscriptionObserver):
class LoadingScene(QWidget):
def __init__(self):
super().__init__()

Expand Down Expand Up @@ -59,7 +59,7 @@ def __init__(self):
self.showDownloadState()

# Here it is the function update that came from the TranscriptionObserver
def update(self, state):
def updateState(self, state):
if state == TranscriptionState.IDLE:
self.showIdleState()
pass
Expand Down
4 changes: 2 additions & 2 deletions window/scene/MainScene.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtGui import QDragEnterEvent, QDropEvent
from Utils import Utils
from window.scene.mainScene.DragAndDrop import DragAndDrop
from window.scene.mainScene.UserInputForTranscriptionWidget import UserInputForTranscriptionWidget
import threading


Expand All @@ -15,7 +15,7 @@ def __init__(self):
self.resize(1280, 556)
self.center()

self.dragDiv = DragAndDrop()
self.dragDiv = UserInputForTranscriptionWidget()

hLayout = QHBoxLayout()
hLayout.addWidget(self.dragDiv)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@
from window.scene.LoadingScene import *
import threading

class TranscribeVideoThread(QThread):
# This thread will also observe the transcription to signal to the UserInputForTranscriptionWidget that
# the state changed
class TranscribeVideoThread(QThread, TranscriptionObserver):
# Here, we signal that the pyqtSignal take 1 arg and it is a list
finished_signal = pyqtSignal(list)
update_state_signal = pyqtSignal(TranscriptionState)

def __init__(self, url, loadingScene):
def __init__(self, url):
super().__init__()
self.url = url
self.loadingScene = loadingScene;

def run(self):
trController = TranscriptionController.getInstance()
trController.addSubscriber(self.loadingScene)
trController.addSubscriber(self)
result = trController.startTranscription(self.url);
trController.removeSubscriber(self.loadingScene)
trController.removeSubscriber(self)
self.finished_signal.emit(result["segments"])

class DragAndDrop(QWidget):
def update(self, state):
self.update_state_signal.emit(state)

class UserInputForTranscriptionWidget(QWidget):
def __init__(self):
super().__init__()
self.setAcceptDrops(True)
Expand Down Expand Up @@ -75,6 +79,7 @@ def __init__(self):
self.vLayout.addWidget(self.divWidget)
self.setLayout(self.vLayout)

self.loadingScene = None


# ---------------------------------------------------------------------------- #
Expand Down Expand Up @@ -105,17 +110,22 @@ def dropEvent(self, event: QDropEvent):
# This is the function called when we click on the button
def launchTranscription(self, url):

loadingScene = LoadingScene()
self.transcribe_thread = TranscribeVideoThread(url, loadingScene)
self.loadingScene = LoadingScene()
self.transcribe_thread = TranscribeVideoThread(url)
# Connect the callback to the finished signal !
self.transcribe_thread.finished_signal.connect(self.nextPage)
self.transcribe_thread.update_state_signal.connect(self.updateLoadingSceneState)
# TODO : ADD A NEW UPDATE STATE SIGNAL AND CONNECT IT TO THE LOADING SCENE
self.transcribe_thread.start()

# To avoid circular import here lol
from window.SceneManager import SceneManager
SceneManager.getInstance().newScene(loadingScene)
SceneManager.getInstance().newScene(self.loadingScene)

def updateLoadingSceneState(self, state):
self.loadingScene.updateState(state)
DLog.goodlog("STATE UPDATED IN THREAD")
pass

def nextPage(self, result: list):
# Cause of circular import, i can't import SceneManager at the module level (and it's normal lol)
Expand Down

0 comments on commit 107a816

Please sign in to comment.