class AllKeysDialog(QDialog): def __init__( self, parent=None, ): super(AllKeysDialog, self).__init__(parent) layout = QVBoxLayout(self) label = QLabel("Below you can see a list with all the supported keys.") self.list = QListWidget() self.listAllKeys() layout.addWidget(label) layout.setAlignment(label, Qt.Alignment.AlignTop) layout.addWidget(self.list) self.setLayout(layout) self.window().setMaximumWidth(300) # TODO: list all supported keys def listAllKeys(self): self.list.addItems([x for x in SUPPORTED_KEYS]) # static method to create the dialog and return (date, time, accepted) @staticmethod def getDialog(parent=None): dialog = AllKeysDialog(parent) result = dialog.exec() return result == QDialog.DialogCode.Accepted
class TwoListSelection(QtWidgets.QWidget): def __init__(self, parent=None): super(TwoListSelection, self).__init__(parent) self.setup_layout() def setup_layout(self): lay = QtWidgets.QHBoxLayout(self) selectedLayout = QVBoxLayout() self.mInput = QListWidget() selectedLayout.addWidget(QLabel("Selected:")) selectedLayout.addWidget(self.mInput) availableLayout = QVBoxLayout() self.mOuput = QListWidget() availableLayout.addWidget(QLabel("Available")) availableLayout.addWidget(self.mOuput) self.mButtonToSelected = QtWidgets.QPushButton(">>") self.mBtnMoveToAvailable = QtWidgets.QPushButton(">") self.mBtnMoveToSelected = QtWidgets.QPushButton("<") self.mButtonToAvailable = QtWidgets.QPushButton("<<") vlay = QtWidgets.QVBoxLayout() vlay.addStretch() vlay.addWidget(self.mButtonToSelected) vlay.addWidget(self.mBtnMoveToAvailable) vlay.addWidget(self.mBtnMoveToSelected) vlay.addWidget(self.mButtonToAvailable) vlay.addStretch() self.mBtnUp = QtWidgets.QPushButton("Up") self.mBtnDown = QtWidgets.QPushButton("Down") vlay2 = QtWidgets.QVBoxLayout() vlay2.addStretch() vlay2.addWidget(self.mBtnUp) vlay2.addWidget(self.mBtnDown) vlay2.addStretch() lay.addLayout(selectedLayout) lay.addLayout(vlay) lay.addLayout(availableLayout) lay.addLayout(vlay2) self.update_buttons_status() self.connections() @QtCore.pyqtSlot() def update_buttons_status(self): self.mBtnUp.setDisabled(not bool(self.mOuput.selectedItems()) or self.mOuput.currentRow() == 0) self.mBtnDown.setDisabled( not bool(self.mOuput.selectedItems()) or self.mOuput.currentRow() == (self.mOuput.count() - 1)) self.mBtnMoveToAvailable.setDisabled( not bool(self.mInput.selectedItems()) or self.mOuput.currentRow() == 0) self.mBtnMoveToSelected.setDisabled( not bool(self.mOuput.selectedItems())) def connections(self): self.mInput.itemSelectionChanged.connect(self.update_buttons_status) self.mOuput.itemSelectionChanged.connect(self.update_buttons_status) self.mBtnMoveToAvailable.clicked.connect( self.on_mBtnMoveToAvailable_clicked) self.mBtnMoveToSelected.clicked.connect( self.on_mBtnMoveToSelected_clicked) self.mButtonToAvailable.clicked.connect( self.on_mButtonToAvailable_clicked) self.mButtonToSelected.clicked.connect( self.on_mButtonToSelected_clicked) self.mBtnUp.clicked.connect(self.on_mBtnUp_clicked) self.mBtnDown.clicked.connect(self.on_mBtnDown_clicked) @QtCore.pyqtSlot() def on_mBtnMoveToAvailable_clicked(self): self.mOuput.addItem(self.mInput.takeItem(self.mInput.currentRow())) @QtCore.pyqtSlot() def on_mBtnMoveToSelected_clicked(self): self.mInput.addItem(self.mOuput.takeItem(self.mOuput.currentRow())) @QtCore.pyqtSlot() def on_mButtonToAvailable_clicked(self): while self.mOuput.count() > 0: self.mInput.addItem(self.mOuput.takeItem(0)) @QtCore.pyqtSlot() def on_mButtonToSelected_clicked(self): while self.mInput.count() > 0: self.mOuput.addItem(self.mInput.takeItem(0)) @QtCore.pyqtSlot() def on_mBtnUp_clicked(self): row = self.mOuput.currentRow() currentItem = self.mOuput.takeItem(row) self.mOuput.insertItem(row - 1, currentItem) self.mOuput.setCurrentRow(row - 1) @QtCore.pyqtSlot() def on_mBtnDown_clicked(self): row = self.mOuput.currentRow() currentItem = self.mOuput.takeItem(row) self.mOuput.insertItem(row + 1, currentItem) self.mOuput.setCurrentRow(row + 1) def addAvailableItems(self, items): self.mInput.addItems(items) def get_left_elements(self): r = [] for i in range(self.mInput.count()): it = self.mInput.item(i) r.append(it.text()) return r def get_right_elements(self): r = [] for i in range(self.mOuput.count()): it = self.mOuput.item(i) r.append(it.text()) return r
class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("📸 Random Image Tools V1.2 🔨") self.image_list = [] # ToDo: Add a "rename?" flag. Like rename image files # to store the directory path for later use self.directory_path_name = "/" # Set up the layouts layer_one = QHBoxLayout() # Select a folder, selected directory layer_one_and_a_half = QHBoxLayout() # Selected directory contents layer_two = QHBoxLayout() # Second line of buttons layer_two_vertical_one = QVBoxLayout() # Store the first column w/checkbox and "Convert" layer_two_vertical_two = QVBoxLayout() # Store the second column w/checkbox and "Open in File Browser" layer_three = QHBoxLayout() # Conversion process state vertical_layout_parent = QVBoxLayout() # Parent widget widget = QWidget() # Displays selected directory self.directory_label = QLabel() self.directory_label.setText("Directory to be worked on will show here ") self.directory_label.show() # Displays "Select folder" button self.select_a_folder_button = QPushButton() self.select_a_folder_button.setText("Select a folder:") self.select_a_folder_button.clicked.connect(self.select_folder_prompt) self.select_a_folder_button.show() # Displays the image contents of the selected folder self.image_paths_list_widget = QListWidget() self.image_paths_list_widget.show() # Displays button to initiate image conversion self.convert_to_png_button = QPushButton() self.convert_to_png_button.setText("Convert to PNG") self.convert_to_png_button.clicked.connect(self.convert_folder_to_png) self.convert_to_png_button.show() # Check boxes for "Create new folder for PNGs" and "Delete original files after converting" self.create_new_folder_checkbox = QCheckBox() self.create_new_folder_checkbox.setText("Create new folder to store converted PNG's?") self.create_new_folder_checkbox.show() self.delete_original_files_checkbox = QCheckBox() self.delete_original_files_checkbox.setText("Delete original files after converting them to PNG?") self.create_new_folder_checkbox.show() # Displays button to open selected directory in the file browser self.show_folder_button = QPushButton() self.show_folder_button.setText("Open selected folder in file browser") self.show_folder_button.clicked.connect(self.open_folder) self.show_folder_button.show() # Displays label when conversion is finished, and the corresponding progress bar self.conversion_finished_or_error_label = QLabel() self.conversion_finished_or_error_label.setText("👀 waiting for you to press \"Convert to PNG\" ") # Put the find folder button and folder selected button together layer_one.addWidget(self.select_a_folder_button) layer_one.addWidget(self.directory_label) # Image paths of selected folder layer_one_and_a_half.addWidget(self.image_paths_list_widget) # Put the convert button and open-in-finder button together layer_two_vertical_one.addWidget(self.convert_to_png_button) layer_two_vertical_one.addWidget(self.delete_original_files_checkbox) layer_two.addLayout(layer_two_vertical_one) layer_two_vertical_two.addWidget(self.show_folder_button) layer_two_vertical_two.addWidget(self.create_new_folder_checkbox) layer_two.addLayout(layer_two_vertical_two) # Label and progress bar layer_three.addWidget(self.conversion_finished_or_error_label) layer_three.setAlignment(Qt.AlignmentFlag.AlignHCenter) # Put the "convert to png" button beneath vertical_layout_parent.addLayout(layer_one) vertical_layout_parent.addLayout(layer_one_and_a_half) vertical_layout_parent.addLayout(layer_two) vertical_layout_parent.addLayout(layer_three) widget.setLayout(vertical_layout_parent) self.setCentralWidget(widget) # Prompts user to select a folder, stores the given folder path and displays chosen path to user def select_folder_prompt(self): # Clear self.image_list and QListWidget to prepare for newly selected folder. self.image_list.clear() self.image_paths_list_widget.clear() # Append a "/" otherwise it will mix the folder name and containing image file together directory = str(QFileDialog.getExistingDirectory(self, "Select Directory")) + "/" # Update QLabel to new directory, and store it in self for future use self.directory_label.setText(directory) self.directory_path_name = directory # Update self.image_list field image_list = self.scan_for_jpg_file_paths() self.image_list = image_list # Populated the QListWindow() with the update self.image_list field self.image_paths_list_widget.addItems(self.image_list) # Given a path name, will open it in the Folder browser app def open_folder(self): subprocess.call(["open", "-R", self.directory_path_name]) # Given the current state of the directory_path_name folder, will scan for image files in that folder def scan_for_jpg_file_paths(self): image_list = [] for root, dirs, files in os.walk(self.directory_path_name, topdown=True): for filename in files: if '.jpeg' or '.jpg' or '.webp' or '.gif' or '.icns' in filename: if '.png' not in filename: absolute_path = self.directory_path_name + filename # Avoid adding duplicates if absolute_path not in image_list: image_list.append(absolute_path) return image_list # Given a non-empty folder path, converts all jpg images in it to png. # Todo: Store png images in a new folder? # ToDo: Add a "Delete images after converting?" # ToDo: Add functionality for checkboxes # Ok so that the QListWidget can update, I'm going to have to get the image_list before calling this function. # In other words, image_list is a field in the MainWindow subclass, and is update after selecting the folder, and # called before calling this function. def convert_folder_to_png(self): self.conversion_finished_or_error_label.setText("Converting") # Progress bar depends on independent variable, length of image list = x if len(self.image_list) > 0: self.convert_images_to_png() self.conversion_finished_or_error_label.setText("Conversion finished") if len(self.image_list) <= 0: self.conversion_finished_or_error_label.setText("There are no image files in this folder") # This will be called after a folder has been selected and the "Convert to PNG" button has been pressed def convert_images_to_png(self): self.conversion_finished_or_error_label.setText("...") # Convert images # ToDo : Put the checkbox logic here # Check if user wanted images stored in a new folder # Check if user wanted to delete original images after conversion # Placeholder for converted_PNG folder user_wanted_png_in_new_folder = self.create_new_folder_checkbox.isChecked() user_wanted_unconverted_image_deleted = self.delete_original_files_checkbox.isChecked() converted_png_folder_name = "" # Create folder if user wanted converted images stored in a new folder if user_wanted_png_in_new_folder: converted_png_folder_name = self.directory_path_name + "Converted PNG Files/" os.mkdir(converted_png_folder_name) for image_path in self.image_list: # Get absolute path absolute_image_path = os.path.abspath(image_path) if ".DS_Store" not in absolute_image_path: # Mac adds .DS_Store, just a way to ignore these pesky files unconverted_image = Image.open(absolute_image_path) # Image object if user_wanted_png_in_new_folder: # Get just the image name and extension name_and_extension_of_image = extract_image_name_and_extension(absolute_image_path) # Append image name and extension to the converted image folder path image_path_stored_in_converted_png_folder = converted_png_folder_name + name_and_extension_of_image # Store in converted image folder path unconverted_image.save(rename_image_path_to_png(image_path_stored_in_converted_png_folder)) else: unconverted_image.save(rename_image_path_to_png(absolute_image_path)) if user_wanted_unconverted_image_deleted: os.remove(absolute_image_path)