|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import os, sys, subprocess, shutil |
| 3 | + |
| 4 | +# disable cache usage must be before any local imports |
| 5 | +sys.dont_write_bytecode = True |
| 6 | + |
| 7 | +from PyQt6.QtWidgets import QApplication, QMainWindow, QFileDialog |
| 8 | +from PyQt6.uic import loadUi |
| 9 | + |
| 10 | +class flexqrc(QMainWindow): |
| 11 | + def __init__(self): |
| 12 | + super().__init__() |
| 13 | + |
| 14 | + if os.path.split(sys.argv[0])[0] == '/usr/bin': |
| 15 | + self.lib_path = '/usr/lib/libflexgui' |
| 16 | + loadUi(os.path.join(self.lib_path, 'flexqrc.ui'), self) |
| 17 | + else: |
| 18 | + srcPath = os.path.split(os.path.realpath(sys.argv[0]))[0] |
| 19 | + self.lib_path = os.path.join(srcPath, 'libflexgui') |
| 20 | + loadUi(os.path.join(srcPath, 'flexqrc.ui'), self) |
| 21 | + stylesheet = os.path.join(self.lib_path, 'flexqrc.qss') |
| 22 | + with open(stylesheet,'r') as fh: |
| 23 | + self.setStyleSheet(fh.read()) |
| 24 | + |
| 25 | + self.images_directory = False |
| 26 | + self.files = [] |
| 27 | + self.qrc_file = '' |
| 28 | + |
| 29 | + self.get_images_pb.setEnabled(False) |
| 30 | + self.build_qrc_pb.setEnabled(False) |
| 31 | + self.get_config_dir_pb.setEnabled(False) |
| 32 | + self.build_resources_pb.setEnabled(False) |
| 33 | + |
| 34 | + self.get_images_dir_pb.clicked.connect(self.get_images_dir) |
| 35 | + self.get_images_pb.clicked.connect(self.get_images) |
| 36 | + self.build_qrc_pb.clicked.connect(self.build_qrc) |
| 37 | + self.get_config_dir_pb.clicked.connect(self.get_config_dir) |
| 38 | + self.build_resources_pb.clicked.connect(self.build_resources) |
| 39 | + |
| 40 | + self.setWindowTitle('Flex Resource Builder') |
| 41 | + check_rcc = subprocess.run(["which", "rcc"], capture_output=True, text=True) |
| 42 | + if check_rcc.returncode == 0: |
| 43 | + rcc_version = subprocess.run(['rcc', '-v'], capture_output=True, text=True) |
| 44 | + self.rcc_version_lb.setText(f'{rcc_version.stdout.strip()}') |
| 45 | + self.rcc = True |
| 46 | + else: |
| 47 | + self.rcc_version_lb.setText('rcc was not found') |
| 48 | + self.rcc = False |
| 49 | + |
| 50 | + self.show() |
| 51 | + |
| 52 | + def get_images_dir(self): |
| 53 | + options = QFileDialog.Option.ShowDirsOnly |
| 54 | + options |= QFileDialog.Option.DontUseNativeDialog |
| 55 | + result = QFileDialog.getExistingDirectory(self, 'Select Images Directory', |
| 56 | + options=options) |
| 57 | + if result: |
| 58 | + self.images_directory = result |
| 59 | + self.image_dir_lb.setText(result) |
| 60 | + self.get_images_pb.setEnabled(True) |
| 61 | + |
| 62 | + def get_images(self): |
| 63 | + if self.images_directory: |
| 64 | + options = QFileDialog.Option.DontUseNativeDialog |
| 65 | + files, filter = QFileDialog.getOpenFileNames(self,'Select Images', |
| 66 | + self.images_directory, "All Files (*);;Python Files (*.py)", options=options) |
| 67 | + if files: |
| 68 | + self.images_lw.addItems(files) |
| 69 | + for file in files: |
| 70 | + self.files.append(file) |
| 71 | + self.build_qrc_pb.setEnabled(True) |
| 72 | + self.get_images_lb.setText(f'{len(files)} images selected') |
| 73 | + |
| 74 | + def build_qrc(self): |
| 75 | + self.qrc_file = os.path.join(self.images_directory, 'resources.qrc') |
| 76 | + contents = [] |
| 77 | + contents.append('<RCC>\n') |
| 78 | + contents.append(' <qresource prefix="\\">\n') |
| 79 | + for file in self.files: |
| 80 | + contents.append(f' <file>{os.path.basename(file)}</file>\n') |
| 81 | + contents.append(' </qresource>\n') |
| 82 | + contents.append('</RCC>') |
| 83 | + with open(self.qrc_file, 'w') as f: |
| 84 | + f.writelines(contents) |
| 85 | + self.get_config_dir_pb.setEnabled(True) |
| 86 | + self.build_qrc_lb.setText('Build QRC Done') |
| 87 | + |
| 88 | + def get_config_dir(self): |
| 89 | + if os.path.isdir(os.path.expanduser('~/linuxcnc/configs')): |
| 90 | + configsDir = os.path.expanduser('~/linuxcnc/configs') |
| 91 | + else: |
| 92 | + configsDir = os.path.expanduser('~/') |
| 93 | + #QFileDialog::QFileDialog(QWidget *parent = nullptr, |
| 94 | + # const QString &caption = QString(), const QString &directory = QString(), |
| 95 | + # const QString &filter = QString()) |
| 96 | + options = QFileDialog.Option.ShowDirsOnly |
| 97 | + options |= QFileDialog.Option.DontUseNativeDialog |
| 98 | + result = QFileDialog.getExistingDirectory(self, 'Select Configuration Directory', |
| 99 | + configsDir, options=options) |
| 100 | + if result: |
| 101 | + self.config_directory = result |
| 102 | + self.config_dir_lb.setText(result) |
| 103 | + if self.rcc: |
| 104 | + self.build_resources_pb.setEnabled(True) |
| 105 | + else: |
| 106 | + self.build_resources_lb.setText('install rcc') |
| 107 | + |
| 108 | + def build_resources(self): |
| 109 | + os.chdir(self.images_directory) |
| 110 | + result = subprocess.run(['rcc', '-g', 'python', '-o', 'resources.py', |
| 111 | + 'resources.qrc'], capture_output=True, text=True) |
| 112 | + if result.returncode == 0: # success |
| 113 | + resource_file = os.path.join(self.images_directory, 'resources.py') |
| 114 | + self.build_resources_lb.setText('Build Success') |
| 115 | + # fix the file first lol |
| 116 | + # Read in the file |
| 117 | + with open(resource_file, 'r') as file: |
| 118 | + filedata = file.read() |
| 119 | + # Replace the target string |
| 120 | + filedata = filedata.replace('PySide2', 'PyQt6') |
| 121 | + # Write the file out again |
| 122 | + |
| 123 | + with open(resource_file, 'w') as file: |
| 124 | + file.write(filedata) |
| 125 | + shutil.copy2(resource_file, self.config_directory) |
| 126 | + |
| 127 | + #print('build') |
| 128 | + #print(os.getcwd()) |
| 129 | + #shutil.copy2(self.qrc_file, self.config_directory) |
| 130 | + #os.chdir(self.config_directory) |
| 131 | + #print(os.getcwd()) |
| 132 | + #result = subprocess.run(['rcc', '-g', 'python', '-o', 'resources.py', |
| 133 | + # 'resources.qrc'], capture_output=True, text=True) |
| 134 | + #print(result) |
| 135 | + |
| 136 | + |
| 137 | + # rcc -g python -o resources.py resources.qrc |
| 138 | + |
| 139 | +app = QApplication(sys.argv) |
| 140 | +gui = flexqrc() |
| 141 | +sys.exit(app.exec()) |
0 commit comments