|
| 1 | +import os |
| 2 | +from PyQt6.QtWidgets import ( |
| 3 | + QMenu, |
| 4 | + QWidget, |
| 5 | + QMenuBar, |
| 6 | + QMainWindow, |
| 7 | + QHBoxLayout, |
| 8 | + QFileDialog, |
| 9 | + QMessageBox, |
| 10 | +) |
| 11 | + |
| 12 | +from PyQt6.QtCore import QTimer, Qt |
| 13 | +from src.image_model import ImageModel |
| 14 | +from PyQt6.QtGui import QKeySequence, QAction |
| 15 | +from src.image_gallery_widget import ImageGalleryWidget |
| 16 | +from src.thumbnails_list_widget import ThumbnailListWidget |
| 17 | + |
| 18 | + |
| 19 | +class MainWindow(QMainWindow): |
| 20 | + """ |
| 21 | + Combines the ImageGalleryWidget (center), |
| 22 | + the ThumbnailsListWidget (left), and a member for |
| 23 | + opening folders and starting/stopping a slideshow |
| 24 | + """ |
| 25 | + |
| 26 | + def __init__(self): |
| 27 | + super().__init__() |
| 28 | + self.setWindowTitle("Customizable Image Gallery") |
| 29 | + self.resize(1200, 800) |
| 30 | + |
| 31 | + # Model |
| 32 | + self._model: ImageModel = ImageModel() |
| 33 | + |
| 34 | + # Widgets |
| 35 | + self._image_gallery_widget = ImageGalleryWidget(self._model) |
| 36 | + self._thumbnails_list = ThumbnailListWidget(self.on_thumbnails_selected) |
| 37 | + self._model.set_images([], self._thumbnails_list) |
| 38 | + # Timer for Slideshow |
| 39 | + self._slideshow_timer = QTimer() |
| 40 | + self._slideshow_timer.setInterval(2000) # 2 seconds per image |
| 41 | + self._slideshow_timer.timeout.connect(self.handle_slideshow_step) |
| 42 | + self._slideshow_running = True |
| 43 | + |
| 44 | + # Layout |
| 45 | + central_widget = QWidget() |
| 46 | + main_layout = QHBoxLayout() |
| 47 | + main_layout.addWidget(self._thumbnails_list, stretch=1) |
| 48 | + main_layout.addWidget(self._image_gallery_widget, stretch=3) |
| 49 | + central_widget.setLayout(main_layout) |
| 50 | + self.setCentralWidget(central_widget) |
| 51 | + |
| 52 | + # Menubar |
| 53 | + menubar = self.menuBar() if self.menuBar() else QMenuBar(self) |
| 54 | + file_menu = menubar.addMenu("File") |
| 55 | + slideshow_menu = menubar.addMenu("Slideshow") |
| 56 | + |
| 57 | + open_folder_action = QAction("Open Folder", self) |
| 58 | + open_folder_action.triggered.connect(self.open_folder) |
| 59 | + file_menu.addAction(open_folder_action) |
| 60 | + |
| 61 | + start_slideshow_action = QAction("Start Slideshow", self) |
| 62 | + start_slideshow_action.triggered.connect(self.start_slideshow) |
| 63 | + slideshow_menu.addAction(start_slideshow_action) |
| 64 | + |
| 65 | + stop_slideshow_action = QAction("Stop Slideshow", self) |
| 66 | + stop_slideshow_action.triggered.connect(self.stop_slideshow) |
| 67 | + slideshow_menu.addAction(stop_slideshow_action) |
| 68 | + |
| 69 | + # Keyboard shortcut (Left/Right arrow keys) |
| 70 | + prev_action = QAction("Previous", self) |
| 71 | + prev_action.setShortcut(QKeySequence(Qt.Key.Key_Left)) |
| 72 | + prev_action.triggered.connect(self.show_previous_image) |
| 73 | + self.addAction(prev_action) |
| 74 | + |
| 75 | + next_action = QAction("Next", self) |
| 76 | + next_action.setShortcut(QKeySequence(Qt.Key.Key_Right)) |
| 77 | + next_action.triggered.connect(self.show_next_image) |
| 78 | + self.addAction(next_action) |
| 79 | + |
| 80 | + def open_folder(self): |
| 81 | + """ |
| 82 | + Opens a folder dialog and loads images into the model |
| 83 | + """ |
| 84 | + folder_path = QFileDialog.getExistingDirectory( |
| 85 | + self, |
| 86 | + "Select Folder", |
| 87 | + ) |
| 88 | + if folder_path: |
| 89 | + valid_extensions = { |
| 90 | + ".png", |
| 91 | + ".jpg", |
| 92 | + ".jpeg", |
| 93 | + ".bmp", |
| 94 | + ".gif", |
| 95 | + } |
| 96 | + image_paths = [ |
| 97 | + os.path.join(folder_path, f) |
| 98 | + for f in os.listdir(folder_path) |
| 99 | + if os.path.splitext(f.lower())[1] in valid_extensions |
| 100 | + ] |
| 101 | + image_paths.sort() |
| 102 | + |
| 103 | + if not image_paths: |
| 104 | + QMessageBox.warning(self, "Warning", "No images found in this folder") |
| 105 | + return |
| 106 | + |
| 107 | + self._model.set_images(image_paths, self._thumbnails_list) |
| 108 | + |
| 109 | + # Update UI |
| 110 | + self._image_gallery_widget.load_current_image() |
| 111 | + self._thumbnails_list.populate(image_paths) |
| 112 | + self._thumbnails_list.select_index(self._model._current_index) |
| 113 | + |
| 114 | + def start_slideshow(self): |
| 115 | + if self._model._image_paths: |
| 116 | + self._slideshow_timer.start() |
| 117 | + self._slideshow_running = True |
| 118 | + |
| 119 | + def stop_slideshow(self): |
| 120 | + self._slideshow_timer.stop() |
| 121 | + self._slideshow_running = False |
| 122 | + |
| 123 | + def handle_slideshow_step(self): |
| 124 | + """ |
| 125 | + Move to the next image automatically. If we reach the end, wrap around. |
| 126 | + """ |
| 127 | + |
| 128 | + if not self._model._image_paths: |
| 129 | + return |
| 130 | + |
| 131 | + if self._model._current_index >= len(self._model._image_paths) - 1: |
| 132 | + # Wrap to First |
| 133 | + self._model._current_index = 0 |
| 134 | + else: |
| 135 | + self._model.next_image() |
| 136 | + |
| 137 | + self.update_display() |
| 138 | + |
| 139 | + def on_thumbnails_selected(self, index): |
| 140 | + """ |
| 141 | + Called when user selects a thumbnail in the list. |
| 142 | + """ |
| 143 | + self._model.jump_to_index(index) |
| 144 | + self.update_display() |
| 145 | + |
| 146 | + def show_previous_image(self): |
| 147 | + self._model.previous_image() |
| 148 | + self.update_display() |
| 149 | + |
| 150 | + def show_next_image(self): |
| 151 | + self._model.next_image() |
| 152 | + self.update_display() |
| 153 | + |
| 154 | + def update_display(self): |
| 155 | + self._image_gallery_widget.load_current_image() |
| 156 | + self._thumbnails_list.select_index(self._model._current_index) |
0 commit comments