Upload on a separate thread
offlate/ui/editor.py
| 20 | 20 | ||
| 21 | 21 | from .spellcheckedit import SpellCheckEdit | |
| 22 | 22 | from .tagclickedit import TagClickEdit | |
| 23 | + | from .parallel import RunnableCallback, RunnableSignals | |
| 23 | 24 | ||
| 24 | 25 | import math | |
| 25 | 26 | ||
… | |||
| 278 | 279 | super().__init__() | |
| 279 | 280 | self.manager = manager | |
| 280 | 281 | self.projectManagerWindow = projectManagerWindow | |
| 282 | + | self.threadpool = QThreadPool() | |
| 281 | 283 | self.initUI() | |
| 282 | 284 | ||
| 283 | 285 | def initOpenProjects(self, menu): | |
… | |||
| 327 | 329 | self.projectManagerWindow.new() | |
| 328 | 330 | ||
| 329 | 331 | def send(self): | |
| 330 | - | self.tabs.currentWidget().send() | |
| 332 | + | self.actionLabel.setText(self.tr("Uploading {}...").format(self.tabs.currentWidget().project.name)) | |
| 333 | + | worker = UploadRunnable(self.tabs.currentWidget()) | |
| 334 | + | worker.signals.finished.connect(self.sent) | |
| 335 | + | self.threadpool.start(worker) | |
| 336 | + | ||
| 337 | + | def sent(self, name): | |
| 338 | + | self.actionLabel.setText(self.tr("Upload of {} finished!").format(name)) | |
| 331 | 339 | ||
| 332 | 340 | def update(self): | |
| 333 | 341 | self.tabs.currentWidget().update() | |
… | |||
| 472 | 480 | ||
| 473 | 481 | self.setGeometry(0, 0, 800, 600) | |
| 474 | 482 | self.setWindowTitle('Offlate') | |
| 483 | + | ||
| 484 | + | class UploadRunnable(QRunnable, RunnableCallback): | |
| 485 | + | def __init__(self, widget): | |
| 486 | + | super().__init__() | |
| 487 | + | self.widget = widget | |
| 488 | + | self.signals = RunnableSignals() | |
| 489 | + | ||
| 490 | + | def run(self): | |
| 491 | + | self.widget.send() | |
| 492 | + | self.signals.finished.emit(self.widget.project.name) | |
offlate/ui/manager.py
| 26 | 26 | from .new import NewWindow | |
| 27 | 27 | from .settings import SettingsWindow | |
| 28 | 28 | from .editor import EditorWindow | |
| 29 | + | from .parallel import RunnableCallback, RunnableSignals | |
| 29 | 30 | ||
| 30 | 31 | from ..manager import ProjectManager | |
| 31 | 32 | ||
… | |||
| 273 | 274 | worker.signals.finished.connect(self.finishReport) | |
| 274 | 275 | self.threadpool.start(worker) | |
| 275 | 276 | ||
| 276 | - | class NewRunnableSignals(QObject): | |
| 277 | - | finished = pyqtSignal(str) | |
| 278 | - | progress = pyqtSignal(str, int) | |
| 279 | - | error = pyqtSignal(str, str) | |
| 280 | - | restart_required = pyqtSignal(str, str, int, dict, str) | |
| 281 | - | ||
| 282 | - | class RunnableCallback: | |
| 283 | - | def progress(self, amount): | |
| 284 | - | if int(round(amount)) > self.oldamount: | |
| 285 | - | self.oldamount = int(round(amount)) | |
| 286 | - | self.signals.progress.emit(self.name, amount) | |
| 287 | - | ||
| 288 | - | def project_exists(self): | |
| 289 | - | self.error = self.parent.tr('A project with the same name already exists. \ | |
| 290 | - | The new project was not created. You should first remove the same-named project.') | |
| 291 | - | ||
| 292 | - | def project_present(self, directory): | |
| 293 | - | self.error = self.parent.tr('Your filesystem contains a same-named \ | |
| 294 | - | directory for your new project. The new project was not created. You should \ | |
| 295 | - | first remove the same-named directory: "{}".'.format(directory)) | |
| 296 | - | ||
| 297 | - | def project_error(self, error): | |
| 298 | - | if isinstance(error, UnsupportedFormatException): | |
| 299 | - | self.error = self.parent.tr('The project you added uses the {} format, \ | |
| 300 | - | but it is not supported yet by Offlate. You can try to update the application, \ | |
| 301 | - | or if you are on the latest version already, report it as a bug.'.format(error.unsupportedFormat)) | |
| 302 | - | elif isinstance(error, ProjectNotFoundSystemException): | |
| 303 | - | self.error = self.parent.tr('The project {} you added could not be found \ | |
| 304 | - | in the translation platform you selected. Did you make a typo while entering the \ | |
| 305 | - | name or other parameters?'.format(error.projectNotFound)) | |
| 306 | - | else: | |
| 307 | - | self.error = self.parent.tr('An unexpected error occured while \ | |
| 308 | - | fetching the project: {}. You should report this as a bug.'.format(str(error))) | |
| 309 | - | ||
| 310 | 277 | class NewRunnable(QRunnable, RunnableCallback): | |
| 311 | 278 | def __init__(self, parent, name, lang, system, info): | |
| 312 | 279 | super().__init__() | |
… | |||
| 315 | 282 | self.system = system | |
| 316 | 283 | self.info = info | |
| 317 | 284 | self.parent = parent | |
| 318 | - | self.signals = NewRunnableSignals() | |
| 285 | + | self.signals = RunnableSignals() | |
| 319 | 286 | self.oldamount = -1 | |
| 320 | 287 | self.error = None | |
| 321 | 288 | ||
… | |||
| 337 | 304 | self.system = system | |
| 338 | 305 | self.info = info | |
| 339 | 306 | self.parent = parent | |
| 340 | - | self.signals = NewRunnableSignals() | |
| 307 | + | self.signals = RunnableSignals() | |
| 341 | 308 | self.oldamount = -1 | |
| 342 | 309 | self.error = None | |
| 343 | 310 | ||
offlate/ui/parallel.py unknown status 1
| 1 | + | # Copyright (c) 2019 Julien Lepiller <julien@lepiller.eu> | |
| 2 | + | # | |
| 3 | + | # This program is free software: you can redistribute it and/or modify | |
| 4 | + | # it under the terms of the GNU Affero General Public License as | |
| 5 | + | # published by the Free Software Foundation, either version 3 of the | |
| 6 | + | # License, or (at your option) any later version. | |
| 7 | + | # | |
| 8 | + | # This program is distributed in the hope that it will be useful, | |
| 9 | + | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 10 | + | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 11 | + | # GNU Affero General Public License for more details. | |
| 12 | + | # | |
| 13 | + | # You should have received a copy of the GNU Affero General Public License | |
| 14 | + | # along with this program. If not, see <https://www.gnu.org/licenses/>. | |
| 15 | + | #### | |
| 16 | + | ||
| 17 | + | from PyQt5.QtCore import * | |
| 18 | + | ||
| 19 | + | class RunnableCallback: | |
| 20 | + | def progress(self, amount): | |
| 21 | + | if int(round(amount)) > self.oldamount: | |
| 22 | + | self.oldamount = int(round(amount)) | |
| 23 | + | self.signals.progress.emit(self.name, amount) | |
| 24 | + | ||
| 25 | + | def project_exists(self): | |
| 26 | + | self.error = self.parent.tr('A project with the same name already exists. \ | |
| 27 | + | The new project was not created. You should first remove the same-named project.') | |
| 28 | + | ||
| 29 | + | def project_present(self, directory): | |
| 30 | + | self.error = self.parent.tr('Your filesystem contains a same-named \ | |
| 31 | + | directory for your new project. The new project was not created. You should \ | |
| 32 | + | first remove the same-named directory: "{}".'.format(directory)) | |
| 33 | + | ||
| 34 | + | def project_error(self, error): | |
| 35 | + | if isinstance(error, UnsupportedFormatException): | |
| 36 | + | self.error = self.parent.tr('The project you added uses the {} format, \ | |
| 37 | + | but it is not supported yet by Offlate. You can try to update the application, \ | |
| 38 | + | or if you are on the latest version already, report it as a bug.'.format(error.unsupportedFormat)) | |
| 39 | + | elif isinstance(error, ProjectNotFoundSystemException): | |
| 40 | + | self.error = self.parent.tr('The project {} you added could not be found \ | |
| 41 | + | in the translation platform you selected. Did you make a typo while entering the \ | |
| 42 | + | name or other parameters?'.format(error.projectNotFound)) | |
| 43 | + | else: | |
| 44 | + | self.error = self.parent.tr('An unexpected error occured while \ | |
| 45 | + | fetching the project: {}. You should report this as a bug.'.format(str(error))) | |
| 46 | + | ||
| 47 | + | class RunnableSignals(QObject): | |
| 48 | + | finished = pyqtSignal(str) | |
| 49 | + | progress = pyqtSignal(str, int) | |
| 50 | + | error = pyqtSignal(str, str) | |
| 51 | + | restart_required = pyqtSignal(str, str, int, dict, str) |