diff options
author | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2024-08-04 23:35:06 -0700 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2024-08-04 23:35:06 -0700 |
commit | 613c8900924ea79a195b3da3cefa50d3a2bd3e58 (patch) | |
tree | db541d20158cb333f829bab154d444f7fe8e7d65 /App.py |
Diffstat (limited to 'App.py')
-rw-r--r-- | App.py | 210 |
1 files changed, 210 insertions, 0 deletions
@@ -0,0 +1,210 @@ +import sys +from os.path import basename +import webbrowser + +from PySide6.QtCore import Qt, QSize, QTimer +from PySide6.QtWidgets import ( + QApplication, QMainWindow, + QVBoxLayout, QHBoxLayout, + QWidget, QFileDialog, + QLabel, QPushButton, + QCheckBox, QMessageBox, +) + +from Konvert import ls_size, process_dir + +APP_VERSION = "0.1.0" + +class DirectoryPicker(QWidget): + def __init__(self): + super().__init__() + + self.selected_dir = "" + + layout = QHBoxLayout() + layout.setContentsMargins(0, 0, 0, 0) + + layout.addWidget(QLabel("Folder:")) + + self.select_btn = QPushButton("Browse") + self.select_btn.clicked.connect(self.select_dir) + layout.addWidget(self.select_btn) + + self.setLayout(layout) + + def select_dir(self): + self.selected_dir = str(QFileDialog.getExistingDirectory(self, "Select a Folder")) + + if self.selected_dir == "": + self.select_btn.setText("Browse") + else: + self.select_btn.setText(basename(self.selected_dir)) + + +class DirectoryInfo(QWidget): + def __init__(self): + super().__init__() + + self.size = 0 + + layout = QVBoxLayout() + layout.setContentsMargins(0, 0, 0, 0) + + self.directory_name_label = QLabel("") + self.directory_name_label.setWordWrap(True) + layout.addWidget(self.directory_name_label) + + self.directory_size_label = QLabel("") + self.directory_size_label.setWordWrap(True) + layout.addWidget(self.directory_size_label) + + self.setLayout(layout) + + def update_data(self, _dir, recurse=False): + if _dir == "": + self.directory_name_label.setText("") + self.directory_size_label.setText("") + self.size = 0 + return + + self.size = ls_size(_dir, recurse) + + self.directory_name_label.setText(f"{_dir}") + self.directory_size_label.setText(f"{self.size} Image(s) Found.") + + +class KonvertAboutWindow(QWidget): + def __init__(self): + super().__init__() + + self.setWindowTitle(f"About - Konvert {APP_VERSION}") + self.setFixedSize(QSize(300, 180)) + + layout = QVBoxLayout() + + label = QLabel( + "Konvert - Easy way to bulk convert image files.\n" + + f"Version - {APP_VERSION}\n\n" + + "Copyright (c) 2024 Vidhu Kant Sharma\n" + + "This program comes with absolutely no warranty.\n" + + "See GNU GPL Version 3 or later for more details." + ) + layout.addWidget(label) + + view_source_btn = QPushButton("View Source Code") + view_source_btn.clicked.connect(lambda: webbrowser.open("https://git.vidhukant.com/konvert")) + layout.addWidget(view_source_btn) + + view_license_btn = QPushButton("View License") + view_license_btn.clicked.connect(lambda: webbrowser.open("https://git.vidhukant.com/konvert/tree/LICENSE")) + layout.addWidget(view_license_btn) + + self.setLayout(layout) + + +class KonvertMainWindow(QMainWindow): + def __init__(self): + super().__init__() + + self.setWindowTitle("Konvert") + self.setFixedSize(QSize(350, 230)) + + self.about_window = None + + self.recurse = True + self.delete_original = False + + menu = self.menuBar() + + about_menubar_btn = menu.addAction("&About") + about_menubar_btn.triggered.connect(lambda: self.toggle_about_page()) + + exit_menubar_btn = menu.addAction("&Exit") + exit_menubar_btn.triggered.connect(lambda: self.close()) + + layout = QVBoxLayout() + + self.directory_picker = DirectoryPicker() + self.directory_picker.select_btn.clicked.connect(self.update_dir) + layout.addWidget(self.directory_picker) + + recurse_checkbox = QCheckBox() + recurse_checkbox.setText("Also search inside subfolders") + recurse_checkbox.setCheckState(Qt.CheckState.Checked) + recurse_checkbox.stateChanged.connect(self.handle_recurse_checkbox) + layout.addWidget(recurse_checkbox) + + delete_checkbox = QCheckBox() + delete_checkbox.setText("Delete original files") + delete_checkbox.setCheckState(Qt.CheckState.Unchecked) + delete_checkbox.stateChanged.connect(self.handle_delete_checkbox) + layout.addWidget(delete_checkbox) + + self.directory_info = DirectoryInfo() + layout.addWidget(self.directory_info) + + self.start_btn = QPushButton("Convert") + self.start_btn.setDisabled(True) + self.start_btn.clicked.connect(self.start) + layout.addWidget(self.start_btn) + + widget = QWidget() + widget.setLayout(layout) + self.setCentralWidget(widget) + + def _start(self): + process_dir(self.directory_picker.selected_dir, self.recurse, self.delete_original) + + success_text = f"Successfully converted {self.directory_info.size} HEIC image(s) to PNG." + if self.delete_original: + success_text += " Also deleted the original file(s)." + + # show success dialog + dialog = QMessageBox(self) + dialog.setWindowTitle("Yee Haw!!!") + dialog.setText(success_text) + dialog.exec() + + # clear selected dir and stuff + self.start_btn.setText("Convert") + self.directory_picker.select_btn.setText("Browse") + self.directory_picker.selected_dir = "" + self.update_dir() + + def start(self): + # disable the button + self.start_btn.setText("Processing... Please Wait") + self.start_btn.setDisabled(True) + + QTimer.singleShot(500, lambda: self._start()) + + def update_dir(self): + selected_dir = self.directory_picker.selected_dir + + self.directory_info.update_data(selected_dir, self.recurse) + + self.start_btn.setDisabled(self.directory_info.size < 1) + + def handle_recurse_checkbox(self, state): + self.recurse = (state == Qt.CheckState.Checked.value) + self.update_dir() + + def handle_delete_checkbox(self, state): + self.delete_original = (state == Qt.CheckState.Checked.value) + + def toggle_about_page(self): + if self.about_window is None: + self.about_window = KonvertAboutWindow() + self.about_window.show() + else: + self.about_window.close() + self.about_window = None + + +if __name__ == "__main__": + app = QApplication(sys.argv) + + window = KonvertMainWindow() + window.show() + + app.exec() |