|
1 | 1 | import sys |
2 | 2 | import os |
| 3 | +import json |
3 | 4 | from PyQt5.QtCore import Qt |
4 | | -from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QPushButton, QFileDialog |
| 5 | +from PyQt5.QtWidgets import ( |
| 6 | + QApplication, QWidget, QVBoxLayout, QLabel, QPushButton, |
| 7 | + QFileDialog, QCheckBox, QHBoxLayout, QSpacerItem, QSizePolicy |
| 8 | +) |
| 9 | +from PyQt5.QtGui import QFont |
| 10 | +from update import check_for_update, result_update, update_application, CURRENT_VERSION, LATEST_VERSION |
| 11 | +from debug import debug_print |
| 12 | + |
| 13 | +CONFIG_FILE = 'config.json' |
5 | 14 |
|
6 | 15 | class DragDropWindow(QWidget): |
7 | 16 | def __init__(self): |
8 | 17 | super().__init__() |
9 | 18 | self.setWindowTitle('AdminFreeExec') |
10 | 19 | self.setGeometry(100, 100, 500, 250) |
11 | 20 |
|
| 21 | + self.dark_mode = False |
| 22 | + self.layout = QVBoxLayout() |
| 23 | + self.setLayout(self.layout) |
| 24 | + |
| 25 | + self.load_config() |
| 26 | + self.init_main_screen() |
| 27 | + |
| 28 | + def load_config(self): |
| 29 | + if os.path.exists(CONFIG_FILE): |
| 30 | + with open(CONFIG_FILE, 'r') as file: |
| 31 | + config = json.load(file) |
| 32 | + self.dark_mode = config.get('dark_mode', False) |
| 33 | + |
| 34 | + def save_config(self): |
| 35 | + config = { |
| 36 | + 'dark_mode': self.dark_mode, |
| 37 | + } |
| 38 | + with open(CONFIG_FILE, 'w') as file: |
| 39 | + json.dump(config, file) |
| 40 | + |
| 41 | + def init_main_screen(self): |
| 42 | + self.clear_layout() |
| 43 | + |
| 44 | + self.top_layout = QHBoxLayout() |
| 45 | + self.settings_button = QPushButton('⚙', self) |
| 46 | + self.settings_button.setFixedSize(30, 30) |
| 47 | + self.settings_button.setStyleSheet("font-size: 18px; border: none;") |
| 48 | + self.settings_button.clicked.connect(self.show_settings_screen) |
| 49 | + self.top_layout.addWidget(self.settings_button) |
| 50 | + |
| 51 | + if result_update(): |
| 52 | + self.update_button = QPushButton('🔄', self) |
| 53 | + self.update_button.setFixedSize(30, 30) |
| 54 | + self.update_button.setStyleSheet("font-size: 18px; border: none;") |
| 55 | + self.update_button.setToolTip("Update available") |
| 56 | + self.update_button.clicked.connect(self.show_update_screen) |
| 57 | + self.top_layout.addWidget(self.update_button) |
| 58 | + |
| 59 | + self.top_layout.addStretch() |
| 60 | + |
| 61 | + self.layout.addLayout(self.top_layout) |
| 62 | + |
| 63 | + self.center_layout = QVBoxLayout() |
| 64 | + |
12 | 65 | self.label = QLabel('Arrastra un archivo aquí', self) |
13 | 66 | self.label.setAlignment(Qt.AlignCenter) |
14 | | - self.label.setStyleSheet('font-size: 20px;') |
| 67 | + self.label.setFont(QFont("Arial", 14)) |
| 68 | + |
| 69 | + self.center_layout.addWidget(self.label) |
| 70 | + |
| 71 | + self.layout.addStretch() |
| 72 | + self.layout.addLayout(self.center_layout) |
| 73 | + self.layout.addStretch() |
15 | 74 |
|
16 | | - self.button = QPushButton('Abrir archivo', self) |
| 75 | + self.bottom_layout = QVBoxLayout() |
| 76 | + |
| 77 | + self.button = QPushButton('Seleccionar archivo', self) |
17 | 78 | self.button.clicked.connect(self.open_file) |
| 79 | + self.bottom_layout.addWidget(self.button) |
| 80 | + |
| 81 | + self.version_credit_layout = QHBoxLayout() |
| 82 | + |
| 83 | + self.credit_label = QLabel("AdminFreeExec v0.1.0 by Rompelhd", self) |
| 84 | + self.credit_label.setFont(QFont("Arial", 10)) |
| 85 | + self.credit_label.setAlignment(Qt.AlignRight) |
| 86 | + |
| 87 | + self.version_credit_layout.addStretch() |
| 88 | + self.version_credit_layout.addWidget(self.credit_label) |
| 89 | + |
| 90 | + self.bottom_layout.addLayout(self.version_credit_layout) |
| 91 | + |
| 92 | + spacer = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding) |
| 93 | + self.layout.addItem(spacer) |
| 94 | + |
| 95 | + self.layout.addLayout(self.bottom_layout) |
| 96 | + |
| 97 | + self.apply_theme() |
| 98 | + |
| 99 | + def show_settings_screen(self): |
| 100 | + self.clear_layout() |
| 101 | + |
| 102 | + self.dark_mode_checkbox = QCheckBox("Modo Oscuro", self) |
| 103 | + self.dark_mode_checkbox.setFont(QFont("Arial", 12)) |
| 104 | + self.dark_mode_checkbox.setChecked(self.dark_mode) |
| 105 | + |
| 106 | + self.settings_top_layout = QVBoxLayout() |
| 107 | + self.settings_top_layout.addWidget(self.dark_mode_checkbox) |
| 108 | + |
| 109 | + self.button_layout = QHBoxLayout() |
| 110 | + |
| 111 | + self.save_button = QPushButton("Guardar", self) |
| 112 | + self.save_button.setFont(QFont("Arial", 12)) |
| 113 | + self.save_button.clicked.connect(self.save_settings) |
| 114 | + |
| 115 | + self.discard_button = QPushButton("Descartar", self) |
| 116 | + self.discard_button.setFont(QFont("Arial", 12)) |
| 117 | + self.discard_button.clicked.connect(self.init_main_screen) |
| 118 | + |
| 119 | + self.button_layout.addWidget(self.save_button) |
| 120 | + self.button_layout.addWidget(self.discard_button) |
| 121 | + |
| 122 | + self.version_credit_layout = QHBoxLayout() |
| 123 | + |
| 124 | + self.credit_label = QLabel("AdminFreeExec v0.2.1 by Rompelhd", self) |
| 125 | + self.credit_label.setFont(QFont("Arial", 10)) |
| 126 | + self.credit_label.setAlignment(Qt.AlignRight) |
18 | 127 |
|
19 | | - layout = QVBoxLayout() |
20 | | - layout.addWidget(self.label) |
21 | | - layout.addWidget(self.button) |
| 128 | + self.version_credit_layout.addStretch() |
| 129 | + self.version_credit_layout.addWidget(self.credit_label) |
22 | 130 |
|
23 | | - self.setLayout(layout) |
| 131 | + self.settings_bottom_layout = QVBoxLayout() |
| 132 | + self.settings_bottom_layout.addLayout(self.button_layout) |
| 133 | + self.settings_bottom_layout.addLayout(self.version_credit_layout) |
| 134 | + |
| 135 | + spacer = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding) |
| 136 | + self.layout.addLayout(self.settings_top_layout) |
| 137 | + self.layout.addItem(spacer) |
| 138 | + self.layout.addLayout(self.settings_bottom_layout) |
| 139 | + |
| 140 | + self.apply_theme() |
| 141 | + |
| 142 | + def show_update_screen(self): |
| 143 | + self.clear_layout() |
| 144 | + |
| 145 | + self.update_layout = QVBoxLayout() |
| 146 | + self.update_label = QLabel(f"¡Nueva actualización disponible a la versión {LATEST_VERSION}!", self) |
| 147 | + self.update_label.setAlignment(Qt.AlignCenter) |
| 148 | + self.update_label.setFont(QFont("Arial", 14)) |
| 149 | + self.update_layout.addWidget(self.update_label) |
| 150 | + |
| 151 | + self.layout.addStretch() |
| 152 | + self.layout.addLayout(self.update_layout) |
| 153 | + self.layout.addStretch() |
| 154 | + |
| 155 | + self.button_layout = QHBoxLayout() |
| 156 | + |
| 157 | + self.update_button = QPushButton("Actualizar", self) |
| 158 | + self.update_button.setFont(QFont("Arial", 12)) |
| 159 | + self.update_button.clicked.connect(update_application) |
| 160 | + |
| 161 | + self.discard_button = QPushButton("Atrás", self) |
| 162 | + self.discard_button.setFont(QFont("Arial", 12)) |
| 163 | + self.discard_button.clicked.connect(self.init_main_screen) |
| 164 | + |
| 165 | + self.button_layout.addWidget(self.update_button) |
| 166 | + self.button_layout.addWidget(self.discard_button) |
| 167 | + |
| 168 | + self.layout.addLayout(self.button_layout) |
| 169 | + |
| 170 | + self.bottom_layout = QVBoxLayout() |
| 171 | + |
| 172 | + self.version_credit_layout = QHBoxLayout() |
| 173 | + |
| 174 | + self.credit_label = QLabel(f"AdminFreeExec v{CURRENT_VERSION} by Rompelhd", self) |
| 175 | + self.credit_label.setFont(QFont("Arial", 10)) |
| 176 | + self.credit_label.setAlignment(Qt.AlignRight) |
| 177 | + |
| 178 | + self.version_credit_layout.addStretch() |
| 179 | + self.version_credit_layout.addWidget(self.credit_label) |
| 180 | + |
| 181 | + self.bottom_layout.addLayout(self.version_credit_layout) |
| 182 | + |
| 183 | + spacer = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding) |
| 184 | + self.layout.addItem(spacer) |
| 185 | + |
| 186 | + self.layout.addLayout(self.bottom_layout) |
| 187 | + |
| 188 | + |
| 189 | + def save_settings(self): |
| 190 | + self.dark_mode = self.dark_mode_checkbox.isChecked() |
| 191 | + self.save_config() |
| 192 | + self.init_main_screen() |
24 | 193 |
|
25 | 194 | def open_file(self): |
26 | | - file_path, _ = QFileDialog.getOpenFileName(self, 'Seleccionar un archivo') |
| 195 | + file_path, _ = QFileDialog.getOpenFileName(self, 'Seleccionar un archivo', "", "Ejecutables (*.exe)") |
27 | 196 | if file_path: |
28 | 197 | self.label.setText(f"Archivo seleccionado: {file_path}") |
29 | 198 | self.run_as_invoker(file_path) |
30 | 199 |
|
31 | 200 | def run_as_invoker(self, exe_path): |
32 | 201 | exe_path = f'"{exe_path}"' |
33 | | - os.system(f'cmd /min /C "set __COMPAT_LAYER=RUNASINVOKER && start "" {exe_path}"') |
| 202 | + os.system(f'cmd /min /C "set __COMPAT_LAYER=RUNASINVOKER && start \"\" {exe_path}\"') |
34 | 203 |
|
35 | 204 | def dragEnterEvent(self, event): |
36 | 205 | if event.mimeData().hasUrls(): |
37 | | - event.accept() |
38 | | - else: |
39 | | - event.ignore() |
| 206 | + event.acceptProposedAction() |
40 | 207 |
|
41 | 208 | def dropEvent(self, event): |
42 | | - file_path = event.mimeData().urls()[0].toLocalFile() |
43 | | - self.label.setText(f"Archivo arrastrado: {file_path}") |
44 | | - self.run_as_invoker(file_path) |
| 209 | + urls = event.mimeData().urls() |
| 210 | + if urls and urls[0].isLocalFile(): |
| 211 | + file_path = urls[0].toLocalFile() |
| 212 | + self.label.setText(f"Archivo seleccionado: {file_path}") |
| 213 | + self.run_as_invoker(file_path) |
| 214 | + |
| 215 | + def clear_layout(self): |
| 216 | + while self.layout.count(): |
| 217 | + item = self.layout.takeAt(0) |
| 218 | + if item.layout(): |
| 219 | + self.clear_layout_recursive(item.layout()) |
| 220 | + elif item.widget(): |
| 221 | + item.widget().deleteLater() |
45 | 222 |
|
| 223 | + def clear_layout_recursive(self, layout): |
| 224 | + while layout.count(): |
| 225 | + item = layout.takeAt(0) |
| 226 | + if item.layout(): |
| 227 | + self.clear_layout_recursive(item.layout()) |
| 228 | + elif item.widget(): |
| 229 | + item.widget().deleteLater() |
| 230 | + |
| 231 | + def apply_theme(self): |
| 232 | + if self.dark_mode: |
| 233 | + self.setStyleSheet("background-color: #2E2E2E; color: white;") |
| 234 | + else: |
| 235 | + self.setStyleSheet("background-color: white; color: black;") |
46 | 236 |
|
47 | 237 | if __name__ == '__main__': |
| 238 | + check_for_update() |
| 239 | + |
48 | 240 | app = QApplication(sys.argv) |
49 | 241 | window = DragDropWindow() |
50 | 242 | window.setAcceptDrops(True) |
51 | 243 | window.show() |
| 244 | + app.aboutToQuit.connect(window.save_config) |
52 | 245 | sys.exit(app.exec_()) |
0 commit comments