Add a progress bar in status and support callbacks when creating new projects
offlate/manager.py
72 | 72 | with open(self.basedir + '/projects.json', 'w') as f: | |
73 | 73 | f.write(json.dumps([])) | |
74 | 74 | ||
75 | - | def createProject(self, name, lang, system, data): | |
75 | + | def createProject(self, name, lang, system, data, callback=None): | |
76 | + | if callback is not None: | |
77 | + | callback.progress(0) | |
78 | + | ||
76 | 79 | projectpath = self.basedir + '/' + name | |
77 | 80 | Path(projectpath).mkdir(parents=True) | |
78 | 81 | try: | |
79 | 82 | proj = self.loadProject(name, lang, system, data) | |
80 | - | proj.initialize(projectpath) | |
83 | + | proj.initialize(projectpath, callback) | |
81 | 84 | if system == TRANSLATION_PROJECT: | |
82 | 85 | self.projects.append({"name": name, "lang": lang, "system": system, | |
83 | 86 | "info": {"version": proj.version}}) | |
… | |||
86 | 89 | "info": data}) | |
87 | 90 | except UnsupportedFormatException: | |
88 | 91 | rmdir(projectpath) | |
92 | + | ||
93 | + | if callback is not None: | |
94 | + | callback.progress(100) | |
95 | + | ||
89 | 96 | self.writeProjects() | |
90 | 97 | ||
91 | 98 | def loadProject(self, name, lang, system, data): |
offlate/systems/git.py
19 | 19 | from os import rename, remove | |
20 | 20 | ||
21 | 21 | class Progress(pygit2.remote.RemoteCallbacks): | |
22 | + | def __init__(self, callback): | |
23 | + | super().__init__() | |
24 | + | self.callback = callback | |
25 | + | ||
22 | 26 | def transfer_progress(self, stats): | |
23 | - | try: | |
24 | - | print("{}/{}\r".format(stats.received_objects, stats.total_objects), file=sys.stderr, end='') | |
25 | - | except: | |
26 | - | pass | |
27 | + | if self.callback is not None: | |
28 | + | self.callback.progress((100.0 * stats.received_objects) / \ | |
29 | + | stats.total_objects) | |
27 | 30 | ||
28 | 31 | class GitProject: | |
29 | 32 | def __init__(self, conf, name, lang, data = {}): | |
… | |||
37 | 40 | self.updateURI() | |
38 | 41 | self.updateFiles() | |
39 | 42 | ||
40 | - | def initialize(self, basedir): | |
43 | + | def initialize(self, basedir, callback=None): | |
41 | 44 | self.basedir = basedir | |
42 | 45 | self.updateURI() | |
43 | - | self.clone(basedir + "/current") | |
46 | + | self.clone(basedir + "/current", callback) | |
44 | 47 | self.updateFiles() | |
45 | 48 | ||
46 | 49 | def updateURI(self): | |
… | |||
49 | 52 | def updateFiles(self): | |
50 | 53 | self.translationfiles = [] | |
51 | 54 | ||
52 | - | def clone(self, directory): | |
53 | - | print("Cloning {} in {}".format(self.uri, directory)) | |
54 | - | pygit2.clone_repository(self.uri, directory, callbacks=Progress()) | |
55 | + | def clone(self, directory, callback=None): | |
56 | + | pygit2.clone_repository(self.uri, directory, callbacks=Progress(callback)) | |
55 | 57 | repo = pygit2.Repository(directory) | |
56 | 58 | branch = repo.lookup_branch(self.branch) | |
57 | 59 | ref = repo.lookup_reference(branch.name) |
offlate/systems/tp.py
48 | 48 | self.updateFileName() | |
49 | 49 | self.updateGettextNames() | |
50 | 50 | ||
51 | - | def initialize(self, basedir): | |
51 | + | def initialize(self, basedir, callback=None): | |
52 | 52 | self.basedir = basedir | |
53 | 53 | self.updateVersion() | |
54 | 54 | self.updateFileName() |
offlate/systems/transifex.py
37 | 37 | self.files = json.load(f) | |
38 | 38 | self.slugs = [x['slug'] for x in self.files] | |
39 | 39 | ||
40 | - | def initialize(self, basedir): | |
40 | + | def initialize(self, basedir, callback=None): | |
41 | 41 | self.basedir = basedir | |
42 | 42 | self.updateFileList() | |
43 | 43 | with open(self.basedir + '/project.info', 'w') as f: |
offlate/ui/editor.py
433 | 433 | self.next2Shortcut.activated.connect(self.nextItem) | |
434 | 434 | ||
435 | 435 | self.countLabel = QLabel() | |
436 | + | self.actionLabel = QLabel() | |
437 | + | self.actionProgress = QProgressBar() | |
438 | + | self.actionProgress.setEnabled(False) | |
436 | 439 | self.statusBar() | |
437 | 440 | self.statusBar().addWidget(self.countLabel) | |
441 | + | self.statusBar().addWidget(self.actionLabel) | |
442 | + | self.statusBar().addWidget(self.actionProgress) | |
438 | 443 | ||
439 | 444 | openMenu = QMenu(self.tr('Open'), self) | |
440 | 445 | self.initOpenProjects(openMenu) |
offlate/ui/manager.py
117 | 117 | hbox.addLayout(global_vbox, 1) | |
118 | 118 | ||
119 | 119 | self.setLayout(hbox) | |
120 | + | self.parent().statusBar() | |
121 | + | self.actionLabel = QLabel() | |
122 | + | self.actionProgress = QProgressBar() | |
123 | + | self.actionProgress.setEnabled(False) | |
124 | + | self.parent().statusBar().addWidget(self.actionLabel) | |
125 | + | self.parent().statusBar().addWidget(self.actionProgress) | |
120 | 126 | ||
121 | 127 | # Actions | |
122 | 128 | self.searchfield.textChanged.connect(self.filter) | |
… | |||
156 | 162 | worker = NewRunnable(self, w.getProjectName(), w.getProjectLang(), | |
157 | 163 | w.getProjectSystem(), w.getProjectInfo()) | |
158 | 164 | worker.signals.finished.connect(self.openProject) | |
165 | + | worker.signals.finished.connect(self.finishReport) | |
166 | + | worker.signals.progress.connect(self.reportProgress) | |
159 | 167 | self.threadpool.start(worker) | |
160 | 168 | ||
169 | + | def reportProgress(self, name, progress): | |
170 | + | self.actionProgress.setEnabled(True) | |
171 | + | self.actionProgress.setValue(progress) | |
172 | + | self.actionLabel.setText( | |
173 | + | self.tr('Creating project {}??? ({}%)').format(name, progress)) | |
174 | + | self.editor.actionProgress.setEnabled(True) | |
175 | + | self.editor.actionProgress.setValue(progress) | |
176 | + | self.editor.actionLabel.setText( | |
177 | + | self.tr('Creating project {}??? ({}%)').format(name, progress)) | |
178 | + | ||
179 | + | def finishReport(self, name): | |
180 | + | self.actionProgress.setValue(0) | |
181 | + | self.actionProgress.setEnabled(False) | |
182 | + | self.actionLabel.setText("") | |
183 | + | self.editor.actionProgress.setValue(0) | |
184 | + | self.editor.actionLabel.setText("") | |
185 | + | self.editor.actionProgress.setEnabled(False) | |
186 | + | ||
161 | 187 | def settings(self): | |
162 | 188 | w = SettingsWindow(self.manager.getConf()) | |
163 | 189 | w.exec_() | |
… | |||
209 | 235 | ||
210 | 236 | class NewRunnableSignals(QObject): | |
211 | 237 | finished = pyqtSignal(str) | |
238 | + | progress = pyqtSignal(str, int) | |
212 | 239 | ||
213 | 240 | class NewRunnable(QRunnable): | |
214 | 241 | def __init__(self, parent, name, lang, system, info): | |
… | |||
219 | 246 | self.info = info | |
220 | 247 | self.parent = parent | |
221 | 248 | self.signals = NewRunnableSignals() | |
249 | + | self.oldamount = -1 | |
222 | 250 | ||
223 | 251 | def run(self): | |
224 | 252 | self.parent.manager.createProject(self.name, self.lang, self.system, | |
225 | - | self.info) | |
226 | - | #self.parent.openProject(self.name) | |
253 | + | self.info, self) | |
227 | 254 | self.signals.finished.emit(self.name) | |
228 | 255 | self.parent.filter() | |
229 | 256 | ||
257 | + | def progress(self, amount): | |
258 | + | if int(round(amount)) > self.oldamount: | |
259 | + | self.oldamount = int(round(amount)) | |
260 | + | self.signals.progress.emit(self.name, amount) | |
261 | + | ||
230 | 262 | class EditRunnable(QRunnable): | |
231 | 263 | def __init__(self, parent, name, lang, system, info): | |
232 | 264 | super().__init__() |