From f1b078affce5b7a1634c337951aec90364945428 Mon Sep 17 00:00:00 2001 From: grimhilt Date: Tue, 1 Aug 2023 21:24:34 +0200 Subject: [PATCH] make everything work with screen manager --- src/api/abl/playlist.py | 11 +++++++++ src/index.py | 7 +++--- src/screen/ScreenManager.py | 38 ++++++++++++++++++++++++++++++ src/screen/SlideShow.py | 35 ++++++++++++++++++++++++++++ src/screen/slideshow.py | 46 ------------------------------------- 5 files changed, 88 insertions(+), 49 deletions(-) create mode 100644 src/screen/ScreenManager.py create mode 100644 src/screen/SlideShow.py delete mode 100644 src/screen/slideshow.py diff --git a/src/api/abl/playlist.py b/src/api/abl/playlist.py index 7fba8f0..aadd637 100644 --- a/src/api/abl/playlist.py +++ b/src/api/abl/playlist.py @@ -86,3 +86,14 @@ def update(playlist_id): return jsonify(success=True) +@playlist.route('//activate', methods=["POST"]) +def activate(playlist_id): + screen_manager = ScreenManager.getInstance() + screen_manager.activate_playlist(playlist_id) + return jsonify(success=True) + +@playlist.route('//disactivate', methods=["POST"]) +def disactivate(playlist_id): + screen_manager = ScreenManager.getInstance() + screen_manager.disactivate_playlist() + return jsonify(success=True) diff --git a/src/index.py b/src/index.py index ba70c5a..d6d4dd2 100644 --- a/src/index.py +++ b/src/index.py @@ -1,10 +1,11 @@ from api import create_api +from screen.ScreenManager import ScreenManager - - api = create_api() +screen_manager = ScreenManager().getInstance() if __name__ == '__main__': - api.run(host="0.0.0.0", port=5500, debug=True) + #api.run(host="0.0.0.0", port=5500, debug=True) + api.run(host="0.0.0.0", port=5500) diff --git a/src/screen/ScreenManager.py b/src/screen/ScreenManager.py new file mode 100644 index 0000000..617dcb5 --- /dev/null +++ b/src/screen/ScreenManager.py @@ -0,0 +1,38 @@ +import tkinter as tk +from .SlideShow import SlideShow +from api.dao.Playlist import PlaylistDao +from multiprocessing import Process + +def create_slideshow(files): + root = tk.Tk() + root.title("Slideshow") + SlideShow(root, files) + root.mainloop() + +class ScreenManager: + __instance = None + __slideshow_process = None + + @staticmethod + def getInstance(): + if ScreenManager.__instance == None: + SreenManager() + return ScreenManager.__instance + + def __init__(self): + if ScreenManager.__instance != None: + raise Exception("Screen manager already exists!") + else: + ScreenManager.__instance = self + + def activate_playlist(self, playlist_id): + (_, files) = PlaylistDao.get_playlist(playlist_id) + self.disactivate_playlist() + x = Process(target=create_slideshow, args=(files,)) + x.start() + self.__slideshow_process = x + + def disactivate_playlist(self): + if self.__slideshow_process: + self.__slideshow_process.terminate() + self.__slideshow_process = None diff --git a/src/screen/SlideShow.py b/src/screen/SlideShow.py new file mode 100644 index 0000000..e4b8293 --- /dev/null +++ b/src/screen/SlideShow.py @@ -0,0 +1,35 @@ +import tkinter as tk +from PIL import ImageTk, Image +import time + +class SlideShow: + def __init__(self, root, files): + self.root = root + self.files = files + self.idx = 0 + + self.image_label = tk.Label(root) + self.image_label.pack() + self.image_label.pack(fill=tk.BOTH, expand=True) + + self.root.after(100, self.next_file) + + def next_file(self): + file = self.files[self.idx] + path = './data/' + file['name'] + image = Image.open(path) + + # Get root window width and height + screen_width = self.root.winfo_width() + screen_height = self.root.winfo_height() + + # Resize the image to fit the screen + image = image.resize((screen_width, screen_height), Image.ANTIALIAS) + + photo = ImageTk.PhotoImage(image) + self.image_label.config(image=photo) + self.image_label.image = photo + + self.idx = (self.idx + 1) % len(self.files) + self.root.after(file['seconds'] * 1000, self.next_file) + diff --git a/src/screen/slideshow.py b/src/screen/slideshow.py deleted file mode 100644 index 7abbb89..0000000 --- a/src/screen/slideshow.py +++ /dev/null @@ -1,46 +0,0 @@ -import tkinter as tk -from PIL import ImageTk, Image -import os -import time - -class SlideshowApp: - def __init__(self, root, image_paths): - self.root = root - self.paths = image_paths - self.idx = 0 - - self.image_label = tk.Label(root) - self.image_label.pack() - self.image_label.pack(fill=tk.BOTH, expand=True) - - self.show_next_image() - - def show_next_image(self): - img_path = self.paths[self.idx] - image = Image.open(img_path) - - # Get root window width and height - screen_width = self.root.winfo_width() - screen_height = self.root.winfo_height() - - # Resize the image to fit the screen - image = image.resize((screen_width, screen_height), Image.ANTIALIAS) - - photo = ImageTk.PhotoImage(image) - self.image_label.config(image=photo) - self.image_label.image = photo - - self.idx = (self.idx + 1) % len(self.paths) - self.root.after(2000, self.show_next_image) - -def main(): - root = tk.Tk() - root.title("Image Slideshow") - - - img_paths = ["./data/960x0-1053101061.jpg", "./data/my-linux-desktop-1131547615.png", "./data/glzrkk83f4621-1200x671-54057802.jpg", "./data/yjdoiycuw04lvrebijtw-605729284.jpg"] - app = SlideshowApp(root, img_paths) - root.mainloop() - -if __name__ == "__main__": - main()